Need help with ema crossover strategy

Hi , I need help with my strategy.It trades multiple stocks.It buys when the ema 50 crosses the ema 200 from below and sells when the opposite.But when I backtest the strategy ,it fails to take any trade .Thank you

Here is the code

import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
import talib
import pandas as pd

def initialize(context):
context.stocks=[ sid(24),
sid(5061),
sid(8347),
sid(46631),
sid(4151),
sid(1091),
sid(3149),
sid(8151),
sid(5938),
sid(25006)]

#def Vwap(prices,volumes):
#return (prices*volumes).sum()/volumes.sum()

def handle_data(context,data):
for stock in context.stocks:
hist=data.history(stock,'price',50,'1d')
ema1=talib.EMA(hist,timeperiod=50)
ema2=talib.EMA(hist,timeperiod=200)

#sma_50=hist.mean()
#sma_20=hist[-20:].mean()

open_orders=get_open_orders()

if ema1[-1]>ema2[-1] :
if stock not in open_orders:
order_target_percent(stock,0.1)
elif ema1[-1]<ema2[-1] :
if stock not in open_orders:
print("sell")
order_target_percent(stock,-0.1)

1 response

Are you trading on minute or daily data?
handle_data implies minute data, but assuming your intent is to trade daily data, how about something like this?

import quantopian.algorithm as algo
import talib

def initialize(context):
context.stocks=[ sid(24),
sid(5061),
sid(8347),
sid(46631),
sid(4151),
sid(1091),
sid(3149),
sid(8151),
sid(5938),
sid(25006)]

print('bought {} at ${}, ema1={}, ema2={}'.format(stock.symbol,hist.iloc[-1],ema1[-1],ema2[-1])) if (ema1[-1] < ema2[-1]) and (stock in context.portfolio.positions): order_target_percent(stock,0) print('sold {} at${}, ema1={}, ema2={}'.format(stock.symbol,hist.iloc[-1],ema1[-1],ema2[-1]))