Back to Community
Yesterday's Top Gainers - Requesting Help

Hi Everyone

I'm new so please bear with me. I wanted to create a strategy that gets the top gainers of over 20% for the previous day, see if there is a positive price moment again for today, if so buy, set trailing stop.

Right now the back-testing doesn't seem to really be working, I haven't figured out why. If anyone has any suggestions for this to improve it please go for it. I would like to see if anyone has gotten decent results from something like this.

Thanks!

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.

import numpy as np
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume, Returns, SimpleMovingAverage, CustomFactor
        
class Pct_Ret(CustomFactor):
    inputs = [USEquityPricing.close]
    def compute(self, today, assets, out, prices):
        current = prices[-1]
        previous = prices[0]
        ret = ((current-previous)/(1.0*previous))
        out[:] = ret
        
def initialize(context):
    #schedule_function(hoping_for_gains, date_rules.every_day(), time_rules.market_open())
    schedule_function(praise_stock_gods, date_rules.every_day(), time_rules.market_open(minutes=15))
    
    #Set Lookback period
    context.returns_lookback = 1
    
    # Create and attach our pipeline (dynamic stock selector), defined below.
    attach_pipeline(make_pipeline(context), 'big_gainers')

    set_long_only()

def make_pipeline(context):
    
    # Create a pipeline object. 
    pipe = Pipeline()

    daily_returns = Pct_Ret(window_length=2)# percent return from the last day
    pipe.add(daily_returns, 'daily_returns')
    
    sma_short = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)  
    pipe.add(sma_short, "sma_short")
    
    pct_return = daily_returns >= 0.20

    pipe.set_screen(pct_return & (sma_short >= 3.00))
    
    
    
    return pipe


def before_trading_start(context, data):
    
    # Pipeline_output returns a pandas DataFrame with the results of our factors
    # and filters.
    context.output = pipeline_output('big_gainers')
    
    # value in the low_returns column.
    context.securities = context.output

    update_universe(context.securities.index)
    print (context.securities.index)

def praise_stock_gods(context,data):
    
    # Get any open orders that we may have, to prevent double ordering.
    open_orders = get_open_orders()
    for stock in context.securities.index:
        if stock not in open_orders:
            if stock in data:
                open_price = data[stock].open_price
                #log.info( str(stock) + ' Open Price = ' + str(open_price))
                price = data[stock].price
                #log.info( str(stock) + ' Price = ' + str(price))
                #returns = data[stock].returns()
                #log.info(str(stock) + ' Returns = ' +  str(data[stock].returns()))
                #percent_change = price / open_price *100
                #log.info( str(stock) + ' % Change = ' + str(percent_change))
                #position = context.portfolio.positions[stock].amount
                settled_cash = context.account.settled_cash
                if data[stock].price >= data[stock].open_price*1.02 and settled_cash > 2200 and context.portfolio.positions[stock].amount == 0:
                    order_target_value(stock, 2000)
                    log.info(str(stock) + ' Purchased ')
                 
#def hoping_for_gains(context,data):
    
    #Exiting Positions Next Morning


def handle_data(context, data):
    open_orders = get_open_orders()
    for stock in context.portfolio.positions:
        if stock not in open_orders:
            if stock in data:
                price = data[stock].price
                #log.info(str(stock) + ' Price = ' + str(price))
                high = data[stock].high
                #log.info(str(stock) + ' High = ' + str(high))
                position = context.portfolio.positions[stock].amount
                if price <= high*0.95 and position > 0:
                    order_target_percent(stock, 0)
                    log.info(str(stock) + ' Sold Stop Loss')
    
There was a runtime error.