processingclouds wrote: ↑Mon Mar 28, 2022 9:07 pm
Hey,
By default, during both historical and real-time calculation, strategy will enter on the bar’s close.
When forwardtesting, you have the option of configuring script calculation to occur on every real-time tick. To enable this, specify it in the script’s code using: strategy(............., calc_on_every_tick=true).
NOTE: You can also look in process_orders_on_close specification.
hi
I was already using the calc_on_every_tick, but it still does not work. When i look at the historic data it is still buying at the open of candle [+1].
I use the process_orders_on_close to make a test, but it was buying at the close of candle [0].
Below is my code
thanks for all the support so far
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/
// © shabovi
//@version=5
strategy("Test", 'Test Inside Bar ET', overlay = true, initial_capital = 500,
default_qty_value = 100, default_qty_type = strategy.percent_of_equity,
commission_type = strategy.commission.percent, commission_value = 0.075, calc_on_every_tick = true, process_orders_on_close = true)
//input ema
ema1 = input(8, title='EMA-1')
ema2 = input(80, title='EMA-2')
//ema
pema1 = ta.ema(close, ema1)
pema2 = ta.ema(close, ema2)
//Definition of inside bar
inside = high[1] < high[2] and low[1] > low[2]
goLong = close > high[1] + 20
goShort = close < low[1] - 20
//conditions to entry
emalong = pema1[1] > pema2[1]
emashort = pema2[1] > pema1[1]
long = low[1] > pema1[1]
short = high[1] < pema1[1]
//entry stop and target
entrylong = high[1] + 20
entryshort = low[1] - 20
stopL = low [2] - (200*10*syminfo.mintick)
stopS = high [2] + (200*10*syminfo.mintick)
targetL = ((2*(high[1] - stopL))+high[1])*100*syminfo.mintick
targetS = ((2*(low[1] - stopS))+low[1])*100*syminfo.mintick
//
var stopLossLongSaved = 0.0
var takeProfitLongSaved = 0.0
var stopLossShortSaved = 0.0
var takeProfitShortSaved = 0.0
longc = inside and emalong and long and goLong
shortc = inside and emashort and short and goShort
if longc
if strategy.position_size == 0
stopLossLongSaved := stopL
takeProfitLongSaved := targetL
strategy.entry('Long', strategy.long, stop = entrylong)
if shortc
if strategy.position_size == 0
stopLossShortSaved := stopS
takeProfitShortSaved := targetS
strategy.entry('Short', strategy.short, stop = entryshort)
//exit trade
strategy.exit(id="LE", from_entry="Long", limit = takeProfitLongSaved, stop = stopLossLongSaved)
strategy.exit(id="SE", from_entry="Short", limit = takeProfitShortSaved, stop = stopLossShortSaved)
//plot
plot(longc ? stopL : na, color = color.red, style = plot.style_linebr, linewidth = 3, offset = -1)
plot(longc ? targetL : na, color = color.lime, style = plot.style_linebr, linewidth = 3, offset = -1)
plot(shortc ? stopS : na, color = color.red, style = plot.style_linebr, linewidth = 3, offset = -1)
plot(shortc ? targetS : na, color = color.lime, style = plot.style_linebr, linewidth = 3, offset = -1)
plot(pema1, color = color.white, linewidth = 2)
plot(pema2, color = color.white, linewidth = 2)