0

I am new to pinescript coding and I am trying to make a strategy for placing orders automatically on a Algo trading website through taking out signals from Tradingview webhook Url. I made a simple strategy for moving average cross over. However on some days there are no signals fired because the price is either above or below the moving average and there was a Buy or sell on previous session.

I was wondering if it is possible take a clue from previous day signal and continue with it on the next day and fire a new signal along the same trend.

I tried to use the logic of First bar HIGH or LOW break out and it works. But now I am unable to use these both signals with condition or a flag on priority whichever fires first. Would really appreciate if anyone can guide me here.

The code that I have formulated with the help of few fellow members here for First candle H/L break is as below

[study("Moving average Crossover strategy",overlay=true)

ma = input(true, title="***** MINOR TREND *****" )
Short_term_trend   = input(defval=23)

cns_up = 0                  // Declare the up counter
cns_up := nz(cns_up\[1\])     // Get the previous value of it

cns_dwn = 0                 // Declare the up counter
cns_dwn := nz(cns_dwn\[1\])   // Get the previous value of it

isLong = false              // A flag for going LONG
isLong := nz(isLong\[1\])     // Get the previous value of it

isShort = false             // A flag for going SHORT
isShort := nz(isShort\[1\])   // Get the previous value of it

ema23=ema (hlc3,Short_term_trend)

up      = close > ema23
dn      = close < ema23

cns_up  := up  ? cns_up + 1 : 0
cns_dwn := dn  ? cns_dwn + 1 : 0

buySignal  = not isLong  and (cns_up >= 1)      // Check the BUY condition
sellSignal = not isShort and (cns_dwn >= 1)    // Check the SELL condition

if (buySignal) 
    isLong := true
    isShort := false
 
if (sellSignal) 
    isLong := false
    isShort := true

plot(ma and ema23 ? ema23 : na,color=color.aqua,linewidth=2)
plotshape(buySignal ,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.large)
plotshape(sellSignal , title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.large)][1]

// Code for First bar High low break

study("Close of first bar of a day", overlay=true)


isNewDay = time("D") != time("D")[1] 

var plotcond = false
var firstBarHighValue = high
var firstBarLowValue = low

if isNewDay
    firstBarLowValue := low
    firstBarHighValue := high
    plotcond := true


buy  = close > firstBarHighValue and close>open and plotcond
sell = close < firstBarLowValue and close<open and plotcond

if (buy or sell) and (not isNewDay)
    plotcond := false


plotshape(buy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.large)
plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.large)
4

0 回答 0