conversion indicator to strategy not working

Viewed 31

I am trying to test this indicator as strategy. As indicator displays on the chart fine. If I change "indicator" to "strategy" I can save it without errors but does not display any more. As well not displaying anything if I add it to the default startegy template in tradingview (keep "strategy" desciptor, remove "indicator", no errors saving). Thank you.

//@version=5

indicator("Test10_v1",overlay=true,max_bars_back=1000,max_lines_count=500,max_labels_count=500)
length = input.float(500,'Window Size',maxval=500,minval=0)
h      = input.float(8.,'Bandwidth')
mult   = input.float(3.) 
src    = input.source(close,'Source')


up_col = input.color(#39ff14,'Colors',inline='col')
dn_col = input.color(#ff1100,'',inline='col')
disclaimer = input(false, 'Hide Disclaimer')
//----
n = bar_index
var k = 2
var upper = array.new_line(0) 
var lower = array.new_line(0) 

lset(l,x1,y1,x2,y2,col)=>
    line.set_xy1(l,x1,y1)
    line.set_xy2(l,x2,y2)
    line.set_color(l,col)
    line.set_width(l,2)

if barstate.isfirst
    for i = 0 to length/k-1
        array.push(upper,line.new(na,na,na,na))
        array.push(lower,line.new(na,na,na,na))
//----
line up = na
line dn = na
//----
cross_up = 0.
cross_dn = 0.
if barstate.islast
    y = array.new_float(0)
    
    sum_e = 0.
    for i = 0 to length-1
        sum = 0.
        sumw = 0.
        
        for j = 0 to length-1
            w = math.exp(-(math.pow(i-j,2)/(h*h*2)))
            sum += src[j]*w
            sumw += w
        
        y2 = sum/sumw
        sum_e += math.abs(src[i] - y2)
        array.push(y,y2)

    mae = sum_e/length*mult
    
    for i = 1 to length-1
        y2 = array.get(y,i)
        y1 = array.get(y,i-1)
        
        up := array.get(upper,i/k)
        dn := array.get(lower,i/k)
        
        lset(up,n-i+1,y1 + mae,n-i,y2 + mae,up_col)
        lset(dn,n-i+1,y1 - mae,n-i,y2 - mae,dn_col)
        
        if src[i] > y1 + mae and src[i+1] < y1 + mae
            label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center)
        if src[i] < y1 - mae and src[i+1] > y1 - mae
            label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center)
    
    cross_up := array.get(y,0) + mae
    cross_dn := array.get(y,0) - mae


//----
var tb = table.new(position.top_right, 1, 1
  , bgcolor = #35202b)

if barstate.isfirst and not disclaimer
    table.cell(tb, 0, 0, 'Repaints'
      , text_size = size.small
      , text_color = #cc2f3c)
2 Answers

(line 35): As strategy calculates on the close of each bar, the if barstate.islast condition will be false for symbols with the last bar not being confirmed. Use if barstate.islastconfirmedhistory or barstate.isrealtime condition instead.

For a gradual progress (through which you can learn the mysteries of Pine), first create two variables in your Indicator.

One should be true when you want to open a trade, and the other should be true when you want to close a trade (this is a good process of handling the situation when both of them are true)

To test the validity of your script (still being an Indicator) use plotchar() and draw fancy characters below/above the candle where your would have started/closed the trade.

When this works as expected, THEN turn the Indicator to a Strategy and you only need to change the behavior attached to your variables above to open and close a deal when they became true.

Watch out that Indicator and Strategy behaves differently. When a condition is true and you draw a signal character when that happens, Indicator will display that character on the candle it happened BUT Strategy will start the on the next candle's start (so it looks like it is off with 1 candle). For more precision turn on "On bar close" in your strategy's properties or add the process_orders_on_close = true statement to your strategy definition.

Related