ramaParticipant
Senior
// Conditions to enter long positions
indicator1 = Average[50](close)
if (close crosses over indicator1) then
c2=0
endif
if (close crosses under indicator1) then
c2=0
endif
if close>=indicator1 then
c3= close-indicator1+c2
endif
if close<= indicator1 then
c3= -close+indicator1+c2
endif
return c3
ramaParticipant
Senior
I have written the code, it give some results but not expect
I expect the max value should be 420
where as I get 250 which not correct
Lines 3 through 10 are useless, since you never assign C2 any value.
C3 is the distance between CLOSE and the indicator for that single bar.
If you want the distance from CLOSE and the crossover then you’ll have to save the value of the indicator, or its BarIndex, when the crossover occurs.
ramaParticipant
Senior
I have corrected the code
it works fine
// Conditions to enter long positions
indicator1 = Average[50](close)
if (close crosses over indicator1) then
c2=indicator1
endif
if close>Average[50](close) then
c3=close-c2[1]
endif
return c3
Line 7 should reference c2 (not c2[1]), because when there is a crossover it would return the value of the previous crossover. From the candle following the crossover on it wouldn’t make any difference (despite it is logically wrong).
ramaParticipant
Senior
If I want to add number of bars in the same code what should i do. if I want to trade contrarian say if the value c3>=500 after x number of bars only. I dont want sell in very sharp trend, I want to keep some minimum number of bars
There you go:
// Conditions to enter long positions
indicator1 = Average[50](close)
if (close crosses over indicator1) then
c2=indicator1
MyBar = barindex
endif
if close>Average[50](close) then
c3=close-c2
endif
if c3 > 500*pipsize And (barindex - MyBar) > 5 then
MyBar = barindex
c2=indicator1
Else
c3 = 0
endif
return c3