MTF Sequential Setup
07/10/2019 at 10:28 PM #102415
The problem is how to develop a code that applies a sequential MTF setup ?
In UT1 an event A is recognized then for a wile (as n periods), we are waiting for a new event B in UT2.
Then repeat a few more sequential signals, waiting a new event C in UT3, then UT4…
(UT1 > UT2 > UT3 > UT4)
I tested several ideas with “summation[n](event)>0” “if event then timeevent = currenttime”, “barindex…”, but nothing works,
due to the impossibility to mark a first eventA in UT1 and for n period to not detect new eventA
then only to detect an eventB in UT2 and for n period not to detect again a new eventB
then only to detect an eventC in UT3…07/10/2019 at 11:35 PM #102416
There you go:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354ONCE LookBack = 5 //after 5 bars cancel eventTIMEFRAME(Daily,updateonclose)ONCE barA = 0eventA = average[10,0](close) CROSSES OVER average[30,0](close)IF eventA THENbarA = barindex //save the bar numberENDIFIF barA AND (barindex - barA) >= Lookback THEN //if too many bars have elapsed, then cancel eventeventA = 0ENDIFIF average[10,0](close) CROSSES UNDER average[30,0](close) THEN //cancel event on the opposite eventeventA = 0ENDIF//TIMEFRAME(4 hour,updateonclose)ONCE barB = 0eventB = rsi(close) CROSSES OVER 30IF eventB THENbarB = barindexENDIFIF barB AND (barindex - barB) >= Lookback THENeventB = 0ENDIFIF rsi(close) > 70 THENeventB = 0ENDIF//TIMEFRAME(1 hour,updateonclose)ONCE barC = 0eventC = MACD[12,26,9](close) > 0IF eventC THENbarC = barindexENDIFIF barC AND (barindex - barC) >= Lookback THENeventC = 0ENDIFIF MACD[12,26,9](close) < 0 THENeventC = 0ENDIF//TIMEFRAME(default)ONCE barD = 0eventD = (high = highest(high))IF eventD THENbarD = barindexENDIFIF barD AND (barindex - barD) >= Lookback THENeventD = 0ENDIFIF eventA AND eventB AND eventC AND eventD AND Not OnMarket THENBUY 1 CONTRACT AT MARKETSET STOP PLOSS 20SET TARGET PROFIT 60ENDIF07/11/2019 at 7:14 AM #102429
Thank you so much for your code proposal, that I immediately tried to understand 🙂
- For starters I would just like to check that I have been clear enough in the explanation of my sequential signal need.
The system need to detect event A in UT1 and to stop detection for n1 period.
During those n1 period it need to detect envent B in UT2 and to stop detection for n2 period
During those n2 period it need to detect event C in UT3 and to stop detection for n3 period
Why to stop detection ? Because of the need to check barindexA < barindexB < barindexC. If the code doesnt stop detection, the sequential setup on the market could be right but not the barindex sequence, and vice versa.
I can now try to understand your proposal and to discuss it :
- If I’m right, it detect the eventX and save the bar number (I need that for compare barA<barB<barC<barD) and goes eventX variable from 0 to 1.
But what happening if the eventX arrives again in the next bar ? Does it save again the new bar number and barX increases by 1 ?(I need him not to do it)
- Also, if too many bars have elapsed, then it cancel eventX by going eventX variable from 1 to 0. (I need that for to no longer consider that the event took place in the recognition of the sequential signal)
- Also, on a condition (you chose the opposite condition of the eventX) it goes eventX variable from 1 to 0.
Would it be possible through a similar or a different method to block the detection of event A for n periods once it has been detected a first time ?
We could then get the right sequential purchase signal
I look forward to reading you !07/11/2019 at 9:50 AM #102442
This is a squential approach, it will test event B only if event A has occurred and so on for the next events.
Event A will only last LookBackA bars, then it will be cleared, the same for all other events.
In my first version I just added an opposite condition to cancel events, but it’s not mandatory, especially now that there is a cancellation due to elapsed bars.
My first version did not detect the 4 events sequentially, just as they occurred. It only entered a trade when ALL events were true. When a new X event occurred, it simply updated its barindex value.
This new version is a more strict coding of what you requested.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657TIMEFRAME(Daily,updateonclose)ONCE LookBackA = 5 //after 5 bars reenable checking eventONCE barA = 0IF barA AND (barindex - barA) >= LookBackA THENbarA = 0ENDIFIF barA = 0 THENeventA = average[10,0](close) CROSSES OVER average[30,0](close)IF eventA THENbarA = barindex //save the bar numberENDIFENDIF//TIMEFRAME(4 hour,updateonclose)ONCE LookBackB = 5ONCE barB = 0IF (barB AND (barindex - barB) >= LookBackB) OR barA = 0 THENbarB = 0ENDIFIF barB = 0 AND barA THENeventB = rsi(close) CROSSES OVER 30IF eventB THENbarB = barindexENDIFENDIF//TIMEFRAME(1 hour,updateonclose)ONCE LookBackC = 5ONCE barC = 0IF (barC AND (barindex - barC) >= LookBackC) OR barB = 0 THENbarC = 0ENDIFIF barC = 0 AND barB THENeventC = MACD[12,26,9](close) > 0IF eventC THENbarC = barindexENDIFENDIF//TIMEFRAME(default)ONCE LookBackD = 5ONCE barD = 0IF (barD AND (barindex - barD) >= LookBackD) OR barC = 0 THENbarD = 0ENDIFIF barD = 0 AND barC THENeventD = (high = highest(high))IF eventD THENbarD = barindexENDIFENDIF//IF eventA AND eventB AND eventC AND eventD AND Not OnMarket THENBUY 1 CONTRACT AT MARKETSET STOP PLOSS 20SET TARGET PROFIT 60ENDIF07/11/2019 at 9:59 AM #10244407/19/2019 at 10:18 AM #102899
Thank you so much RobertoGozzi, I tried it, and after gaining more knowledge about proorder, I understand better your work and it look clever and simple (a very nice code)
GraHal, that library is incredible !
Have a nice Day
1 user thanked author for this post.07/19/2019 at 10:43 AM #10290307/19/2019 at 11:39 AM #102912
I couldn’t resist! 🙂
Changed only TF’s, SL /TP and LookBacks.
Anybody please backtest on 200k bars and post results.
Also post any improvements … looks like it has potential and be a shame to waste Philippe’s idea and Roberto’s excellent coding.
Results attached are with spread = 4.
ITF attached so you can quickly and easily use the settings in the Optimiser.
You must be logged in to access attached files.07/21/2019 at 8:58 AM #103051
with MTF, you can not amend a variable set in a timeframe within a lower timeframe. With your code Roberto, any idea how you could reset to 0 variable eventA eventB eventC on case eventD is not=1??07/21/2019 at 9:11 AM #103053
If eventD is not 1, nothing happens, why resetting the other ones?
In any case you should add another variable, say FLAG, on the DEFAULT TF, so that when it is set all other TF’s clear their variables or don’t set their values or, even better, don’t check their conditions.07/21/2019 at 9:40 AM #10305607/21/2019 at 9:55 AM #103058
To be clearer, if you use a volatility filter for exemple as last condition ( eventD), if this condition is not right directly, by the time it does, the market has already moved, so you don’t want to take the position.07/21/2019 at 10:22 AM #103059
You set eventD=0 so no position will be opened.
You may set FLAG=1 in the DEFAULT TF, then you may want to clear it when needed and use FLAG in lines 8,21 and 34 with IF:123IF Flag THENeventA = average[10,0](close) CROSSES OVER average[30,0](close)ENDIF08/31/2019 at 7:24 PM #106036
I noticed in your code you are using the average function with the period defined as 10 or 30 followed by comma and 0 such as “average[10,0](close)”.
What does the zero mean in this average calculation? Is that a variable to define the type of average e.g. simple/geometric etc?
Hope you can clarify.
Thanks08/31/2019 at 7:38 PM #106037
It’s the type of average as from https://www.prorealcode.com/documentation/average/