16

Is there a way to create an indicator which reflects the current price of a stock in Pine Script? I need this indicator because I need to enter an order before the candle closes (when there is a specific crossover) and the data for back testing is provided bar by bar. I think an indicator can allow me to do this and if not is there another way to tackle this.

I am not an experienced pine scripter and any help will be greatly appreciated:) Thanks,

e2e4
  • 3,428
  • 6
  • 18
  • 30
Aryagm
  • 530
  • 1
  • 7
  • 18
  • Yes, this would be super useful. I have a label at the last bar with the current open PnL and it needs to display the current close price for that. It's a strategy with 24h bars so there would be a 1d delay in PnL following... – dorien Mar 29 '22 at 02:35

2 Answers2

16

The close during the unconfirmed bar represents the current price of the asset.

However, Tradingview's backtesting engine will execute the order only on the next candle. See here why - https://www.tradingview.com/pine-script-docs/en/v4/language/Execution_model.html#execution-model

e2e4
  • 3,428
  • 6
  • 18
  • 30
5

This answer is late, but since the solution is not posted, I will provide it here. To solve this, you need pinescript to calculate the prices on the fly, and this is a feature you should turn on. Check the following example:

//@version=5
strategy(title="My Strategy", calc_on_every_tick=true)

calc_on_every_tick variable should be set to true, and there you go. Now the close value represents the current price, and not bar-close.

Source: Calculate bars on every tick

Maubeh
  • 395
  • 1
  • 6
  • 13