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:
thank you very much in advance!!