I see 'Order's limit (9000) was reached' error

This error means that the strategy placed more orders, or closed more trades, than the maximum number allowed. These limitations vary by plan and enable our servers to work more efficiently.

To avoid this error, you can use the trim_orders parameter in the strategy() function. With this parameter set to true, each new order appears in the List of Trades, and the oldest order above the order limit is removed.

 

Here's an example:

//@version=5
strategy("My strategy", overlay = true, margin_long = 100, margin_short = 100, trim_orders = true)

if bar_index % 2 == 0
    strategy.entry("My Long Entry Id", strategy.long)

if bar_index % 2 != 0
    strategy.entry("My Short Entry Id", strategy.short)

Alternatively, you can limit the dates when a strategy places orders by checking for a time range in the order condition. The following example script establishes a time range for placing orders by checking whether the time of the current bar is between two timestamps.

//@version=5
strategy("My strategy", overlay = true, margin_long = 100, margin_short = 100)

enableFilter = input(true,  "Enable Backtesting Range Filtering")
fromDate     = input.time(timestamp("20 Jul 2023 00:00 +0300"), "Start Date")
toDate       = input.time(timestamp("20 Jul 2099 00:00 +0300"), "End Date")

tradeDateIsAllowed = not enableFilter or (time >= fromDate and time <= toDate)

longCondition =  ta.crossover(ta.sma(close, 14),  ta.sma(close, 28))
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))

if longCondition and tradeDateIsAllowed
    strategy.entry("Long", strategy.long)

if shortCondition and tradeDateIsAllowed
    strategy.entry("Short", strategy.short)