Please can someone help me code this Strategy
Forums › ProRealTime English forum › ProOrder support › Please can someone help me code this Strategy
- This topic has 9 replies, 3 voices, and was last updated 8 years ago by robertogozzi.
-
-
10/10/2016 at 1:50 PM #14579
Can someone please help me to code this.
Condition to Buy or Sell. Happening in sequence
- ADX to drop to value 13.5
- ADX to recover to above Value 20
- ADXR to go above Value 20 at this time ADX should be still above 20
- 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
10/10/2016 at 5:32 PM #14595This code should do, if I understood what you need:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253DEFPARAM CumulateOrders = False //No more than 1 trade allowedONCE Adx13 = 0 //Make sure it has an initial valueONCE Adx20 = 0 //Make sure it has an initial valueADXval = ADX[21] //Current ADX valueEma4 = ExponentialAverage[4] //Current value for all EMA'sEma9 = ExponentialAverage[9]Ema20 = ExponentialAverage[20]IF ONMARKET THEN //If a trade is entered, then reset flags to falseAdx13 = 0 // to restart the whole sequence for next tradesAdx20 = 0IF LONGONMARKET THENIF Ema4 CROSSES UNDER Ema9 THEN // Exit LONG trades if neededSELL AT MARKETENDIFENDIFIF SHORTONMARKET THENIF Ema4 CROSSES OVER Ema9 THEN // Exit SHORT trades if neededEXITSHORT AT MARKETENDIFENDIFELSE //otherwise start the sequence...IF Adx13 = 0 THEN //If 13.5 not reached yet, thenAdx20 = 0 // reset this flag to 0, just in case and...Adx13 = (ADXval <= 13.5) // set this one to its current valueELSEAdx20 = (ADXval > 20) //If 13.5 already reached, set this flag to itsENDIF // current value (false or true, whichever the case)IF Adx20 THEN //If this flag was set, but ADX drops below 20, thenIF ADXval < 20 THEN // it must be reset to falseAdx20 = 0ENDIFENDIFENDIF// Conditions to enter a LONG tradec1 = (Ema4 > Ema9) AND (Ema9 > Ema20)IF c1 AND Adx20 THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to enter a SHORT tradec2 = (ema4 < Ema9) AND (Ema9 < Ema20)IF c2 AND Adx20 THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Stop e targetSET TARGET PPROFIT 10SET STOP PLOSS 10Just 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.
10/10/2016 at 5:36 PM #14596It was automatically so badly indented, I hope this is better:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253DEFPARAM CumulateOrders = False //No more than 1 trade allowedONCE Adx13 = 0 //Make sure it has an initial valueONCE Adx20 = 0 //Make sure it has an initial valueADXval = ADX[21] //Current ADX valueEma4 = ExponentialAverage[4] //Current value for all EMA'sEma9 = ExponentialAverage[9]Ema20 = ExponentialAverage[20]IF ONMARKET THEN //If a trade is entered, then reset flags to falseAdx13 = 0 // to restart the whole sequence for next tradesAdx20 = 0IF LONGONMARKET THENIF Ema4 CROSSES UNDER Ema9 THEN // Exit LONG trades if neededSELL AT MARKETENDIFENDIFIF SHORTONMARKET THENIF Ema4 CROSSES OVER Ema9 THEN // Exit SHORT trades if neededEXITSHORT AT MARKETENDIFENDIFELSE //otherwise start the sequence...IF Adx13 = 0 THEN //If 13.5 not reached yet, thenAdx20 = 0 // reset this flag to 0, just in case and...Adx13 = (ADXval <= 13.5) // set this one to its current valueELSEAdx20 = (ADXval > 20) //If 13.5 already reached, set this flag to itsENDIF // current value (false or true, whichever the case)IF Adx20 THEN //If this flag was set, but ADX drops below 20, thenIF ADXval < 20 THEN // it must be reset to falseAdx20 = 0ENDIFENDIFENDIF// Conditions to enter a LONG tradec1 = (Ema4 > Ema9) AND (Ema9 > Ema20)IF c1 AND Adx20 THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to enter a SHORT tradec2 = (ema4 < Ema9) AND (Ema9 < Ema20)IF c2 AND Adx20 THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Stop e targetSET TARGET PPROFIT 10SET STOP PLOSS 1010/10/2016 at 6:26 PM #14601Thanks 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.10/11/2016 at 5:35 AM #1462110/13/2016 at 3:32 PM #14855Hi 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 valueADXval = 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 410/14/2016 at 12:12 PM #14897Actually 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.
10/15/2016 at 4:24 PM #14949I paste the code, more readable, but without significant changes that make it behave correctly:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556DEFPARAM CumulateOrders = False //No more than 1 trade allowedONCE Adx13 = 0 //Make sure it has an initial valueONCE Adx20 = 0 //Make sure it has an initial valueADXval = ADX[21] //Current ADX valueEma4 = ExponentialAverage[9] //Current value for all EMA'sEma9 = ExponentialAverage[13]Ema8 = ExponentialAverage[40]Ema20 = ExponentialAverage[20]IF ONMARKET THEN //If a trade is entered, then reset flags to falseAdx13 = 0 // to restart the whole sequence for next tradesAdx20 = 0IF LONGONMARKET THENIF Ema4 CROSSES UNDER Ema8 THEN // Exit LONG trades if neededSELL AT MARKETENDIFENDIFIF SHORTONMARKET THENIF Ema4 CROSSES OVER Ema8 THEN // Exit SHORT trades if neededEXITSHORT AT MARKETENDIFENDIFELSE //otherwise start the sequence...IF Adx13 = 0 THEN // If 13.5 not reached yet, thenAdx20 = 0 // reset this flag to 0, just in case and...Adx13 = (ADXval <= 13.5) // set this one to its current valueELSEAdx20 = (ADXval > 20) // If 13.5 already reached, set this flag to itsENDIF // 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 THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to enter a SHORT trade//c2 = (ema4 < Ema9) AND (Ema9 < Ema20)c2 = ((ema4 < Ema9) AND (Ema9 < Ema20) OR 1)IF c2 AND Adx20 THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Stop & targetSET TARGET PPROFIT 5SET STOP $LOSS 4I 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.
11/12/2016 at 2:17 AM #16402After 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):
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364DEFPARAM CumulateOrders = False //No more than 1 trade allowedONCE Adx13 = 0 //Make sure it has an initial valueONCE Adx20 = 0 //Make sure it has an initial valueONCE AdxR20 = 0 //Make sure it has an initial valueADXval = ADX[21] //Current ADX valueADXRval = ADXR[21] //Current ADXR valueEma4 = ExponentialAverage[4] //Current value for all EMA'sEma9 = ExponentialAverage[9]Ema20 = ExponentialAverage[20]IF ONMARKET THEN //If a trade is entered, then reset flags to falseAdx13 = 0 // to restart the whole sequence for next tradesAdx20 = 0AdxR20 = 0IF LONGONMARKET THENIF Ema4 CROSSES UNDER Ema9 THEN // Exit LONG trades if neededSELL AT MARKETENDIFENDIFIF SHORTONMARKET THENIF Ema4 CROSSES OVER Ema9 THEN // Exit SHORT trades if neededEXITSHORT AT MARKETENDIFENDIFELSE //otherwise start the sequenceIF (AdxVal <= 13.5) THENIF Adx20 THENAdx13 = 0ENDIFENDIFIF Adx13 = 0 THEN // If 13.5 not reached yet, thenAdx20 = 0 // reset this flag to 0, just in case and...AdxR20= 0Adx13 = (ADXval <= 13.5) // ...set this one to its current valueELSEAdx20 = (ADXval > 20) // If 13.5 already reached, set this flag to itsAdxR20= (ADXRVal > 20) // current value (false or true,whichever the case)ENDIFENDIF// Conditions to enter a LONG tradec1 = (Ema4 > Ema9) AND (Ema9 > Ema20)IF c1 AND Adx20 AND AdxR20 THENAdx13 = 0Adx20 = 0AdxR20 = 0BUY 1 CONTRACT AT MARKETENDIF// Conditions to enter a SHORT tradec2 = (ema4 < Ema9) AND (Ema9 < Ema20)IF c2 AND Adx20 AND AdxR20 THENAdx13 = 0Adx20 = 0AdxR20 = 0SELLSHORT 1 CONTRACT AT MARKETENDIF// Stop & targetSET TARGET PPROFIT 5SET STOP $LOSS 41 user thanked author for this post.
11/12/2016 at 2:23 AM #16403Sorry, but indentation still was not appealing, so I used Notepad to amend it:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364DEFPARAM CumulateOrders = False //No more than 1 trade allowedONCE Adx13 = 0 //Make sure it has an initial valueONCE Adx20 = 0 //Make sure it has an initial valueONCE AdxR20 = 0 //Make sure it has an initial valueADXval = ADX[21] //Current ADX valueADXRval = ADXR[21] //Current ADXR valueEma4 = ExponentialAverage[4] //Current value for all EMA'sEma9 = ExponentialAverage[9]Ema20 = ExponentialAverage[20]IF ONMARKET THEN //If a trade is entered, then reset flags to falseAdx13 = 0 // to restart the whole sequence for next tradesAdx20 = 0AdxR20 = 0IF LONGONMARKET THENIF Ema4 CROSSES UNDER Ema9 THEN // Exit LONG trades if neededSELL AT MARKETENDIFENDIFIF SHORTONMARKET THENIF Ema4 CROSSES OVER Ema9 THEN // Exit SHORT trades if neededEXITSHORT AT MARKETENDIFENDIFELSE //otherwise start the sequenceIF (AdxVal <= 13.5) THENIF Adx20 THENAdx13 = 0ENDIFENDIFIF Adx13 = 0 THEN // If 13.5 not reached yet, thenAdx20 = 0 // reset this flag to 0, just in case and...AdxR20= 0Adx13 = (ADXval <= 13.5) // ...set this one to its current valueELSEAdx20 = (ADXval > 20) // If 13.5 already reached, set this flag to itsAdxR20= (ADXRVal > 20) // current value (false or true,whichever the case)ENDIFENDIF// Conditions to enter a LONG tradec1 = (Ema4 > Ema9) AND (Ema9 > Ema20)IF c1 AND Adx20 AND AdxR20 THENAdx13 = 0Adx20 = 0AdxR20 = 0BUY 1 CONTRACT AT MARKETENDIF// Conditions to enter a SHORT tradec2 = (ema4 < Ema9) AND (Ema9 < Ema20)IF c2 AND Adx20 AND AdxR20 THENAdx13 = 0Adx20 = 0AdxR20 = 0SELLSHORT 1 CONTRACT AT MARKETENDIF// Stop & targetSET TARGET PPROFIT 5SET STOP $LOSS 41 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on