Please can someone help me code this Strategy

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #14579 quote
    Tom Hoo
    Participant
    New

    Can someone please help me to code this.

    Condition to Buy or Sell. Happening in sequence

    1. ADX to drop to value 13.5
    2. ADX to recover to above Value 20
    3. ADXR to go above Value 20 at this time ADX should be still above 20
    4. When 1 ,2,3 is fulfilled. BUY if EMA 4 >EMA 9 >EMA 20.    SELL if EMA 4 < EMA 9 < EMA 20

     

    Exit

    Exit Long when EMA 4 cross below EMA 9

    Exit Short when EMA 4 Cross above EMA 9

     

    Please it will be greatly appreciated. I know nothing about programing

    #14595 quote
    robertogozzi
    Moderator
    Master

    This code should do, if I understood what you need:

    DEFPARAM CumulateOrders = False              //No more than 1 trade allowed
    
    ONCE Adx13 = 0                               //Make sure it has an initial value
    ONCE Adx20 = 0                               //Make sure it has an initial value
    
    ADXval = ADX[21]                             //Current ADX value
    
    Ema4   = ExponentialAverage[4]               //Current value for all EMA's
    Ema9   = ExponentialAverage[9]
    Ema20  = ExponentialAverage[20]
    
    IF ONMARKET THEN                             //If a trade is entered, then reset flags to false
    Adx13 = 0                                 //   to restart the whole sequence for next trades
    Adx20 = 0
    IF LONGONMARKET THEN
    IF Ema4 CROSSES UNDER Ema9 THEN        //   Exit LONG trades if needed
    SELL AT MARKET
    ENDIF
    ENDIF
    IF SHORTONMARKET THEN
    IF Ema4 CROSSES OVER Ema9 THEN         //   Exit SHORT trades if needed
    EXITSHORT AT MARKET
    ENDIF
    ENDIF
    ELSE                                         //otherwise start the sequence...
    IF Adx13 = 0 THEN                         //If 13.5 not reached yet, then
    Adx20 = 0                              //   reset this flag to 0, just in case and...
    Adx13 = (ADXval <= 13.5)               //   set this one to its current value
    ELSE
    Adx20 = (ADXval > 20)                  //If 13.5 already reached, set this flag to its
    ENDIF                                     //   current value (false or true, whichever the case)
    IF Adx20 THEN                             //If this flag was set, but ADX drops below 20, then
    IF ADXval < 20 THEN                    //   it must be reset to false
    Adx20 = 0
    ENDIF
    ENDIF
    ENDIF
    
    //               Conditions to enter a LONG trade
    c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    IF c1 AND Adx20 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    //               Conditions to enter a SHORT trade
    c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    IF c2 AND Adx20 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    //     Stop e target
    SET TARGET PPROFIT 10
    SET STOP PLOSS 10
    

    Just try it.

    I hope someone else puts an eye on it just to make sure it is correct, I did not start coding strategies much time ago.

    #14596 quote
    robertogozzi
    Moderator
    Master

    It was automatically so badly indented, I hope this is better:

    DEFPARAM CumulateOrders = False              //No more than 1 trade allowed
    
    ONCE Adx13 = 0                               //Make sure it has an initial value
    ONCE Adx20 = 0                               //Make sure it has an initial value
    
    ADXval = ADX[21]                             //Current ADX value
    
    Ema4   = ExponentialAverage[4]               //Current value for all EMA's
    Ema9   = ExponentialAverage[9]
    Ema20  = ExponentialAverage[20]
    
    IF ONMARKET THEN                             //If a trade is entered, then reset flags to false
       Adx13 = 0                                 //   to restart the whole sequence for next trades
       Adx20 = 0
       IF LONGONMARKET THEN
          IF Ema4 CROSSES UNDER Ema9 THEN        //   Exit LONG trades if needed
             SELL AT MARKET
          ENDIF
       ENDIF
       IF SHORTONMARKET THEN
          IF Ema4 CROSSES OVER Ema9 THEN         //   Exit SHORT trades if needed
             EXITSHORT AT MARKET
          ENDIF
       ENDIF
    ELSE                                         //otherwise start the sequence...
       IF Adx13 = 0 THEN                         //If 13.5 not reached yet, then
          Adx20 = 0                              //   reset this flag to 0, just in case and...
          Adx13 = (ADXval <= 13.5)               //   set this one to its current value
       ELSE
          Adx20 = (ADXval > 20)                  //If 13.5 already reached, set this flag to its
       ENDIF                                     //   current value (false or true, whichever the case)
       IF Adx20 THEN                             //If this flag was set, but ADX drops below 20, then
          IF ADXval < 20 THEN                    //   it must be reset to false
             Adx20 = 0
          ENDIF
       ENDIF
    ENDIF
    
    //               Conditions to enter a LONG trade
    c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    IF c1 AND Adx20 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    
    //               Conditions to enter a SHORT trade
    c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    IF c2 AND Adx20 THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    //     Stop e target
    SET TARGET PPROFIT 10
    SET STOP PLOSS 10
    
    Wing and Tom Hoo thanked this post
    #14601 quote
    Nicolas
    Keymaster
    Master

    Thanks a lot Roberto for helping others members. You deserve to be awarded for this 😉

    Code is not indended automatically I’m sorry. This is something I need to work to… All codes you see on website which is indended has been made manually..


    @tom
    Please update your country in your profile. Thanks.

    #14621 quote
    Tom Hoo
    Participant
    New

    Thanks Roberto

    Can’t wait to try it out later to night. Many thanks for even trying.

    #14855 quote
    Tom Hoo
    Participant
    New

    Hi Mr Roberto

    The Algo works fine when i Back test it. But i am not sure why when I run it it for the whole of today. It does not seems to follow the same logic.

    As you can see it made a few order when ADX are below 20.

    It should not have since ADX had not went down to 13.5 and was never above 20 as well.

    Did i mess up the code while i was editing ?

     

    DEFPARAM CumulateOrders = False //No more than 1 trade allowed

    ONCE Adx13 = 0 //Make sure it has an initial value
    ONCE Adx20 = 0 //Make sure it has an initial value

    ADXval = ADX[21] //Current ADX value

    Ema4 = ExponentialAverage[9] //Current value for all EMA’s
    Ema9 = ExponentialAverage[13]
    Ema8 = ExponentialAverage[40]
    Ema20 = ExponentialAverage[20]

    IF ONMARKET THEN //If a trade is entered, then reset flags to false
    Adx13 = 0 // to restart the whole sequence for next trades
    Adx20 = 0
    IF LONGONMARKET THEN
    IF Ema4 CROSSES UNDER Ema8 THEN // Exit LONG trades if needed
    SELL AT MARKET
    ENDIF
    ENDIF
    IF SHORTONMARKET THEN
    IF Ema4 CROSSES OVER Ema8 THEN // Exit SHORT trades if needed
    EXITSHORT AT MARKET
    ENDIF
    ENDIF
    ELSE //otherwise start the sequence…
    IF Adx13 = 0 THEN //If 13.5 not reached yet, then
    Adx20 = 0 // reset this flag to 0, just in case and…
    Adx13 = (ADXval <= 13.5) // set this one to its current value
    ELSE
    Adx20 = (ADXval > 20) //If 13.5 already reached, set this flag to its
    ENDIF // current value (false or true, whichever the case)
    IF Adx20 THEN //If this flag was set, but ADX drops below 20, then
    IF ADXval < 20 THEN // it must be reset to false
    Adx20 = 0
    ENDIF
    ENDIF
    ENDIF

    // Conditions to enter a LONG trade
    c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    IF c1 AND Adx20 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    // Conditions to enter a SHORT trade
    c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    IF c2 AND Adx20 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF

    // Stop e target
    SET TARGET PPROFIT 5
    SET STOP $LOSS 4

    Slide1.jpg Slide1.jpg
    #14897 quote
    robertogozzi
    Moderator
    Master

    Actually ProOrder enters trades which shouldn’t be entered, but the Algo does seem correct.

    I tried to limit trades to Oct. 12th 10-18, just to test fewer trades and I found that ALL trades should not have entered!

    I wonder if the problem is in the instructions ONCE and/or ONMARKET, but I am pretty sure the logic is fine.

    Maybe Nicolas could help us better.

    #14949 quote
    robertogozzi
    Moderator
    Master

    I paste the code, more readable, but without significant changes that make it behave correctly:

    DEFPARAM CumulateOrders = False        //No more than 1 trade allowed
    
    ONCE Adx13 = 0                         //Make sure it has an initial value
    ONCE Adx20 = 0                         //Make sure it has an initial value
    
    ADXval     = ADX[21]                   //Current ADX value
    
    Ema4       = ExponentialAverage[9]     //Current value for all EMA's
    Ema9       = ExponentialAverage[13]
    Ema8       = ExponentialAverage[40]
    Ema20      = ExponentialAverage[20]
    
    IF ONMARKET THEN                       //If a trade is entered, then reset flags to false
       Adx13 = 0                           //  to restart the whole sequence for next trades
       Adx20 = 0
       IF LONGONMARKET THEN
          IF Ema4 CROSSES UNDER Ema8 THEN  //  Exit LONG trades if needed
             SELL AT MARKET
          ENDIF
       ENDIF
       IF SHORTONMARKET THEN
          IF Ema4 CROSSES OVER Ema8 THEN   //  Exit SHORT trades if needed
             EXITSHORT AT MARKET
          ENDIF
       ENDIF
    ELSE                                   //otherwise start the sequence...
       IF Adx13 = 0 THEN                   //  If 13.5 not reached yet, then
          Adx20 = 0                        //    reset this flag to 0, just in case and...
          Adx13 = (ADXval <= 13.5)         //    set this one to its current value
       ELSE
          Adx20 = (ADXval > 20)            //  If 13.5 already reached, set this flag to its
       ENDIF                               //    current value (false or true,whichever the case)
       //IF Adx20 THEN            useless  //  If this flag was set, but ADX drops below 20, then
       //   IF ADXval <= 20 THEN     "     //    it must be reset to false
       //      Adx20 = 0             "
       //   ENDIF                    "
       //ENDIF                       "
    ENDIF
    
    //             Conditions to enter a LONG trade
    //c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    c1 = ((Ema4 > Ema9) AND (Ema9 > Ema20) OR 1)
    IF c1 AND Adx20 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    
    //             Conditions to enter a SHORT trade
    //c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    c2 = ((ema4 < Ema9) AND (Ema9 < Ema20) OR 1)
    IF c2 AND Adx20 THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    //  Stop & target
    SET TARGET PPROFIT 5
    SET STOP $LOSS 4
    

    I modified lines 42 and 49 so that it only enters trades when ADX > 20 (and should have previously gone at or below 13.5), no matter the ema’s. It appears that Adx20 contains the value 1 (true) as if line 31 had been executed, which shouldn’t have been.

    I even wonder if too many IF…THEN…ELSE’s may confuse ProBackTest/ProOrder.

    I also commented out some useless lines (33-37).

    Any further help is highly appreciated.

    #16402 quote
    robertogozzi
    Moderator
    Master

    After thinking it over in my spare time I could write the correct code.

    Please try it and check if trades are entered/exited correctly (of course you will have to adjust values for profitable trades):

    DEFPARAM CumulateOrders = False        //No more than 1 trade allowed
    
    ONCE Adx13  = 0                        //Make sure it has an initial value
    ONCE Adx20  = 0                        //Make sure it has an initial value
    ONCE AdxR20 = 0                        //Make sure it has an initial value
    
    ADXval     = ADX[21]                   //Current ADX  value
    ADXRval    = ADXR[21]                  //Current ADXR value
    
    Ema4       = ExponentialAverage[4]     //Current value for all EMA's
    Ema9       = ExponentialAverage[9]
    Ema20      = ExponentialAverage[20]
    
    IF ONMARKET THEN                       //If a trade is entered, then reset flags to false
    Adx13  = 0                          //  to restart the whole sequence for next trades
    Adx20  = 0
    AdxR20 = 0
    IF LONGONMARKET THEN
    IF Ema4 CROSSES UNDER Ema9 THEN  //  Exit LONG trades if needed
    SELL AT MARKET
    ENDIF
    ENDIF
    IF SHORTONMARKET THEN
    IF Ema4 CROSSES OVER Ema9 THEN   //  Exit SHORT trades if needed
    EXITSHORT AT MARKET
    ENDIF
    ENDIF
    ELSE                                   //otherwise start the sequence
    IF (AdxVal <= 13.5) THEN
    IF Adx20 THEN
    Adx13 = 0
    ENDIF
    ENDIF
    IF Adx13 = 0 THEN                   //  If 13.5 not reached yet, then
    Adx20 = 0                        //    reset this flag to 0, just in case and...
    AdxR20= 0
    Adx13 = (ADXval <= 13.5)         //    ...set this one to its current value
    ELSE
    Adx20 = (ADXval  > 20)           //  If 13.5 already reached, set this flag to its
    AdxR20= (ADXRVal > 20)           //    current value (false or true,whichever the case)
    ENDIF
    ENDIF
    
    //             Conditions to enter a LONG trade
    c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    IF c1 AND Adx20 AND AdxR20 THEN
    Adx13  = 0
    Adx20  = 0
    AdxR20 = 0
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    //             Conditions to enter a SHORT trade
    c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    IF c2 AND Adx20 AND AdxR20 THEN
    Adx13  = 0
    Adx20  = 0
    AdxR20 = 0
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    //  Stop & target
    SET TARGET PPROFIT 5
    SET STOP $LOSS 4
    
    
    
    ALE thanked this post
    #16403 quote
    robertogozzi
    Moderator
    Master

    Sorry, but indentation still was not appealing, so I used Notepad to amend it:

    DEFPARAM CumulateOrders = False        //No more than 1 trade allowed
    
    ONCE Adx13  = 0                        //Make sure it has an initial value
    ONCE Adx20  = 0                        //Make sure it has an initial value
    ONCE AdxR20 = 0                        //Make sure it has an initial value
    
    ADXval     = ADX[21]                   //Current ADX  value
    ADXRval    = ADXR[21]                  //Current ADXR value
    
    Ema4       = ExponentialAverage[4]     //Current value for all EMA's
    Ema9       = ExponentialAverage[9]
    Ema20      = ExponentialAverage[20]
    
    IF ONMARKET THEN                       //If a trade is entered, then reset flags to false
       Adx13  = 0                          //  to restart the whole sequence for next trades
       Adx20  = 0
       AdxR20 = 0
       IF LONGONMARKET THEN
          IF Ema4 CROSSES UNDER Ema9 THEN  //  Exit LONG trades if needed
             SELL AT MARKET
          ENDIF
       ENDIF
       IF SHORTONMARKET THEN
          IF Ema4 CROSSES OVER Ema9 THEN   //  Exit SHORT trades if needed
             EXITSHORT AT MARKET
          ENDIF
       ENDIF
    ELSE                                   //otherwise start the sequence
       IF (AdxVal <= 13.5) THEN
          IF Adx20 THEN
             Adx13 = 0
          ENDIF
       ENDIF
       IF Adx13 = 0 THEN                   //  If 13.5 not reached yet, then
          Adx20 = 0                        //    reset this flag to 0, just in case and...
          AdxR20= 0
          Adx13 = (ADXval <= 13.5)         //    ...set this one to its current value
       ELSE
          Adx20 = (ADXval  > 20)           //  If 13.5 already reached, set this flag to its
          AdxR20= (ADXRVal > 20)           //    current value (false or true,whichever the case)
       ENDIF
    ENDIF
    
    //             Conditions to enter a LONG trade
    c1 = (Ema4 > Ema9) AND (Ema9 > Ema20)
    IF c1 AND Adx20 AND AdxR20 THEN
       Adx13  = 0
       Adx20  = 0
       AdxR20 = 0
       BUY 1 CONTRACT AT MARKET
    ENDIF
    
    //             Conditions to enter a SHORT trade
    c2 = (ema4 < Ema9) AND (Ema9 < Ema20)
    IF c2 AND Adx20 AND AdxR20 THEN
       Adx13  = 0
       Adx20  = 0
       AdxR20 = 0
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    //  Stop & target
    SET TARGET PPROFIT 5
    SET STOP $LOSS 4
    
    Cosmic1 thanked this post
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Please can someone help me code this Strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Tom Hoo @tom_hoo Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by robertogozzi
9 years, 3 months ago.

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