Hi,
Can anyone kindly write me a sample code using the pipeline to select the top 30 stocks based on their returns (Price today - price of yesterday/ price of yest).
Thank you so much!
Cheers.
Hi,
Can anyone kindly write me a sample code using the pipeline to select the top 30 stocks based on their returns (Price today - price of yesterday/ price of yest).
Thank you so much!
Cheers.
Hi Whimsbee,
I get this while trying to run in the backtester; "The 'quantopian.research' module is not available in the backtester. It is only available in research."
Any idea how to overcome this issue?
Cheers.
The research environment and the IDE/backtester currently have different APIs.
https://www.quantopian.com/help#research-env
hmmm... well run_pipeline doesn't work in the backtester. Instead, you will
attach_pipeline in your initialize() function.pipeline_output in your before_trading_start() function and attach it to the context.schedule_function or handle_data methods via the context.View Go to Backtest and then view the Log Output
Can I just replace context.tradeables_list (fixed list of securities) with "context.output" in my strategy attached below?
initialize is only called once. Do you want to select these stocks only once? Or do you want to refresh your list everyday?
Your handle_data appears to be expected to be called once per day. This function is called once per minute. I do not believe the behavior would be as expected. You should look to before_trading_start or schedule_function to do something once per day.
To directly answer your question, you will have to move the "initialization" of the tradables_list into before_trading_start for the simple reason that, similar to run_pipeline, you are not allowed to execute pipeline_output in the initialize function. Depending on the answer to my first question, you can either check if you have already set context.tradeables_list or always reassign it in the before_trading_start function.
if not context.tradeables_list: #remove this line to always reassign
context.tradeables_list = o
I will leave it to you to determine how to convert o in my backtest above to a list of sids. I'm about to fall asleep. :)
Remember print is your friend. If you get to Backtest #368, you are doing great - or maybe you should be using the Notebook..
I wish to refresh my list daily.
I tried but cant seems to get it to work, will be great if you can combine your backtest into my strategy.
Thank you so much!
Here is how you get the list of symbols from the DataFrame:
def getSymbolList(df):
return map(lambda a: str(a[1].symbol), df.index.values)
print getSymbolList(result.head(30))
And here you can see what other properties are available on a zipline.assets._assets.Equity beyond symbol
print dir(result.index.values[0][1])
Or without lambda. For some reason this time unicode.
def getSidList(df):
return [s[1].symbol for s in df.index.values]
print getSidList(result.head(20))
[u'ESPR', u'NKTR', u'MEMP', u'BLPH', u'VKTX_W', u'CTMX', u'MVIS', u'DRIO_W', u'DDC', u'YGE', u'MCF', u'WINS', u'BIOA_WS', u'NAK', u'HOS', u'HRMN_W', u'JSYN_W', u'AUPH', u'TST', u'TZOO']
str(s[1].symbol)
But to use the data.history() function in the BackTester you will just use the df.index.values.
In the BackTester df.index.values does not have a tuple array, it is just an array of Equity objects. No DateTime
I guess this is another difference of run_pipeline for Notebook and pipeline_output for BackTester.
Okay. All prepared. You just have to insert your logic to place orders.
This should pull the top 30 as you originally specified plus what is in your portpolio.
It will generate the context.mean_1, the context.mean_2, the context.trend_n and the context.trend_p for each security in that daily generated list. I am even giving you flexibility with the constants: 30, 10, 3
Your logic will go into the last function SPTracker_TimeToTrade to determine when to place the orders. You may opt to use a handle_data function if you would like to execute an order based on additional information that has not yet been obtained. But, since all of the data is "stale" 1d, yesterday's close, I have just selected two arbitrary time of days for you to place orders via schedule_functions.
Good Luck. I look forward to seeing how you make this work. I will help out more as I can.
:$: :$: :$:
Hi Whimsbee,
There's a few more questions:
-If I want to select the top 30 stocks out of a pre-selected pool of lets say 50 how do I do it?
-I dont really get the context.trend_p = tp and context.trend_n = tn in your coding, can you explain a little more?
-There isnt much signal from the universe from your algorithm do you know why? (Not much transactions were been executed)
Thank you!