3 Color Candle based on indicator conditions (i.e. momentum, MA positions etc)

 

Hi, I like to ask to some expert so kind to modify an indicator of mine.

Actually the indicator posted (KPHighlights) does not exactly paint the candles in an orthodox way, it just paints some fat colored vert lines (histogram) under the candles but this is enough for my need.

Now Im wondering if it's possible to add a 3rd condition to the code I posted so candles will be painted in 3 different colors, currently the embedded indicator conditions paint them showing just bull and bear colors... neutral is missing.

I've been trying to make it working myself (see 2nd attachment) but it failed... just 2 colors as the original one.

Thanks

Marco

the original one

#include

//+------------------------------------------------------------------+

//| KPHighlights.mq4 |

//| Copyright © 2007, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Marco Taggiasco aka ontherun"

#property link "http://www.metaquotes.net"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 CornflowerBlue

#property indicator_color2 DeepPink

//+------------------------------------------------------------------+

//| Common External variables |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| External variables |

//+------------------------------------------------------------------+

extern double nAccountedBars = 300;

//+------------------------------------------------------------------+

//| Special Convertion Functions |

//+------------------------------------------------------------------+

int LastTradeTime;

double ExtHistoBuffer[];

double ExtHistoBuffer2[];

void SetLoopCount(int loops)

{

}

void SetIndexValue(int shift, double value)

{

ExtHistoBuffer[shift] = value;

}

void SetIndexValue2(int shift, double value)

{

ExtHistoBuffer2[shift] = value;

}

//+------------------------------------------------------------------+

//| End |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Initialization |

//+------------------------------------------------------------------+

int init()

{

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID,5,CornflowerBlue);

SetIndexBuffer(0, ExtHistoBuffer);

SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID,5,DeepPink);

SetIndexBuffer(1, ExtHistoBuffer2);

return(0);

}

int start()

{

//+------------------------------------------------------------------+

//| Local variables |

//+------------------------------------------------------------------+

int CurrentBar = 0;

double price = 0;

double FM,SM;

SetLoopCount(0);

for(CurrentBar =0;CurrentBar <=nAccountedBars ;CurrentBar ++)

{ FM=iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

SM=iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

if( FM>0 && SM>0 )

{

SetIndexValue(CurrentBar, High[CurrentBar]);

SetIndexValue2(CurrentBar, Low[CurrentBar]);

}

if( FM<0 && SM<0 )

{

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue2(CurrentBar, High[CurrentBar]);

}

} return(0);

}[/CODE]

one of mine attempts

[CODE]#include

//+------------------------------------------------------------------+

//| KPHighlights#2.mq4 |

//| Copyright © 2007, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Marco Taggiasco aka ontherun"

#property link "http://www.metaquotes.net"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_color1 CornflowerBlue

#property indicator_color2 DeepPink

#property indicator_color3 Yellow

//+------------------------------------------------------------------+

//| Common External variables |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| External variables |

//+------------------------------------------------------------------+

extern double nAccountedBars = 300;

//+------------------------------------------------------------------+

//| Special Convertion Functions |

//+------------------------------------------------------------------+

int LastTradeTime;

double ExtHistoBuffer[];

double ExtHistoBuffer2[];

double ExtHistoBuffer3[];

void SetLoopCount(int loops)

{

}

void SetIndexValue(int shift, double value)

{

ExtHistoBuffer[shift] = value;

}

void SetIndexValue2(int shift, double value)

{

ExtHistoBuffer2[shift] = value;

}

void SetIndexValue3(int shift, double value)

{

ExtHistoBuffer3[shift] = value;

}

//+------------------------------------------------------------------+

//| End |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Initialization |

//+------------------------------------------------------------------+

int init()

{

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID,5,CornflowerBlue);

SetIndexBuffer(0, ExtHistoBuffer);

SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID,5,DeepPink);

SetIndexBuffer(1, ExtHistoBuffer2);

SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID,5,Yellow);

SetIndexBuffer(2, ExtHistoBuffer3);

return(0);

}

int start()

{

//+------------------------------------------------------------------+

//| Local variables |

//+------------------------------------------------------------------+

int CurrentBar = 0;

double price = 0;

double FM,SM;

SetLoopCount(0);

for(CurrentBar =0;CurrentBar <=nAccountedBars ;CurrentBar ++)

{ FM=iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

SM=iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

if( FM>0 && SM>0 )

{

SetIndexValue(CurrentBar, High[CurrentBar]);

SetIndexValue2(CurrentBar, Low[CurrentBar]);

}

if( FM<0 && SM<0 )

{

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue2(CurrentBar, High[CurrentBar]);

}

else //if (( FM0 ) || ( FM>0 && SM<0 ))

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue3(CurrentBar, High[CurrentBar]);

} return(0);

}
 
ontherun:
Hi, I like to ask to some expert so kind to modify an indicator of mine.

Actually the indicator posted (KPHighlights) does not exactly paint the candles in an orthodox way, it just paints some fat colored vert lines (histogram) under the candles but this is enough for my need.

Now Im wondering if it's possible to add a 3rd condition to the code I posted so candles will be painted in 3 different colors, currently the embedded indicator conditions paint them showing just bull and bear colors... neutral is missing.

I've been trying to make it working myself (see 2nd attachment) but it failed... just 2 colors as the original one.

Thanks

Marco

the original one

#include

//+------------------------------------------------------------------+

//| KPHighlights.mq4 |

//| Copyright © 2007, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Marco Taggiasco aka ontherun"

#property link "http://www.metaquotes.net"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 CornflowerBlue

#property indicator_color2 DeepPink

//+------------------------------------------------------------------+

//| Common External variables |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| External variables |

//+------------------------------------------------------------------+

extern double nAccountedBars = 300;

//+------------------------------------------------------------------+

//| Special Convertion Functions |

//+------------------------------------------------------------------+

int LastTradeTime;

double ExtHistoBuffer[];

double ExtHistoBuffer2[];

void SetLoopCount(int loops)

{

}

void SetIndexValue(int shift, double value)

{

ExtHistoBuffer[shift] = value;

}

void SetIndexValue2(int shift, double value)

{

ExtHistoBuffer2[shift] = value;

}

//+------------------------------------------------------------------+

//| End |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Initialization |

//+------------------------------------------------------------------+

int init()

{

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID,5,CornflowerBlue);

SetIndexBuffer(0, ExtHistoBuffer);

SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID,5,DeepPink);

SetIndexBuffer(1, ExtHistoBuffer2);

return(0);

}

int start()

{

//+------------------------------------------------------------------+

//| Local variables |

//+------------------------------------------------------------------+

int CurrentBar = 0;

double price = 0;

double FM,SM;

SetLoopCount(0);

for(CurrentBar =0;CurrentBar <=nAccountedBars ;CurrentBar ++)

{ FM=iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

SM=iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

if( FM>0 && SM>0 )

{

SetIndexValue(CurrentBar, High[CurrentBar]);

SetIndexValue2(CurrentBar, Low[CurrentBar]);

}

if( FM<0 && SM<0 )

{

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue2(CurrentBar, High[CurrentBar]);

}

} return(0);

}[/CODE]

one of mine attempts

[CODE]#include

//+------------------------------------------------------------------+

//| KPHighlights#2.mq4 |

//| Copyright © 2007, MetaQuotes Software Corp. |

//| http://www.metaquotes.net |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Marco Taggiasco aka ontherun"

#property link "http://www.metaquotes.net"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_color1 CornflowerBlue

#property indicator_color2 DeepPink

#property indicator_color3 Yellow

//+------------------------------------------------------------------+

//| Common External variables |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| External variables |

//+------------------------------------------------------------------+

extern double nAccountedBars = 300;

//+------------------------------------------------------------------+

//| Special Convertion Functions |

//+------------------------------------------------------------------+

int LastTradeTime;

double ExtHistoBuffer[];

double ExtHistoBuffer2[];

double ExtHistoBuffer3[];

void SetLoopCount(int loops)

{

}

void SetIndexValue(int shift, double value)

{

ExtHistoBuffer[shift] = value;

}

void SetIndexValue2(int shift, double value)

{

ExtHistoBuffer2[shift] = value;

}

void SetIndexValue3(int shift, double value)

{

ExtHistoBuffer3[shift] = value;

}

//+------------------------------------------------------------------+

//| End |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Initialization |

//+------------------------------------------------------------------+

int init()

{

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID,5,CornflowerBlue);

SetIndexBuffer(0, ExtHistoBuffer);

SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID,5,DeepPink);

SetIndexBuffer(1, ExtHistoBuffer2);

SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID,5,Yellow);

SetIndexBuffer(2, ExtHistoBuffer3);

return(0);

}

int start()

{

//+------------------------------------------------------------------+

//| Local variables |

//+------------------------------------------------------------------+

int CurrentBar = 0;

double price = 0;

double FM,SM;

SetLoopCount(0);

for(CurrentBar =0;CurrentBar <=nAccountedBars ;CurrentBar ++)

{ FM=iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

SM=iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,CurrentBar)-iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,CurrentBar);

if( FM>0 && SM>0 )

{

SetIndexValue(CurrentBar, High[CurrentBar]);

SetIndexValue2(CurrentBar, Low[CurrentBar]);

}

if( FM<0 && SM<0 )

{

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue2(CurrentBar, High[CurrentBar]);

}

else //if (( FM0 ) || ( FM>0 && SM<0 ))

SetIndexValue(CurrentBar, Low[CurrentBar]);

SetIndexValue3(CurrentBar, High[CurrentBar]);

} return(0);

}

You can do it the way it is done in the attached indicator (it is your indicator with a few corrections)

Files:
_test.mq4  4 kb
Reason: