Backtester online dating
def can_buy(context, data): latest = data[context.spy].close_price h = history(200,'1d','close_price') avg = h[context.spy].mean() return latest avg # This function is for adding new positions, by iterating through the # eligible stocks in order of momentum, and buying them if we have (anticipate # having) enough cash to do so.def add_positions(context, data, cash_available): s = "" for i in range(0,len(context.pool)): sid = index[i] if ((sid not in context.portfolio.positions) & (sid in data)): desired_shares = desired_position_size_in_shares(context, data, sid) cash_req = desired_shares * data[sid].close_price if ((cash_req Top 10 long positions of all time (and max%) [u' BTU' u' BBD' u' AGN' u' OIBR' u' TII' u' GENZ' u' AGNC' u' DELL' u' KMP' u' CAG'] [ 0.525 0.159 0.149 0.147 0.141 0.14 0.129 0.124 0.124 0.123] This means BTU gets up to 52.5% of the portfolio.In other words, the algorithm is trying to buy 50 of the "new" shares, which is equal to 50x15=750 of the "old" shares.I suspect the best fix is to calculate the ATR using back adjusted prices, rather than close prices.I'm having trouble debugging the ATR Custom Factor, as I'm not sure how to slice into the arrays using the index columns to get the window of prices for BTU.class ATR(Custom Factor): [...] btu_sid = symbol(' BTU')def compute(self, today, assets, out, close, high, low): btu_idx = assets.get_loc(btu_sid) close[btu_idx] # use me like this to get BTU prices [...] Our [quantopian's] data uses adjusted close prices.
However, it seems the order_target function is using the old backtester paradigm of back-adjusting the data for all past and future splits, before the algorithm starts.
As far as I can tell, the divisor is the total value of the portfolio including any cash.
So this is a huge concentration, which doesn't seem right.
Position sizing is critical to the success of this (and most algorithms), and ATR position sizing is pretty common. Since ATR isn't used in te actual pre-universe screening, only for actual position sizing, it would be simplest to just calculate ATR on data from 'history()' and use that to determine position sizes.
I'll do that when I have time, or of course anyone else is welcome to.