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)
cond1EL=0
PeriodEL=0
cond1EL = (((Average[PeriodEL](close)[2]<Average[PeriodEL](close)[1] and Average[PeriodEL](close)<Average[PeriodEL](close)[1])))
FOR PeriodEL = 50 TO 100 DO
IF longonmarket and (cond1EL) THEN
sell at market
endif
NEXT
graph PeriodEL
Many Thanks
Please 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.
cond1EL = (((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. 🙂
I 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?
What 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 :
cond1EL=0
// PeriodEL=0 // This can be left out.
If LongOnMarket then
FOR PeriodEL = 50 TO 100 DO
cond1EL = (((Average[PeriodEL](close)[2]<Average[PeriodEL](close)[1] and Average[PeriodEL](close)<Average[PeriodEL](close)[1])))
IF cond1EL THEN
sell at market
// Command to Exit the loop here !
endif
NEXT
endif
graph PeriodEL
Roberto 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!
if islastbarupdate then
cond1L=0
PeriodEL1=34
FOR PeriodEL1 = 50 TO 100 DO
cond1L = (((EndPointAverage[PeriodEL1](close)[2]<EndPointAverage[PeriodEL1](close)[1] and EndPointAverage[PeriodEL1](close)<EndPointAverage[PeriodEL1](close)[1])))
IF (cond1L) THEN
Long=1
break
else
PeriodEL=34
endif
NEXT
endif
Ligne 10 of the code above should read “sell at market” instead of “Long=1”. I copied the code of the indicator instead of the system.
Next, 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.
Is there a way to make it better? Thanks
Is the BREAK instruction in the right position?
I think it is.
if islastbarupdate then
With 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. 🙂
What 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.
if longonmarket then
if ((EndPointAverage[PeriodMAL0](close)[2]<EndPointAverage[PeriodMAL0](close)[1] and EndPointAverage[PeriodMAL0](close)<EndPointAverage[PeriodMAL0](close)[1])) then
sell at market
endif
endif
Try this one:
p58 = (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 then
if p58 OR p75 OR p79 OR p93 OR p100 then
sell at market
endif
endif