简体   繁体   English

在 Pine Script V5 中为指标组织风险管理

[英]Organizing Risk Management for an indicator in Pine Script V5

The problem is this.问题是这样的。 I want my indicator to have a plotshape(Long,Short,TP,Stop and AVG) By AVG I mean averaging, and just with him I have a problem, I prescribed the code and variables that should show AVG to the first touch and after go either to Stop or to TP, but the code does not display anything on the chart.我希望我的指标有一个 plotshape(Long,Short,TP,Stop and AVG) AVG 我的意思是平均,只是和他一起我有一个问题,我规定了应该在第一次触摸和之后显示 AVG 的代码和变量go 止损或止盈,但代码不会在图表上显示任何内容。

` `

////Фикс РМ///
stopPer = input.float(5, title='Stop Loss %') / 100
takePer = input.float(5, title='Take Profit %') / 100

//определить, каким был последний сигнал (длинным или коротким)
long_short = 0
long_last = long and (nz(long_short[1]) == 0 or nz(long_short[1]) == -1)
short_last = short and (nz(long_short[1]) == 0 or nz(long_short[1]) == 1)
long_short := long_last ? 1 : short_last ? -1 : long_short[1]

//Расчет входа
longPrice = ta.valuewhen(long_last, close, 0)
shortPrice = ta.valuewhen(short_last, close, 0)
//Фиксированый стоп и ТП
longStop = longPrice * (1 - stopPer)
shortStop = shortPrice * (1 + stopPer)
longTake = longPrice * (1 + takePer)
shortTake = shortPrice * (1 - takePer)
//Разрисовка линий
plot(long_short == 1 ? longStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Fixed SL')
plot(long_short == -1 ? shortStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Short Fixed SL')
plot(long_short == 1 ? longTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Long Fixed TP')
plot(long_short == -1 ? shortTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Short Fixed TP')

//удаляет первый бар для SL/TP (вы не можете войти в сделку при закрытии бара, а затем на этом же баре установить SL)
longBar1 = ta.barssince(long_last)
longBar2 = longBar1 >= 1 ? true : false
shortBar1 = ta.barssince(short_last)
shortBar2 = shortBar1 >= 1 ? true : false

//Проверка на СЛ по закрытию свечи
longSLhit = long_short == 1 and longBar2 and low < longStop
plotshape(longSLhit, style=shape.labelup, location=location.belowbar, color=color.new(color.gray, 0), size=size.tiny, title='Long SL', text=' Long SL', textcolor=color.new(color.white, 0))
shortSLhit = long_short == -1 and shortBar2 and high > shortStop
plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color.new(color.gray, 0), size=size.tiny, title='Short SL', text=' Short SL', textcolor=color.new(color.white, 0))

//Проверка на ТП по закрытию свечи
longTPhit = long_short == 1 and longBar2 and high > longTake
plotshape(longTPhit, style=shape.labeldown, location=location.abovebar, color=color.new(color.purple, 0), size=size.tiny, title='Long TP', text='Long TP', textcolor=color.new(color.white, 0))
shortTPhit = long_short == -1 and shortBar2 and low < shortTake
plotshape(shortTPhit, style=shape.labelup, location=location.belowbar, color=color.new(color.purple, 0), size=size.tiny, title='Short TP', text='Short TP', textcolor=color.new(color.white, 0))
////AVG////
var averaging = 0
var averaging_done = 0
var short_averaging_done = 0

priceDrop = (close / longPrice - 1) * 100
if long_last and priceDrop >= 0
    averaging := 1

averaging_active = long_short == 1 and averaging == 1 and averaging_done == 0 and close < longPrice * (1 - 0.02)
longPrice := averaging_active ? (longPrice + close) / 2 : longPrice
longStop := averaging_active ? (longPrice * (1 - stopPer)) : longStop
longTake := averaging_active ? (longPrice * (1 + takePer)) : longTake
averaging_done := averaging_active ? 1 : averaging_done

short_averaging_active = long_short == -1. and averaging == 1 and short_averaging_done == 0 and close > shortPrice * (1 - 0.02)
shortPrice := short_averaging_active ? (shortPrice + close) / 2 : shortPrice
shortStop := short_averaging_active ? (shortPrice * (1 + stopPer)) : shortStop
shortTake := short_averaging_active ? (shortPrice * (1 - takePer)) : shortTake
short_averaging_done := short_averaging_active ? 1 : short_averaging_done

plotshape(averaging_active, style=shape.labelup, location=location.belowbar, color=color.new(color.purple, 0), size=size.tiny, title='AVG', text='AVG', textcolor=color.new(color.white, 0))
plotshape(short_averaging_active, style=shape.labelup, location=location.belowbar, color=color.new(color.purple, 0), size=size.tiny, title='AVG', text='AVG', textcolor=color.new(color.white, 0))
//Ресет лонгов и шортов
long_short := (long_short == 1 or long_short == 0) and longBar2 and (longSLhit or longTPhit) ? 0 : (long_short == -1 or long_short == 0) and shortBar2 and (shortSLhit or shortTPhit) ? 0 : long_short
///////

` `

The main attempts gave me an unhappy effect, in the form of a lot of plotshape( AVG), which started from the bar signal and to the opposite.主要尝试以大量 plotshape(AVG) 的形式给我带来了不愉快的效果,它从条形信号开始并向相反方向移动。

That looks like my "Fixed Percent Stop Loss & Take Profit" script availble at https://www.tradingview.com/script/vONzvOkb-Fixed-Percent-Stop-Loss-Take-Profit-For-Study-Scripts/这看起来像我的“固定百分比止损和止盈”脚本,网址为https://www.tradingview.com/script/vONzvOkb-Fixed-Percent-Stop-Loss-Take-Profit-For-Study-Scripts/

It's awesome that people are making use of this script and customizing it for their own needs!人们正在使用这个脚本并根据自己的需要对其进行自定义,这真是太棒了!

Could you expand on what you mean by "average" please?你能详细解释一下你所说的“平均”是什么意思吗? What is it exactly you're trying to find an average of?您要寻找的平均值究竟是多少? And are you sure you want to use plotshape() rather than plot()?您确定要使用 plotshape() 而不是 plot() 吗? Surely a line to represent an average would be more beneficial than a fixed point along the x-axis?代表平均值的线肯定比沿 x 轴的固定点更有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM