Announcement

Collapse
No announcement yet.

Intermittent results

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intermittent results

    I have expanded on my previous code to include a trend line that changes color depending on a few variables. I am tracking the high and low of the day with the global variables xHigh and xLow that reset each day on the first bar of the day. When the trend line moves up it turns green and if it ticks above the xHigh it turns blue. When it ticks down it turns purple and when it ticks below the xLow it turns red. I also put a warning level in to indicate if there has been a deep retracement without a new tick up or down.

    The color changes work perfectly when I replay data however in real time them occasionally fail. Sometimes price will be ticking up above xHigh and the line will be blue then it will suddenly start to be green as price continues to tick above xHigh, then it will turn blue again. If I reload the indicator it will be all blue as it should be. I must have missed some subtle piece of code that would keep the color correct.

    I have heavily commented the code.

    Murray

    PHP Code:
    /*****************************************************************
    Trend Band

    Murray Mortlock

    Dec 2015
    ******************************************************************/

    var nTrendLine null;
    var 
    nTrendLineLast null;
    var 
    aRange = new Array();
    var 
    cTrendLineColor null;
    var 
    cTrendLineColorLast null;
    var 
    xHigh;
    var 
    xLow;
    var 
    nFlatCount 0;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Trend Band");
        
    setShowTitleParameters(false);
        
    setPlotType(PLOTTYPE_INSTANTCOLORLINE0);
        
        
    setCursorLabelName("TrendLine"0);
        
    setDefaultBarStyle(PS_SOLID,0);
        
    setDefaultBarThickness(5,0);

    var 
    colShadowColor = new FunctionParameter("ShadowColor"FunctionParameter.COLOR);
        
    colShadowColor.setName("Shadow Color")
        
    colShadowColor.setDefault(Color.RGB(113,113,113));
        
    var 
    colBGColor = new FunctionParameter("NewHighColor"FunctionParameter.COLOR);
        
    colBGColor.setName("New High")
        
    colBGColor.setDefault(Color.RGB(0,0,255));
        
    var 
    colBGColor = new FunctionParameter("UptrendColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Uptrend")
        
    colBGColor.setDefault(Color.RGB(0,255,0));
        
    var 
    colBGColor = new FunctionParameter("UptrendWarningColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Uptrend Warning")
        
    colBGColor.setDefault(Color.RGB(255,255,0));
        
    var 
    colBGColor = new FunctionParameter("DowntrendWarningColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Downtrend Warning")
        
    colBGColor.setDefault(Color.RGB(255,155,0));
        
    var 
    colBGColor = new FunctionParameter("DowntrendColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Downtrend")
        
    colBGColor.setDefault(Color.RGB(255,0,255));
        
    var 
    colBGColor = new FunctionParameter("NewLowColor"FunctionParameter.COLOR);
        
    colBGColor.setName("New Low")
        
    colBGColor.setDefault(Color.RGB(255,0,0));
        
    var 
    0;
        
    aRange[x] = new FunctionParameter("ShadowWidth"FunctionParameter.NUMBER);
        
    with(aRange[x++]) {
            
    setLowerLimit(1);
            
    setDefault(16);
        }
            
    var 
    0;
        
    aRange[y] = new FunctionParameter("TrendTicks"FunctionParameter.NUMBER);
        
    with(aRange[y++]) {
            
    setLowerLimit(1);
            
    setDefault(36);
        }
            
    var 
    0;
        
    aRange[z] = new FunctionParameter("WarningTicks"FunctionParameter.NUMBER);
        
    with(aRange[z++]) {
            
    setLowerLimit(1);
            
    setDefault(24);
        }

    }

    function 
    main(ShadowWidth,ShadowColor,TrendTicks,WarningTicks,NewHighColor,UptrendColor,UptrendWarningColor,DowntrendWarningColor,DowntrendColor,NewLowColor) {

    // Set trend and warning levels
        
        
    var nTrend getMinTick() * TrendTicks;
        var 
    nWarning getMinTick() * WarningTicks;

    // Set trendline level for first bar
        
        
    if(getCurrentBarCount()<2) {
            
    nTrendLine=(high()+low())/2;
            
    cTrendLineColor Color.RGB(0,255,0);
        }

    // Set new bar parameters

        
    if(getBarState()==BARSTATE_NEWBAR) {
            
            
    nTrendLineLast=nTrendLine// Capture value of trendline on last bar
            
    cTrendLineColorLast cTrendLineColor  // Capture color of trendline on last bar
            
    nFlatCount nFlatCount +1// Count number of bars without tick up or down
            
    if (getDay() != getDay(-1)) { // Set high and low values at start of the day
                
    xHigh nTrendLineLast nTrend;
                
    xLow nTrendLineLast nTrend;
            }
        }
        
    // Set trendline value

        
    if (nTrendLineLast < (high()-nTrend))
            
    nTrendLine = (high() - nTrend);
        else if (
    nTrendLineLast > (low() + nTrend))
            
    nTrendLine = (low() + nTrend);

    // Set shadow width and color
        
        
    var UpperBand nTrendLine ShadowWidth;
        var 
    LowerBand nTrendLine ShadowWidth;
        
    setBarBgColor(ShadowColor,0,UpperBand,LowerBand);

    // Fix TrendLine Level

        
    if (nTrendLineLast < (high()-nTrend))
            
    nTrendLine = (high() - nTrend);
        else if (
    nTrendLineLast > (low() + nTrend))
            
    nTrendLine = (low() + nTrend);

    // Fix TrendLine Color - will be the same as it was on the previous bar if not changed by the process below

        
    cTrendLineColor cTrendLineColorLast// Capture the current color in case no changes are to be made
        
    // When trendline ticks up
        
        
    if (nTrendLine nTrendLineLast) { //If TrendLine is greater than previous bar TrendLine
            
    nFlatCount 0//Set count of flat line bars to 0
            
    if (high() > xHigh) { //if the high is higher than the high of the day
                
    cTrendLineColor NewHighColor//then the color will be the new high color (blue)
                
    xHigh high(); // and the highest value of the day will be changed to the new high
            
    }
            else 
    cTrendLineColor UptrendColor// if not the color will be the higher color (green)
        
    }

    // When trendline ticks down

        
    else if (nTrendLine nTrendLineLast) { //if the miband is less than the previous bar TrendLine
            
    nFlatCount 0//set the count of the flat line bars to 0
            
    if (low() < xLow) { //if the low is lower than the low of the day
                
    cTrendLineColor NewLowColor//then the color will be the new low color (red)
                
    xLow low(); // and the lowest value of the day will be changed to the new low
            
    }
            else 
    cTrendLineColor DowntrendColor// if not then the color will be the low color (purple)
        
    }

    //if the TrendLine has been the same volue for more than 8 bars

        
    if (nFlatCount 8) { 
    // if the high is greater than the nB7 value AND the color is red or purple
            
    if (high() > (nTrendLine nWarning) && (cTrendLineColor == NewLowColor || cTrendLineColor == DowntrendColor))
                
    cTrendLineColor DowntrendWarningColor//then change the color to the warning color (orange)
    // if the low is lower than the nB1 value AND the color is blue or green
            
    else if (low() < (nTrendLine nWarning) && (cTrendLineColor == NewHighColor || cTrendLineColor == UptrendColor))
                
    cTrendLineColor UptrendWarningColor//then change the color to the warning color (yellow)
        
    }
        
        
    setBarFgColor(cTrendLineColor,0);
       
        return 
    nTrendLine;



  • #2
    Murray
    That is because your logic is not taking into consideration intrabar updates of your values.
    What you need to do is add debug statements to check all your values throughout the section of code that colors the line in blue or green. Then use the Bar Replay feature but set the Play as resolution not to the same Interval as the chart but to a lower one so that you get multiple executions per bar (see this article in the eSignal KnowledgeBase if you are unfamiliar with the feature).
    Given that you can replay each intrabar update one at a time this will give you plenty of opportunity to review the output of the debug statements and find the flaw in your logic.
    Once you figure out the issue you then have to apply the same corrections to the logic for the opposite direction
    Alex


    Originally posted by CPOMorty View Post
    I have expanded on my previous code to include a trend line that changes color depending on a few variables. I am tracking the high and low of the day with the global variables xHigh and xLow that reset each day on the first bar of the day. When the trend line moves up it turns green and if it ticks above the xHigh it turns blue. When it ticks down it turns purple and when it ticks below the xLow it turns red. I also put a warning level in to indicate if there has been a deep retracement without a new tick up or down.

    The color changes work perfectly when I replay data however in real time them occasionally fail. Sometimes price will be ticking up above xHigh and the line will be blue then it will suddenly start to be green as price continues to tick above xHigh, then it will turn blue again. If I reload the indicator it will be all blue as it should be. I must have missed some subtle piece of code that would keep the color correct.

    I have heavily commented the code.

    Murray

    PHP Code:
    /*****************************************************************
    Trend Band

    Murray Mortlock

    Dec 2015
    ******************************************************************/

    var nTrendLine null;
    var 
    nTrendLineLast null;
    var 
    aRange = new Array();
    var 
    cTrendLineColor null;
    var 
    cTrendLineColorLast null;
    var 
    xHigh;
    var 
    xLow;
    var 
    nFlatCount 0;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Trend Band");
        
    setShowTitleParameters(false);
        
    setPlotType(PLOTTYPE_INSTANTCOLORLINE0);
        
        
    setCursorLabelName("TrendLine"0);
        
    setDefaultBarStyle(PS_SOLID,0);
        
    setDefaultBarThickness(5,0);

    var 
    colShadowColor = new FunctionParameter("ShadowColor"FunctionParameter.COLOR);
        
    colShadowColor.setName("Shadow Color")
        
    colShadowColor.setDefault(Color.RGB(113,113,113));
        
    var 
    colBGColor = new FunctionParameter("NewHighColor"FunctionParameter.COLOR);
        
    colBGColor.setName("New High")
        
    colBGColor.setDefault(Color.RGB(0,0,255));
        
    var 
    colBGColor = new FunctionParameter("UptrendColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Uptrend")
        
    colBGColor.setDefault(Color.RGB(0,255,0));
        
    var 
    colBGColor = new FunctionParameter("UptrendWarningColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Uptrend Warning")
        
    colBGColor.setDefault(Color.RGB(255,255,0));
        
    var 
    colBGColor = new FunctionParameter("DowntrendWarningColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Downtrend Warning")
        
    colBGColor.setDefault(Color.RGB(255,155,0));
        
    var 
    colBGColor = new FunctionParameter("DowntrendColor"FunctionParameter.COLOR);
        
    colBGColor.setName("Downtrend")
        
    colBGColor.setDefault(Color.RGB(255,0,255));
        
    var 
    colBGColor = new FunctionParameter("NewLowColor"FunctionParameter.COLOR);
        
    colBGColor.setName("New Low")
        
    colBGColor.setDefault(Color.RGB(255,0,0));
        
    var 
    0;
        
    aRange[x] = new FunctionParameter("ShadowWidth"FunctionParameter.NUMBER);
        
    with(aRange[x++]) {
            
    setLowerLimit(1);
            
    setDefault(16);
        }
            
    var 
    0;
        
    aRange[y] = new FunctionParameter("TrendTicks"FunctionParameter.NUMBER);
        
    with(aRange[y++]) {
            
    setLowerLimit(1);
            
    setDefault(36);
        }
            
    var 
    0;
        
    aRange[z] = new FunctionParameter("WarningTicks"FunctionParameter.NUMBER);
        
    with(aRange[z++]) {
            
    setLowerLimit(1);
            
    setDefault(24);
        }

    }

    function 
    main(ShadowWidth,ShadowColor,TrendTicks,WarningTicks,NewHighColor,UptrendColor,UptrendWarningColor,DowntrendWarningColor,DowntrendColor,NewLowColor) {

    // Set trend and warning levels
        
        
    var nTrend getMinTick() * TrendTicks;
        var 
    nWarning getMinTick() * WarningTicks;

    // Set trendline level for first bar
        
        
    if(getCurrentBarCount()<2) {
            
    nTrendLine=(high()+low())/2;
            
    cTrendLineColor Color.RGB(0,255,0);
        }

    // Set new bar parameters

        
    if(getBarState()==BARSTATE_NEWBAR) {
            
            
    nTrendLineLast=nTrendLine// Capture value of trendline on last bar
            
    cTrendLineColorLast cTrendLineColor  // Capture color of trendline on last bar
            
    nFlatCount nFlatCount +1// Count number of bars without tick up or down
            
    if (getDay() != getDay(-1)) { // Set high and low values at start of the day
                
    xHigh nTrendLineLast nTrend;
                
    xLow nTrendLineLast nTrend;
            }
        }
        
    // Set trendline value

        
    if (nTrendLineLast < (high()-nTrend))
            
    nTrendLine = (high() - nTrend);
        else if (
    nTrendLineLast > (low() + nTrend))
            
    nTrendLine = (low() + nTrend);

    // Set shadow width and color
        
        
    var UpperBand nTrendLine ShadowWidth;
        var 
    LowerBand nTrendLine ShadowWidth;
        
    setBarBgColor(ShadowColor,0,UpperBand,LowerBand);

    // Fix TrendLine Level

        
    if (nTrendLineLast < (high()-nTrend))
            
    nTrendLine = (high() - nTrend);
        else if (
    nTrendLineLast > (low() + nTrend))
            
    nTrendLine = (low() + nTrend);

    // Fix TrendLine Color - will be the same as it was on the previous bar if not changed by the process below

        
    cTrendLineColor cTrendLineColorLast// Capture the current color in case no changes are to be made
        
    // When trendline ticks up
        
        
    if (nTrendLine nTrendLineLast) { //If TrendLine is greater than previous bar TrendLine
            
    nFlatCount 0//Set count of flat line bars to 0
            
    if (high() > xHigh) { //if the high is higher than the high of the day
                
    cTrendLineColor NewHighColor//then the color will be the new high color (blue)
                
    xHigh high(); // and the highest value of the day will be changed to the new high
            
    }
            else 
    cTrendLineColor UptrendColor// if not the color will be the higher color (green)
        
    }

    // When trendline ticks down

        
    else if (nTrendLine nTrendLineLast) { //if the miband is less than the previous bar TrendLine
            
    nFlatCount 0//set the count of the flat line bars to 0
            
    if (low() < xLow) { //if the low is lower than the low of the day
                
    cTrendLineColor NewLowColor//then the color will be the new low color (red)
                
    xLow low(); // and the lowest value of the day will be changed to the new low
            
    }
            else 
    cTrendLineColor DowntrendColor// if not then the color will be the low color (purple)
        
    }

    //if the TrendLine has been the same volue for more than 8 bars

        
    if (nFlatCount 8) { 
    // if the high is greater than the nB7 value AND the color is red or purple
            
    if (high() > (nTrendLine nWarning) && (cTrendLineColor == NewLowColor || cTrendLineColor == DowntrendColor))
                
    cTrendLineColor DowntrendWarningColor//then change the color to the warning color (orange)
    // if the low is lower than the nB1 value AND the color is blue or green
            
    else if (low() < (nTrendLine nWarning) && (cTrendLineColor == NewHighColor || cTrendLineColor == UptrendColor))
                
    cTrendLineColor UptrendWarningColor//then change the color to the warning color (yellow)
        
    }
        
        
    setBarFgColor(cTrendLineColor,0);
       
        return 
    nTrendLine;


    Comment


    • #3
      Hi Alex,

      Almost as soon as I read your reply I realized that I was resetting the value of the high or low intrabar and needed to lock that event in. I think I have it resolved. I will post the code once I have it thoroughly tested.

      Thanks,

      Murray

      Comment


      • #4
        Updated Logic

        Here is my code with the logic updated. I have locked in the result intrabar if there is a New High or New Low.

        I run this in tick based or sub minute charts. The bar replay does not seem to allow for intrabar updates on those charts so I tested on a 3-minute chart. It looks a bit different but seems to do the job.

        Murray

        PHP Code:
        /*****************************************************************
        Trend Band

        Murray Mortlock

        Dec 2015
        ******************************************************************/

        var nTrendLine null;
        var 
        nTrendLineLast null;
        var 
        aRange = new Array();
        var 
        cTrendLineColor null;
        var 
        cTrendLineColorLast null;
        var 
        xHigh;
        var 
        xLow;
        var 
        nFlatCount 0;
        var 
        bNewHighLow false;

        function 
        preMain() {
            
        setPriceStudy(true);
            
        setStudyTitle("Trend Band");
            
        setShowTitleParameters(false);
            
        setPlotType(PLOTTYPE_INSTANTCOLORLINE0);
            
            
        setCursorLabelName("TrendLine"0);
            
        setDefaultBarStyle(PS_SOLID,0);
            
        setDefaultBarThickness(5,0);

        var 
        colShadowColor = new FunctionParameter("ShadowColor"FunctionParameter.COLOR);
            
        colShadowColor.setName("Shadow Color")
            
        colShadowColor.setDefault(Color.RGB(113,113,113));
            
        var 
        colBGColor = new FunctionParameter("NewHighColor"FunctionParameter.COLOR);
            
        colBGColor.setName("New High")
            
        colBGColor.setDefault(Color.RGB(0,0,255));
            
        var 
        colBGColor = new FunctionParameter("UptrendColor"FunctionParameter.COLOR);
            
        colBGColor.setName("Uptrend")
            
        colBGColor.setDefault(Color.RGB(0,255,0));
            
        var 
        colBGColor = new FunctionParameter("UptrendWarningColor"FunctionParameter.COLOR);
            
        colBGColor.setName("Uptrend Warning")
            
        colBGColor.setDefault(Color.RGB(255,255,0));
            
        var 
        colBGColor = new FunctionParameter("DowntrendWarningColor"FunctionParameter.COLOR);
            
        colBGColor.setName("Downtrend Warning")
            
        colBGColor.setDefault(Color.RGB(255,155,0));
            
        var 
        colBGColor = new FunctionParameter("DowntrendColor"FunctionParameter.COLOR);
            
        colBGColor.setName("Downtrend")
            
        colBGColor.setDefault(Color.RGB(255,0,255));
            
        var 
        colBGColor = new FunctionParameter("NewLowColor"FunctionParameter.COLOR);
            
        colBGColor.setName("New Low")
            
        colBGColor.setDefault(Color.RGB(255,0,0));
            
        var 
        0;
            
        aRange[x] = new FunctionParameter("ShadowWidth"FunctionParameter.NUMBER);
            
        with(aRange[x++]) {
                
        setLowerLimit(1);
                
        setDefault(16);
            }
                
        var 
        0;
            
        aRange[y] = new FunctionParameter("TrendTicks"FunctionParameter.NUMBER);
            
        with(aRange[y++]) {
                
        setLowerLimit(1);
                
        setDefault(36);
            }
                
        var 
        0;
            
        aRange[z] = new FunctionParameter("WarningTicks"FunctionParameter.NUMBER);
            
        with(aRange[z++]) {
                
        setLowerLimit(1);
                
        setDefault(24);
            }

        }

        function 
        main(ShadowWidth,ShadowColor,TrendTicks,WarningTicks,NewHighColor,UptrendColor,UptrendWarningColor,DowntrendWarningColor,DowntrendColor,NewLowColor) {

        // Set trend and warning levels
            
            
        var nTrend getMinTick() * TrendTicks;
            var 
        nWarning getMinTick() * WarningTicks;

        // Set trendline level for first bar
            
            
        if(getCurrentBarCount()<2) {
                
        nTrendLine=(high()+low())/2;
                
        cTrendLineColor Color.RGB(0,255,0);
            }

        // Set new bar parameters

            
        if(getBarState()==BARSTATE_NEWBAR) {
                
                
        nTrendLineLast=nTrendLine// Capture value of trendline on last bar
                
        cTrendLineColorLast cTrendLineColor  // Capture color of trendline on last bar
                
        nFlatCount nFlatCount +1// Count number of bars without tick up or down
                
        bNewHighLow false// Create local variable to lock in the high low status on new value
                
        if (getDay() != getDay(-1)) { // Set high and low values at start of the day
                    
        xHigh nTrendLineLast nTrend;
                    
        xLow nTrendLineLast nTrend;
                }
            }
            
        // Set trendline value

            
        if (nTrendLineLast < (high()-nTrend))
                
        nTrendLine = (high() - nTrend);
            else if (
        nTrendLineLast > (low() + nTrend))
                
        nTrendLine = (low() + nTrend);

        // Set shadow width and color
            
            
        var UpperBand nTrendLine ShadowWidth;
            var 
        LowerBand nTrendLine ShadowWidth;
            
        setBarBgColor(ShadowColor,0,UpperBand,LowerBand);

        // Fix TrendLine Level

            
        if (nTrendLineLast < (high()-nTrend))
                
        nTrendLine = (high() - nTrend);
            else if (
        nTrendLineLast > (low() + nTrend))
                
        nTrendLine = (low() + nTrend);

        // Fix TrendLine Color - will be the same as it was on the previous bar if not changed by the process below

            
        cTrendLineColor cTrendLineColorLast// Capture the current color in case no changes are to be made
            
        // When trendline ticks up
            
            
        if (nTrendLine nTrendLineLast) { //If TrendLine is greater than previous bar TrendLine
                
        nFlatCount 0//Set count of flat line bars to 0
                
        if (high() > xHigh) { //if the high is higher than the high of the day
                    
        cTrendLineColor NewHighColor//then the color will be the new high color (blue)
                    
        bNewHighLow true// Lock in new intrabar high
                    
        xHigh high(); // and the highest value of the day will be changed to the new high
                
        }
                else if (
        bNewHighLow == false) {
                    
        cTrendLineColor UptrendColor// if not the color will be the higher color (green)
                
        }
            }

        // When trendline ticks down

            
        else if (nTrendLine nTrendLineLast) { //if the miband is less than the previous bar TrendLine
                
        debugClear();
                
        debugPrintln("New High Low 1=" +bNewHighLow);
                
        nFlatCount 0//set the count of the flat line bars to 0
                
        if (low() < xLow) { //if the low is lower than the low of the day
                    
        cTrendLineColor NewLowColor//then the color will be the new low color (red)
                    
        bNewHighLow true// Lock in new intrabar low
                    
        xLow low(); // and the lowest value of the day will be changed to the new low
                
        }
                else if (
        bNewHighLow == false) {
                    
        debugPrintln("New High Low 2=" +bNewHighLow);
                    
        cTrendLineColor DowntrendColor// if not then the color will be the low color (purple)
                
        }
            }

        //if the TrendLine has been the same volue for more than 8 bars

            
        if (nFlatCount 8) { 
        // if the high is greater than the nB7 value AND the color is red or purple
                
        if (high() > (nTrendLine nWarning) && (cTrendLineColor == NewLowColor || cTrendLineColor == DowntrendColor))
                    
        cTrendLineColor DowntrendWarningColor//then change the color to the warning color (orange)
        // if the low is lower than the nB1 value AND the color is blue or green
                
        else if (low() < (nTrendLine nWarning) && (cTrendLineColor == NewHighColor || cTrendLineColor == UptrendColor))
                    
        cTrendLineColor UptrendWarningColor//then change the color to the warning color (yellow)
            
        }
            
            
        setBarFgColor(cTrendLineColor,0);
           
            return 
        nTrendLine;


        Last edited by CPOMorty; 12-07-2015, 10:37 AM.

        Comment


        • #5
          Hi Alex,

          I am baffled now. I am running this code on a range chart which I cannot test using the Bar Replay because I cannot create the intrabar ticks on that time frame with bar replay.

          When I test this on a 3-min chart using 1-min updates it seems to work fine, but this morning in real time the market was moving down and the trend line was purple. I was monitoring the variable bNewHighLow in the formula output window and when a new daily low occurred the value of bNewHighLow changed to true but the color of the line stayed purple. The line should have changed to red before the value of bNewHighLow changed to true. When I reload the indicator of course it changed to red as it should have.

          It is difficult to find a spot on the 3-min chart that this is replicated exactly to run it in bar replay, but it looks to me like my logic is sound now and I cannot understand how the value of bNewHighLow can change without the color of the trend line changing to red.

          Murray

          Comment


          • #6
            Murray

            I am running this code on a range chart which I cannot test using the Bar Replay because I cannot create the intrabar ticks on that time frame with bar replay.
            While it is true that in Bar Replay you cannot recreate range bars tick by tick you can still recreate the same logical setup using minute bars as it is not exclusive to range bars

            I was monitoring the variable bNewHighLow in the formula output window and when a new daily low occurred the value of bNewHighLow changed to true but the color of the line stayed purple.
            More than likely that is not what actually happened. The plot turned to red but on the subsequent tick it turned back to purple with the sequence of executions being fast enough to mask the color changes

            I cannot understand how the value of bNewHighLow can change without the color of the trend line changing to red.
            Before I illustrate why this is happening note that I made the following changes at my end to your script



            Another change I made (albeit not shown in the image) was to assign xHigh and xLow to the UpperBand and LowerBand (ie the shadow) so as to better visualize the breakouts

            The following image illustrates what happened on the first execution of a low breakout



            As you can see the plot was colored in red, both debug statements (in lines 142 and 148) were executed, bNewHighLow was set to true and xLow was set to to 2074
            Now here is what happened on the next execution



            The line went back to purple however notice that neither debug statement (in lines 148 and 152) was executed hence it is not this block of code that is causing this.
            Lastly here is what happened after reloading the script (while Bar Replay was still paused after the prior execution)



            Again both debug statements in lines 142 and 148 were executed and the plot was colored in red.
            Given the above what you should have been looking for is every instance in your code that colors the plot either prior to that block of code or after and debugged each one.
            In doing so you would eventually have found that the culprit is the line of code shown in the following image



            which executes at every tick and colors the plot as per its prior color.
            At the second execution after the line was colored in red this line of code changed the color back to the one at the prior bar (ie purple). Because neither subsequent condition was true (ie low() was not less than xLow and bNewHighLow was true) none of the commands to color the plot was executed.

            Barring something I may have missed (in as much as I have not been looking for other issues) you should be able to safely remove this line as it serves no purpose within the logic you are using. In fact the logic in your code is based on breakouts of highs and lows which do not change intrabar unlike for example a logic based on the close which can straddle a breakout back and forth (thus requiring some code to re-establish a pre-existing state be it color, value or what have you)
            Alex


            Originally posted by CPOMorty View Post
            Hi Alex,

            I am baffled now. I am running this code on a range chart which I cannot test using the Bar Replay because I cannot create the intrabar ticks on that time frame with bar replay.

            When I test this on a 3-min chart using 1-min updates it seems to work fine, but this morning in real time the market was moving down and the trend line was purple. I was monitoring the variable bNewHighLow in the formula output window and when a new daily low occurred the value of bNewHighLow changed to true but the color of the line stayed purple. The line should have changed to red before the value of bNewHighLow changed to true. When I reload the indicator of course it changed to red as it should have.

            It is difficult to find a spot on the 3-min chart that this is replicated exactly to run it in bar replay, but it looks to me like my logic is sound now and I cannot understand how the value of bNewHighLow can change without the color of the trend line changing to red.

            Murray

            Comment


            • #7
              Murray
              Here is another example of how that line of code is affecting the coloring of the plot.
              The following image shows two charts being played in Bar Replay concurrently, the top chart with the line of code in question and the bottom one with that same line of code commented out



              If you look at the Data Window you will see that the values in the top chart are in orange at the breakout whereas the value in the bottom chart is blue. You can also see the difference in color of the two plots at the cursor
              Alex

              Comment


              • #8
                Alex,

                Thanks so much. Not being a pro it is easy to miss some of those subtleties. I will have to spend some time figuring out why I was seeing my result differently in real time vs bar replay because obviously when you ran it in the time chart it failed. I must have not had the conditions I thought I did.

                I had the thought that I had to set the initial color of the previous bar in case there was no change. Obviously I was missing something there and didn't see it resetting every tick to that. I really appreciate the help and lesson in troubleshooting. I knew that when I was watching it in real time and getting the change when I also thought I had locked in a value and it couldn't change that I had too be missing something, but I was looking in totally the wrong place.

                Murray

                Comment


                • #9
                  Hi Alex,

                  On question... the reason that I had that line of code was that felt I had to call the color from the last bar to set is as the default. Now I am getting the impression that is unnecessary. When we create and color something as I have done here does it automatically take on the previous value when the new bar opens and hold that until we cause it to change?

                  Thanks,

                  Murray

                  Comment


                  • #10
                    Murray

                    When we create and color something as I have done here does it automatically take on the previous value when the new bar opens and hold that until we cause it to change?
                    No it does not if you use setBarFgColor(). You can easily verify this with a simple script that applies the color to a plot based on a condition as in the following example



                    As you can see once the condition is no longer true the corresponding color is not applied to the plot which reverts to the default color
                    If however you use setDefaultBarFgColor() then it persists until it is changed changed by some other event. Again a simple script can illustrate this



                    Notice that once the color is applied it persists as a default color unless some other condition changes it. Once the new condition is no longer true the color reverts to the most recent color used as a default
                    So why is your color persisting even without that line of code and without using setDefaultBarFgColor().
                    Because you save the color as a global variable (which retains its value between iterations of the script) and then assign it to setBarFgColor() - on each iteration - at the very end of your script
                    So here is the same sample script shown above but with the color used in a global variable (which sort of mimics your script)



                    Notice that in this case once a condition assigns a value to the global variable this is carried through on every subsequent bar until some other event changes the value of that variable.
                    Anyhow what I would suggest at this point is that you extensively debug your formula and run it one execution at a time using Bar Replay as that will allow you to better understand what happens at each line of code, when and why it executes, etc. Also when in doubt about a behavior try creating simple(r) scripts such as the ones shown above as this allows you to focus on a specific issue rather than having to deal with a multitude of inter-dependent variables.
                    Lastly you may want to go through these forums [and the KnowledgeBase] as they contain a wealth of information related to efs and most of these topics have been covered at length before
                    Alex


                    Originally posted by CPOMorty View Post
                    Hi Alex,

                    On question... the reason that I had that line of code was that felt I had to call the color from the last bar to set is as the default. Now I am getting the impression that is unnecessary. When we create and color something as I have done here does it automatically take on the previous value when the new bar opens and hold that until we cause it to change?

                    Thanks,

                    Murray

                    Comment

                    Working...
                    X