• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SanzProphet

DYI Investing, Quant tools and thoughts on the market

  • Start Here
  • Crypto Management
  • About
  • Contact
  • Disclaimer

Simple Regime Switching for SP500

September 13, 2013 by sanz prophet 24 Comments

black-in-white-big-300x225
image from  http://brucekrasting.com/

Let us consider two possible ways to trade the SP500.

1. If the index falls today, we buy tomorrow at the open. This is a “mean-reversion” strategy.
2. If the index rises today, we buy tomorrow at the open. A “follow-through” strategy.

From the graphs below, we can see that neither of these strategies worked well from 1960 to today.

2013-08-21_0314-300x219

Mean Reversion Trading On SP500
2013-08-21_0315-300x220
Follow-Thru (momentum) trading on SP500

Let’s introduce a qualifier that will tell us which strategy to trade at what time.

We will try the most basic one: The correlation between today’s return (close to yesterday’s close) to the previous day’s return. If it is negative we ‘ll use a contrarian logic. If the correlation is positive we ‘ll use a momentum logic.

The indicator of choice is the 2-period Relative Strength Index (RSI).

So if correlation between yesterday’s and today’s return is less than zero we buy on a correction. Otherwise we buy on strength. We trade at the next Open.

2013-08-21_0313-300x215
Here’s the Amibroker Code:
 <!–more–>
Dayreturn=ROC(C,1);
AutoCor=Correlation(Dayreturn,Ref(Dayreturn,-1),22);
BuyContr=RSI(2)<20;
SellContr=RSI(2)>70;
BuyMom=RSI(2)>60;
SellMoM=RSI(2)<50;
Buy=IIf(AutoCor<0,BuyContr,BuyMom);
Sell=IIf(AutoCor<0,sellContr,sellMom);
SetTradeDelays(1,1,1,1);
BuyPrice=SellPrice=O;
qty=1;
PositionSize=-100/qty;
SetOption(“MaxOpenPositions”,qty);

Filed Under: contrarian, momentum, SPY

Reader Interactions

Comments

  1. Anonymous says

    September 13, 2013 at 4:01 pm

    Could you provide statistics on the trades ?
    Thankseb

    Reply
  2. RickJ says

    September 13, 2013 at 4:02 pm

    Is there any chance to get tradestation code for your setups? Please:)

    Rick

    Reply
  3. Sanz Prophet says

    September 13, 2013 at 5:45 pm

    I did not provide stats since this is just a starting point for developing a trading model and is based on a non-tradable Index. If we could trade the Index itself (Yahoo:^GSPC) with a $10 commission (unrealistic in the older days), starting with $100,000, these would be the stats:

    Ending capital 14729949.9
    Net Profit 14629949.9
    Net Profit % 14629.95%
    Exposure % 43.54%
    Net Risk Adjusted Return % 33598.65%
    Annual Return % 9.74%
    Risk Adjusted Return % 22.36%
    Total transaction costs 29780

    All trades 1489
     Avg. Profit/Loss 9825.35
     Avg. Profit/Loss % 0.35%
     Avg. Bars Held 4.95

    Winners 789 (52.99 %)
     Total Profit 40647125.81
     Avg. Profit 51517.27
     Avg. Profit % 1.55%
     Avg. Bars Held 5.82
     Max. Consecutive 11
     Largest win 954706.82
     # bars in largest win 6

    Losers 700 (47.01 %)
     Total Loss -26017175.91
     Avg. Loss -37167.39
     Avg. Loss % -0.99%
     Avg. Bars Held 3.98
     Max. Consecutive 9
     Largest loss -1232294.78
     # bars in largest loss 20

    Max. trade drawdown -2373806.04
    Max. trade % drawdown -22.90%
    Max. system drawdown -2631919.2
    Max. system % drawdown -26.20%
    Recovery Factor 5.56
    CAR/MaxDD 0.37
    RAR/MaxDD 0.85
    Profit Factor 1.56
    Payoff Ratio 1.39
    Standard Error 1548134.06
    Risk-Reward Ratio 0.13
    Ulcer Index 4.73
    Ulcer Performance Index 0.92
    Sharpe Ratio of trades 1.3
    K-Ratio 0.0175

    Reply
  4. Sanz Prophet says

    September 13, 2013 at 5:48 pm

    I wish I could help but I have no experience and do not own Tradestation. It should be easy to translate the code, though.

    Reply
  5. CZ says

    September 13, 2013 at 6:31 pm

    I think this is the equivalent EL code (it could be more elegant maybe but it seems to work)

    Input:
    ContrRSILow(20),
    ContrRSIHigh(70),
    MomRSILow(60),
    MomRSIHigh(50);

    Variables:
    BuyContr(False),
    SellContr(False),
    BuyMom(False),
    SellMom(False),
    AutoCor(0),
    Dayreturn(0);

    Dayreturn = RateofChange(Close,1);
    AutoCor = Correlation(Dayreturn, Dayreturn[1],22);

    if RSI(close, 2) < ContrRSILow then BuyContr = True else BuyContr = false;
    if RSI(close, 2) > ContrRSIHigh then SellContr = True else SellContr = false;
    if RSI(close, 2) > MomRSILow then BuyMom = True else BuyMom = false;
    if RSI(close, 2) < MomRSIHigh then SellMom = True else SellMom = false;

    if AutoCor < 0 and BuyContr = True then buy this bar close; //Buy the Contrarian signal
    If AutoCor > 0 and BuyMom = True then Buy this bar close; //Buy the momentum signal
    If AutoCor < 0 and SellContr = True then sell this bar close; //Sell the contrarian signal
    if AutoCor > 0 and SellMom = True then sell this bar close; //Sell the momentum signal

    Reply
  6. Sanz Prophet says

    September 14, 2013 at 11:04 am

    Thank you CZ.

    Reply
  7. James says

    September 14, 2013 at 1:22 pm

    Should the 2 Amibroker equity charts at the top (mean-reversion and momentum) not be opposite of each other? Or is one log, other linear?

    Reply
  8. CZ says

    September 14, 2013 at 3:48 pm

    No problem. Happy to give back!

    Reply
  9. Anonymous says

    September 16, 2013 at 12:48 pm

    Why is it Correlation 22? Optimized? What's the time series function in R? acf? can someone show an example?

    Reply
  10. Sanz Prophet says

    September 16, 2013 at 1:53 pm

    James,

    BuyContr=RSI(2)<20;
    SellContr=RSI(2)>70;
    BuyMom=RSI(2)>60;
    SellMoM=RSI(2)<50;

    These 2 systems are not exactly symmetrical.

    Reply
  11. Sanz Prophet says

    September 16, 2013 at 2:24 pm

    Anonymous,
    The 22-day (one month) correlation was chosen arbitrarily.

    Reply
  12. Anonymous says

    September 17, 2013 at 4:58 pm

    Need use SellShort instead of Sell for EL

    Input:
    ContrRSILow(20),
    ContrRSIHigh(70),
    MomRSILow(60),
    MomRSIHigh(50);

    Variables:
    BuyContr(False),
    SellContr(False),
    BuyMom(False),
    SellMom(False),
    AutoCor(0),
    Dayreturn(0);

    Dayreturn = RateofChange(Close,1);
    AutoCor = Correlation(Dayreturn, Dayreturn[1],22);

    if RSI(close, 2) < ContrRSILow then BuyContr = True else BuyContr = false;
    if RSI(close, 2) > ContrRSIHigh then SellContr = True else SellContr = false;
    if RSI(close, 2) > MomRSILow then BuyMom = True else BuyMom = false;
    if RSI(close, 2) < MomRSIHigh then SellMom = True else SellMom = false;

    if AutoCor < 0 and BuyContr = True then buy this bar close; //Buy the Contrarian signal
    If AutoCor > 0 and BuyMom = True then Buy this bar close; //Buy the momentum signal
    If AutoCor < 0 and SellContr = True then SellShort this bar close; //Sell the contrarian signal
    if AutoCor > 0 and SellMom = True then SellShort this bar close; //Sell the momentum signal

    Reply
  13. crakes says

    September 17, 2013 at 7:20 pm

    Well, I was suffering through your drought of posts and then this post. If this is the product of a drought then I can survive another drought. Well done Sanz!

    Reply
  14. crakes says

    September 17, 2013 at 7:38 pm

    One observation on your logic… this looks like a "poor man's Hurst exponent."

    Reply
  15. Akros Sorka says

    September 19, 2013 at 6:34 am

    Hello, could you explain why I get zero in AutoCor?
    Where should I look? What may influence on it?

    Reply
  16. Sanz Prophet says

    September 19, 2013 at 10:43 am

    Crakes,
    Thank you very mush for the encouraging comments! And, yes you could say that this is a poor man's Hurst as far as attempting to classify the SP500 timeSeries as mean reverting or trending..

    Reply
  17. Akros Sorka says

    September 23, 2013 at 7:53 am

    How can you buy or sell by open price while the day has not yet been closed?
    You may do it only by Close price or Open price of the next day: Buyprice=Sellprice=Ref(O,1);
    And this code is only for backtesting.

    Reply
  18. Sanz Prophet says

    September 23, 2013 at 8:31 am

    Akros,
    The code is correct and does not look forward.
    SetTradeDelays(1,1,1,1);
    BuyPrice=SellPrice=O;
    This code instructs the backtester to trade on Open on the Next day After the signal.

    Please refer to http://www.amibroker.com/guide/afl/afl_index.php?m=2 as well as many examples here
    http://www.amibroker.com/library/list.php

    Reply
  19. Akros Sorka says

    September 23, 2013 at 2:01 pm

    Sorry Sanz, but the principle of code working is so that after closing candle, in the end of the day, rule gives signal by open price of current candle.
    The problem is that you have no confidence that Close price would be better than Open of the day. And only in the next day your order with old open price will be put in the market.
    Am I clear?

    Reply
  20. Akros Sorka says

    September 27, 2013 at 5:24 pm

    Am I right? This Ami code should be corrected…

    Reply
  21. Sanz Prophet says

    September 27, 2013 at 5:54 pm

    Akros,
    The way the code works is that it gets a signal at the end of day t and buys, sells or holds on day t+1. So if at the end of the day on Monday it gets a buy signal, it buys at the open on Tuesday.
    Feel free to backtest the code in Amibroker and check the results manually. Please let me know if you find problems.

    Reply
  22. Akros Sorka says

    September 27, 2013 at 7:02 pm

    Dear Sanz, I have already checked this code in my Ami 5.30.
    And I receive Open price of the day when signal appears. Not in the next day. That's why I wrote you.

    Reply
  23. Akros Sorka says

    September 27, 2013 at 7:13 pm

    And in Explore mode I see that ROC is changing from -2 to 2: -2, -1, -0,0,1,2.
    AutoCor is always -1, -0, 0 or 1.
    Are they correct? Shouldn't they be with decimal point?

    Reply
  24. Akros Sorka says

    September 27, 2013 at 7:30 pm

    This comment has been removed by the author.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Keep in touch

Managed Crypto Funds

via TokenSets.com

Categories

Archives

Footer

RSS Portfolios from Logical-Invest.com

  • Bond ETF Rotation Strategy: Low-volatility portfolios for smart investors from Logical Invest
  • BUG Permanent Portfolio Strategy: Quantitative investment models from Logical Invest
  • Crypto & Leveraged Top 2 Strategy: Risk-adjusted investment portfolios from Logical Invest
  • Dow 30 Strategy: Quantitative investment models from Logical Invest

Pages

  • About
  • Contact
  • Disclaimer
  • Start Here

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · @SanzProphet.com· Log in