Announcement

Collapse
No announcement yet.

Current Bar Color

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

  • Current Bar Color

    Is there a reliable way to force the color of the current bar (the bar currently being printed on an Advanced Chart)? I have the following code:
    PHP Code:
    if (close() > open()) setPriceBarColor(Color.lime); 
    else               
    if (
    close() < open())  setPriceBarColor(Color.red)   ; 
    But I find that the color is erratic, sometimes the color of the preceding bar, sometimes the default color established in preMain (setDefaultPriceBarColor(Color.khaki)).

    It appears that there is some problem recognizing the "current bar", and its associated price (close())? Is this correct?
    I find that if I dump the values, getCurrentBarIndex() never goes below -1. I thought from the doco the current bar was always 0?
    Note that when the bar is complete, when the next bar comes in, the prior current bar, the one I am having trouble with, gets set to the correct color i.e. as per the code above
    What am I missing?
    Thanks
    George
    P.S. I have given setComputeOnClose() both possible values and it does not appear to make a difference.

  • #2
    close() is the last price and open() is the first price of this bar.
    if you want the price of the close from the prior bar you need to use close(-1)

    Yuo will get the default color when c=0, right?

    Comment


    • #3
      David
      No, applying the index value of 0 (close(0), open(0)) etc., produces the same erroneous results.
      Here is a complete EFS, including the invoked function .
      George
      /************************************************** *****************

      ************************************************** ******************/

      /* var trade = new IBBroker; */var nContracts = 1;
      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() {
      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,C)",0);
      setDefaultBarFgColor(Color.fushcia,0); setPlotType( PLOTTYPE_HISTOGRAM, 0 );
      setCursorLabelName("MACD(12,26,9,C)",1);
      setDefaultBarFgColor(Color.red,1);
      setCursorLabelName("MACDSig(12,26,9,C)",2);
      setDefaultBarFgColor(Color.green,2);
      setCursorLabelName("Up",3);
      setDefaultBarFgColor(Color.green,3);
      setCursorLabelName("Down",4);
      setDefaultBarFgColor(Color.red,4);

      setComputeOnClose(true);
      }
      function CheckForNullsUndefined()
      {
      if ((vMACD_Hist = null) || (vMACD_Hist = undefined))
      vMACD_Hist = false;
      if ((vMACD_Hist_1 = null) || (vMACD_Hist_1 = undefined))
      vMACD_Hist = false;
      if ((vMACD_Hist_2 = null) || (vMACD_Hist_2 = undefined))
      vMACD_Hist = false;
      if ((vEMA = null) || (vEMA = undefined))
      vEMA = false;
      if ((vEMA_1 = null) || (vEMA_1 = undefined))
      vEMA_1 = false;
      if ((vEMA_2 = null) || (vEMA_2 = undefined))
      vEMA_2 = false;
      }

      function PriceBarColors() {
      if (close(0) > open(0)) setPriceBarColor(Color.lime)
      else
      if (close(0) < open(0)) setPriceBarColor(Color.red);
      return;
      }

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

      if ( /* (vMACD_Hist >= vMACD_Hist_1) && (vMACD_Hist_1 >= vMACD_Hist_2)&&*/
      (vEMA > vEMA_1) && (vEMA_1 > vEMA_2) )
      TrendIsUp = true; else TrendIsUp = false;
      /* debugPrintln("Bar " + getCurrentBarIndex()+"TrendIsUp:" + TrendIsUp + " TrendIsDown:" + TrendIsDown ) ;*/
      return (TrendIsUp,TrendIsDown);
      }
      function main(EMAlen,MACDfast,MACDslow,MACDsmoothing)
      { PriceBarColors();
      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",true);
      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 );
      debugPrintln("Bar " + getCurrentBarIndex()+" UP Before Set= " + TrendIsUp + " UP After Set= " + UpTrend );
      debugPrintln("Bar " + getCurrentBarIndex()+" DN Before Set= " + TrendIsDown + " DN After Set= " + DownTrend );
      debugPrintln("Bar " + getCurrentBarIndex()+" UP " + UpTrend + " DN " + DownTrend ); */

      return new Array (vMACD_Hist,vMACD,vMACD_Signal,UpTrend,DownTrend);
      }

      Comment

      Working...
      X