How to define the exclusive output of the first correction after the start of a new trend?
Dear all!
Unfortunately I am still quite inexperienced and I have no idea how to solve this. I am asking for the output of the first correction after the start of a new trend in the screener.
Trend is defined as EMA 100 > EMA 200, as well as momentum higher timeframe. (that is clear)
Correction is to be defined by a negative histogram for uptrends or vice versa.
I want to avoid the output of further corrections in the trend, only the first correction should be relevant. I hope someone can possibly solve this. Many thanks in advance.
I tested it and it seems to work as expected:
IF BarIndex <= 1 THEN
CrossOver = 0
Flag = 0
ENDIF
Ema100 = average[100,1](close)
Ema200 = average[200,1](close)
MyMACD = MACD[12,26,9](close)
IF Ema100 CROSSES OVER Ema200 THEN
CrossOver = 1
Flag = 0
ELSIF Ema100 CROSSES UNDER Ema200 THEN
CrossOver = 0
Flag = 0
ENDIF
MacdCross = MyMacd CROSSES UNDER 0
IF Flag THEN
MacdCross = 0
ELSIF CrossOver AND MacdCross THEN
Flag = 1
ENDIF
SCREENER[MacdCross]
wow, that was really fast, thanks a lot! But as far as I can see I’m not getting the results I want, I only changed the code slightly because I am using a very slow macd (30/60/25). e.g. usdhuf, tf 1 hour, is returned as hit, but there is already the 4th correction (macd negative). But anyway I got an idea how I can fix this. Thanks a lot
I used
IF BarIndex <= 1 THEN
CrossOver = 0
Flag = 0
ENDIF
Ema100 = average[100,1](close[1])
Ema200 = average[200,1](close[1])
MyMACD = MACD[30,60,25](close[1])
IF Ema100 CROSSES OVER Ema200 THEN
CrossOver = 1
Flag = 0
ELSIF Ema100 CROSSES UNDER Ema200 THEN
CrossOver = 0
Flag = 0
ENDIF
MacdCross = MyMacd CROSSES UNDER 0
IF Flag THEN
MacdCross = 0
ELSIF CrossOver AND MacdCross THEN
Flag = 1
ENDIF
SCREENER[MacdCross]
I think i have to add a count so that the flags / histogram are counted while the trend is still present and an output only occurs when count = 1.
Hi guys, I haven’t dealt with this question for a while and would now like to try to get an answer. The above code does not give the desired results in the screener. The question would be the first correction defined as a MACD cross after the start of a trend, e.g. by an EMA cross. Unfortunately, my knowledge is not sufficient for this. Maybe someone has an idea.
Hello,
Indeed you may need a count. Though the code of @robertogozzi looks good, I would try something like this (not tested):
IF BarIndex <= 1 THEN
CrossOver = 0
Flag = 0
ENDIF
Ema100 = average[100,1](close)
Ema200 = average[200,1](close)
MyMACD = MACD[12,26,9](close)
IF Ema100 CROSSES OVER Ema200 THEN
CrossOver = 1
Flag = 0
ELSIF Ema100 CROSSES UNDER Ema200 THEN
CrossOver = 0
Flag = 0
ENDIF
MacdCross = MyMacd CROSSES UNDER 0
IF CrossOver AND MacdCross THEN
Flag = Flag+1
ENDIF
SCREENER[Flag=1]
Line 5 to line 17 makes perfect sense to me, but I don’t understand counting events logically.
The suggested code unfortunately does not work and gives no results. It seems that the counting does not work.
Thanks nevertheless!
Hello,
To better know how the code works, you may start to develop an indicator to put on price that will show your signals. Then you will be able to screen more confidently I believe.
IF BarIndex <= 1 THEN
CrossOver = 0
Flag = 0
ENDIF
Ema100 = average[100,1](close)
Ema200 = average[200,1](close)
MyMACD = MACD[12,26,9](close)
IF Ema100 CROSSES OVER Ema200 THEN
CrossOver = 1
Flag = 0
ELSIF Ema100 CROSSES UNDER Ema200 THEN
CrossOver = 0
Flag = 0
ENDIF
MacdCross = MyMacd CROSSES UNDER 0
IF CrossOver AND MacdCross THEN
Flag = Flag+1
DRAWARROWUP(barindex,low-1*AverageTrueRange[14])coloured(204,0,0)
DRAWTEXT("N#Flag#",barindex,low-1.5*AverageTrueRange[14],SansSerif,Bold,11)coloured(0,0,0)
ENDIF
RETURN
PS: The screener may be SCREENER[CrossOver and MacdCross and Flag=1]
My code is working as expected, with any MACD setting you choose, as from my pic.
Try increasing the units on your chart, an Ema200 will require many more bars than its periods to be correctly calculated. Use at least 1K bars.
I’m sorry, but I don’t think that’s the case.
Attached is the screener, as well as the results in 4 hours.
As an example USDCAD – it gives me every single correction, but only the very first is asked for.
Yes, you asked for the very first one in your first post “I am asking for the output of the first correction after the start of a new trend in the screener“.
Yes, indeed, and can write a code for a screener that gives me any correction, but I am not able to filter only the first correction after the start of a new trend – that is what I want to learn
This one will allow you to detect a) the first correction only or b) all corrections:
IF BarIndex <= 1 THEN
CrossOver = 0
Flag = 0
FirstOnly = 1 //1=detect ONLY the first correction, 0=detect ALL corrections
ENDIF
Ema100 = average[100,1](close)
Ema200 = average[200,1](close)
MyMACD = MACD[30,60,25](close)
IF Ema100 CROSSES OVER Ema200 THEN
CrossOver = 1
Flag = 0
ELSIF Ema100 CROSSES UNDER Ema200 THEN
CrossOver = 0
Flag = 0
ENDIF
MacdCross = MyMacd CROSSES UNDER 0
IF Flag THEN
MacdCross = 0
ELSIF CrossOver AND MacdCross THEN
IF FirstOnly THEN
Flag = 1
ENDIF
ENDIF
SCREENER[MacdCross]
simply change 1 to 0 in line 4 to detect ALL corrections.