在 Pine 中:如果价格达到低于入场价的 X 个刻度,如何完全关闭这个多头订单

问题描述 投票:0回答:1

到目前为止我正在使用这段代码:

strategy.entry("My Long Entry ", strategy.long)
strategy.exit("My Exit 1", from_entry = "My Long Entry ", profit=15000, qty_percent = 50)
strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 50, trail_offset=10000, trail_points=15000)

但我想避免这种回撤:

那么,当价格达到刚好低于入场价的水平(我想用刻度指定)时,我怎样才能关闭整个交易(请注意,我在两个退出中都使用了 qty_percent)?

谢谢

[编辑]

[编辑之二] 完整代码:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © me
//@version=5
strategy("Just Trend V2", process_orders_on_close=false, overlay=true, initial_capital=100, default_qty_value=1000, currency='USDT', default_qty_type=strategy.percent_of_equity, calc_on_every_tick = true, max_labels_count=500, use_bar_magnifier = true, margin_long = 1., commission_value = 0.07)
backtestStartDate = input.time(timestamp("12 Mar 2023 GMT+01:00"), title="Start Date", group="Backtest Time Period")
backtestEndDate = input.time(timestamp("13 Mar 2023 GMT+01:00"), title="End Date", group="Backtest Time Period")
inTradeWindow = time > backtestStartDate and time <= backtestEndDate



// ### Four Smoothed Moving Averages

len1 = 21
//input(21, minval=1, title="Length 1", group = "Smoothed MA Inputs")
src1 = close
//input(close, title="Source 1", group = "Smoothed MA Inputs")
smma1 = 0.0
sma_1 = ta.sma(src1, len1)
smma1 := na(smma1[1]) ? sma_1 : (smma1[1] * (len1 - 1) + src1) / len1
plot(smma1, color=color.white, linewidth=2, title="21 SMMA")

len2 = 50
//input(50, minval=1, title="Length 2", group = "Smoothed MA Inputs")
src2 = close
//input(close, title="Source 2", group = "Smoothed MA Inputs")
smma2 = 0.0
sma_2 = ta.sma(src2, len2)
smma2 := na(smma2[1]) ? sma_2 : (smma2[1] * (len2 - 1) + src2) / len2
plot(smma2, color=color.new(#6aff00,0), linewidth=2, title="50 SMMA")

h100 = input.bool(title="Show 100 Line", defval=true, group = "Smoothed MA Inputs")
len3 = 100
//input(100, minval=1, title="Length 3", group = "Smoothed MA Inputs")
src3 = close
//input(close, title="Source 3", group = "Smoothed MA Inputs")
smma3 = 0.0
sma_3 = ta.sma(src3, len3)
smma3 := na(smma3[1]) ? sma_3 : (smma3[1] * (len3 - 1) + src3) / len3
sma3plot = plot(h100 ? smma3 : na, color=color.new(color.yellow,0), linewidth=2, title="100 SMMA")

len4 = 200
//input(200, minval=1, title="Length 4", group = "Smoothed MA Inputs")
src4 = close
//input(close, title="Source 4", group = "Smoothed MA Inputs")
smma4 = 0.0
sma_4 = ta.sma(src4, len4)
smma4 := na(smma4[1]) ? sma_4 : (smma4[1] * (len4 - 1) + src4) / len4
sma4plot = plot(smma4, color=color.new(#ff0500,0), linewidth=2, title="200 SMMA")

// Trend Fill

trendFill = input.bool(title="Show Trend Fill", defval=true, group = "Smoothed MA Inputs") 
ema2 = ta.ema(close, 2)
ema2plot = plot(ema2, color=color.new(#2ecc71, 50), style=plot.style_line, linewidth=1, title="EMA(2)", editable = false)

//bool condBuy = (ema2 > smma4 and trendFill) and close > smma1 and smma1 > smma2 and smma2 > smma3 and smma3 > smma4
bool condBuy = (ema2 > smma4 and trendFill) and close > smma1 and close > smma2 and close > smma3 and close > smma4

bool condSell = close < smma1 or close < smma2 or close < smma3 or close < smma4


fill(ema2plot, sma4plot, color=  ema2 > smma4 and trendFill ? color.new(color.green, 50) : ema2 < smma4 and trendFill ? color.new(color.red, 50) : na, title = "Trend Fill")

// End ###






var Limit_Below_Entry_Price = 0.0

// LONG TRADES
if inTradeWindow and condBuy and strategy.opentrades > 0 == false

   
    strategy.entry("My Long Entry ", strategy.long, limit = close)

    Limit_Below_Entry_Price := close - 100

    strategy.exit("My Exit 1", from_entry = "My Long Entry ", profit=15000, qty_percent = 50) // 50*syminfo.mintick

    strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 50, trail_offset=10000, trail_points=15000)


if close < Limit_Below_Entry_Price
    strategy.close_all()



if inTradeWindow
    a = str.tostring(Limit_Below_Entry_Price)
    label.new(bar_index, na, a, yloc = yloc.abovebar, size = size.normal,textcolor = color.white)
pine-script pine-script-v5
1个回答
0
投票

您可以使用:

var Limit_Below_Entry_Price = 0.0
Limit_Value = 100 // Here you choose how many $$$ below your entry price trigger the close_all()

if (your logic to decide to long)
    strategy.entry("My Long Entry ", strategy.long)
    Limit_Below_Entry_Price := close - Limit_Value
    
    strategy.exit("My Exit 1", from_entry = "My Long Entry ", profit=15000, qty_percent = 50)
    strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 50, trail_offset=10000, trail_points=15000)

if close < Limit_Below_Entry_Price
        strategy.close_all()
© www.soinside.com 2019 - 2024. All rights reserved.