Page 1 of 1

Entry at Inside Bar High rupture

Posted: Mon Mar 21, 2022 4:13 pm
by shabovi
I'm learning to code and i'm having trouble to make my strategy.entry(long). I have some conditions to make the entry (inside bar above ema1 and ema2, ema1 above ema2), having them being met I would like to enter when the inside bar High is surpassed, but only if it is surpassed THE NEXT CANDLE by 20usd. My stop loss would be: the low of the candle before the inside bar less 20 usd. My target would be: 2 times my risk.
The script is partially working. The problem is that it is buying whenever it crosses the inside bar high + 20usd. Even if it is some candles after. I would like it to enter only if it's price is surpassed the next candle.

Code: Select all

inside = high < high[1] and low > low[1]
emalong = pema1 > pema2
long = low > pema1
longc = emalong and long and inside

entrylong = high + (200*10*syminfo.mintick)
stopL = low [1] - (200*10*syminfo.mintick)
targetL = ((2*(high - stopL))+high)*100*syminfo.mintick

var stopLossLongSaved = 0.0
var takeProfitLongSaved = 0.0
var stopLossShortSaved = 0.0
var takeProfitShortSaved = 0.0

longc = afterStartDate and inside and emalong and long

if longc
    if strategy.position_size == 0
        stopLossLongSaved := stopL
        takeProfitLongSaved := targetL
        strategy.entry('Long', strategy.long, stop = entrylong)

plot(longc ? stopL : na, color = color.red, style = plot.style_linebr)
plot(longc ? targetL : na, color = color.lime, style = plot.style_linebr)

Re: Entry at Inside Bar High rupture

Posted: Mon Mar 28, 2022 2:58 pm
by processingclouds
Hey, so technically you are looking at 3 candles. [0] Last Candle , [1] and [2]
Your Inside Bar is cancel [1] , the one preceding last candle.

So you want to :
1. Have an inside bar i.e. high[1] < high[2] and low[1] > low[2]
2. ema1 above ema2 i.e. pema1[1] > pema2[1]
3. Low of inside bar above ema1 and ema2 , i.e. low[1] > pema1[1]
4. Inside bar high +20 USD is above current high i.e. high > high[1] + 20

So pretty much what you have , in the beginning of the code, except point 4, where you can define the inside bar high + 20 as a logic condition itself for going long.

Re: Entry at Inside Bar High rupture

Posted: Mon Mar 28, 2022 4:43 pm
by shabovi
processingclouds wrote:
Mon Mar 28, 2022 2:58 pm
Hey, so technically you are looking at 3 candles. [0] Last Candle , [1] and [2]
Your Inside Bar is cancel [1] , the one preceding last candle.

So you want to :
1. Have an inside bar i.e. high[1] < high[2] and low[1] > low[2]
2. ema1 above ema2 i.e. pema1[1] > pema2[1]
3. Low of inside bar above ema1 and ema2 , i.e. low[1] > pema1[1]
4. Inside bar high +20 USD is above current high i.e. high > high[1] + 20

So pretty much what you have , in the beginning of the code, except point 4, where you can define the inside bar high + 20 as a logic condition itself for going long.
I did try this before, but the thing is that the tradingview is making the buy at the candle [-1].
I think my entry is correct with

Code: Select all

entrylong = high[1] + (200*10*syminfo.mintick)
IDK, maybe the correct code should be something like:

4. Current price after the inside bar is Inside bar high +20 USD, i.e. close > high[1] + 20
I'm using the ''close'' because it represents the current price, witch is when i would make the buy, but its not happening when it should.

Image of tradingview buy: https://ibb.co/r7hVtj1

Re: Entry at Inside Bar High rupture

Posted: Mon Mar 28, 2022 9:07 pm
by processingclouds
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.

Re: Entry at Inside Bar High rupture

Posted: Tue Mar 29, 2022 2:19 pm
by shabovi
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)