Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
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  
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)  
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):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close(minutes=10))  
    context.stocks=[ sid(24),  
                     sid(5061),  
                     sid(8347),  
                     sid(46631),  
                     sid(4151),  
                     sid(1091),  
                     sid(3149),  
                     sid(8151),  
                     sid(5938),  
                     sid(25006)]  

def trade(context,data):  
    weight = 1/len(context.stocks)  
    for stock in context.stocks:  
        hist=data.history(stock,'price',250,'1d')  
        ema1=talib.EMA(hist,timeperiod=50)  
        ema2=talib.EMA(hist,timeperiod=200)  
        uptrend = ema1[-1] > ema2[-1]  
        we_are_flat  = stock not in context.portfolio.positions  
        # can we afford to buy one whole share of the stock (BRK_A, GOOG)?  
        we_can_afford_it = hist.iloc[-1] < weight*context.portfolio.portfolio_value  
        if uptrend and we_are_flat and we_can_afford_it:  
            order_target_percent(stock,weight)  
            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]))