I am trying to make an RSI trading strategy that executes an end of bar trade entry when RSI closes below a certain level on that same bar. All my entries are off (late) by exactly one bar.
Right now, this is what I'm doing for that part:
// Open a long position when RSI(5) is below 20
if (rsiValue \< 20)
entryPrice := na(entryPrice) ? close : entryPrice // Store entry price when first entering
// Exit each individual lot when the lot is profitable at the close of a bar
exitCondition = close \> entryPrice
Right now, it's executing the trades on the following bar (and closing the trades late too, but one fire at a time). I assume this is because RSI is being calculated on the closing price of the bar, and executing a trade after that close on the same bar is impossible. Maybe I'm wrong on that, but that's what I'm assuming. If anyone has a fix for that and can tell me how to code it, I'd be grateful. I'm desperate to get this strategy to work because I'm having to backtest by hand right now and it takes FOREVER. I don't need it to be able to execute as an actual trading system, it's just for backtesting, if that matters.
I don't know if this is possible, but can I possibly get RSI to calculate just before the close, so the trade can still trigger after? I need the trade entry to be at the close of the same bar close as the RSI trigger conditions are met. The inter-bar period is critical to capture for what I'm trying to do. Also, if this isn't the forum for this question and someone knows where else to send me for answers, I'd appreciate that almost as much. Thanks in advance to anyone who can give me any kind of guidance on dealing with this problem.
NOTE: As of 11/26/24 question has been answered. Thank you @Starr Lucky. The solution was to place process_orders_on_close = true in my strategy description.
By default, your orders will be executed on the open of the next bar. If you want your orders to be executed on the close of the current bar, set process_orders_on_close=true
in your strategy()
call.
process_orders_on_close (const bool) When set to true, generates an additional attempt to execute orders after a bar closes and strategy calculations are completed. If the orders are market orders, the broker emulator executes them before the next bar's open. If the orders are price-dependent, they will only be filled if the price conditions are met. This option is useful if you wish to close positions on the current bar. This setting can also be changed in the strategy's "Settings/Properties" tab. Optional. The default is false.