Announcement

Collapse
No announcement yet.

Color of Current BAR

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

  • Color of Current BAR

    Could someone please advise me how to precisely identify the color of the Current Bar, the one currently being built? I have a few lines of code which print green if open() > close() and red if open() < close(), but this does not work for the current bar (until that bar is completed, and the next bar starts being built, at which point the color is correct). My preference is that the code do what it says, or alternatively, if unable to do so (maybe there is not yet a close()?????), then print as the DefaultBarColor has been Set in preMain .

    I find it terribly misleading to see a bar plummet in price, and yet be printing green, and vice versa.
    Thanks
    George

  • #2
    George
    On my machine this paints the current bar immediately as the conditions change
    Alex

    PHP Code:
    function preMain() {

    setColorPriceBars(true);

        
    setPriceStudy(true);
        
    setStudyTitle("test-color");
    }

    function 
    main() {

            if (
    close() < open()) 
            
    setPriceBarColor(Color.red)
     
            else if (
    close() > open()) 
            
    setPriceBarColor(Color.blue)

            else if (
    == 1
            
    setPriceBarColor(Color.black);
     
        return 
    null;

    Comment


    • #3
      Alexis - thanks.
      I do have setPriceStudy(false), unlike you, but changing it makes no difference. I have looked at this for weeks!
      Here is the calling function:
      var MACD_Signal_Diff =0.2;
      var nContracts = 1 ;
      var At ='@';
      var JustBought = false;
      var JustCovered = false;
      var JustSold = false;
      var Sell_Mess = "SELL";
      var Sell_Close = "SELL Cls";
      var Sell_Short = "SELL Shrt";
      var Buy_Mess = "BUY";
      var Buy_Lng = "BUY Lng";
      var Buy_Cvr = "BUY Cvr";
      var EOD_Buy = "BUY EOD";
      var EOD_Sell = "SELL EOD";
      var DOJI_Mess ="DOJI";
      var BearP_Rising = "Buying Opp";
      var BullP_Falling = "Selling Opp";
      var t = "True";var f="False";
      function preMain() {
      setComputeOnClose(true);
      setPriceStudy(false);
      setColorPriceBars(true);
      setDefaultPriceBarColor(Color.khaki);
      setStudyTitle("L/T Trend");
      /* setCursorLabelName("MA(13,C)e",0);
      setDefaultBarFgColor(Color.black,0);setDefaultBarS tyle( PS_DASH, 0 );
      setDefaultBarThickness(2,0);*/
      setCursorLabelName("MACDHist(12,26,9,E)",0);
      setDefaultBarFgColor(Color.fushcia,0); setPlotType( PLOTTYPE_HISTOGRAM, 0 );
      setCursorLabelName("MACD(12,26,9,E)",1);
      setDefaultBarFgColor(Color.red,1);
      setCursorLabelName("MACDSig(12,26,9,E)",2);
      setDefaultBarFgColor(Color.green,2);
      setCursorLabelName("UpTrend",3);
      setDefaultBarFgColor(Color.green,3);
      setCursorLabelName("DownTrend",4);
      setDefaultBarFgColor(Color.red,4);

      }
      function getTrend() {
      if ( (vEMA < vEMA_1) && (vEMA_1 < vEMA_2) && (vMACD_Hist <= vMACD_Hist_1)) /* && (vMACD_Hist_1 <= vMACD_Hist_2) */
      TrendIsDown = true; else TrendIsDown = false;

      if ( (vEMA > vEMA_1) && (vEMA_1 > vEMA_2) && (vMACD_Hist >= vMACD_Hist_1)) /*&& (vMACD_Hist_1 >= vMACD_Hist_2)&&*/
      TrendIsUp = true; else TrendIsUp = false;
      /* debugPrintln("Bar " + getCurrentBarIndex()+"TrendIsUp:" + TrendIsUp + " TrendIsDown:" + TrendIsDown ) ;*/
      return (TrendIsUp,TrendIsDown)
      }
      function main(EMAlen,MACDfast,MACDslow,MACDsmoothing) {
      if (EMAlen == null) EMAlen = 13;
      if (MACDfast == null) MACDfast=12;
      if (MACDslow== null) MACDslow = 26;
      if (MACDsmoothing == null) MACDsmoothing = 9;
      addBand(0, PS_SOLID, 1, Color.black, "Center");
      var EMA = new MAStudy(EMAlen, 0, "Close", MAStudy.EXPONENTIAL);
      var MACD = new MACDStudy (MACDfast,MACDslow,MACDsmoothing,"Close",false,fal se);
      vEMA = EMA.getValue(MAStudy.MA,0);
      vEMA_1 = EMA.getValue(MAStudy.MA,-1);
      vEMA_2 = EMA.getValue(MAStudy.MA,-2);
      vMACD = MACD.getValue(MACDStudy.MACD);
      vMACD_Signal = MACD.getValue(MACDStudy.SIGNAL);
      vMACD_Hist = MACD.getValue(MACDStudy.HIST,0);
      vMACD_Hist_1 = MACD.getValue(MACDStudy.HIST,-1);
      vMACD_Hist_2 = MACD.getValue(MACDStudy.HIST,-2);
      /* debugPrintln("Bar " + getCurrentBarIndex()+"EMA= " + vEMA +"vEMA_1= "+vEMA_1);*/
      getTrend();
      setGlobalValue("ESTrendIsUp",TrendIsUp); setGlobalValue("ESTrendIsDown",TrendIsDown);
      UpTrend=getGlobalValue("ESTrendIsUp");
      DownTrend=getGlobalValue("ESTrendIsDown");
      if (UpTrend) UpTrend = t; else UpTrend = f;
      if (DownTrend) DownTrend = t; else DownTrend = f;
      /*debugPrintln("Bar " + getCurrentBarIndex()+"TrendIsUp: " + TrendIsUp + " TrendIsDown: " + TrendIsDown*/
      callFunction("SubroutineLib.efs","PriceBarColors", open(),close());
      return new Array (vMACD_Hist,vMACD,vMACD_Signal,UpTrend,DownTrend)
      }
      *******Here is called function...note it still did not work when embedded in the code above...******
      function preMain()
      { setPriceStudy(true)
      return
      }
      function PriceBarColors(vOpen,vClose) {
      /*debugPrintln(getSymbol()+"Bar: "+getCurrentBarIndex()+" vOpen: " +vOpen+" vCLose: " +vClose) */
      if (vClose > vOpen) setPriceBarColor(Color.lime)
      else
      if (vClose < vOpen) setPriceBarColor(Color.red)


      return null
      }
      function main()
      {
      return
      }

      Comment


      • #4
        George
        You have setComputeOnClose(true) which means that the conditions will be verified only on the close of each bar and then carried forward to the next close when they will be verified again.
        Alex

        Comment


        • #5
          Alexis - sadly, changing setComputeOnClose(false); does not help me....
          George

          Comment


          • #6
            George
            The command is setComputeOnClose() without anything in the brackets so presumably it is still on even with false.
            Just REM it out with // at the beginning of the line.
            Alex

            Comment


            • #7
              Alexis - commenting out the clause did not help either, but I did finally find the problem.
              One must go Tools, EFS,Settings, Make all formulas etc...and UNCHECK this option. Thie setting appears even to over-ride whatever one has explicitly coded in the EFS.
              Many Thanks for your time.
              George

              Comment


              • #8
                George
                Yes that is a global setting that applies to every single efs being run.
                I am surprised it was on. It should not be by default.
                Anyhow good to know you have it running now
                Alex

                Comment


                • #9
                  Alex - I may have set it myself. To tell you the truth, I want my system to issue signals when a bar is completed. My understanding is that this requires that I setComputeonClose to be true, that I do not want signals based on intra-tick noise, but only on completion of a bar.
                  Is this a correct understanding of what the clause does? It is not well documented.
                  Thanks again
                  George

                  Comment


                  • #10
                    George
                    Then setComputeOnClose() will ensure that the formula is computed only at the close of the bar and not on every tick.
                    The drawback is that the generated signals remain in effect for the next bar until a new close happens.
                    If the paint bar is triggered by the same conditions that generate the trading signals then I don't think there is much you can do about that.
                    If instead the paintbar is generated by other conditions then you could separate the sections of the formula and have some computed only on the close.
                    Try doing a search on getBarState() as this is a topic that has been covered frequently.
                    Alex

                    Comment


                    • #11
                      Alex
                      Tricky problem...I am moving now to modularize my code. Commonly used functions are removed and placed in a "subroutine library" and are then CALLFunction'ed. Even so, it is not so easy. I will look into BAR_STATE.
                      THANKS
                      George

                      Comment


                      • #12
                        Further to this, I find that I have a greater need to setComputeOnClose(), which gets me back where I started from. I'll have to find someother way to deal with this issue.
                        Alexis, is the a command/function that returns the current price bar color?
                        Thanks
                        George

                        Comment


                        • #13
                          George
                          Not that I am aware of
                          Alex

                          Comment

                          Working...
                          X