Reputation: 1
I have two short trades running:
They are set at a 1:4 ratio. Both trades are executed successfully and running at the same time but Trade 1 closes at Trade 2's TP instead of its own TP. Basically, instead of getting a 4RR return I ended up with 0.75RR return on that trade.
Anyone know what the issue may be here?
This is a visually representation of what is happening:
Here is a table of the strategy tester and both trades exit at the same time 2:50.
A summary of what is written in the code for the orders:
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)
Upvotes: 0
Views: 125
Reputation: 3828
By default, the first open order should be closed first, but you can change this behavior by setting the close_entries_rule=
parameter to "ANY"
.
close_entries_rule (const string) Determines the order in which trades are closed. Possible values are: "FIFO" (First-In, First-Out) if the earliest exit order must close the earliest entry order, or "ANY" if the orders are closed based on the from_entry parameter of the strategy.exit function. "FIFO" can only be used with stocks, futures and US forex (NFA Compliance Rule 2-43b), while "ANY" is allowed in non-US forex. Optional. The default is "FIFO".
https://www.tradingview.com/pine-script-reference/v5/#fun_strategy
Upvotes: 0