LéoParticipant
Average
Dear All,
I hope I am posting this subject in the correct section of the website.
I have no experience in programming and I think that the best way for me to understand the codes posted on this website is to try to work on my own projects.
Therefore I tried to write the code below, the idea is to take position when prices crosses MM7 and x prior closes are above or under MM7. A lot of it is based on the SL strategies that I found in a thread created by Gianluca and originaly developped by Nicolas and Ale.
I assume there is a lot of mistakes in this code, but I hardly see my mistake myself.
Can you please comment on this ?
Thank you all for your contribution to this website, very much appreciated.
Best regards.
Alex.
//-------------------------------------------------------------------------
// Code principal : Break MM7 H4 Dax
// Stop strategy adapted from Gianluca/Nicolas/Ale https://www.prorealcode.com/topic/trailing-stop-and-breakeven-codes/
//-------------------------------------------------------------------------
// Dax - IG MARKETS// TIME FRAME H4
//idea1 : Buy when Close crosses over MM7 and Close for the X previous periods were below MM7 (X=5 in this first code)
//idea2 : Set initial SL on the lowest of the X previous periods (X=5 in this first code)
//idea3 : Move SL to entry point when profit > 300 pts
//idea4 : Move SL to highest Close when Close crosses under MM7
//idea5 : If SL > 300 pts no trade
DEFPARAM CumulateOrders = false
MM7= Average[7]
C1= Close crosses over MM7 and Close[1]<MM7[1] and Close[2]<MM7[2] and Close[3]<MM7[3] and Close[4]<MM7[4] and Close[5]<MM7[5]
C2= Close crosses under MM7 and close[1]>MM7[1] and Close[2]>MM7[2] and Close[3]>MM7[3] and Close[4]>MM7[4] and Close[5]>MM7[5]
LongSLvalue= high-lowest[5](low)
ShortSLvalue= highest[5](high)-low
if not onmarket then
if C1 and longSLvalue < 300 then
buy 1 contract at (high+1) stop
endif
if C2 and shortSLvalue < 300 then
sell 1 contract at (low-1) stop
endif
endif
// Stop strategy adapted from Gianluca/Nicolas/Ale https://www.prorealcode.com/topic/trailing-stop-and-breakeven-codes/
//1/TRAILING STOP//////////////////////////////////////////////////////
once trailinstop= 1 //1 on - 0 off
trailingstart = 300 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//1 trailing stop function
if trailinstop>0 then
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
// set initial SL
if newSL=0 then
newSL=longSLvalue
endif
//first move (breakeven)
IF newSL>0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close crosses under MM7 THEN
newSL = low-1
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//Set initial SL
if newSL=0 then
newSL=shortSLvalue
endif
//first move (breakeven)
IF newSL>0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close crosses over MM7 THEN
newSL = high+1
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
///////////////////////////
LéoParticipant
Average
Dear All,
still trying to work on this strategy.
I tried the code below which works well on long position but does not take any short position.
I think I must be missing something very obvious.
Could someone help please ?
Best regards.
Alex.
DEFPARAM CumulateOrders = false
TaillePosition=5
SMA= Average[PeriodeMM]
C1= Close crosses over SMA and Summation[X](Close<SMA)=(X-1)
C2= Close crosses under SMA and Summation[X](Close>SMA)=(X-1)
LongSLvalue= high-lowest[5](low)
ShortSLvalue= highest[5](high)-low
if not longonmarket then
if C1 and longSLvalue < 300 then
buy TaillePosition contract at (high+1) stop
set stop ploss LongSLvalue
endif
endif
if not shortonmarket then
if C2 and shortSLvalue < 300 then
sell TaillePosition contract at (low-1) stop
set stop ploss ShortSLvalue
endif
endif
Change SELL to SELLSHORT in line 22.
BUY opens a long position.
SELL closes a long position
SELLSHORT opens a short position.
EXITSHORT closes a short position.
SMA should be as below, but as Longs have executed then I guess the default (Close) is being assumed by the code?
SMA= Average[PeriodeMM](Close)
Yes GraHal, CLOSE is assumed if that parameter is missing.
But I prefer to always add all parameters without caring about built-in assumptions.
LéoParticipant
Average
Dear Grahal, Vonasi and Roberto,
Thank you for your help – much appreciated.
I updated the code accordingly and backtested on Dow Jones in 1H. Results are not great Especially prior to 2015 but not so bad since.
I will try to optimize X and SMA and repost it here if I find good parameters.
Thank you again for your help.
DEFPARAM CumulateOrders = false
TaillePosition=1
SMA= Average[PeriodeMM](close)
C1= Close crosses over SMA and Summation[X](Close<SMA)=(X-1)
C2= Close crosses under SMA and Summation[X](Close>SMA)=(X-1)
LongSLvalue= high-lowest[5](low)
ShortSLvalue= highest[5](high)-low
if not onmarket then
if C1 and longSLvalue < 300 then
buy TaillePosition contract at (high+1) stop
set stop ploss LongSLvalue
endif
else
if C2 and shortSLvalue < 300 then
sellshort TaillePosition contract at (low-1) stop
set stop ploss ShortSLvalue
endif
endif
If onmarket then
if C1 and longSLvalue < 300 then
exitshort TaillePosition contract at (high+1) stop
endif
else
if C2 and shortSLvalue < 300 then
sell TaillePosition contract at (low-1) stop
endif
endif
LéoParticipant
Average
Hi All,
I simplified the code as below and tested different combinations for PMM and X.
Best results for Dow H1 are with PMM=154 and X=77. (2020 show particularly good results as it was able to capture most of the Feb/March adjustment).
Results are even better with larger SL but I found 300 good for now.
As mentionned this is experimental, but I am happy to hear your comments.
Thanks.
Alex.
DEFPARAM CumulateOrders = false
TaillePosition=2
PeriodeMM = PMM //154
X = XX //77
SLValue = SLV //300
SMA= Average[PeriodeMM](close)
C1= Close crosses over SMA and Summation[X](Close<SMA)=(X-1)
C2= Close crosses under SMA and Summation[X](Close>SMA)=(X-1)
if not longonmarket and C1 then
buy TaillePosition contract at (high+1) stop
set stop ploss SLValue
endif
if not shortonmarket and C2 then
sellshort TaillePosition contract at (low-1) stop
set stop ploss SLValue
endif
if C1 and shortonmarket then
set stop ploss SLValue
endif
if C2 and longonmarket then
set stop ploss SLValue
endif
Beware of over fitting. Divide your optimizing period into at least 1 IS+ 1 OOS periods.
Optimize on 70% of history (In-Sample period) and test the robustness (validation of optimized variables) with the 30% of history (the Out Of Sample period). You can automate this process with the walk forward tool: https://www.prorealcode.com/blog/learning/prorealtime-walk-analysis-tool/
(french videos: Analyse Walk Forward avec ProRealTime et Récapitulatif sur l’utilisation du module Walk Forward sous ProRealTime)
LéoParticipant
Average
Dear Nicolas,
Thank you for this link. I have been through them now and will try backtest + roll forward and post the results here.
Best regards.
LéoParticipant
Average
Dear Nicolas,
I worked on parameters using one IS / OOS (70/30) period as suggested (Dow 1h, 200’000 bars, Tick by tick mode).
WFE is 53,8 % using 150 for PMM and 112 for XX – which I understand is not so bad. I understand it would make sense to have more than one IS/OOS but it is running super slowly.
Alex.
That’s how you have to proceed, well done. If you have put your strategy live at the same moment of the vertical dotted line, then you had made this nice profit (of the OOS period).
LéoParticipant
Average
Thank you Nicolas !
Draw down is important and got worst on the OOS period.
Despite a RR of 2 I am not sure this would be worse to try live !
Alex
Ok, that’s why robustness test is important and should by conducted by anyone creating strategies.
LéoParticipant
Average
Dear Nicolas,
Maybe you can help to understand the issue I have.
I am testing the code below with variables to optimize PMM, XX, SLa and TPa.
The walk forward analysis below (TEST MM7 Break(7)) shows that the best variables would be PMM = 6 or 7, XX = 5 and SLa and TPa = 5 (200 K bares, Tick by tick, spread = 1 )
When I replace the variables in the code for PMM=6, XX=5 SLa=5 and TPa=5 (Test MM7 Break(9)) or PMM=7, XX=5 SLa=5 and TPa=5 (Test MM7 Break(10)) the stratgey fails after few months (200 K bares, Tick by tick, spread = 1 ).
I would have expected a gain curve more or less in line with the results of the walkforward analysis.
I definetly miss something – Can you please help ?
Thank you !
Alex.
DEFPARAM CumulateOrders = false
TaillePosition=1
PeriodeMM = PMM
X = XX
SL = SLa
TP = TPa
SMA= Average[PeriodeMM](close)
ATR = abs(close - open) > ATR * 2
C1= Close crosses over SMA
C1= C1 and Summation[X](Close<SMA)=(X-1)
C1= C1 and ATR
C2= Close crosses under SMA
C2= C2 and Summation[X](Close>SMA)=(X-1)
C2 = C2 and ATR
if not longonmarket and C1 then
buy TaillePosition contract at (high+1) stop
endif
if not shortonmarket and C2 then
sellshort TaillePosition contract at (low-1) stop
endif
set stop ploss SL
Set target pprofit TP