本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//_______ <licence>
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skyrex
//_______ <version>
//@version=5
//_______ <declaration_statement>
strategy(title = "RSI Trend Following Strategy",
shorttitle = "RSI Strategy",
overlay = true,
format = format.inherit,
pyramiding = 1,
calc_on_order_fills = false,
calc_on_every_tick = false,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 30,
initial_capital = 10000,
currency = currency.NONE,
commission_type = strategy.commission.percent,
commission_value = 0.1,
slippage = 5,
use_bar_magnifier = true)
//_______ <constant_declarations>
var const color skyrexGreen = color.new(#2ECD99, 0)
var const color skyrexGray = color.new(#F2F2F2, 0)
var const color skyrexWhite = color.new(#FFFFFF, 0)
//________<variables declarations>
var float stopLossLevel = na
var float takeProfitLevel = na
var float tralingProfitActivationLevel = na
//_______ <inputs>
// Trading bot settings
sourceUuid = input.string(title = "sourceUuid:", defval = "yourBotSourceUuid", group = "🤖Trading Bot Settings🤖")
secretToken = input.string(title = "secretToken:", defval = "yourBotSecretToken", group = "🤖Trading Bot Settings🤖")
// Trading period settings
lookBackPeriodStart = input.time(title = "Trade Start Date/Time", defval = timestamp('2023-01-01T00:00:00'), group = "🕐Trading Period Settings🕐")
lookBackPeriodStop = input.time(title = "Trade Stop Date/Time", defval = timestamp('2025-01-01T00:00:00'), group = "🕐Trading Period Settings🕐")
// Strategy settings
stopLossAtrNum = input.float(defval = 1.75, title = "ATR Stop Loss", step = 0.25, group = "📈Strategy settings📈")
tralingProfitActivationNum = input.float(defval = 2.25, title = "ATR Traling Profit Activation Level", step = 0.25, group = "📈Strategy settings📈")
macdFastLength = input(title = "MACD Fast Length", defval = 12, group = "📈Strategy settings📈")
macdSlowLength = input(title = "MACD Slow Length", defval = 26, group = "📈Strategy settings📈")
macdSignalLength = input.int(title = "MACD Signal Smoothing", minval = 1, maxval = 50, defval = 9, display = display.data_window, group = "📈Strategy settings📈")
macdSmaSource = input.string(title = "Oscillator MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window, group = "📈Strategy settings📈")
macdSmaSignal = input.string(title = "Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window, group = "📈Strategy settings📈")
rsiPeriod = input.int(title = "RSI Length", defval = 14, minval = 1, group = "📈Strategy settings📈")
trailingEmaLength = input.int(20, minval = 10, title = "Trailing EMA Length", group = "📈Strategy settings📈")
//_______ <function_declarations>
//_______ <calculations>
//Calculating Filter EMA
filterEMA = ta.ema(close, 200)
//Average true range (ATR) calculations
ATR = ta.atr(14)
//Calculating RSI
rsi = ta.rsi(close, rsiPeriod)
//Calcultating Stochastic
k = ta.sma(ta.stoch(close, high, low, 14), 1)
d = ta.sma(k, 3)
//Calculating MACD
macdFastMa = macdSmaSource == "SMA" ? ta.sma(close, macdFastLength) : ta.ema(close, macdFastLength)
macdSlowMA = macdSmaSource == "SMA" ? ta.sma(close, macdSlowLength) : ta.ema(close, macdSlowLength)
macd = macdFastMa - macdSlowMA
macdSignal = macdSmaSignal == "SMA" ? ta.sma(macd, macdSignalLength) : ta.ema(macd, macdSignalLength)
hist = macd - macdSignal
//Calculating EMA
trailingEma = ta.ema(close, trailingEmaLength)
//Calculating stop loss level
stopLossLevel := strategy.position_avg_price - stopLossAtrNum * ATR
//Calculating traling profit activation level
tralingProfitActivationLevel := strategy.position_avg_price + tralingProfitActivationNum * ATR
//Calculating dynamic traling take profit level
if strategy.opentrades == 0 or ta.barssince(strategy.opentrades == 1 and strategy.opentrades[1] == 0) < ta.barssince(ta.crossover(high, tralingProfitActivationLevel))
takeProfitLevel := na
else
takeProfitLevel := trailingEma
//_______ <strategy_calls>
//Defining long condition
longCondition = k < 80 and d < 80 and macd > macdSignal and rsi > 50 and low > filterEMA
//Defining trade close condition
closeCondition = close < takeProfitLevel or low <= stopLossLevel
//Strategy entry
//Scrip places the stop order at the upFractalActivationLevel if longCondition is true
if (longCondition and time >= lookBackPeriodStart and time <= lookBackPeriodStop)
strategy.entry(id = "entry1", direction = strategy.long, alert_message = '{\n"base": "' + syminfo.basecurrency + '",\n"quote": "' + syminfo.currency + '",\n"position": "entry1",\n"price": "' + str.tostring(close) + '",\n"sourceUuid": "' + sourceUuid + '",\n"secretToken": "' + secretToken + '",\n"timestamp": "' + str.tostring(timenow) + '"\n}')
//Strategy exit
if (closeCondition)
strategy.close(id = "entry1",immediately = true , alert_message = '{\n"base": "' + syminfo.basecurrency + '",\n"quote": "' + syminfo.currency + '",\n"position": "close",\n"price": "' + str.tostring(close) + '",\n"sourceUuid": "' + sourceUuid + '",\n"secretToken": "' + secretToken + '",\n"timestamp": "' + str.tostring(timenow) + '"\n}')
//_______ <visuals>
pricePlot = plot(close, title="Price", color=color.new(color.blue, 100))
emaPlot = plot(takeProfitLevel, title="EMA", color=skyrexGreen, style=plot.style_linebr, linewidth = 3)
plot(not na(takeProfitLevel) and na(takeProfitLevel[1]) ? takeProfitLevel : na, title="EMA", color=skyrexGreen, join = false, style=plot.style_circles, linewidth = 6)
fill(pricePlot, emaPlot, color=color.new(skyrexGreen, 70))
plotshape(not na(takeProfitLevel) and na(takeProfitLevel[1]) ? true: na,text="Trailing Activated",size=size.small,offset=0,color=color.new(skyrexGreen, 0),textcolor=skyrexWhite,style=shape.triangleup,location=location.belowbar)
//_______ <alerts>