I an trying to write a JSON message for executing Pine Script strategy at TradingView.
// === /STRATEGY ===
// Close the current position fully and reverse to the opposite position
if (xlong)
if (strategy.position_size < 0) // If there's an open short position
strategy.close_all() // Fully close all short positions
strategy.entry("long", strategy.long) // Open a long position
if (xshort)
if (strategy.position_size > 0) // If there's an open long position
strategy.close_all() // Fully close all long positions
strategy.entry("short", strategy.short) // Open a short position
So it's basically trying to determine the previous position. Close it to 0. And open a new one in the opposite direction.
{
"ticker": "{{ticker}}",
"prev_position": "{{strategy.prev_market_position}}",
"close_all": true,
"action": "{{strategy.order.action}}",
"quantity": "{{strategy.order.contracts}}",
"pointer": "Insert your ponter here"
}
The problem is that orders interact with each other and previous order don't close completely. It closes it with the same amount the order was opened loosong all Pnl.
I tried many combinations of lines and commands. didn't help. This idea should simply reverse position with the same amount so close to the current position and open the same size in opposite side with market order.
Thank you