[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)
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.

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.
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.
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 : )
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?
United States
United Kingdom
Home Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Profile Profile Settings Account and Billing Priority Support Ideas Published Followers Following Private Messages Chat Sign Out