Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Array Transforms

highs = history(bar_count=200, frequency='1d', field='high')
lows = history(bar_count=200, frequency='1d', field='low')
ret = ((highs-lows)/lows)*100
retstd = ret.std
retavg = ret.mean()

Is ret an array ?
Does retstd return standard deviation of ret array ?
Does retavg return average of ret array ?

If not how do i make the required array transform ?

6 responses

ret is a pandas DataFrame with 200 rows (one for each day) and a column for each security.

The standard deviation code needs a parenthesis:

retstd = ret.std()  

Then retstd will be a pandas Series of standard deviations, one for each security, indexed by security. Similarly, retavg is a Series of means, one per security.

The attached backtest shows how to log this info (with fewer bars).

This is my algorithm. What I am trying to do is calculate returns as ((high-low)/low)*100 , if previous days returns are more than average returns[past 200 days] + standard deviation of returns[past 200 days] I will buy the stock with 100% portfolio cash . Also at 3:15 pm I want to liquidate all my existing positions. Is this algo right ?

No, the algorithm isn't doing what you want. The following "if" condition is always True, because the arguments to CheckOver are missing:

if CheckOver:  
        order_target_percent(sid(24),1)  

Another issue -- later on, you're comparing a float to a Series:
if (prev_ret > over): You might want to consider rewriting your code without handle_data. You can use a second call to schedule_function to make your ordering logic run at the appropriate time. Within CheckOrder, you can test for early closes and store the result in something like context.early_close. Then you can check that result later in your ordering function.

Whichever path you choose, it's often useful to set breakpoints and step through the code in the debugger. After you hit a breakpoint, you can inspect objects with the print statement, e.g. "print(ret)". For single numeric values, "print" won't display anything; instead, just type the variable name and hit return.

The attached backtest is a skeleton into which you can insert your calculations. It uses two scheduled functions, as suggested in the previous post.

I have added my calculations to the algo. But it is buying and selling every day which is not supposed to happen. Also even if I revert my strategy i.e. buy instead of sell and vice versa the graph still doesn't seem to change much.

You're sometimes using DataFrames or Series when I think you intend to use floats. For example, in this line:

if (retns > 0 and ret > over):  

retns is a float, ret is a DataFrame, and over is a Series.

In this context, you can index a DataFrame by a stock to get a Series (as you did earlier in your code), and you can index a Series by a numeric index to get a value (as you also did earlier).

When in doubt, set a breakpoint in the debugger. When the breakpoint is hit, enter type(variable_name) and hit return to see what you're dealing with.