Market Bias Indicator

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #215956 quote
    Aragorna
    Participant
    Junior
    Hello, is it possible to code this indicator in PRT? I find it very  interesting. Thank’s in advance
    Alessio
    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © Professeur_X
    //@version=5
    indicator(title=’HA Market Bias’, shorttitle=’HA Market Bias’, overlay=true)
    tf(_res, _exp, gaps_on) =>
        gaps_on == 0 ? request.security(syminfo.tickerid, _res, _exp) : gaps_on == true ? request.security(syminfo.tickerid, _res, _exp, barmerge.gaps_on, barmerge.lookahead_off) : request.security(syminfo.tickerid, _res, _exp, barmerge.gaps_off, barmerge.lookahead_off)
    ha_htf = ”
    show_ha = input.bool(true, “Show HA Plot/ Market Bias”, group=”HA Market Bias”)
    ha_len = input(100, ‘Period’, group=”HA Market Bias”)
    ha_len2 = input(100, ‘Smoothing’, group=”HA Market Bias”)
    // Calculations {
    o = ta.ema(open, ha_len)
    c = ta.ema(close, ha_len)
    h = ta.ema(high, ha_len)
    l = ta.ema(low, ha_len)
    haclose = tf(ha_htf, (o + h + l + c) / 4, 0)
    xhaopen = tf(ha_htf, (o + c) / 2, 0)
    haopen = na(xhaopen[1]) ? (o + c) / 2 : (xhaopen[1] + haclose[1]) / 2
    hahigh = math.max(h, math.max(haopen, haclose))
    halow = math.min(l, math.min(haopen, haclose))
    o2 = tf(ha_htf, ta.ema(haopen, ha_len2), 0)
    c2 = tf(ha_htf, ta.ema(haclose, ha_len2), 0)
    h2 = tf(ha_htf, ta.ema(hahigh, ha_len2), 0)
    l2 = tf(ha_htf, ta.ema(halow, ha_len2), 0)
    ha_avg = (h2 + l2) / 2
    // }
    // Oscillator {
    osc_len = input.int(7, “Oscillator Period”, group=”HA Market Bias”)
    osc_bias = 100 *(c2 – o2)
    osc_smooth = ta.ema(osc_bias, osc_len)
    sigcolor=
      (osc_bias>0)and(osc_bias>=osc_smooth)?color.new(color.lime,35):
      (osc_bias>0)and(osc_bias<osc_smooth)?color.new(color.lime,75):
      (osc_bias<0)and(osc_bias<=osc_smooth)?color.new(color.red,35):
      (osc_bias < 0) and (osc_bias > osc_smooth) ? color.new(color.red, 75) :
      na
    // }
    // Plots {
    p_h = plot(h2, “Bias High”, color=color(na), display=display.none, editable=false)
    p_l = plot(l2, “Bias Low”, color=color(na), display=display.none, editable=false)
    p_avg = plot(ha_avg, “Bias Avergae”, color=color(na), display=display.none, editable=false)
    fill(p_l, p_h, show_ha ? sigcolor : na)
    col = o2 > c2 ? color.red : color.lime
    plotcandle(show_ha ? o2 : na, h2, l2, c2, title=’heikin smoothed’, color=col)
    // }
    #215962 quote
    JS
    Participant
    Senior

    Hi Alessio,

    I had to split the indicator so that “HA Market Bias” indicator can be added in the price chart and the “Oscillator Market Bias” below the price chart…

    First, the “HA Market BIAS”…

     

    HAlen=100 //HA period
    HAlen2=100 //HA smoothing period
    
    //Calculations
    xO=ExponentialAverage[HAlen](Open)
    xC=ExponentialAverage[HAlen](Close)
    xH=ExponentialAverage[HAlen](High)
    xL=ExponentialAverage[HAlen](Low)
    
    HAClose=(xO+xH+xL+xC)/4
    xHAOpen=(xO+xC)/2
    HAOpen=(xHAOpen[1]+HAClose[1])/2
    HAHigh=Max(xH,Max(HAOpen,HAClose))
    HALow=Min(xL,Min(HAOpen,HAClose))
    
    O2=ExponentialAverage[HAlen2](HAOpen)
    C2=ExponentialAverage[HAlen2](HAClose)
    H2=ExponentialAverage[HAlen2](HAHigh)
    L2=ExponentialAverage[HAlen2](HALow)
    
    HAAvg=(H2+L2)/2
    
    Return H2 as "BIAS High", L2 as "BIAS Low", HAAvg as "BIAS Average"
    #215974 quote
    Aragorna
    Participant
    Junior
    thank’s a lot, I hope it’ll be possible to replicate the colour of the oscillator on the first indicator
    #216026 quote
    JS
    Participant
    Senior

    Hi Alessio / @Aragorna

    Here is the bias indicator with the colors of the oscillator…

    Hope it helps you…

    HAlen=100 //HA period
    HAlen2=100 //HA smoothing period
    
    //Calculations
    xO=ExponentialAverage[HAlen](Open)
    xC=ExponentialAverage[HAlen](Close)
    xH=ExponentialAverage[HAlen](High)
    xL=ExponentialAverage[HAlen](Low)
    
    HAClose=(xO+xH+xL+xC)/4
    xHAOpen=(xO+xC)/2
    HAOpen=(xHAOpen[1]+HAClose[1])/2
    HAHigh=Max(xH,Max(HAOpen,HAClose))
    HALow=Min(xL,Min(HAOpen,HAClose))
    
    O2=ExponentialAverage[HAlen2](HAOpen)
    C2=ExponentialAverage[HAlen2](HAClose)
    H2=ExponentialAverage[HAlen2](HAHigh)
    L2=ExponentialAverage[HAlen2](HALow)
    
    HAAvg=(H2+L2)/2
    
    OSCLen=7 //Oscillator period
    
    OSCBias=100*(C2-O2)
    OSCSmooth=ExponentialAverage[OSCLen](OSCBias)
    
    If OscBias>0 and OscBias>=OscSmooth then
    R=0
    G=255
    B=0
    ElsIf OscBias>0 and OscBias<OscSmooth then
    R=0
    G=150
    B=0
    ElsIf OscBias<0 and OscBias<=OscSmooth then
    R=255
    G=0
    B=0
    ElsIf OscBias<0 and OscBias>OscSmooth then
    R=150
    G=0
    B=0
    EndIf
    
    Drawcandle(O2,H2,L2,C2) Coloured(R,G,B)
    
    Return H2 as "BIAS High", L2 as "BIAS Low", HAAvg as "BIAS Average"
    #216041 quote
    Aragorna
    Participant
    Junior
    thank you JS. I was thinking about a strategy that uses this market Bias and stifness indicator. I don’t know if it’s something good enough, this strategy is both long and short on Nasdaq, 1Hour TF. consider I use IB, not IG. so things could be a little different.
    defparam cumulateorders= false
    //  BIAS market+stifness indicator
    
    HAlen=MM //
    HAlen2=NN //
     
    //Calculations
    xO=ExponentialAverage[HAlen](Open)
    xC=ExponentialAverage[HAlen](Close)
    xH=ExponentialAverage[HAlen](High)
    xL=ExponentialAverage[HAlen](Low)
     
    HAClose=(xO+xH+xL+xC)/4
    xHAOpen=(xO+xC)/2
    HAOpen=(xHAOpen[1]+HAClose[1])/2
    HAHigh=Max(xH,Max(HAOpen,HAClose))
    HALow=Min(xL,Min(HAOpen,HAClose))
     
    O2=ExponentialAverage[HAlen2](HAOpen)
    C2=ExponentialAverage[HAlen2](HAClose)
    H2=ExponentialAverage[HAlen2](HAHigh)
    L2=ExponentialAverage[HAlen2](HALow)
     
    HAAvg=(H2+L2)/2
     
    //Return H2 as "BIAS High", L2 as "BIAS Low", HAAvg as "BIAS Average"
    
    P    = 100
    LB   = PP                             //LookBack periods- 60
    LB     = max(1,min(999,LB))             //range allowed: 1-999
    
    
    HAAvgL   = HAAvg + (0.2 * STD[P](close)) //add a buffer for LONG trends
    HAAvgS   = HAAvg - (0.2 * STD[P](close)) //subtract a buffer for SHORT
    
    ContoL = summation[LB](close > HAAvgL)    //tally how many times CLOSE was above SMA in the LB period
    ContoS = summation[LB](close < HAAvgS)    //tally how many times CLOSE was below SMA in the LB period
    //
    StiffL = 100 * (ContoL / LB)            //UPtrend %
    StiffS = 100 * (ContoS / LB)            //DOWNtrend %
    //RETURN StiffL AS "UPtrend %",StiffS AS "DOWNtrend %",90 AS "90",10 AS "10",50 as "50"
    
    
    If not longonmarket and Close Crosses Over HAAvgS and Summation[QQ](StiffL<10) and (StiffL > 10) then  //50
    Buy 1 contract at Market
    EndIf
    If not SHORTONMARKET and Close Crosses under HAAvgS and Summation[QQ](StiffL>90) and (StiffL < 90) then  //50
    SELLSHORT  1 contract at Market
    EndIf
    
    // Stops und Targets
    SET STOP pLOSS TT//200
    SET TARGET PPROFIT UU//400
    
    //trailing stop function
    trailingstart = RR// 20trailing will start @trailinstart points profit-50
    trailingstep = 10// 5trailing step to move the "stoploss"
     
    //reset the stoploss value
    IF NOT LONGONMARKET  THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    
    this strategy instead is only long. I did Forwars test for this last one, that seems good. Consider I can see 100K bars.
    defparam cumulateorders= false
    //  BIAS market+stifness indicator
    
    HAlen=MM //
    HAlen2=NN //
     
    //Calculations
    xO=ExponentialAverage[HAlen](Open)
    xC=ExponentialAverage[HAlen](Close)
    xH=ExponentialAverage[HAlen](High)
    xL=ExponentialAverage[HAlen](Low)
     
    HAClose=(xO+xH+xL+xC)/4
    xHAOpen=(xO+xC)/2
    HAOpen=(xHAOpen[1]+HAClose[1])/2
    HAHigh=Max(xH,Max(HAOpen,HAClose))
    HALow=Min(xL,Min(HAOpen,HAClose))
     
    O2=ExponentialAverage[HAlen2](HAOpen)
    C2=ExponentialAverage[HAlen2](HAClose)
    H2=ExponentialAverage[HAlen2](HAHigh)
    L2=ExponentialAverage[HAlen2](HALow)
     
    HAAvg=(H2+L2)/2
     
    //Return H2 as "BIAS High", L2 as "BIAS Low", HAAvg as "BIAS Average"
    
    P    = 100
    LB   = PP                             //LookBack periods- 60
    LB     = max(1,min(999,LB))             //range allowed: 1-999
    
    
    HAAvgL   = HAAvg + (0.2 * STD[P](close)) //add a buffer for LONG trends
    HAAvgS   = HAAvg - (0.2 * STD[P](close)) //subtract a buffer for SHORT
    
    ContoL = summation[LB](close > HAAvgL)    //tally how many times CLOSE was above SMA in the LB period
    ContoS = summation[LB](close < HAAvgS)    //tally how many times CLOSE was below SMA in the LB period
    //
    StiffL = 100 * (ContoL / LB)            //UPtrend %
    StiffS = 100 * (ContoS / LB)            //DOWNtrend %
    //RETURN StiffL AS "UPtrend %",StiffS AS "DOWNtrend %",90 AS "90",10 AS "10",50 as "50"
    
    
    If not longonmarket and Close Crosses Over HAAvgS and Summation[QQ](StiffL<10) and (StiffL > 10) then  //50
    Buy 1 contract at Market
    EndIf
    
    
    // Stops und Targets
    SET STOP pLOSS TT//200
    SET TARGET PPROFIT UU//400
    
    //trailing stop function
    trailingstart = RR// 20trailing will start @trailinstart points profit-50
    trailingstep = 10// 5trailing step to move the "stoploss"
     
    //reset the stoploss value
    IF NOT LONGONMARKET  THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    
    KumoNoJuzza thanked this post
    #216042 quote
    Aragorna
    Participant
    Junior
    sorry I forgot variable optimization: I upload the 2 strategy
    Nicolas and GraHal thanked this post
    #216101 quote
    Nicolas
    Keymaster
    Master
    I just add this Market Bias indicator in the Library, thank you @JS for providing the translation! Market Bias indicator coloured
    JS thanked this post
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Market Bias Indicator


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Aragorna @aragorna Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by Nicolas
2 years, 8 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 06/10/2023
Status: Active
Attachments: 4 files
Logo Logo
Loading...