DinGrogu

Price - TP/SL

Prices
With this library, you can easily manage prices such as stop loss, take profit, calculate differences, prices from a lower timeframe, and get the order size and commission from the strategy properties tab.
Note that the order size and commission only work with strategies!

Usage
Take Profit & Stop Loss

var bool open_trade = false
open_trade := strategy.position_size != 0
bars_since_opened = strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) + 1 : 0

// ############################################################
// # TAKE PROFIT
// ############################################################

take_profit = input.string(title='Take Profit', defval='OFF', options=['OFF', 'ON'], group='TAKE PROFIT')
take_profit_percentage = input.float(title='Take Profit (% or X)', defval=0, minval=0, step=0.1, group='TAKE PROFIT')
take_profit_bars = input.int(title='Take Profit Bars', defval=0, minval=0, step=1, group='TAKE PROFIT')
take_profit_indication = input.string(title='Take Profit Plot', defval='OFF', options=['ON', 'OFF'], group='TAKE PROFIT')
take_profit_color = input.color(title='Take Profit Color', defval=#26A69A, group='TAKE PROFIT')

take_profit_price = math.round_to_mintick(strategy.position_avg_price)
take_profit_plot = plot(take_profit == 'ON' and take_profit_indication == 'ON' and open_trade and bars_since_opened >= take_profit_bars and take_profit_percentage > 0 and nz(take_profit_price) ? take_profit_price : na, color=take_profit_color, style=plot.style_linebr, linewidth=1, title='TP', offset=0)

// ############################################################
// # STOP LOSS
// ############################################################

stop_loss = input.string(title='Stop Loss', defval='OFF', options=['OFF', 'ON'], group='STOP LOSS')
stop_loss_percentage = input.float(title='Stop Loss (% or X)', defval=0, minval=0, step=0.1, group='STOP LOSS')
stop_loss_bars = input.int(title='Stop Loss Bars', defval=0, minval=0, step=1, group='STOP LOSS')
stop_loss_indication = input.string(title='Stop Loss Plot', defval='OFF', options=['ON', 'OFF'], group='STOP LOSS')
stop_loss_color = input.color(title='Stop Loss Color', defval=#FF5252, group='STOP LOSS')

stop_loss_price = math.round_to_mintick(strategy.position_avg_price)
stop_loss_plot = plot(stop_loss == 'ON' and stop_loss_indication == 'ON' and open_trade and bars_since_opened >= stop_loss_bars and stop_loss_percentage > 0 and nz(stop_loss_price) ? stop_loss_price : na, color=stop_loss_color, style=plot.style_linebr, linewidth=1, title='SL', offset=0)

// ############################################################
// # STRATEGY
// ############################################################

var limit_price = 0.0
var stop_price = 0.0

limit_price := take_profit == 'ON' ? price.take_profit_price(take_profit_price, take_profit_percentage, take_profit_bars, bars_since_opened) : na
stop_price := stop_loss == 'ON' ? price.stop_loss_price(stop_loss_price, stop_loss_percentage, stop_loss_bars, bars_since_opened) : na

strategy.exit(id='TP/SL', comment='TP/SL', from_entry='LONG', limit=limit_price, stop=stop_price)

Calculate difference between 2 prices:
price.difference(close, close[1])

Get last price from lower timeframe:
price.ltf(request.security_lower_tf(ticker, '1', close))

Get the order size from the properties tab:
price.order_size()

Get the commission from the properties tab.
price.commission()
파인 라이브러리

트레이딩뷰 정신에 따라 오써는 이 파인 코드를 오픈 소스 라이브러리로 퍼블리쉬하여 당사 커뮤니티의 다른 파인 프로그래머들이 쓸 수 있도록 하였습니다. 오써에게 찬사를! 여러분은 이 라이브러리를 프라이빗 또는 오픈 소스 퍼블리케이션에 쓸 수 있지만 퍼블리케이션에 재사용은 하우스룰을 따릅니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

이 라이브러리를 쓰시겠습니까?

텍스트를 클립보드에 카피한 뒤 님의 스크립트에 붙여 넣기.