本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © starshiptrade
//@version=5
indicator("Trend Following Moron TFM 10% System",shorttitle = "TFM 10% System" ,overlay=true)
// User input for moving average type (EMA or SMA)
ma_type = input.string("SMA", title="Moving Average Type", options=["SMA", "EMA"])
// User inputs for daily, weekly, and monthly moving average lengths
daily_ma_length = input.int(250, title="Daily Moving Average Length", minval=1)
weekly_ma_length = input.int(50, title="Weekly Moving Average Length", minval=1)
monthly_ma_length = input.int(12, title="Monthly Moving Average Length", minval=1)
// Function to select the correct MA type based on user input
ma_function(src, len) =>
ma_type == "EMA" ? ta.ema(src, len) : ta.sma(src, len)
// Calculate moving averages for different time frames
sma_daily = ma_function(close, daily_ma_length)
sma_weekly = ma_function(close, weekly_ma_length)
sma_monthly = ma_function(close, monthly_ma_length)
// Function to calculate trend-based MA color
ma_color(ma_value) =>
low > ma_value ? color.green : high < ma_value ? color.red : color.gray
// Plot all moving averages with trend-based color
plot(timeframe.isdaily ? sma_daily : na, title="Daily MA", color=ma_color(sma_daily), linewidth=2)
plot(timeframe.isweekly ? sma_weekly : na, title="Weekly MA", color=ma_color(sma_weekly), linewidth=2)
plot(timeframe.ismonthly ? sma_monthly : na, title="Monthly MA", color=ma_color(sma_monthly), linewidth=2)
// Calculate the 52-week closing high (weekly timeframe)
lookback_period_weekly = 52
highestCloseWeekly = ta.highest(close, lookback_period_weekly)
// Calculate the 12-month closing high (monthly timeframe)
lookback_period_monthly = 12
highestCloseMonthly = ta.highest(close, lookback_period_monthly)
// Background fill for daily chart based on 50-week high
lookback_period_daily = 252 // Approximate 52 weeks with 252 trading days
highestCloseDaily = ta.highest(close, lookback_period_daily)
// User input to switch between ATR or Percentage method
method = input.string("Percentage", title="Method", options=["ATR", "Percentage"])
// User input for ATR-based levels
atr_multiple_level1 = input.float(2.0, title="ATR Multiple for Level 1", minval=0.1)
atr_multiple_level2 = input.float(4.0, title="ATR Multiple for Level 2", minval=0.1)
// User input for Percentage-based levels
percent_level1 = input.float(5.0, title="% Percentage for Level 1", minval=0.1) / 100
percent_level2 = input.float(10.0, title="% Percentage for Level 2", minval=0.1) / 100
// Calculate ATR over a specified period (e.g., 14 days)
atr_length = input.int(14, title="ATR Length", minval=1)
atr = ta.atr(atr_length)
// Define percentage-based levels for daily chart based on user input
level1_percentage_daily = highestCloseDaily * (1 - percent_level1)
level2_percentage_daily = highestCloseDaily * (1 - percent_level2)
// Define percentage-based levels for weekly chart
level1_percentage_weekly = highestCloseWeekly * (1 - percent_level1)
level2_percentage_weekly = highestCloseWeekly * (1 - percent_level2)
// Define percentage-based levels for monthly chart
level1_percentage_monthly = highestCloseMonthly * (1 - percent_level1)
level2_percentage_monthly = highestCloseMonthly * (1 - percent_level2)
// Define ATR-based levels for daily chart
level1_atr_daily = highestCloseDaily - atr * atr_multiple_level1
level2_atr_daily = highestCloseDaily - atr * atr_multiple_level2
// Define ATR-based levels for weekly chart
level1_atr_weekly = highestCloseWeekly - atr * atr_multiple_level1
level2_atr_weekly = highestCloseWeekly - atr * atr_multiple_level2
// Define ATR-based levels for monthly chart
level1_atr_monthly = highestCloseMonthly - atr * atr_multiple_level1
level2_atr_monthly = highestCloseMonthly - atr * atr_multiple_level2
// Select between ATR or Percentage method for each timeframe
level1_daily = method == "ATR" ? level1_atr_daily : level1_percentage_daily
level2_daily = method == "ATR" ? level2_atr_daily : level2_percentage_daily
level1_weekly = method == "ATR" ? level1_atr_weekly : level1_percentage_weekly
level2_weekly = method == "ATR" ? level2_atr_weekly : level2_percentage_weekly
level1_monthly = method == "ATR" ? level1_atr_monthly : level1_percentage_monthly
level2_monthly = method == "ATR" ? level2_atr_monthly : level2_percentage_monthly
// Define colors for the filled areas between levels
colorHighToLevel1 = color.new(color.green, 80)
colorLevel1ToLevel2 = color.new(color.yellow, 80)
colorLevel2ToZero = color.new(color.red, 80)
// Plot the closing highs as horizontal lines
lineHighDaily = plot(timeframe.isdaily ? highestCloseDaily : na, title="52-Week Closing High (Daily)", color=#2195f300, linewidth=2, style=plot.style_line)
lineHighWeekly = plot(timeframe.isweekly ? highestCloseWeekly : na, title="52-Week Closing High (Weekly)", color=#2195f300, linewidth=2, style=plot.style_line)
lineHighMonthly = plot(timeframe.ismonthly ? highestCloseMonthly : na, title="12-Month Closing High (Monthly)", color=#2195f300, linewidth=2, style=plot.style_line)
// Plot the selected levels
level1PlotDaily = plot(timeframe.isdaily ? level1_daily : na, title="Level 1 (Daily)", color=color.rgb(255, 82, 82, 100), linewidth=2, style=plot.style_line)
level2PlotDaily = plot(timeframe.isdaily ? level2_daily : na, title="Level 2 (Daily)", color=#ff990000, linewidth=2, style=plot.style_line)
level1PlotWeekly = plot(timeframe.isweekly ? level1_weekly : na, title="Level 1 (Weekly)", color=color.rgb(255, 82, 82, 100), linewidth=2, style=plot.style_line)
level2PlotWeekly = plot(timeframe.isweekly ? level2_weekly : na, title="Level 2 (Weekly)", color=#ff990000, linewidth=2, style=plot.style_line)
level1PlotMonthly = plot(timeframe.ismonthly ? level1_monthly : na, title="Level 1 (Monthly)", color=color.rgb(255, 82, 82, 100), linewidth=2, style=plot.style_line)
level2PlotMonthly = plot(timeframe.ismonthly ? level2_monthly : na, title="Level 2 (Monthly)", color=#ff990000, linewidth=2, style=plot.style_line)
// Fill the area between the highest close and Level 1
fill(lineHighDaily, level1PlotDaily, color=colorHighToLevel1)
fill(level1PlotDaily, level2PlotDaily, color=colorLevel1ToLevel2)
fill(lineHighWeekly, level1PlotWeekly, color=colorHighToLevel1)
fill(level1PlotWeekly, level2PlotWeekly, color=colorLevel1ToLevel2)
fill(lineHighMonthly, level1PlotMonthly, color=colorHighToLevel1)
fill(level1PlotMonthly, level2PlotMonthly, color=colorLevel1ToLevel2)
lineZero = plot(0, title="Zero Line", color=color.new(color.green, 0))
fill(level2PlotDaily, lineZero, color=colorLevel2ToZero)
fill(level2PlotWeekly, lineZero, color=colorLevel2ToZero)
fill(level2PlotMonthly, lineZero, color=colorLevel2ToZero)
// --- Moving Average Labels for Distance, Change, and Bars Above/Below --- //
ma_label(maValue, labelColor) =>
var float cumulativeDaysAbove = 0
var float cumulativeDaysBelow = 0
var float startPriceAbove = na
var float startPriceBelow = na
var label maLabel = na
if (low > maValue)
if (cumulativeDaysAbove == 0)
startPriceAbove := close
cumulativeDaysAbove := cumulativeDaysAbove + 1
cumulativeDaysBelow := 0
startPriceBelow := na
else if (high < maValue)
if (cumulativeDaysBelow == 0)
startPriceBelow := close
cumulativeDaysBelow := cumulativeDaysBelow + 1
cumulativeDaysAbove := 0
startPriceAbove := na
else
cumulativeDaysAbove := 0
cumulativeDaysBelow := 0
startPriceAbove := na
startPriceBelow := na
priceChangeAbove = na(startPriceAbove) ? na : (close - startPriceAbove) / startPriceAbove * 100
priceChangeBelow = na(startPriceBelow) ? na : (close - startPriceBelow) / startPriceBelow * 100
distanceFromMA = (close - maValue) / maValue * 100
if (cumulativeDaysAbove > 0)
if (na(maLabel))
maLabel := label.new(bar_index, maValue - 0.5, text="Bars: " + str.tostring(cumulativeDaysAbove) + "\nChange: " + str.tostring(priceChangeAbove, "#.#") + "%\nDist: " + str.tostring(distanceFromMA, "#.#") + "%", color=labelColor, style=label.style_label_upper_left, textcolor=color.white, size=size.small)
else
label.set_xy(maLabel, bar_index, maValue - 0.5)
label.set_text(maLabel, "Bars: " + str.tostring(cumulativeDaysAbove) + "\nChange: " + str.tostring(priceChangeAbove, "#.#") + "%\nDist: " + str.tostring(distanceFromMA, "#.#") + "%")
else if (cumulativeDaysBelow > 0)
if (na(maLabel))
maLabel := label.new(bar_index, maValue + 0.5, text="Bars: " + str.tostring(cumulativeDaysBelow) + "\nChange: " + str.tostring(priceChangeBelow, "#.#") + "%\nDist: " + str.tostring(distanceFromMA, "#.#") + "%", color=color.red, style=label.style_label_lower_left, textcolor=color.white, size=size.small)
else
label.set_xy(maLabel, bar_index, maValue + 0.5)
label.set_text(maLabel, "Bars: " + str.tostring(cumulativeDaysBelow) + "\nChange: " + str.tostring(priceChangeBelow, "#.#") + "%\nDist: " + str.tostring(distanceFromMA, "#.#") + "%")
else
if (not na(maLabel))
label.delete(maLabel)
maLabel := na
maLabel
// Apply labels for Daily, Weekly, and Monthly MAs
if (timeframe.isdaily)
ma_label(sma_daily, color.green)
if (timeframe.isweekly)
ma_label(sma_weekly, color.blue)
if (timeframe.ismonthly)
ma_label(sma_monthly, color.purple)
// --- Buy and Sell Logic --- //
// Define buy and sell conditions for daily, weekly, and monthly charts
buyConditionDaily = close >= highestCloseDaily * 0.90 and low > sma_daily and ta.lowest(low, 2) > sma_daily
buyConditionWeekly = close >= highestCloseWeekly * 0.90 and low > sma_weekly and ta.lowest(low, 2) > sma_weekly
buyConditionMonthly = close >= highestCloseMonthly * 0.90 and low > sma_monthly and ta.lowest(low, 2) > sma_monthly
sellConditionDaily = close < highestCloseDaily * 0.90 and close < sma_daily
sellConditionWeekly = close < highestCloseWeekly * 0.90 and close < sma_weekly
sellConditionMonthly = close < highestCloseMonthly * 0.90 and close < sma_monthly
// Initialize flags for buy and sell labels
var bool buyLabelShown = false
var bool sellLabelShown = false
// Plot Buy arrows for Daily, Weekly, and Monthly conditions
if (timeframe.isdaily and buyConditionDaily and not buyLabelShown)
label.new(bar_index, low, text="BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
buyLabelShown := true
if (timeframe.isweekly and buyConditionWeekly and not buyLabelShown)
label.new(bar_index, low, text="BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
buyLabelShown := true
if (timeframe.ismonthly and buyConditionMonthly and not buyLabelShown)
label.new(bar_index, low, text="BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
buyLabelShown := true
// Reset the buy flag if the conditions are not met
if not (buyConditionDaily or buyConditionWeekly or buyConditionMonthly)
buyLabelShown := false
// Plot Sell arrows for Daily, Weekly, and Monthly conditions
if (timeframe.isdaily and sellConditionDaily and not sellLabelShown)
label.new(bar_index, high, text="SELL", style=label.style_label_down, color=color.red, textcolor=color.white)
sellLabelShown := true
if (timeframe.isweekly and sellConditionWeekly and not sellLabelShown)
label.new(bar_index, high, text="SELL", style=label.style_label_down, color=color.red, textcolor=color.white)
sellLabelShown := true
if (timeframe.ismonthly and sellConditionMonthly and not sellLabelShown)
label.new(bar_index, high, text="SELL", style=label.style_label_down, color=color.red, textcolor=color.white)
sellLabelShown := true
// Reset the sell flag if the conditions are not met
if not (sellConditionDaily or sellConditionWeekly or sellConditionMonthly)
sellLabelShown := false
// --- Table for Buy/Sell Rules --- //
var table myTable = table.new(position=position.top_right, columns=2, rows=2, frame_color=color.black, frame_width=1)
if (bar_index == 1)
table.cell(myTable, 0, 0, text="Buy: Close within 10% of 50-Week/12-Month High + 2 Bar Upside Landry Light ( Lows > MA)", text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 0, 1, text="Sell: Close below 10% of 50-Week/12-Month High + Close < MA", text_color=color.white, bgcolor=color.red)