[RichG] Easy MTF Strategy

As we all know, timeframe agreement is a powerful tool. This strategy uses 4 time frames and the Keltner ATR for entries and exits.
Remove from Favorite Scripts Add to Favorite Scripts
strategy("[RichG] Easy MTF Strategy", overlay=false)

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

transaction_size = input(1, "Contract/Share Amount")

src = close, len = 20
out = sma(src, len)
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, qty=transaction_size, when=strategy.position_size == 0)

shortCondition = TF_global_bear
if (shortCondition)
    strategy.entry("MTF_Short", strategy.short, qty=transaction_size, when=strategy.position_size == 0)
strategy.close("MTF_Long", when=exitCondition_Long)    
strategy.close("MTF_Short", when=exitCondition_Short)
Nice, been looking for something like this!
Thanks! :) Hope it helps!
nightsuspect RichGTastyTrader
it repaints ?
+1 Reply
Does this give signals in real time? So if watching a stock, will it give the entry and exit in real time (when it happens), or is it all on end of day? This looks really good.
RichGTastyTrader doctorandrose
Yep - it works like the other TradingView strategies in that when you execute the strategy, it will recalculate everything. I have used it on 5min / 15min / 60min / 1 day charts.
How would you trade this with stop lose? It would be scary to trade with no stop lose!
RichGTastyTrader doctorandrose
This is just a strategy, feel free to tweak it as necessary :) My stop losses are more discretionary depending on how much I am willing to risk. I'm not even sure if TradingView platform supports setting stops/trailstops in strategies... I'll have to look into it.
NOTE: I believe I posted the incorrect version of this script. This one does NOT have the Keltner ATR entries/exists. It is purely based off of timeframe agreement. I will post the correct version shortly.
Could you please explain in simple terms how this all works? Is it four moving averages 1D, 5D, 15D, 45D, and when then all cross or in the same direction it goes long or short? I'm very interested in your strategy (indicator); just trying to understand it, before I use it to place any trades. What do all the colors mean?
RichGTastyTrader doctorandrose
Sure thing -- the time frames are somewhat arbitrary and were picked based on my personal time horizon preferences. When they are green from the bottom up, the very top bars will be green as well and that is a buy signal. When they are all red, the top bar will be red and that is a sell signal. When the very top bar is white, there is nothing to do. The reason why you see lots of top bars green w/o a trade in the strategy is because I wanted to limit it to just being in one trade at a time. So that means the strategy is already in a trade and it only enters a trade when your position count is zero. Hope that helps - I'd be happy to answer any other questions. I am no expert, but really think this indicator is a good tool to use (I use it as well but it isn't the only thing I base decisions off of)
Oh I see, it works so well because you know in hindsight what the larger TF closing prices are.
Nice indicator, RichG. Quick question, does it repaint and if so, how do you handle that?
Thanks, narv! I am no expert in how TradingView handles this, but I believe that if you go in the properties of the indicator, there is an option to "Recalculate on Every Tick". That should do the trick. Otherwise, I am not sure since it is handled by the TradingView platform rather than the developer.
+1 Reply
Is your Keltner ATR version better than the current version you have posted? I have used the Keltner Channel as a method to trade. When you post it, can you give a simple definition as to how it works?
RichGTastyTrader doctorandrose
I think the Keltner one is less risky and more realistic because it signals a close once the price closes above the ATR (if you are short) or below the ATR (if you are long). So, you get more entries/exits and less duration in the trade. Work has been busy lately, but I will post the correct version soon - I just need to tweak a few things.
RichGTastyTrader doctorandrose
Hello, I just posted the Keltner ATR version of the script. It works the same as this strategy, but when the price closes above/below the ATR of the opposite direction of your position, it will trigger a close transaction.
[RichG] Easy MTF Strategy v1.1
How would you program your first posted strategy, so that it will show on the chart ALL the buy and sell signals. I understand that if it's already long, it wont post (chart or buy/sell another contract), but I would like to SEE on the CHART each time there is ANOTHER buy or sell signal. This way if you are not already in the trade (which is unlikely), you can take the NEXT signal (which your code will not currently chart). Can you write a version that shows each signal?
RichGTastyTrader doctorandrose
Interesting -- ya, that makes sense. I will look into it. Thanks for the feedback : )
What does the first line mean? What does the "white" color mean?
williane doctorandrose
Means all of the time frames are not in agreement. You'll only get red/green on the top line if all time frames are the same.
Thanks Rich, great script. I see it performs very well in the simulation, have you tried this other systems (MT4 or something) or a live account? Always been curious about a system like this, just makes sense to me.
williane williane
Following up on my own comment...I see why it performs so well in backtests; in real-time, your entry points can move quite a bit as your highest time-frame changes from red to green or vise versa. This obviously doesn't happen when viewing results in the past. I still find it an incredibly useful tool to help identify the current trend. I actually converted the script to an indicator and added the ability for alerts (receive an alert when the overall trend first changes from red to green or green to red)
sorry for the delay, this slipped by me. thank you for the insightful comment -- i'm glad you find it useful! just another tool to tip you off on the trend is how i use it along with other indicators like TTM Squeeze and such. i havent tried using only this strategy in a real account.
Thanks; so is it a "buy" signal if the price closes higher than it opened on ALL the following time frames: daily, 5 day, 15 day & 45 day ? being a pine script newbie the "TF_#_time" definition mystifies me as I don't understand how the language knows to look at the daily , 5 day, 15 day & 45 day prices from "D","Timeframe #" ; "5D", "Timeframe #" ; "15D", "Timeframe #" ; "45D", "Timeframe #" ?
Thanks for the comment. You are correct -- all of the timeframes need to be in alignment for it to signal a "buy". The magic behind the timeframe variables is in the "security" function. The security function tells pinescript to get chart data for a different symbol/ time resolution. So you can ask it to retrieve info from any timeframe. I am passing the TF_#_time into the security function so it gives me back pricing info for that period. From the pinescript PDF:

"The function ‘security’ lets the user to request data from additional symbols and resolutions,
other than the ones to which the indicator is applied.....

The lowest resolution is ‘minute’
which is set by the literal “1”. It’s possible to request any number of minutes: “5”, “10”, “21”, etc.
‘Hourly’ resolution is also set by minutes. For example, the following lines signify an hour, two
hours and four hours respectively: “60”, “120”, “240”. A resolution with a value of 1 day is set by
the symbols “D” or “1D”. It’s possible to request any number of days: “2D”, “3D”, etc. Weekly
and monthly resolutions are set in a similar way: “W”, “1W”, “2W”, …, “M”, “1M”, “2M”. “M” and
“1M” are sorts of one month resolution value. “W” and “1W” are the same weekly resolution

There is a good pinescript PDF here:

Hope it helps.

United States
United Kingdom
Home Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Account and Billing Sign Out