TradingView
marcoderoni
17 พฤษภา 2020 เวลา 16 นาฬิกา 4 นาที

Combing in MACD and MTF 

Bitcoin / TetherUSBinance

คำอธิบาย

Hi all, I'm trying to wedge in the MACD into a multiple timeframe. Scope is to create:
1) an alert when the MACD across all timeframes is positive,
2) an alert when the MACD across all timeframes is negative, and
3) one when neither of them is applicable.

Would anyone be so kind to give it some thoughts, please?

//@version=2
strategy("[RichG] Easy MTF Strategy", overlay=false)

TF_1_time = input("3", "Timeframe 1")
TF_2_time = input("5", "Timeframe 2")
TF_3_time = input("15", "Timeframe 3")
TF_4_time = input("30", "Timeframe 4")

fastLen = input(title="Fast Length", type=integer, defval=12)
slowLen = input(title="Slow Length", type=integer, defval=26)
sigLen = input(title="Signal Length", type=integer, defval=9)
[macdLine, signalLine, _] = macd(close, fastLen, slowLen, sigLen)

width = 5
upcolor = green
downcolor = red
neutralcolor = blue
linestyle = line

TF_1 = security(tickerid, TF_1_time, open) < security(tickerid, TF_1_time, close) ? true:false
TF_1_color = TF_1 ? upcolor:downcolor

TF_2 = security(tickerid, TF_2_time, open) < security(tickerid, TF_2_time, close) ? true:false
TF_2_color = TF_2 ? upcolor:downcolor

TF_3 = security(tickerid, TF_3_time, open) < security(tickerid, TF_3_time, close) ? true:false
TF_3_color = TF_3 ? upcolor:downcolor

TF_4 = security(tickerid, TF_4_time, open) < security(tickerid, TF_4_time, close) ? true:false
TF_4_color = TF_4 ? upcolor:downcolor

TF_global = TF_1 and TF_2 and TF_3 and TF_4
TF_global_bear = TF_1 == false and TF_2 == false and TF_3 == false and TF_4 == false
TF_global_color = TF_global ? green : TF_global_bear ? red : white
TF_trigger_width = TF_global ? 6 : width

plot(1, style=linestyle, linewidth=width, color=TF_1_color)
plot(5, style=linestyle, linewidth=width, color=TF_2_color)
plot(10, style=linestyle, linewidth=width, color=TF_3_color)
plot(15, style=linestyle, linewidth=width, color=TF_4_color)
plot(25, style=linestyle, linewidth=4, color=TF_global_color)

exitCondition_Long = TF_global_bear
exitCondition_Short = TF_global

longCondition = TF_global
if (longCondition)
strategy.entry("MTF_Long", strategy.long)

shortCondition = TF_global_bear
if (shortCondition)
strategy.entry("MTF_Short", strategy.short)

strategy.close("MTF_Long", when=exitCondition_Long)
strategy.close("MTF_Short", when=exitCondition_Short)

เอกสารเผยแพร่

cleaned up the MACD references in the MTF snippets

เอกสารเผยแพร่

updated the exit points

เอกสารเผยแพร่

polished the exit points

เอกสารเผยแพร่

polished the name

เอกสารเผยแพร่

Guys, I need some help, please.

1) why doesn't my script close a position when TF_global_off kicks in? and
2) why there is a gap of time between the fulfilment of a condition (all green or all red) and the actual opening?

thanks in advance

เอกสารเผยแพร่

further polishing

เอกสารเผยแพร่

this should be the final version. Exit points correctly visualised and good results.
only regret I have is that I would prefer to be able to count data from the open of the candle, instead of the close.
ความคิดเห็น
asclepius22
Hi, great work! thank you. May i ask what is the settings for the time frames that is working best for you?
marcoderoni
STUDY here below
(any suggestion on how to avoid repainting other than setting "once per bar close"?)

==================

//@version=4
study("MACD on MTF", overlay=false)

TF_1_time = input(title = "Timeframe 1", type = input.resolution, defval= "30")
TF_2_time = input(title = "Timeframe 2", type = input.resolution, defval= "30")
TF_3_time = input(title = "Timeframe 3", type = input.resolution, defval= "30")
TF_4_time = input(title = "Timeframe 4", type = input.resolution, defval= "15")

fastLen = input(title="Fast Length", type=input.integer, defval=12)
slowLen = input(title="Slow Length", type=input.integer, defval=26)
sigLen = input(title="Signal Length", type=input.integer, defval=9)
[macdLine, signalLine, _] = macd(close, fastLen, slowLen, sigLen)

width = 5
upcolor = color.green
downcolor = color.red
neutralcolor = color.blue
linestyle = plot.style_line

TF_1 = security(syminfo.tickerid, TF_1_time, signalLine) < security(syminfo.tickerid, TF_1_time, macdLine) ? true:false
TF_1_color = TF_1 ? upcolor:downcolor

TF_2 = security(syminfo.tickerid, TF_2_time, signalLine) < security(syminfo.tickerid, TF_2_time, macdLine) ? true:false
TF_2_color = TF_2 ? upcolor:downcolor

TF_3 = security(syminfo.tickerid, TF_3_time, signalLine) < security(syminfo.tickerid, TF_3_time, macdLine) ? true:false
TF_3_color = TF_3 ? upcolor:downcolor

TF_4 = security(syminfo.tickerid, TF_4_time, signalLine) < security(syminfo.tickerid, TF_4_time, macdLine) ? true:false
TF_4_color = TF_4 ? upcolor:downcolor

TF_global = TF_1 == true and TF_2 == true and TF_3 == true and TF_4 == true
TF_global_bear = TF_1 == false and TF_2 == false and TF_3 == false and TF_4 == false
f_01(_c) => _c ? 1 : 0
TF_count = f_01(TF_1) + f_01(TF_2) + f_01(TF_3) + f_01(TF_4)
TF_global_off = TF_count > 0 and TF_count < 4

TF_global_color = TF_global ? color.green : TF_global_bear ? color.red : color.white
TF_trigger_width = TF_global ? 6 : width

plot(1, style=linestyle, linewidth=width, color=TF_1_color)
plot(5, style=linestyle, linewidth=width, color=TF_2_color)
plot(10, style=linestyle, linewidth=width, color=TF_3_color)
plot(15, style=linestyle, linewidth=width, color=TF_4_color)
plot(25, style=linestyle, linewidth=4, color=TF_global_color)

exitCondition_Long = TF_global_bear or TF_global_off
exitCondition_Short = TF_global or TF_global_off

longCondition = TF_global
alertcondition(title="LONG", condition=longCondition, message="LONG")

shortCondition = TF_global_bear
alertcondition(title="SHORT", condition=shortCondition, message= "SHORT")

alertcondition(title="EXIT LONG", condition=TF_global_bear or TF_global_off, message= "EXIT LONG")
alertcondition(title="EXIT SHORT", condition=TF_global or TF_global_off, message= "EXIT SHORT")
Littlefish2020
@marcoderoni, you are a genius. I set the time frames to 4 hours and 1 day and can get 85%. Thank you!
Littlefish2020
@marcoderoni, when I try to set the alarm, it says "this alert may trigger differently than expected, since it's based on an indicator that can get repainted." How can we set the alarm? Thanks.
Miketinkle
Good idea
AmyLand
Thanks
Erkam
If you use close in security, this will cause you problems.
marcoderoni
@Erkam, thanks! Can I ask you for one further suggestion? I see that instead of keeping a position, my script is opening and closing it several times, would you know why, by chance?
Erkam
@marcoderoni, You must use close [1] in security.
In this way, you can take the closed candle in resolution.
If you use Close, it will give you fake results.
Erkam
@Erkam, You can use version 3 or version 4 to get real results.
เพิ่มเติม