Back to Community
My first algo, am I on the right path?

I wanted to create an algorithm that looked at the past 50 day average and the past 200 day average. The algorithm would trade if the 50 day average was greater than the 200 day average. I don't think this is a novel idea, I just wanted to try create my first attempt. Did I do it right, and why does it say the return is so high?

Thanks,
Milap

Clone Algorithm
5
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
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.aapl = sid(24)
    
    context.max_notional = 100000 #Long only algo

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.
    vwap200 = data[context.aapl].vwap(200)
    
    vwap50 = data[context.aapl].vwap(50)
    
    if vwap50 > vwap200: #If last 50 day average is greater than 200, buy
        order_target_percent(context.aapl, 1) 
There was a runtime error.
2 responses

Hi ,

The return is high for two reasons :

1 . You are using AAPL

2 . We've been in a bull market for 6 years now .

Instead of AAPL, use SPY and backtest your algo from 2003. Attached, is your algo tested with SPY, same period.

Clone Algorithm
3
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
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.spy = sid(8554)
    
    context.max_notional = 100000 #Long only algo

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.
    vwap200 = data[context.spy].vwap(200)
    
    vwap50 = data[context.spy].vwap(50)
    
    if vwap50 > vwap200: #If last 50 day average is greater than 200, buy
        order_target_percent(context.spy, 1) 
There was a runtime error.

Thanks for the tips, I'll try doing those two tips instead.

Milap