Pinescript Strategy Opens a Buy/Long trade, then it should close a trade if there is a Short/Sell trade but it does not close

179 views Asked by At

I have a strategy that if it opens a trade (whether Long/Buy or Short/Sel), it should continue with the trend but if signals an opposite trade, it should close the first trade, but its not closing anything.

Also on TradingView, it does not show the Close trade (LX or SX) on the chart!

Here is a sample:

bool longCondition = long_entry
bool shortCondition = short_entry

// Create entries based on the cross conditions for both trades biases.
if longCondition and inDateRange
    strategy.entry("LONG", strategy.long, comment=i_alert_txt_entry_long)
    strategy.close("SX", comment=i_alert_txt_exit_short)  // Close the short position if open

if shortCondition and inDateRange
    strategy.entry("SHORT", strategy.short, comment=i_alert_txt_entry_short)
    strategy.close("LX",comment=i_alert_txt_exit_long)  // Close the long position if open

I have tried many variations of the code, It opens a trade normally but it does not close and I have to manually close a trade (with a loss of course)

I have tried multiple conditions it does not work.

Changed from Comment and alerts and does not work.

I am using WunderTrading as a medium between Trading View and Binance (Crypto Trading)

2

There are 2 answers

1
Gu5tavo71 On

It would not be necessary to close one order to open another.

if longCondition and inDateRange
    strategy.entry("LONG", strategy.long, comment=i_alert_txt_entry_long)
if shortCondition and inDateRange
    strategy.entry("SHORT", strategy.short, comment=i_alert_txt_entry_short)

But if you insist, I think the simplest solution is to close the operation, and in the next candle open a new one.

// Create entries based on the cross conditions for both trades biases.
if longCondition and inDateRange
    strategy.close("SX", comment=i_alert_txt_exit_short)  // Close the short position if open
if longCondition[1] and inDateRange[1]
    strategy.entry("LONG", strategy.long, comment=i_alert_txt_entry_long)

if shortCondition and inDateRange
    strategy.close("LX",comment=i_alert_txt_exit_long)  // Close the long position if open
if shortCondition[1] and inDateRange[1]
    strategy.entry("SHORT", strategy.short, comment=i_alert_txt_entry_short)

The explanation is that by default it uses first in, first out (FIFO)
There is a close_entries_rule "ANY" option, but it has many conditions

https://www.tradingcode.net/tradingview/exit-order-sequence/

0
Gu5tavo71 On

This is an example code posted by WunderTrading (ex-WunderBit):

if inDateRange()

    if strategy.position_size == 0 or strategy.position_size > 0 and longCondition
        strategy.entry('long', strategy.long, comment = i_alert_txt_entry_long)

    if strategy.position_size == 0 or strategy.position_size < 0 and shortCondition
        strategy.entry('short', strategy.short, comment = i_alert_txt_entry_short)

Not be necessary to close one order to open another.