-2

The below script is working fine and is generating signals, but not able to convert this into strategy and generate signals

study("Trend Magic", shorttitle="TM", overlay=true)
CCI = 20  
ATR = 5 
Multiplier=1 
original=true 
upSign = '↑' // indicates the indicator shows uptrend
downSign = '↓' // indicates the indicator showing downtrend
colorBuy= #2DFF03 // Good sign for long trade
colorSell = #733629 // Good sign for short trade

thisCCI = cci(close, CCI)
lastCCI = nz(thisCCI[1])
bufferDn= high + Multiplier * sma(tr,ATR)
bufferUp= low - Multiplier * sma(tr,ATR)
if (thisCCI >= 0 and lastCCI < 0) 
    bufferUp := bufferDn[1]
if (thisCCI <= 0 and lastCCI > 0) 
    bufferDn := bufferUp[1]

if (thisCCI >= 0)
     if (bufferUp < bufferUp[1])
        bufferUp := bufferUp[1]
else
    if (thisCCI <= 0)
        if (bufferDn > bufferDn[1])
            bufferDn := bufferDn[1]

x=thisCCI >= 0 ?bufferUp:thisCCI <= 0 ?bufferDn:x[1]
swap=x>x[1]?1:x<x[1]?-1:swap[1]
swap2=swap==1?lime:red
swap3=thisCCI >=0 ?lime:red
swap4=original?swap3:swap2

bullTrendMagic = swap4 == lime and swap4[1] == red
bearTrendMagic = swap4 == red and swap4[1] == lime

plotchar(bullTrendMagic, title="TM", char=upSign, location=location.belowbar, color=colorBuy, transp=0, text="TM", textcolor=colorBuy, size=size.auto)
plotchar(bearTrendMagic, title="TM", char=downSign, location=location.abovebar, color=colorSell, transp=0, text="TM", textcolor=colorSell, size=size.auto)

The script is working fine and is generating signals, but not able to convert this into strategy and generate signals

pavs
  • 1

1 Answers1

0

Here is the information https://www.tradingview.com/pine-script-docs/en/v5/search.html?q=convert that you need to know to convert from v1 to v3, and after that using autoconvert feature to convert it from v3 to v4 and then to the v5.

Here is the information what strategy is. https://www.tradingview.com/pine-script-docs/en/v5/concepts/Strategies.html

The conversion to strategy means that you pass your signal variables into strategy.entry() function.

//@version=5
strategy('Trend Magic', shorttitle='TM', overlay=true)
CCI = 20
ATR = 5
Multiplier = 1
original = true
upSign = '↑'  // indicates the indicator shows uptrend
downSign = '↓'  // indicates the indicator showing downtrend
colorBuy = #2DFF03  // Good sign for long trade
colorSell = #733629  // Good sign for short trade

thisCCI = ta.cci(close, CCI)
lastCCI = nz(thisCCI[1])
bufferDn = high + Multiplier * ta.sma(ta.tr, ATR)
bufferUp = low - Multiplier * ta.sma(ta.tr, ATR)
if thisCCI >= 0 and lastCCI < 0
    bufferUp := bufferDn[1]
    bufferUp
if thisCCI <= 0 and lastCCI > 0
    bufferDn := bufferUp[1]
    bufferDn

if thisCCI >= 0
    if bufferUp < bufferUp[1]
        bufferUp := bufferUp[1]
        bufferUp
else
    if thisCCI <= 0
        if bufferDn > bufferDn[1]
            bufferDn := bufferDn[1]
            bufferDn

var x = 0.0
var xx = 0.0
xx := x
x := thisCCI >= 0 ? bufferUp : thisCCI <= 0 ? bufferDn : xx[1]

var swap = 0.0
var swapp = 0.0
swapp := swap[1]

swap := x > x[1] ? 1 : x < x[1] ? -1 : swapp
swap2 = swap == 1 ? color.lime : color.red
swap3 = thisCCI >= 0 ? color.lime : color.red
swap4 = original ? swap3 : swap2

bullTrendMagic = swap4 == color.lime and swap4[1] == color.red
bearTrendMagic = swap4 == color.red and swap4[1] == color.lime

plotchar(bullTrendMagic, title='TM', char=upSign, location=location.belowbar, color=color.new(colorBuy, 0), text='TM', textcolor=color.new(colorBuy, 0), size=size.auto)
plotchar(bearTrendMagic, title='TM', char=downSign, location=location.abovebar, color=color.new(colorSell, 0), text='TM', textcolor=color.new(colorSell, 0), size=size.auto)

if bullTrendMagic
    strategy.entry('Buy', strategy.long)

if bearTrendMagic
    strategy.entry('Sell', strategy.short)


Starr Lucky
  • 1,578
  • 1
  • 7
  • 8