NandaKishore.Rao
NandaKishore.Rao

Reputation: 131

Pinescript close_entries_rule = "ANY" not exiting the correct trades

It seems like List of Trades generated by the Strategy Tester is not accurate for a strategy that has multiple long entries and short entries with each entry having its own stop loss and take profit. Maybe the overall performance summary is accurate, but the List of Trades. What am I missing? Is there any way to get an accurate log of trades?

If I have a long-only or short-only strategy with multiple entries and each entry having separate SL and TP, it seems alright. Trades generated by below strategy matches entries with the right exit.

//@version=5
strategy("My script", overlay = true, initial_capital = 1000, close_entries_rule = "ANY")

barHour = hour(time)
barMin = minute(time)

var longId = 1
var shortId = 1

timePeriod = time >= timestamp(syminfo.timezone, 2023,2,1)

if timePeriod

    if barHour == 8
        entryId = 'LongEntry_'+str.tostring(longId)
        strategy.order(id=entryId, direction=strategy.long, qty=1, limit=close)
        strategy.exit(id='LongExit_'+str.tostring(longId), from_entry=entryId, stop=close-1000, limit=close+500)
        longId += 1
     else if barHour == 12
        entryId = 'LongEntry_'+str.tostring(longId)
        strategy.order(id=entryId, direction=strategy.long, qty=1, limit=close)
        strategy.exit(id='LongExit_'+str.tostring(longId), from_entry=entryId, stop=close-1000, limit=close+500)
        longId += 1

However, if there is are a combination of short trades and long trades, entries and exits get jumbled up. Trades generated by the below strategy is not accurate.

//@version=5
strategy("My script", overlay = true, initial_capital = 1000, close_entries_rule = "ANY")

barHour = hour(time)
barMin = minute(time)

var longId = 1
var shortId = 1

timePeriod = time >= timestamp(syminfo.timezone, 2023,2,1)

if timePeriod

    if barHour == 8
        entryId = 'LongEntry_'+str.tostring(longId)
        strategy.order(id=entryId, direction=strategy.long, qty=1, limit=close)
        strategy.exit(id='LongExit_'+str.tostring(longId), from_entry=entryId, stop=close-1000, limit=close+500)
        longId += 1
    else if barHour == 12
        entryId = 'LongEntry_'+str.tostring(longId)
        strategy.order(id=entryId, direction=strategy.long, qty=1, limit=close)
        strategy.exit(id='LongExit_'+str.tostring(longId), from_entry=entryId, stop=close-1000, limit=close+500)
        longId += 1
    else if barHour == 10
        entryId = 'ShortEntry_'+str.tostring(shortId)
        strategy.entry(id=entryId, direction=strategy.short, qty=1, limit=close)
        strategy.exit(id='ShortExit_'+str.tostring(shortId), from_entry=entryId, stop=close+1000, limit=close-500)
        shortId += 1

Trades list generated by the above is as follows - Strategy Tester Trades List

ShortEntry_1 should not be closing LongEntry_1. Also, there are two ShortEntry_1 which should not be the case. Is the Strategy Tester accurate overall?

Upvotes: 0

Views: 242

Answers (1)

Cláudio Silva
Cláudio Silva

Reputation: 915

Unfortunately, the Strategy Tester does not support hedging, i.e. opening long and short trades in parallel. As soon as you open a reverse trade, you will close the existing position.

Upvotes: 0

Related Questions