I know some python (not an expert, though) and I created an algo with my own pandas dataframe and connected it with IB. But I would like to backtest it on quantopian, but I really can't get it to work. All algo's I see are old and use deprecated bits of code.
For example this code seems very basic, but backtesting takes ages. Just for timespan of a few years it takes many hours : while I realize it's checking every minute, this makes no sense to me.
What should I do to get this kind of simpel moving average crossovers to work?
def initialize(context):
context.security = symbol('SPY')
def handle_data(context, data):
print(data)
m50 = data[context.security].mavg(50)
m200 = data[context.security].mavg(200)
current_price = data[context.security].price
current_positions = context.portfolio.positions[symbol('SPY')].amount
cash = context.portfolio.cash
#buying condition
if (m50 > m200) and current_positions == 0:
number_of_shares = int(cash/current_price)
order(context.security, number_of_shares)
log.info('Buying shares')
#selling condtion
elif (m50 < m200) and current_positions != 0:
order_target(context.security, 0)
log.info('Selling shares')
record(m50 = m50, m200 = m200, price = current_price)