Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to combine 2 domains into 1 pipeline

i have stocks that are from 2 different domains as they are traded on different stock exchanges, how do i merge them into 1 pipeline?

from quantopian.pipeline.domain import ID_EQUITIES
from quantopian.pipeline.domain import TH_EQUITIES

..........

Add the factor to the pipeline.

pipe_TH = Pipeline(
columns={
'sales': quarterly_sales,
'mktcap': mktcap,
'price': daily_close,
'vol': daily_volume
},
domain=TH_EQUITIES, screen = marketcap_criteria & prices_criteria

2 responses

The simple answer to combining pipelines from different domains is to use the pandas concat method. First run each pipeline separately and then combine the outputs. Something like this.

result_TH = run_pipeline(pipe_TH, start_period, end_period)  
result_ID = run_pipeline(pipe_ID, start_period, end_period)  
result_TH_ID = pd.concat([result_TH, result_ID])

However, nothing is that simple. There are two issues that will pop up. First, all the currency data (such as market cap, price, sales etc) are always in the local currency. So, once the two pipelines are merged it will be hard to do a lot of comparisons. This will even be the case in the original pipeline definitions. For instance the following market cap filter. This will have a different meaning for different domains since the market cap is denominated in the local currency.

marketcap_criteria = factset.Fundamentals.mkt_val.latest > 10000000000

The second issue is a pandas technical detail. One cannot concatenate dataframes with 'categorical' columns if the categories are not the same. What are 'categorical' columns? Anytime there is a string value (such as 'currency') that is a categorical. It's a column with a fixed set of potential values. So, this never comes up if all the pipeline outputs are numbers. Just do a simple concat. However, if one has categories, then first you will need to set the categories of each dataframe to be all possible values between the dataframes. In the example below, we have a pipeline output column of 'currency'.

# First, make a list of all possible currencies across the dataframes. In this case just 2 currencies.  
all_currencies = ['THB', 'IDR']

# Next, set the category values of each dataframe to all_currencies  
result_TH.currency = pd.Categorical(result_TH.currency, categories=all_currencies )  
result_ID.currency = pd.Categorical(result_ID.currency, categories=all_currencies )

# Now simply concat  
result_TH_ID = pd.concat([result_TH, result_ID])

The biggest issue you will have is the first one. It will be difficult to compare values between domains.

Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Dan

Thanks for getting back. There are some alternative 1) is to do an fx (foreign currency) conversion to USD first then merge the 2 dataframes together.
2) Or we can only do factor analysis on ratios like P/E or ROIC where fx is not an issue.

But once we load this into alphalens what do you recommend how we should handle stock price returns which have the same FX problem.