Help please – is this loop correct?
Forums › ProRealTime English forum › ProOrder support › Help please – is this loop correct?
- This topic has 12 replies, 3 voices, and was last updated 2 years ago by Khaled.
-
-
12/28/2021 at 3:00 AM #183925
Hello folks!
I’d like my system to exit long if the MA changes direction at any period between two limits: 50 and 100. Is the following loop correct?
Please note the instruction “graph PeriodEL” shows a constant result of 101. Is it normal?
(can’t insert the code, the icon is missing in the toolbar)
12345678910cond1EL=0PeriodEL=0cond1EL = (((Average[PeriodEL](close)[2]<Average[PeriodEL](close)[1] and Average[PeriodEL](close)<Average[PeriodEL](close)[1])))FOR PeriodEL = 50 TO 100 DOIF longonmarket and (cond1EL) THENsell at marketendifNEXTgraph PeriodELMany Thanks
12/28/2021 at 5:44 AM #183927Please note the instruction “graph PeriodEL” shows a constant result of 101. Is it normal?
Hi Khaled – I did not check the functionality of your loop, but the 101 is normal. The interpreter / compiler will always first add the increment, before it notices that it crosses your set limit (the 100) and then exits. Thus indeed, under the loop you will always observe one increment-step (1 in your case) too high.
1 user thanked author for this post.
12/28/2021 at 5:47 AM #183928cond1EL = (((Average[PeriodEL](close)[2]<Average[PeriodEL](close)[1] and Average[PeriodEL](close)<Average[PeriodEL](close)[1])))
But that should be inside of the loop. Then it may do what you want. 🙂
1 user thanked author for this post.
12/28/2021 at 9:34 AM #183933I think this code may only work in backtest, because in autotrading it will immediately stopped, as the first time PeriodEL retains 0, and NO indicator allows 0 as the number of periods.
What use is the LOOP? Why should you want to check 51 times the same condition and place 51 orders whent it’s true?
1 user thanked author for this post.
12/28/2021 at 9:49 AM #183939What use is the LOOP? Why should you want to check 51 times the same condition and place 51 orders whent it’s true?
I am not Khaled, but the condition would change if Khaled only put the condition within the loop (I can’t guarantee that this is his intention, but it looks like it or else you are right ;-)).
Next, he doesn’t place 51 orders, as his orders are Exits under the condition of OnMarket. Your stipulation, however, would be correct, knowing that the If OnMarket should be the condition to enter the loop to begin with.Suggestion :
12345678910111213cond1EL=0// PeriodEL=0 // This can be left out.If LongOnMarket thenFOR PeriodEL = 50 TO 100 DOcond1EL = (((Average[PeriodEL](close)[2]<Average[PeriodEL](close)[1] and Average[PeriodEL](close)<Average[PeriodEL](close)[1])))IF cond1EL THENsell at market// Command to Exit the loop here !endifNEXTendifgraph PeriodEL1 user thanked author for this post.
12/28/2021 at 10:01 AM #183940Roberto and Peter, thank you for your input. Much appreciated.
What I’m trying to do is to EXIT a Long trade when the EndPointMA changes direction on a higher TF. And this condition should apply for any period between 50 and 100. Example if EndPointMA[50] doesn’t give exit signal, but the EndPointMA[89] gives signal, then Exit Long. In the backtest, the best PERIODS are comprised between 50 and 100, specifically 58, 79, 86, 95, 96, 97, 98, 99 and 100.
I put this in a system and got errors, which I solved (trial and erros) as below.
Any better method to reach the same result?
Is the BREAK instruction in the right position?
is There a way to GRAPH the exact PERIOD at which the Exit took place?
Thanks a lot and have a nice day!
MA Loop to Exit a Trade123456789101112131415161718if islastbarupdate thencond1L=0PeriodEL1=34FOR PeriodEL1 = 50 TO 100 DOcond1L = (((EndPointAverage[PeriodEL1](close)[2]<EndPointAverage[PeriodEL1](close)[1] and EndPointAverage[PeriodEL1](close)<EndPointAverage[PeriodEL1](close)[1])))IF (cond1L) THENLong=1breakelsePeriodEL=34endifNEXTendif12/28/2021 at 10:12 AM #18394412/28/2021 at 10:25 AM #183953Next, he doesn’t place 51 orders, as his orders are Exits under the condition of OnMarket
You’re right. Sorry, I misread that line of code.
1 user thanked author for this post.
12/28/2021 at 10:31 AM #18395712/28/2021 at 10:40 AM #183962Is the BREAK instruction in the right position?
I think it is.
1if islastbarupdate thenWith this If you are over-doing it. So yes, this is good habit (for performance), but it will influence your averages. Thus :
1+2+3 / 3 is really not the same as 2+3+3 / 3.
I hope this example is clear ? … and of not, you’ll get the gist. 🙂1 user thanked author for this post.
12/28/2021 at 3:03 PM #184020What I thought was a solution doesn’t work as expected.
My problem: I’d like to exit a trade as follows. However, the backtest gives optimal PeriodMAL0 : 58, 75, 79 and 93 to 100.
I’d like to find a way to tell the system to pick one of these values and nothing in between. How can I do that please?
Also, is there a way to ask the system to continuously find the optimal PeriodMAL0? a sort of Machine Learning. I tried the few codes on the forum (ML of JuanJ), but no good result.
Thanks a million.
12345if longonmarket thenif ((EndPointAverage[PeriodMAL0](close)[2]<EndPointAverage[PeriodMAL0](close)[1] and EndPointAverage[PeriodMAL0](close)<EndPointAverage[PeriodMAL0](close)[1])) thensell at marketendifendif12/28/2021 at 3:45 PM #184030Try this one:
12345678910p58 = (EndPointAverage[58](close)[2] < EndPointAverage[58](close)[1]) AND (EndPointAverage[58](close) < EndPointAverage[58](close)[1])p75 = (EndPointAverage[75](close)[2] < EndPointAverage[75](close)[1]) AND (EndPointAverage[75](close) < EndPointAverage[75](close)[1])p79 = (EndPointAverage[79](close)[2] < EndPointAverage[79](close)[1]) AND (EndPointAverage[79](close) < EndPointAverage[79](close)[1])p93 = (EndPointAverage[93](close)[2] < EndPointAverage[93](close)[1]) AND (EndPointAverage[93](close) < EndPointAverage[93](close)[1])p100 = (EndPointAverage[100](close)[2] < EndPointAverage[100](close)[1]) AND (EndPointAverage[100](close) < EndPointAverage[100](close)[1])if longonmarket thenif p58 OR p75 OR p79 OR p93 OR p100 thensell at marketendifendif1 user thanked author for this post.
12/28/2021 at 3:50 PM #184031 -
AuthorPosts