简体   繁体   中英

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.

`

////Фикс РМ///
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.

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/

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()? Surely a line to represent an average would be more beneficial than a fixed point along the x-axis?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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