ProRealCode - Trading & Coding with ProRealTime™
Can someone please help me to code this.
Condition to Buy or Sell. Happening in sequence
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
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.
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
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.
Thanks Roberto
Can’t wait to try it out later to night. Many thanks for even trying.
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
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.
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.
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
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
Please can someone help me code this Strategy
This topic contains 9 replies,
has 3 voices, and was last updated by
robertogozzi
9 years, 3 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 10/10/2016 |
| Status: | Active |
| Attachments: | No files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.