TradingView-用于按百分比获利和止损的PINE脚本 [英] Tradingview - pine script for Take profit and Stop loss by percentage

查看:16
本文介绍了TradingView-用于按百分比获利和止损的PINE脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个脚本中合并获利和止损时遇到严重问题。

我正在对TP使用此脚本 https://kodify.net/tradingview/orders/percentage-profit/

这个是SL的 https://kodify.net/tradingview/orders/percentage-stop/

我想出了以下脚本,它看起来不像SL。因此,订单将保持打开状态,直到达到TP%。我需要你的帮助来修复它并激活SL,就像TP一样。

//@version=3
strategy(title="Take profit (% of instrument price)",
     overlay=true, pyramiding=1)

// STEP 1:
// Make inputs that set the take profit % (optional)
longProfitPerc = input(title="Long Take Profit (%)",
     type=float, minval=0.0, step=0.1, defval=3) * 0.01

longLossPerc = input(title="Long Stop Loss (%)",
     type=float, minval=0.0, step=0.1, defval=1) * 0.01

// Calculate moving averages
fastSMA = sma(close, 20)
slowSMA = sma(close, 60)

// Calculate trading conditions
enterLong  = crossover(fastSMA, slowSMA)


// Plot moving averages
plot(series=fastSMA, color=green)
plot(series=slowSMA, color=red)

// STEP 2:
// Figure out take profit price
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
longStopPrice  = strategy.position_avg_price * (1 - longLossPerc)




// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? longExitPrice : na,
     color=green, style=circles,
     linewidth=3, title="Long Take Profit")

plot(series=(strategy.position_size > 0) ? longStopPrice : na,
     color=red, style=cross,
     linewidth=2, title="Long Stop Loss")
// Submit entry orders
if (enterLong)
    strategy.entry(id="EL", long=true)



// STEP 3:
// Submit exit orders based on take profit price
if (strategy.position_size > 0)
    strategy.exit(id="TP", limit=longExitPrice)


if (strategy.position_size > 0)
    strategy.exit(id="SL", stop=longStopPrice)

推荐答案

在这里,添加了几个

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SafetyHammer

//@version=4
strategy(title="Take profit (% of instrument price)", overlay=true, pyramiding=1)

// STEP 1:
// Make inputs that set the take profit % (optional)



FastPeriod = input(title="Fast MA Period", type=input.integer, defval=20, minval=1, group="Moving Average")
SlowPeriod = input(title="Slow MA Period", type=input.integer, defval=60, minval=1, group="Moving Average")



TP1Perc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=2, group="TP & SL") 
TP2Perc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=4, group="TP & SL") 
SLPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=2, group="TP & SL")

TP1_Ratio = input(title="Sell Postion Size % @ TP1", type=input.float, defval=50, step=1, group="TP & SL", tooltip="Example: 50 closing 50% of the position once TP1 is reached")/100


// Calculate moving averages
fastSMA = sma(close, FastPeriod)
slowSMA = sma(close, SlowPeriod)

// Calculate trading conditions
enterLong  = crossover(fastSMA, slowSMA)


// Plot moving averages
plot(series=fastSMA, color=color.green, title="Fase MA")
plot(series=slowSMA, color=color.red, title="Slow MA")

// STEP 2:
// Figure out take profit price
percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)

percentAsPrice(pcnt) =>
    strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
 
current_position_size = abs(strategy.position_size)
initial_position_size = abs(valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))   
    
TP1  = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
TP2  = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)
SL   = strategy.position_avg_price - percentAsPoints(SLPerc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)



// Submit entry orders
if (enterLong) 
    strategy.entry(id="Long", long=true)  



// STEP 3:
// Submit exit orders based on take profit price

if strategy.position_size > 0 
    strategy.exit("TP1", from_entry="Long", qty = initial_position_size * TP1_Ratio, limit = TP1, stop = SL)
    strategy.exit("TP2", from_entry="Long", limit = TP2, stop = SL) 
    
    
// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? TP1 : na, color=color.green, style=plot.style_circles, linewidth=1, title="Take Profit 1")
plot(series=(strategy.position_size > 0) ? TP2 : na, color=color.green, style=plot.style_circles, linewidth=1, title=" Take Profit 2")
plot(series=(strategy.position_size > 0) ? SL : na, color=color.red, style=plot.style_circles, linewidth=1, title="Stop Loss")


这篇关于TradingView-用于按百分比获利和止损的PINE脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆