Tradingview松树编辑器。条目

问题描述 投票:0回答:1
的ATR TP / SL编码问题

我需要使用过去的平均真实范围来编码止盈和止损。我有一个问题,我的代码正在计算最新的ATR,但我找不到从条目中锁定ATR号码的方法。蜡烛关闭后,ATR正在计算,这意味着蜡烛处于活动状态时不存在。将图片添加到我已标记了我要寻找的内容以及松木正在计算的位置。

Paint shows what I'm looking for, original pine shows code calculations

//@version=2
strategy("Heiwa",initial_capital=1000,default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.15, overlay=true)

//WADARINDICATOR----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sensitivity = input(200, title="Sensitivity")
fastLength=input(20, title="FastEMA Length")
slowLength=input(40, title="SlowEMA Length")
channelLength=input(20, title="BB Channel Length")
mult=input(2.0, title="BB Stdev Multiplier")
deadZone=input(20, title="No trade zone threshold")

calc_macd(source, fastLength, slowLength) =>
    fastMA = ema(source, fastLength)
    slowMA = ema(source, slowLength)
    fastMA - slowMA

calc_BBUpper(source, length, mult) => 
    basis = sma(source, length)
    dev = mult * stdev(source, length)
    basis + dev

calc_BBLower(source, length, mult) => 
    basis = sma(source, length)
    dev = mult * stdev(source, length)
    basis - dev

t1 = (calc_macd(close, fastLength, slowLength) - calc_macd(close[1], fastLength, slowLength))*sensitivity
t2 = (calc_macd(close[2], fastLength, slowLength) - calc_macd(close[3], fastLength, slowLength))*sensitivity

e1 = (calc_BBUpper(close, channelLength, mult) - calc_BBLower(close, channelLength, mult))
//e2 = (calc_BBUpper(close[1], channelLength, mult) - calc_BBLower(close[1], channelLength, mult))

trendUp = (t1 >= 0) ? t1 : 0
trendDown = (t1 < 0) ? (-1*t1) : 0
Waddardtdis = input(50, minval=1)

//WADARINDICATOR----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//ATR---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

atrBandDays = input(15, minval=1, title="Days for ATR", type=integer)
atrBand = atr(atrBandDays)

atrPlus1 = close + atrBand
atrPlus2 = close + atrBand*2
atrPlus3 = close + atrBand*3
atrStop = close - atrBand*2 

plot(atrPlus1, color=green)
plot(atrPlus2, color=orange)
plot(atrPlus3, color=red)
plot(atrStop, color=blue)

//ATR---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Heikinashi---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

haTicker = heikinashi(tickerid)
haOpen = security(haTicker, period, open)
haClose = security(haTicker, period, close)

//Heikinashi---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

longCondition = haClose > haOpen and haOpen > haClose[1] and trendUp>e1
exitCondition = haClose < haOpen and  trendDown>Waddardtdis

strategy.entry("Long", strategy.long, when=longCondition)
strategy.exit("TP1", "Long", qty_percent = 60, limit=atrPlus1)
strategy.exit("TP2", "Long", qty_percent = 20, limit=atrPlus2)
strategy.exit("TP3", "Long", qty_percent = 20, limit=atrPlus3)
strategy.exit("SL", "Long", stop = atrStop)
strategy.close ( "Long", when = exitCondition)
stock algorithmic-trading trading pine-script tradingview-api
1个回答
0
投票

我只是保留入口处的atr值,就像这样:

entryAtr = entryAtr[1]

if (longCondition)
    strategy.entry("Long", strategy.long)
    entryAtr := atrBand // save the value to use it for exit
© www.soinside.com 2019 - 2024. All rights reserved.