The problem is how to develop a code that applies a sequential MTF setup ?
Example:
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…
There you go:
ONCE LookBack = 5 //after 5 bars cancel event
TIMEFRAME(Daily,updateonclose)
ONCE barA = 0
eventA = average[10,0](close) CROSSES OVER average[30,0](close)
IF eventA THEN
barA = barindex //save the bar number
ENDIF
IF barA AND (barindex - barA) >= Lookback THEN //if too many bars have elapsed, then cancel event
eventA = 0
ENDIF
IF average[10,0](close) CROSSES UNDER average[30,0](close) THEN //cancel event on the opposite event
eventA = 0
ENDIF
//
TIMEFRAME(4 hour,updateonclose)
ONCE barB = 0
eventB = rsi[14](close) CROSSES OVER 30
IF eventB THEN
barB = barindex
ENDIF
IF barB AND (barindex - barB) >= Lookback THEN
eventB = 0
ENDIF
IF rsi[14](close) > 70 THEN
eventB = 0
ENDIF
//
TIMEFRAME(1 hour,updateonclose)
ONCE barC = 0
eventC = MACD[12,26,9](close) > 0
IF eventC THEN
barC = barindex
ENDIF
IF barC AND (barindex - barC) >= Lookback THEN
eventC = 0
ENDIF
IF MACD[12,26,9](close) < 0 THEN
eventC = 0
ENDIF
//
TIMEFRAME(default)
ONCE barD = 0
eventD = (high = highest[20](high))
IF eventD THEN
barD = barindex
ENDIF
IF barD AND (barindex - barD) >= Lookback THEN
eventD = 0
ENDIF
IF eventA AND eventB AND eventC AND eventD AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
SET STOP PLOSS 20
SET TARGET PROFIT 60
ENDIF
Hi Robertogozzi,
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 !
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.
TIMEFRAME(Daily,updateonclose)
ONCE LookBackA = 5 //after 5 bars reenable checking event
ONCE barA = 0
IF barA AND (barindex - barA) >= LookBackA THEN
barA = 0
ENDIF
IF barA = 0 THEN
eventA = average[10,0](close) CROSSES OVER average[30,0](close)
IF eventA THEN
barA = barindex //save the bar number
ENDIF
ENDIF
//
TIMEFRAME(4 hour,updateonclose)
ONCE LookBackB = 5
ONCE barB = 0
IF (barB AND (barindex - barB) >= LookBackB) OR barA = 0 THEN
barB = 0
ENDIF
IF barB = 0 AND barA THEN
eventB = rsi[14](close) CROSSES OVER 30
IF eventB THEN
barB = barindex
ENDIF
ENDIF
//
TIMEFRAME(1 hour,updateonclose)
ONCE LookBackC = 5
ONCE barC = 0
IF (barC AND (barindex - barC) >= LookBackC) OR barB = 0 THEN
barC = 0
ENDIF
IF barC = 0 AND barB THEN
eventC = MACD[12,26,9](close) > 0
IF eventC THEN
barC = barindex
ENDIF
ENDIF
//
TIMEFRAME(default)
ONCE LookBackD = 5
ONCE barD = 0
IF (barD AND (barindex - barD) >= LookBackD) OR barC = 0 THEN
barD = 0
ENDIF
IF barD = 0 AND barC THEN
eventD = (high = highest[20](high))
IF eventD THEN
barD = barindex
ENDIF
ENDIF
//
IF eventA AND eventB AND eventC AND eventD AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
SET STOP PLOSS 20
SET TARGET PROFIT 60
ENDIF
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
Now I need time for deep backtests with the complete strategy, and I’ll come here for feed backs
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.
Hello Roberto.
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??
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.
Yep sorry I have another case in mind where eventD is the ultimate filtrer and cancel all above signals. Thx for the suggestion, will give a try
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.
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:
IF Flag THEN
eventA = average[10,0](close) CROSSES OVER average[30,0](close)
ENDIF
VNParticipant
Average
Hi there,
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.
Thanks