Back to Community
High Momentum Trading Strategy for Apple

This trading strategy employs 'buy high, sell even higher' approach. It is only profitable when certain conditions are met.

Clone Algorithm
4
Loading...
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
import pandas as pd
import numpy as np


def initialize(context):
    context.aapl = sid(24)
    
    schedule_function(percentage_change, date_rules.every_day(), time_rules.market_open())
    schedule_function(profit_or_loss, date_rules.every_day(), time_rules.market_close())

        
def percentage_change(context,data):
    
    context.aapl = sid(24)
    aapl = context.aapl
    
    aapl_two_days_ago = data.history(aapl,'close',3,'1d')[-3]
    aapl_ytd = data.history(aapl,'close',3,'1d')[-2]
    pct_change = (aapl_ytd - aapl_two_days_ago) / aapl_two_days_ago
    
    if pct_change >= 0.0439 and data.can_trade(aapl):
        order_target_percent(aapl, 1.0)
        
        
def profit_or_loss(context,data):
    
    current_pos = context.portfolio.positions
    
    for stock in current_pos:
        price_now = data.current(stock, 'price')
    
        if (price_now / current_pos[stock].cost_basis) - 1 >= 0.02:
            order_target_percent(stock, 0.0)
            log.info('TAKE PROFIT, BUY @ {}, SELL @ {}'.format('%.2f' % current_pos[stock].cost_basis, '%.2f' % price_now))

        if (price_now / current_pos[stock].cost_basis) - 1 <= -0.01:
            order_target_percent(stock, 0.0)
            log.info('STOP LOSS, BUY @ {}, SELL @ {}'.format('%.2f' % current_pos[stock].cost_basis, '%.2f' % price_now))
There was a runtime error.