0

As seen in the picture below, the trade was initiated one bar after the desired bar. The black arrow indicates the right order place ( open of that day) but for some reason the trade was initiated only on the bar afterward ( blue arrow). How can I solve this problem? attached is the picture : enter image description here

Attached is the code:

//@version=5
strategy("Gap MSFT", overlay=true)

MSFT_close = request.security("MSFT", "D", close[1])
MSFT_open = request.security("MSFT", "D", open[0])

price_change = MSFT_open / MSFT_close > 1.03
if price_change 
    l = label.new(bar_index, na, str.tostring(MSFT_close), 
         color=color.green, 
         textcolor=color.rgb(255, 255, 255),
         style=label.style_arrowup,
         yloc=yloc.abovebar)

    d = label.new(bar_index, na, str.tostring(MSFT_open), 
         color=color.rgb(86, 76, 175), 
         textcolor=color.rgb(255, 255, 255),
         style=label.style_arrowdown,
         yloc=yloc.belowbar)

if price_change 
    strategy.entry("long", strategy.long, 100)
    strategy.exit("Exit Long", from_entry="long", limit=MSFT_open * 1.01)

bgcolor(price_change ? color.new(#c1defa, 51) : na)
migdal menora
  • 169
  • 4
  • 16

1 Answers1

0

By default, Pine script runs once, at the end of the bar close after the last data on that bar. As a consequence, market order will be executed on the next bar's open (I'm not aware of the details but as the last data has been processed there is probably no more 'current bar' where you can place the order).

This is not an issue with crypto as markets are 24/7 but it can be an issue with other markets that ie. close on Friday and open on Monday.

You can force Pine to execute on bar's close (this definition is based on documentation, which is confusing as scripts are run on bar's close by default. This is probably a different close).

Use process_orders_on_close = true in your strategy() definition and you will see the orders on real bar's close.

(This should be the default behavior, I have never seen anybody making a decision on Friday's bar and then make an order on next Monday. The default should be to make decision on a day's bar and in the last minute/second execute a trade as a regular day trader does, who closes positions at then end of the day)

karatedog
  • 2,508
  • 19
  • 29
  • thanks for the answer but it didn't help to solve the issue. – migdal menora Nov 04 '22 at 12:14
  • 1
    Did the trade happened one bar before using this parameter? Anyway you cannot execute the script on an `open` of a bar except if you do some wizardry. If a trading condition is TRUE to start trading on a bar's open (on its first data) it means the condition was TRUE at the close of the previous bar (as there is no other data between a bar's close and the next's open). – karatedog Nov 04 '22 at 12:48
  • Thanks, @karatedog, So a way to solve this can be executing the trade at the close of the first second or minute, while the script uses the last day's close price. Any idea how do it? – migdal menora Nov 04 '22 at 13:23
  • No, Your trade is already executing at the start of the day which is the start of the first hour which is the start of the first minute (and so on). What happened when you have put in that `process_orders_on_close = true` into your strategy() definition? – karatedog Nov 04 '22 at 16:44
  • Actually nothing happened – migdal menora Nov 04 '22 at 18:02