How to add red green color on histogram?

thread: How to add red green color on histogram?

  1. #1

    How to add red green color on histogram?

    Hello friends,

    I'm a not an expert coder and confound the way to add green color to get positive histogram and red color for negative histogram with this indior.


    The indior code :
    Inserted Code // ------------------------------------------------------------------ //| CandleAverage_v1. mq4 | //| Copyright © 2006, Forex-TSD. Com | //| Composed by IgorAD,[email protected] | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //p------------------------------------------------------------------ #property copyright Copyright © 2006, Forex-TSD. Com #property link http://www.forex-tsd.com/ #property indior_separate_window #property indior_buffers 1 #property indior_color1 DeepSkyBlue //-- enter parameters extern int Length=31; extern int H_period=25; extern int L_period=27; extern int C_period=9; //-- buffers dual CandleAvg#91;#93;; dual BarValue#91;#93;; // ------------------------------------------------------------------ //| Custom indior initialization function | // ------------------------------------------------------------------ int init() //-- indiors IndiorBuffers(2); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexBuffer(0,CandleAvg); SetIndexBuffer(1,BarValue); //-- name for DataWindow and indior subwindow label string short_name=CandleAverage( Length ); IndiorShortName(short_name); SetIndexLabel(0,CandleAvg); SetIndexDrawBegin(0,Length); //-- return(0); // ------------------------------------------------------------------ //| Custom indior iteration serve | // ------------------------------------------------------------------ int begin() int change, limit, counted_bars=IndiorCounted(); dual UpDel,DnDel,H,L,C; //-- if(counted_barslt;0) return(-1); if (counted_bars==0) limit=Bars-Length-1; //-- final counted bar is going to be recounted if(counted_barsgt;0) limit=Bars-counted_bars; limitation -- for(shift=limitation; shiftgt;=0; change --) H = iMA(NULL,0, H_period,0, MODE_EMA, PRICE_HIGH, change); L = iMA(NULL,0, L_period,0, MODE_EMA, PRICE_LOW, change); C = iMA(NULL,0, C_period,0, MODE_EMA, PRICE_CLOSE, change); UpDel = H - C; DnDel = C - L; if ( UpDel lt; DnDel ) BarValue#91;change#93; = 1; if ( UpDel gt; DnDel ) BarValue#91;change#93; = -1; if ( UpDel == DnDel ) BarValue#91;change#93; = 0; for(shift=limitation; shiftgt;=0; change --) CandleAvg#91;change#93; = iMAOnArray(BarValue,0,Length,0,MODE_SMA,change); //-- return(0); // ------------------------------------------------------------------ I tried change the code like bellow :

    Inserted Code #property indior_separate_window #property indior_buffers 2 #property indior_color1 DeepSkyBlue #property indior_color2 Red But didn't work because histogram value saved just in 1 buffer, CandleAvg.

    How to separate negative and positive worth from CandleAvg buffer and add red green color?

    Please advice.

    Thanks for the help.




  2. #2
    include two more buffers: [ CandleAvgPlus[], and CandleAvgMinus[] and set to empty value. Then use an if statement within a for loop to fill these two new buffers. .


    If(CandleAvg[I] gt;= somenumber)CandleAvgPlus[I] = CandleAvg[I];
    if(CandleAvg[I] lt; somenumber)CandleAvgMinus[I] = CandleAvg[I];

    Now just plot CandleAvgPlus and CandleAvgMinus rather than CandleAve

    Obviously declare them properly and initialize them properly but this is the method to separate them outside without me writing the code for you under.

    On a different note I learned most...
    Thanks for the aid flyer415.

    Thank you also for your will to rewrite the code for me personally however I prefer to unveil by myself and learn from it.

    I am rather new on MQL coding and must find out a lot.

    I tried to follow your advice and modify the code but didn't work.

    I tried for hours and don't know the reason it isn't working.


    Inserted Code // ------------------------------------------------------------------ //| CandleAverage_v1. mq4 | //| Copyright ? 2006, Forex-TSD. Com | //| Composed by IgorAD,[email protected] | //| http://finance.groups.yahoo.com/group/TrendLaboratory | // ------------------------------------------------------------------ #property copyright Copyright ? 2006, Forex-TSD. Com #property hyperlink http://www.forex-tsd.com/ #property indior_separate_window #property indior_buffers 4 #property indior_color1 DeepSkyBlue #property indior_color3 Green #property indior_color4 Red //-- input parameters extern int Length=31; extern int H_period=25; extern int L_period=27; extern int C_period=9; //-- buffers double CandleAvgNumber91;#93;; double BarValue#91;#93;; double CandleAvgPlus#91;#93;; double CandleAvgMinus#91;#93;; // ------------------------------------------------------------------ //| Custom indior initialization function | // ------------------------------------------------------------------ int init() //-- indiors IndiorBuffers(4); SetIndexStyle(0,DRAW_NONE); SetIndexStyle(1,DRAW_NONE); SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexStyle(3,DRAW_HISTOGRAM); SetIndexBuffer(0,CandleAvg); SetIndexBuffer(1,BarValue); SetIndexBuffer(2,CandleAvgPlus); SetIndexBuffer(3,CandleAvgMinus); //-- name for DataWindow and indior subwindow label string short_name=CandleAverage( Length ); IndiorShortName(short_name); SetIndexLabel(0,CandleAvg); SetIndexDrawBegin(0,Length); //-- return(0); // ------------------------------------------------------------------ //| Custom indior iteration serve | // ------------------------------------------------------------------ int begin() int change, limitation, counted_bars=IndiorCounted(); double UpDel,DnDel,H,L,C; //-- if(counted_barslt;0) return(-1); if (counted_bars==0) limit=Bars-Length-1; //-- last counted bar will be recounted if(counted_barsgt;0) restrict=Bars-counted_bars; limitation -- ; for(shift=limitation; shiftgt;=0; change --) H = iMA(NULL,0, H_period,0, MODE_EMA, PRICE_HIGH, change); L = iMA(NULL,0, L_period,0, MODE_EMA, PRICE_LOW, change); C = iMA(NULL,0, C_period,0, MODE_EMA, PRICE_CLOSE, change); UpDel = H - C; DnDel = C - L; if(CandleAvgNumber91;change#93; gt;= UpDel)CandleAvgPlusNumber91;change#93; = CandleAvg#91;change#93;; if(CandleAvg#91;change#93; lt; DnDel)CandleAvgMinus#91;change#93; = CandleAvg#91;change#93;; if ( UpDel lt; DnDel ) BarValue#91;change#93; # 1; if ( UpDel gt; DnDel ) BarValue#91;change#93; = -1; if ( UpDel == DnDel ) BarValue#91;change#93; = 0; for(shift=limitation; shiftgt;=0; change --) CandleAvgPlus#91;change#93; = iMAOnArray(BarValue,0,Length,0,MODE_SMA,change); CandleAvgMinus#91;change#93; = iMAOnArray(BarValue,0,Length,0,MODE_SMA,change); //-- return(0); // ------------------------------------------------------------------ Please help.

  3. #3
    Member Sireh's Avatar
    43
    Include two buffers: CandleAvgPlus[], and CandleAvgMinus[] and set value to be emptied by both . Then use an if statement within a for loop to fulfill these two new buffers. .


    If(CandleAvg[I] gt;= somenumber)CandleAvgPlus[I] = CandleAvg[I];
    if(CandleAvg[I] lt; somenumber)CandleAvgMinus[I] = CandleAvg[I];

    Now just plot CandleAvgPlus and CandleAvgMinus rather than CandleAve

    Obviously declare them properly and initialize them properly but this is actually the method to separate them outside without me writing the code for you under.

    On a different note I discovered most of my programming just by taking a look at code from other people. In this scenario an file on colour MACD would show the entire item for you real fast

  4. #4
    Member Sireh's Avatar
    43
    Return to your code. You need to allow the indior to calculate the CandleAve[ ] as per regular you seperate the values into negative and positive.

    So that your initial for loop does a bunch of calcs then your next for loop fills CandleAve[]. You do not need to alter those loops.

    Today we must create a third for loop just prior to the return statement. Statements will be used if by that third for loop. Basically if CandleAve[i]gt;= 0 then CandleAvePlus[i] = CandleAve[i] else CandleAveMinus[i]=CandleAve[i].

    Basically, to colour code we want to fundamentally produce the indior buffers as per standard then just prior to the return purpose we seperate them out. For me, colour coding is the last step in an indior.

    Only a little point but since you're plotting CandleAvePlus and CandleAveMinus then they ought to be indiorbuffers 1 and 0. It is obviously better that way otherwise you get numbers mixed up. And I fill the two new buffers with values so they plot nicely.

  5. #5
    Go back to your original code....
    Finally it works perfectly.

    Thank you so much flyer415 for your kindness and your individual teaching me.

    Sorry if I am quite dumb and slow learning MQL code out of you.

    Once more, thank you.



    Here is the modified code :

    Inserted Code // ------------------------------------------------------------------ //| CandleAverage_v1. mq4 | //| Copyright ? 2006, Forex-TSD. Com | //| Composed by IgorAD,[email protected] | //| http://finance.groups.yahoo.com/group/TrendLaboratory | // ------------------------------------------------------------------ #property copyright Copyright ? 2006, Forex-TSD. Com #property hyperlink http://www.forex-tsd.com/ #property indior_separate_window #property indior_buffers 4 #property indior_color1 Green #property indior_color2 Red //-- enter parameters extern int Length=31; extern int H_period=25; extern int L_period=27; extern int C_period=9; //-- buffers dual CandleAvgPlus#91;#93;; dual CandleAvgMinus#91;#93;; dual CandleAvg#91;#93;; dual BarValue#91;#93;; // ------------------------------------------------------------------ //| Custom indior initialization function | // ------------------------------------------------------------------ int init() //-- indiors IndiorBuffers(4); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexBuffer(0,CandleAvgPlus); SetIndexBuffer(1,CandleAvgMinus); SetIndexBuffer(2,CandleAvg); SetIndexBuffer(3,BarValue); //-- title for DataWindow and indior subwindow label string short_name=CandleAverage( Length ); IndiorShortName(short_name); SetIndexLabel(0,CandleAvg); SetIndexDrawBegin(0,Length); //-- return(0); // ------------------------------------------------------------------ //| Custom indior iteration function | // ------------------------------------------------------------------ int start() int shift, limit, counted_bars=IndiorCounted(); dual UpDel,DnDel,H,L,C; //-- if(counted_barslt;0) return(-1); if (counted_bars==0) limit=Bars-Length-1; //-- last counted pub is going to be recounted if(counted_barsgt;0) restrict=Bars-counted_bars; limit ; for(shift=limit; shiftgt;=0; shift) H = iMA(NULL,0, H_period,0, MODE_EMA, PRICE_HIGH, shift); L = iMA(NULL,0, L_period,0, MODE_EMA, PRICE_LOW, shift); C = iMA(NULL,0, C_period,0, MODE_EMA, PRICE_CLOSE, shift); UpDel = H - C; DnDel = C - L; should ( UpDel lt; DnDel ) BarValue#91;shift#93; = 1; should ( UpDel gt; DnDel ) BarValue#91;shift#93; = -1; should ( UpDel == DnDel ) BarValue#91;shift#93; = 0; for(shift=limit; shiftgt;=0; shift) CandleAvg#91;shift#93; = iMAOnArray(BarValue,0,Length,0,MODE_SMA,shift); for(shift=limit; shiftgt;=0; shift--) if(CandleAvg#91;shift#93; gt;= 0)CandleAvgPlus#91;shift#93; = CandleAvg#91;shift#93;; should(CandleAvg#91;shift#93; lt; 0)CandleAvgMinus#91;shift#93; = CandleAvg#91;shift#93;; //-- return(0); // ------------------------------------------------------------------

  6. #6
    Member Sireh's Avatar
    43
    [quote=wieb;3232106]it works flawlessly.

    Thank you a lot for your kindness and your individual teaching me.

    Sorry when I am so slow and dumb learning MQL code out of you.

    Once more, thank you.



    For(shift=limitation; shiftgt;=0; change --)

    CandleAvg[change] = iMAOnArray(BarValue,0,Length,0,MODE_SMA,change);
    /* you could put the code and spare a loop
    if(CandleAvg[change] gt;= 0)CandleAvgPlus[change] = CandleAvg[change];
    if(CandleAvg[change] lt; 0)CandleAvgMinus[change] = CandleAvg[change];
    */




    I learned from others, asking questions and studying code. You could eliminate one for loop as shown from the bolded code.

    Glad it works. You can create any indior multi-colour since it is the identical concept.

  •