Pinescript - 我正在进行两笔空头交易。由于交易 2 触及目标价并平仓,交易 1 在触及目标价之前提前平仓。怎么解决?

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

注意我不太擅长 Pinescript :/

我正在进行两笔空头交易。 交易 1 - 入场:142.510 止损:142.806 目标价:141.324 交易 2 - 入场:142.663 止损:142.761 目标价:142.271

它们的比例设置为 1:4。 两笔交易都已成功执行并同时运行,但交易 1 以交易 2 的目标价而非其自己的目标价结束。基本上,我最终在该交易中获得了 0.75RR 回报,而不是 4RR 回报。

有人知道这可能是什么问题吗?

这是正在发生的事情的直观表示。 Diagram 这是策略测试器的表格,两笔交易在 2:50 同时退出。 List of Trade

订单代码中所写内容的摘要:

strategy("My strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=100, currency=currency.USD, process_orders_on_close=true, pyramiding = 8)
// Strategy is called shortCondition
// Declare variables
i_pctStop = input(1., "% of Risk to Starting Equity Use to Size Positions") 
i_tpFactor = input(4, "Factor of stop determining target profit")
// Save the strat's equity on the first bar, which is equal to initial capital.
var initialCapital = strategy.initial_capital
// STOP LOSS
float shortSL = na
shortSL := shortCondition ? ta.highest(high,1)[1] : longSL[1]
// TAKE PROFIT
shortEntryPrice = close
shortDiffSL = math.abs(shortEntryPrice - shortSL)
float shortTP = na
shortTP := shortEntryPrice - (i_tpFactor * shortDiffSL)
shortpositionValue = initialCapital * i_pctStop / (shortDiffSL / shortEntryPrice)
shortpositionSize = shortpositionValue / shortEntryPrice
// SHORT ENTRY/EXIT
if shortCondition
    strategy.entry("SHORT", strategy.short, qty=shortpositionSize)
    strategy.exit("EXIT SHORT","SHORT", stop=shortSL, limit=shortTP)

// PLOT STOP LOSS
shortPlotSL = strategy.opentrades > 0 and strategy.position_size > 0 ? shortSL : na
plot(shortPlotSL, title='SHORT STOP LOSS', linewidth=2, style=plot.style_linebr, color=color.red)
// PLOT TAKE PROFIT
shortPlotTP = strategy.opentrades > 0 and strategy.position_size > 0 ? shortTP : na
plot(shortPlotTP, title='SHORT TAKE PROFIT', linewidth=2, style=plot.style_linebr, color=color.green)

非常感谢有关修复的深入解释和代码示例!

pine-script-v5 tradingview-api trading
1个回答
0
投票

默认情况下,第一个未平仓订单应首先关闭,但您可以通过将

close_entries_rule=
参数设置为
"ANY"
来更改此行为。

close_entries_rule (const string) 确定交易的顺序 已关闭。可能的值为:“FIFO”(先进先出),如果 最早的退出订单必须关闭最早的入场订单,或者“任何”,如果 订单根据 from_entry 参数关闭 策略.退出函数。 “先进先出”只能与股票、期货一起使用 和美国外汇(NFA 合规规则 2-43b),而“ANY”是允许的 非美国外汇。选修的。默认为“先进先出”。

https://www.tradingview.com/pine-script-reference/v5/#fun_strategy

© www.soinside.com 2019 - 2024. All rights reserved.