Backtesting RSI and MA

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #162339 quote
    kosmo
    Participant
    New

    Hi guys,

    I am trying to figure out how to set up with the following conditions:

    Enter Long position:

    If the RSI crosses under 30 and while the RSI is below 70 and MA9 CROSSES OVER MA21. I have tried with an IF condition with the RSI and if that triggers I want to have the MA9 crosses over the MA21 and only then I want to go long.

    Is it possible to have a system like that and if so Could you please help me with that?

     

    Thanks,

    #162354 quote
    robertogozzi
    Moderator
    Master

    There you go:

    Defparam CumulateOrders = false
    Once CondL    = 0
    Once RsiCross = 0
    MyRsi = Rsi[14](close)
    Ob    = 70
    Os    = 100 - Ob
    Sma9  = average[9,0](close)
    Sma21 = average[21,0](close)
    If OnMarket or (MyRsi >= Ob) then
       CondL    = 0
       RsiCross = 0
    Endif
    If MyRsi crosses under Os then
       RsiCross = 1
    Endif
    CondL = RsiCross and Sma9 crosses over Sma21
    If CondL and not OnMarket Then
       Buy 1 contract at Market
    Endif

    you only need to add SL & TP.

    Midlanddave thanked this post
    #162374 quote
    kosmo
    Participant
    New

    Hi Robertogozzi,

    Thank you so much for your help.

    This was exactly what I looked for, but what about exiting the long position without the TP and SL, I mean to exit the trade once the RSI is over 70 and the MA9 is crossing under the MA21?

    and if I want to reverse the long to short, I need to need to create a new variable eg. CondS?

    #162387 quote
    robertogozzi
    Moderator
    Master

    To exit long trades, you can append these lines to my code:

    ExitL = (MyRsi > Ob) AND (Sma9 CROSSES UNDER Sma21)
    IF ExitL AND LongOnMarket THEN
       SELL AT Market
    ENDIF

    Yes, to support SHORT trades you need to add CondS (and now also ExitS) to which you will have to assign the opposite conditions to both enter and exit trades.

    #162388 quote
    kosmo
    Participant
    New

    I have the code like this, but it does not allow me to exit the market

     

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    Once CondL = 0
    Once RsiCrossUnder = 0
    Once RsiCrossOver = 0
    MyRsi = Rsi[14](close)
    Ob = 70
    Os = 100 - Ob
    Sma9 = average[9,0](close)
    Sma21 = average[21,0](close)
    
    if OnMarket or (MyRsi >= Ob) then
    CondL = 0
    RsiCrossUnder = 0
    endif
    
    if MyRsi crosses under Os then
    RsiCrossUnder = 1
    endif
    
    CondL = RsiCrossUnder and Sma9 crosses over Sma21
    
    if CondL and not OnMarket Then
    Buy 100 contract at Market
    endif
    
    if myRsi crosses over Ob then
    RsiCrossOver = 1
    endif
    
    CondExitL = RsiCrossOver and Sma9 crosses under Sma9
    
    if CondExitL then
    SELL 100 contract AT MARKET
    endif
    #162389 quote
    kosmo
    Participant
    New

    Sorry, I saw the mistake, it was on the line 32, I had sma9 crosses under sma9, it should of been sma9 under sma21

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Backtesting RSI and MA


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kosmo @kosmo Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by kosmo
5 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/22/2021
Status: Active
Attachments: No files
Logo Logo
Loading...