Steven Primo Indicator to Strategy

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #223217 quote
    Kole96
    Participant
    New

    Hi everyone, after a lot of work i finaly converted the Steven Primo bollinger Bands indicator from Tradingview to a prorealcode indicator and added some features.

    i just want to convert this indicator in an autmated Strategy, if you can help, here is the code and i upload the indicator itf

     

    DEFPARAM CalculateOnLastBars = 5000
    // Atributs :
    OffsetArrow   = ArrowDistance * pipsize
    OffsetText = TextDistance * pipsize
    OffsetFifth = FifthDistance * pipsize
    OffsetDistance = ContractDistance * pipsize
    OffsetEntry = EntryText * pipsize
    Pipi = pipsize
    SMA50 = Average[50](close)
    
    // Conditions for Close > Bollinger UP
    indicator3 = Average[20](close)+0.382*std[20](close)
    c7 = (close > indicator3)
    SMA20 = Average[20](close)
    // Conditions for 5 Closes above Bollinger Up
    c9=SUMMATION[5](c7)=5
    if c9  and lastsig=0 then // and close > SMA50 then // Price above SMA50
    if FifthCandle then
    drawtext("5",barindex,low-OffsetFifth,SansSerif,bold,25)coloured(0,200,0)
    lastsig=1
    endif
    endif
    //Setup Reset if closes under Boll Up
    if close<indicator3 then
    lastsig=0
    endif
    // Conditions for Closes < Bollinger Down
    indicator4 = Average[20](close)-0.382*std[20](close)
    c8 = (close < indicator4)
    // Conditions for 5 Closes under Bollinger down
    c10=SUMMATION[5](c8)=5
    if c10  and lastsig2=0 then // and close < SMA50 then // Price below SMA50
    if FifthCandle then
    drawtext("5",barindex,high+OffsetFifth,SansSerif,bold,25)coloured(200,0,0)
    lastsig2=1
    endif
    endif
    //Setup Reset if closes above Boll Down
    if close>indicator4 then
    lastsig2=0
    endif
    
    atr14=averagetruerange[14]
    // BB setup
    longComparison = close > indicator3
    shortComparison = close < indicator4
    
    if longComparison then
    if countlong=0 and longComparison[1] then
    countlong=0
    else
    countlong=countlong+1
    endif
    else
    countlong=0
    endif
    
    if shortComparison then
    if countshort=0 and shortComparison[1] then
    countshort=0
    else
    countshort=countshort+1
    endif
    else
    countshort=0
    endif
    pvHighInRange = barssince(pivothigh)<countlong
    pvLowInRange = barssince(pivotlow)<countshort
    // Pivot setup
    
    pivotH = high[1]>high[2] and high[1]>high //TV Script pivotHigh = ta.pivothigh(1, 1)
    pivotL = low[1]<low[2] and low[1]<low //TV Script pivotLow = ta.pivotlow(1, 1)
    if pivotH   and High[1] = Highest[xPeriod](High) then // Parametrer le nombre de candles pour le pivot, quand je l'ai mis à 3 c presque comme TV mais ce qui est bien dans prorealtime c qu'il est sorti d'une position Long perdante et à tout de suite shorté quand le paramètrage le permettait
    pivotHigh=high[1]
    starthigh=barindex[1]
    pivottype=1
    endif
    if pivotL   and Low[1] = Lowest[xPeriod](Low) then
    pivotLow=low[1]
    startlow=barindex[1]
    pivottype=-1
    endif
    
    
    pvHighInRange = barssince(pivothigh)<countlong
    pvLowInRange = barssince(pivotlow)<countshort
    
    // Entry price
    epLong = (pivotHigh) + ticksize
    epShort = (pivotLow) - ticksize
    
    stopLong = epShort
    stopShort = epLong
    
    // Target price
    targetLong = epLong+(abs(epLong-stopLong)*(extension/100))
    targetShort = epShort-(abs(epShort-stopShort)*(extension/100))
    contractSizeL = Risk/(epLong-stoplong)
    contractSizeS = Risk/(stopshort-epShort)
    
    
    // Entry condition
    //canBuy = countLong >= consecutiveCloses and pvHighInRange and high < epLong and epLong>0
    //canSell = countShort >= consecutiveCloses and pvLowInRange and low > epShort and epShort>0
    
    canBuy = countLong > consecutiveCloses and pvHighInRange  and epLong>0 and high crosses over epLong and orderLong //and close > SMA50  //   and SMA20>SMA50 //modif car tradingview repaint!!
    canSell = countShort > consecutiveCloses and pvLowInRange and epShort>0 and low crosses under epShort and orderShort// and close < SMA50 // and SMA20<SMA50 //modif car tradingview repaint!!
    
    //trading signals
    contractSizeL = round(contractSizeL,2)
    contractSizeS = round(contractSizeS,2)
    atr = average[100](range)*0.25//AverageTrueRange[100](close)
    signal=0
    if canBuy and order<>1 and order <> -1  and signalBuy = 0 then // order <> 1 not longonmarket order <> -1 not shortonmarket, added signalBuy to tell him that we entered once the trade
    drawarrowup(barindex,low-OffsetArrow) coloured("blue")
    drawtext("LONG",barindex,low-OffsetText,SansSerif,Bold,12) COLOURED(0,102,80)
    //drawtext(contractSizeL,barindex,low-OffsetDistance)
    //drawtext(pivothigh+0.1,barindex,low-OffsetEntry,SansSerif,Bold,10) COLOURED(204,0,204)
    drawtext(pivothigh+0.1,barindex[8],pivothigh,SansSerif,Bold,12) COLOURED(0,102,80)
    drawtext("▶",barindex[1],pivothigh) coloured("blue")
    drawtext("à",barindex[13],pivothigh,SansSerif,Bold,12) COLOURED(0,102,80)
    drawtext(contractSizeS,barindex[16],pivothigh,SansSerif,Bold,12) COLOURED(0,102,80)
    takeprofit=targetlong
    stoploss=stoplong
    order = 1 //on market
    orderlevel = pivothigh
    orderbar = barindex
    signalBuy = 1 // signal now = 1 because we had a trade and in the line 112 signalBuy was = 0 , to avoid an another trade we must have signalBuy = 0 that's why in line 141 i added if close crosses under indicator3 signalBuy = 0 and now he can enter an another trade if all conditions were met
    
    elsif canSell and order<>-1 and order <> 1  and signalSell = 0 then // order <> 1 not longonmarket order <> -1 not shortonmarket
    drawarrowdown(barindex,high+OffsetArrow) coloured("red")
    drawtext("SHORT",barindex,high+OffsetText,SansSerif,Bold,12) COLOURED(240,0,10)
    //drawtext(contractSizeS,barindex,high+OffsetDistance)
    drawtext(contractSizeS,barindex[16],pivotlow,SansSerif,Bold,12) COLOURED(240,0,10)
    //drawtext(pivotlow-0.1,barindex,high+OffsetEntry,SansSerif,Bold,10) COLOURED(204,0,204)
    drawtext("à",barindex[13],pivotlow,SansSerif,Bold,12) COLOURED(240,0,10)
    drawtext(pivotlow-0.1,barindex[8],pivotlow,SansSerif,Bold,12) COLOURED(240,0,10)
    drawtext("◀",barindex+1,pivotlow) coloured("red")
    signal=-1
    takeprofit=targetshort
    stoploss=stopshort
    order = -1 //on market
    orderlevel = pivotlow
    orderbar = barindex
    signalSell = 1
    
    endif
    if close < indicator3 then // to tell him that we reset the Setup
    signalBuy = 0
    endif
    if close > indicator4 then
    signalSell = 0
    endif
    //if canbuy and not onmarket then
    //BUY 1 shares at market
    //ENDIF
    
    //plot the tp & sl lines
    if order<>0 then
    drawsegment(orderbar,takeprofit,barindex,takeprofit) coloured("darkgreen") style(line,2)
    drawtext(takeprofit,orderbar-5,takeprofit)
    drawsegment(orderbar,stoploss,barindex,stoploss) coloured("red") style(line,2)
    drawtext(stoploss,orderbar-5,stoploss)
    endif
    
    
    //reset the order/tp/sl
    if order=1 then //case long
    if high crosses over takeprofit or low crosses under stoploss then
    order=0
    drawarrowdown(barindex,high+OffsetArrow) coloured("BlueViolet")
    drawtext("Exit L",barindex,high+OffsetText)
    endif
    endif
    //if order=1 then // Trying to Put YES for Winning Trades
    //if high crosses over takeprofit then
    //drawtext("YES",barindex+1,takeprofit+atr*8) coloured(0,255,0)
    //endif
    //endif
    
    if order=-1 then //case short
    if low crosses under takeprofit or high crosses over stoploss then
    order=0
    drawarrowup(barindex,low-OffsetArrow) coloured("BlueViolet")
    drawtext("Exit S",barindex,low-OffsetText)
    endif
    endif
    ColorBetween (indicator3,indicator4,255,153,255,110)
    
    return indicator3 as "Upper band" coloured(128,0,92), indicator4 as "Lower band" coloured(128,0,92), SMA20 as "MA20" coloured(255,0,0), signalBuy as "LONG", signalSell as "SHORT"
    
    Steven-Primo-Example.jpg Steven-Primo-Example.jpg
    #223223 quote
    robertogozzi
    Moderator
    Master

    Yes please, attach the ITF file, if possible.

    #223228 quote
    Kole96
    Participant
    New

    Ah sorry, i thought it was already in, here is it

    #223234 quote
    Kole96
    Participant
    New
    #223364 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DEFPARAM CumulateOrders = False
    p1,p2,p3,p4,p5 = CALL "Projet Primo 382 a 90%"[0, 5, 100, 0, 0, 50, 3, 6, 8, 10, 9, 1, 10](close)
    c1 = (close CROSSES OVER  p1)
    c2 = (close CROSSES UNDER p1)
    IF c1 AND Not LongOnMarket THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    IF c2 AND Not ShortOnMarket THEN
       SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    SET STOP   %LOSS   1
    SET TARGET %PROFIT 2
    #223368 quote
    Kole96
    Participant
    New

    Thank you for the answer and the work, but the Stops and Targets are not in %, they have to be like in the indicator, with the last Swing Low or High.

    how do i change it, i can send you the original Indicator in tradingview.

    #223371 quote
    robertogozzi
    Moderator
    Master

    It’s just an example. You can change it as you like.

    If you post the conditions to enter Long and Short trades, to set SL and TP, I will code them.

    #223607 quote
    Kole96
    Participant
    New

    From the Line 69 you can see that both Target and Stoploss are based on the Pivothigh or Pivotlow calculated with the variable xperiod that i can changed; here is the code from line 69:

     

    // Pivot setup
     
    pivotH = high[1]>high[2] and high[1]>high //TV Script pivotHigh = ta.pivothigh(1, 1)
    pivotL = low[1]<low[2] and low[1]<low //TV Script pivotLow = ta.pivotlow(1, 1)
    if pivotH   and High[1] = Highest[xPeriod](High) then // Parametrer le nombre de candles pour le pivot, quand je l'ai mis à 3 c presque comme TV mais ce qui est bien dans prorealtime c qu'il est sorti d'une position Long perdante et à tout de suite shorté quand le paramètrage le permettait
    pivotHigh=high[1]
    starthigh=barindex[1]
    pivottype=1
    endif
    if pivotL   and Low[1] = Lowest[xPeriod](Low) then
    pivotLow=low[1]
    startlow=barindex[1]
    pivottype=-1
    endif
     
     
    pvHighInRange = barssince(pivothigh)<countlong
    pvLowInRange = barssince(pivotlow)<countshort
    #223609 quote
    Kole96
    Participant
    New

    When you enter Long, the Stoploss is set to the last Swing Low and the Target is set to the same distance of the Stoploss.

    Here i set the “xperiod” to be adjustable to change the Stoploss as i want.

     

    thank you

    #223610 quote
    Kole96
    Participant
    New
    #224730 quote
    robertogozzi
    Moderator
    Master

    The indicator does not plot and does not return any value for pivots, just the bands and signals.

    I coded the Pivot points inside the strategy:

    DEFPARAM CumulateOrders = False
    ONCE xPeriod = 20
    UpperBand,LowerBand,Sma20,signalBuy,signalSell = CALL "Projet Primo 382 a 90%"[0, 5, 100, 0, 0, 50, 3, 6, 8, 10, 9, xPeriod, 10](close)
    c1 = (close CROSSES OVER  LowerBand) //UpperBand
    c2 = (close CROSSES UNDER UpperBand) //LowerBand
    pivotH = high[1]>high[2] and high[1]>high and High[1] = Highest[xPeriod](High)
    IF PivotH THEN
       stopH = high[1]
    Endif
    pivotL = low[1]<low[2] and low[1]<low     and Low[1]  = Lowest[xPeriod](Low)
    IF PivotL THEN
       stopL = low[1]
    Endif
    IF c1 AND not OnMarket  THEN
       BUY 1 CONTRACT AT MARKET
       SET STOP   PRICE stopL
       SET TARGET PRICE close + abs(close - stopL)
    ENDIF
    IF c2 AND not OnMarket THEN
       SELLSHORT 1 CONTRACT AT MARKET
       SET STOP   PRICE stopH
       SET TARGET PRICE close - abs(close - stopH)
    ENDIF
    //graphonprice stopH coloured("Green")
    //graphonprice stopL coloured("Red")
    #224750 quote
    Kole96
    Participant
    New

    Hi Roberto, thank you for your help.

    Can you explain to me the meaning of the line 3 of your last code? “CALL” function

    Thank you

    #224768 quote
    robertogozzi
    Moderator
    Master

    CALL is the instruction to get data from indicators.

    Indicators receive the data to make calculations in square brackets, those data must match the number of variables declared in the indicator.

    The variables to the left of the “=” (assignment operator) are the place where data RETURNed from indicator is stored (they must be in the same order order as they are written next to RETURN, though their names can be different from those in the indicator).

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

Steven Primo Indicator to Strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Kole96 @kole96 Participant
Summary

This topic contains 12 replies,
has 2 voices, and was last updated by robertogozzi
2 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/05/2023
Status: Active
Attachments: 3 files
Logo Logo
Loading...