Back to Community
Help to run a simple strategy

Hi guys,
I am new in Quantipian and have no programming skills. I would need help in coding and backtesting a strategy with the following items:

  • When ETF RSP crosses above 75 SMA Daily line: BUY ETF PDP
  • When ETF RSP crosses below 75 SMA Daily line: GO TO CASH SELLING PDP POSITION

I assume this should be an easy strategy to code. Unfortunately I do not have the time to learn Phyton as my MBA (Columbia) is becoming very time intensive.

Any help would be appreciated!

Fedreico.

6 responses

Are you sure, since the SMA of RSP never reaches 75

Clone Algorithm
16
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 numpy as np
import math

SMA=ta.SMA(timeperiod=10)

def initialize(context):
    context.max_notional = 1000000
    context.RSP = sid(24744)
    context.PDP = sid(33441)
    context.crossline=75

def handle_data(context, data):
    SMA_data = SMA(data)
    RSP_SMA = SMA_data[context.RSP]
    
    current_PDP_shares = context.portfolio.positions[context.PDP].amount

    if not np.isnan(RSP_SMA):
        if RSP_SMA > context.crossline and current_PDP_shares == 0:
            num_shares = math.floor(context.max_notional / data[context.RSP].close_price)
            order(context.PDP, num_shares)
            log.info('SMA is at ' + str(RSP_SMA) + ', buying ' + str(num_shares)  + ' shares')
        elif RSP_SMA < context.crossline and current_PDP_shares > 0:
            order(context.PDP -1 * current_PDP_shares)
            log.info('SMA is at ' + str(RSP_SMA) + ', selling ' + str(current_PDP_shares) + ' shares')

    record(RSP_SMA=RSP_SMA, PDPprice=data[context.PDP].close_price)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Xin,
By 75 I meant 75-Day Simple Moving Average. Not 75 as a price level.
When ETF RSP price crosses above its 75-day SMA buy the ETF PDP
When ETF RSP price goes below 75 day SMA sell PDP and go tocash.

Haha, silly mistake

Here we go, hope this time i am right

Clone Algorithm
16
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 numpy as np
import math

SMA=ta.SMA(timeperiod=75)

def initialize(context):
    context.max_notional = 1000000
    context.RSP = sid(24744)
    context.PDP = sid(33441)
    context.invested=0

def handle_data(context, data):
    SMA_data = SMA(data)
    RSP_SMA = SMA_data[context.RSP]
    RSP_price = data[context.RSP].price
    
    current_PDP_shares = context.portfolio.positions[context.PDP].amount

    if not np.isnan(RSP_SMA):
        if context.invested==0:
            if RSP_price > RSP_SMA:
                 num_shares = int(context.max_notional / data[context.PDP].price)
                 order(context.PDP, num_shares)
                 context.invested=1
                 log.info('SMA is at ' + str(RSP_SMA) + ', buying ' + str(num_shares)  + ' shares')
        else:
            if RSP_price < RSP_SMA:
                order(context.PDP, -1 * current_PDP_shares)
                context.invested=0
                log.info('SMA is at ' + str(RSP_SMA) + ', selling ' + str(current_PDP_shares) + ' shares') 
   
    
    
    record(RSP_SMA=RSP_SMA, RSPprice=data[context.RSP].close_price)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Xin,
Thanks for the coding. I have some doubts:
1- Why the performance in 2008 is negative ? According to the strategy they should be in cash from Jul 2008 and entering a long position in April 2009.
2- Which is the benckmark?
3- Let me explain the strategy again just in case of a small misunderstanding:

If price of ETF RSP is above 75-day SMA go LONG the ETF PDP. Just use the ETF RSP as a reference only on when to buy PDP.
If price of ETF RSP price goes below 75-day SMA sell PDP and go to cash. Be in cash all the time RSP price is below its 75-day SMA line.

In other words, when RSP trades above its 75 day SMA go long PDP and sell PDP going to cash when RSP trades below its 75-day SMA.

Is this what you understood ?
Thanks again!

I think my understand is right. And you can see a flat line between 2008 Jun, and 2008 Sep, And after that there are several crosses actually.

And I have no idea of how they build benchmark here, it is built automatically.

Thanks so much Xin