本页源码来自 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 = "Fractal Breakout Trend Following Strategy",
shorttitle = "Fractal Strategy",
overlay = true,
format = format.inherit,
pyramiding = 1,
calc_on_order_fills = false,
calc_on_every_tick = true,
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)
//_______ <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)
var const int atrLen = 13
var const int atrLookback = 52
//________<variables declarations>
var color barcolor = na
var float stopLossLevel = na
var int skip_count = 0
var float upFractalLevel = na
var float upFractalActivationLevel = na
var float downFractalLevel = na
var float downFractalActivationLevel = na
bool highCrossesUpfractalLevel = ta.crossover(high, upFractalLevel)
//_______ <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
stopLossPercent = input.float(defval = 0.03, title = "Stop loss", step = 0.005, group = "📈Strategy settings📈")
atrThreshold = input.int(defval=50, minval=0, maxval=100, step=1, title = "ATR Threshold value", group = "📈Strategy settings📈")
atrPeriod = input.int(defval=5, minval=1, maxval=30, title='ATR Period', group = "📈Strategy settings📈")
//_______ <function_declarations>
//@function Used to calculate Simple moving average for Alligator
//@param src Sourse for smma Calculations
//@param length Number of bars to calculate smma
//@returns The calculated smma value
smma(src, length) =>
var float smma = na
sma_value = ta.sma(src, length)
smma := na(smma) ? sma_value : (smma * (length - 1) + src) / length
smma
//_______ <calculations>
//Upfractal calculation
upFractalPrice = ta.pivothigh(2, 2)
upFractal = not na(upFractalPrice)
//Downfractal calculation
downFractalPrice = ta.pivotlow(2, 2)
downFractal = not na(downFractalPrice)
//Calculating ATR and ATR normalized
atrValue = ta.atr(atrLen)
atrNormalized = ta.percentrank(atrValue, atrLookback)
//Calculating average ATR for ATR period number of bars
var atrValues = array.new<float>(size = atrPeriod, initial_value = 0)
array.push(atrValues, atrNormalized)
array.remove(atrValues, 0)
atrAvg = array.avg(atrValues)
//Calculating chart's time frame Alligator's teeth line
teeth = smma(hl2, 8)[5]
//Calculating upfractal level
if upFractal
upFractalLevel := upFractalPrice
else if highCrossesUpfractalLevel
upFractalLevel := na
else
upFractalLevel := upFractalLevel[1]
//Calculating upfractal activation level, taking into account the Alligator teeth line
if upFractalLevel >= teeth
upFractalActivationLevel := upFractalLevel
else
upFractalActivationLevel := na
//Calculating down fractal level
if downFractal
downFractalLevel := downFractalPrice
else
downFractalLevel := downFractalLevel[1]
//Calculating down fractal activation level, taking into account the Alligator teeth line
if downFractalLevel <= teeth
downFractalActivationLevel := downFractalLevel
else
downFractalActivationLevel := downFractalActivationLevel
//Calculating dynamic stop loss level: script chooses the higher value: user-given stop loss percent or down fractal
stopLossLevel := strategy.position_avg_price*(1-stopLossPercent) > downFractalActivationLevel ? strategy.position_avg_price*(1-stopLossPercent) : downFractalActivationLevel
//_______ <strategy_calls>
//defining long condition:
//1)Script defines if current upfractal activetion level is not na
//2)Average value for normalized ATR for last atrPeriod number of bars shall be lower than user-defined trashold value
longCondition = not na(upFractalActivationLevel) and atrAvg <= atrThreshold
//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, stop=upFractalActivationLevel, 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
strategy.exit(id = "entry1", stop=stopLossLevel, 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>
//_______ <alerts>