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
from quantopian.pipeline.filters import QTradableStocksUS
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:
print("buy")
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)