Thorseidon
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: March 1st, 2023

How to trigger alerts in real time (calc_on_every_tick on)

Hi guys, hope you are doing well.
I'm an intermediate level coder, but still I had been struggling for some weeks with a simple code and I cannot make it work. Please I need your help.
The intention is to trigger an entry at the exact time upper/lower Bollinger band line is crossed, and close it when BB MA is crossed (both are checked on real time, before bar closes). I need to link this with an external bot provider so I need to trigger alerts. Strategy and entry alerts are working well, but exit is not working.

This is the exit condition. When this happens, both strategy.exit and alert() code lines are triggered. I have tried adding and removing strategy (and leaving only alert code), but nothing helped.
realtimeCloseCond = barstate.isrealtime and (strategy.position_size > 0 and close < SL_long) or (strategy.position_size < 0 and close > SL_short)

My best hypothesis is that calc_on_every_tick on is messing things around someway and strategy.position_size never actually changed (stayed at zero)

I was wondering if you had any similar issue working with calc_on_every_tick on that could help me understand this error. I feel there is something about the way code works with calc_on_every_tick on that I don't know and may be explaining this issue.

Thanks in advance and regards

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: How to trigger alerts in real time (calc_on_every_tick on)

Hey,

I don't know of any issues specifically. Can you please reply with the whole block of code for the exit condition, exit statement and alert and I can have a look if anything seems amiss.

Thorseidon
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: March 1st, 2023

Re: How to trigger alerts in real time (calc_on_every_tick on)

Hi Steve,
Thanks a lot for your help. Actually I somehow solved it (I did so many changes that I'm not sure what was off). Now code triggers entry and exit signals correctly!

Kind regards

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: How to trigger alerts in real time (calc_on_every_tick on)

Well done on working it out yourself. Sometimes a rewrite of a section of code is all it takes, albeit frustrating at times!

twobeers
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: February 2nd, 2023

Re: How to trigger alerts in real time (calc_on_every_tick on)

Hey together,

I'm struggeling with the same issue a couple of days I hope someone can help me.
Actually the entry should happen in the very moment after the price reaches a specific level at the realtime-bar.

So I turned "calc_on_every_tick = true" but the entry will then only take place at the next bar at the open price.
If I turn "process_orders_on_close = true" the results are also not correct and the entry comes on the right bar but not at the exact real-time-price but on the close-price.
It's just the entrys. If profit target gets reached it works.

Here is my code:

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
strategy("ATR Strategie", overlay = true, initial_capital = 100000, max_bars_back = 5000,
 calc_on_every_tick = true, pyramiding = 50, process_orders_on_close = true) //use_bar_magnifier = false, process_orders_on_close = false)

positionsize          = input.int(title="Position size", defval=1, step=1)
entry_1               = input.float(title="Entry 1", defval=1, step=0.5)
i_target1             = input.float(title="Profit % Target 1", defval=10)

// Trade sessions
session               = input.session(title="Open time", defval ="1000-2250") 
session_price         = input.session(title="Open Price", defval ="1000-1004")

isinsession(_sess) => not na(time(timeframe.period, _sess))

// For checking if we are in the session
_session         = isinsession(session) //and not isinsession(dax_session)[1]
sessionprice     = isinsession(session_price)

// getting the ATR value for the dashed lines
atr                 = ta.atr(14)

// defining arrays for storing opening price and atr value
var a_openprice     = array.new_float(1)
var a_low_atr1      = array.new_float(1)
var a_high_atr1     = array.new_float(1)

// storing the opening price at 10:00 and the ATR value { \\ 
if sessionprice 
    array.push(a_openprice, barstate.isconfirmed ? open : na)
    array.shift(a_openprice)

    array.push(a_low_atr1, low - (atr*entry_1))
    array.shift(a_low_atr1)

    array.push(a_high_atr1, high + (atr*entry_1))
    array.shift(a_high_atr1)

    // drawing the atr value dashed lines for making trade decisions
    line.new(bar_index, array.get(a_low_atr1, array.size(a_low_atr1)-1), bar_index+20, array.get(a_low_atr1, array.size(a_low_atr1)-1), style = line.style_dashed, color = color.green)
    line.new(bar_index, array.get(a_high_atr1, array.size(a_high_atr1)-1), bar_index+20, array.get(a_high_atr1, array.size(a_high_atr1)-1), style = line.style_dashed, color = color.red)

// Save stops & targets (exits are not coded yet)
var float tradeStop    = na // not coded so far
var float t_target     = na
buySignal1             = false
sellSignal1            = false

// 1. Buy order Requirements
if _session and close <= array.get(a_low_atr1, array.size(a_low_atr1)-1) //green "line.new" dashed
    buySignal1 := true

// 2. Sell order Requirements
if _session and close >= array.get(a_high_atr1, array.size(a_high_atr1)-1) //red "line.new" dashed
    sellSignal1 := true

// Enter buy order 1
if buySignal1 and strategy.position_size == 0
    t_target := array.get(a_openprice, array.size(a_openprice)-1)
    positionSize = positionsize
    strategy.entry(id="Long_1", direction=strategy.long, qty=positionSize)

// Enter sell order 1
if sellSignal1 and strategy.position_size == 0
    t_target := array.get(a_openprice, array.size(a_openprice)-1)
    positionSize = positionsize
    strategy.entry(id="Short_1", direction=strategy.short, qty=positionSize)

// Manage exit orders (TP & SL)
strategy.exit(id="Long Profit", from_entry="Long_1", limit=t_target, qty_percent=100)
strategy.exit(id="Short Profit", from_entry="Short_1", limit=t_target, qty_percent=100)

// close all positions at the end of the session
t = time(timeframe.period, session)
session_over = not na(t[1]) and na(t)
if session_over
    strategy.close_all("close end of session")

// Draw entry and exit data to chart   
plotshape(buySignal1, style=shape.diamond, color=color.green, location=location.belowbar)
plot(strategy.position_size != 0 ? tradeStop : na, color=color.red, style=plot.style_linebr, title="Stop Loss")
plot(strategy.position_size != 0 ? t_target : na, color=color.green, style=plot.style_linebr, title="Profit Target 1")

// Draws a green backround for the session
bg = false

if _session
    bg := true

bgcolor(bg ? color.new(color.green, 80) : na)

If calc_on_every_tick = true and process_orders_on_close = true you can see the results on the image below:

Image

thank you very much in advance!!

RobAgrees
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: March 17th, 2023
Contact: TradingView Profile

Re: How to trigger alerts in real time (calc_on_every_tick on)

I believe it's because you are using var when initializing your variables, which doesn't update the value intrabar, hence it still orders on close because that's when the next value in the series is updated. You'll want to use varip instead. But note, it can't be backtested accurately because the intrabar can't be simulated.

twobeers
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: February 2nd, 2023

Re: How to trigger alerts in real time (calc_on_every_tick on)

Hey RobAgrees,

sorry for the late response! Thank you very much that helped :)
Have a nice weekend.

Greetings from Cologne / Germany

Tobias

Return to “Pine Script Q&A”