Hello everyone
First of all, my apologies for my English …
I am trying to write the code of one setup i use manually, trying to Improve it, runnig backtests.
The idea is to identify a change in direction of a moving average.
For example,
Exp Moving Average of 9, five days up. The trigger signal is first day pointing down.
// Conditions to enter long positions
indicator1 = ExponentialAverage[9](close)
c1 = (indicator1[1] < indicator1[2])
c2 = (indicator1[2] < indicator1[3])
c3 = (indicator1[3] < indicator1[4])
c4 = (indicator1[4] < indicator1[5])
c5 = (indicator1[5] < indicator1[6])
c6 = (indicator1 > indicator1[1])
c7 = (close > high[1])
IF c1 AND c2 AND c3 AND c4 AND c5 AND c6 AND c7 THEN
BUY 10000 CASH AT MARKET
ENDIF
I wrote the code like this.
Is this correct? It seems to work, but maybe there is a better way…
Any help is welcome.
Thank You
Hello everyone
I Finished first version of my setup. Inspired in the setup of Larry Williams.
The idea is to capture the signal from the direction of change MM9 Exp.
I still do not know if there is another way to encode the change of direction of a moving average. This was the one that occurred to me.
If anybody know other way, please share here.
But the code are not working as it should.
We should open the position in the breakout of the high / low after the candle that gives the signal; and not in the opening.
Any sugestions? I dont know what is wrong…
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = ExponentialAverage[9](close)
c1 = (indicator1[1] < indicator1[2])
c2 = (indicator1[2] < indicator1[3])
c3 = (indicator1[3] < indicator1[4])
c4 = (indicator1[4] < indicator1[5])
c5 = (indicator1[5] < indicator1[6])
c6 = (indicator1 > indicator1[1])
c7 = (close > high[1])
IF c1 AND c2 AND c3 AND c4 AND c5 AND c6 AND c7 THEN
BUY 10000 CASH AT MARKET
ENDIF
// Conditions to exit long positions
c8 = (close CROSSES UNDER indicator1)
IF c8 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
c1 = (indicator1[1] > indicator1[2])
c2 = (indicator1[2] > indicator1[3])
c3 = (indicator1[3] > indicator1[4])
c4 = (indicator1[4] > indicator1[5])
c5 = (indicator1[5] > indicator1[6])
c6 = (indicator1 < indicator1[1])
c7 = (close < Low[1])
IF c1 AND c2 AND c3 AND c4 AND c5 AND c6 AND c7 THEN
SELLSHORT 10000 CASH AT MARKET
ENDIF
// Conditions to exit short positions
c8 = (close CROSSES OVER indicator1)
IF c8 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP %LOSS 5
Thank you all
Conditions are always tested at a bar close and trade initiated at the open of the next bar. So obviously, your conditions need to be offset +1 somewhere.
About your moving average conditions, you could have made a loop through bars to test them with less code lines, but that’s ok too with the way you done 🙂
Nicolas if you have time would you mind providing an example of this with a Loop instead of all the conditions. Thank you for your guidance.
Nicolas, can you teach us how to code the loop to identify a change in the direction of the moving average? Please consider that the average should remain 5 days before turn.
Thank you.
I see 2 ways to make this test:
1/ first idea is a FOR/NEXT loop in the previous 5 bars:
test=0
for i = 1 to 5 do
if indicator1[i]>indicator[i+1] then
test = test+1
endif
next
c6 = (indicator1 < indicator1[1])
c7 = (close < Low[1])
if test=5 and c6 and c7 then
SELLSHORT 1 SHARE AT MARKET
endif
2/ second idea is a SUMMATION of the test result over the last 5 periods:
test = SUMMATION[5](indicator1>indicator[1])
c6 = (indicator1 < indicator1[1])
c7 = (close < Low[1])
if test=5 and c6 and c7 then
SELLSHORT 1 SHARE AT MARKET
endif
Nicolas, thank you for quick answer.
i am very happy and gratefull for your help, because i am learning a lot.
Of course I could not write this loop code, because I am still trying to take the first steps in Prorealcoding.
Now i will do some backtest and try to make this thing work.
I will give some news about it.
I have several good daily setups that would like to code in PRT but the problem is the opening.
Like in this setup, i want to open position in the breakout of the last bar.