Change Code from Bar to Candlestick

 

Hi All,

Can anyone tell me how to convert the source code below from Bar to Candlestick? Right now it only shows bars even when I choose the Candlestick option in MT4.

//+------------------------------------------------------------------+
//|                                                    Open.mq4 |
//|                                                              CK1 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Lime
#property indicator_color2  Red
 
//
//
//
//
//
 
//extern int Length = 8;
//extern int Price  = PRICE_CLOSE;
 
//
//
//
//
//
 
double bartu[];
double bartd[];
double trend[];
double vel[];
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
 
int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,bartu); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,bartd); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,vel);
      SetIndexBuffer(3,trend);
   return(0);
}
int deinit() { return(0); }
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
 
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
 
   //
   //
   //
   //
   //
  
   for(int i=limit; i>=0; i--)
   {
      //vel[i] = iVelocity(iMA(NULL,0,1,0,MODE_SMA,Price,i),Length,i);
      vel[i] = iMA(NULL,0,8,0,MODE_EMA,0,i);
         bartu[i] = EMPTY_VALUE;
         bartd[i] = EMPTY_VALUE;
            if (iClose(NULL,0,i )> vel[i]) trend[i] =  1;
            if (iClose(NULL,0,i) < vel[i]) trend[i] = -1;
               if (trend[i]== 1) { bartu[i] = High[i]; bartd[i] = Low[i]; }
               if (trend[i]==-1) { bartd[i] = High[i]; bartu[i] = Low[i]; }
   }
   return(0);
}
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
 
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. http://getsupplementhelp.com/testro-t3...
 
Volcom: Can anyone tell me how to convert the source code below from Bar to Candlestick? Right now it only shows bars even when I choose the Candlestick option in MT4.
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. It displays a histogram (a vertical bar) on top of your chart. That has nothing to do with candlesticks.
  3. If you want it to display as a candlestick, you'll have to add another pair of buffers, and change their size. Look how the Heiken Ashi indicator does it.
 
Can you show me what the code would be and where to implement it?
 

I already "showed you what the code would be" had you bothered to look up the indicator mentioned.

You have only four choices:

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
          No free help
          urgent help.
 
whroeder1:

I already "showed you what the code would be" had you bothered to look up the indicator mentioned.

You have only four choices:

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
          No free help
          urgent help.

@whroeder1 - If your demeanor on this forum is going to be that of negative towards novice MT4 coders, then please find another profession. If all you can do is provide a general template of an answer, then don't respond and let others (who have helped me before) provide me the code I am asking for. Once a code is provided, that is MY WAY of learning for future development how to implement it on the next go around. As you can CLEARLY see, I am not asking someone on this forum to code me an indicator that I thought up. So get lost already dude.

With that said, is anyone else out there kind enough to show me what and where the code would be implemented to reflect candlesticks instead of bars in the Source Code listed above?

 

Dear Volcom,

Try this after int init()

 int Chart_ID;
  ChartSetInteger(Chart_ID,CHART_MODE,CHART_CANDLES);

Read reference manual Chart Properties


Get yourself a copy of this book, it is very helpful  Expert Advisor Programming for MetaTrader 4

Also use SRC to post code.

 
GrumpyDuckMan:

Dear Volcom,

Try this after int init()

Read reference manual Chart Properties


Get yourself a copy of this book, it is very helpful  Expert Advisor Programming for MetaTrader 4

Also use SRC to post code.

Hi @GrumpyDuckMan,

I get three errors when placing this code and still can't quite figure it out. I scanned through the Cart Properties manual and still unsure. Any ideas?

Line 43) 'int' - semicolon expected

Line 44) 'ChartSetInteger' - declaration without type

Line 46) '{' function definition unexpected


34)//-------------------------------------------------------------------
35)//
36)//
37//
38)//
39)//
40) 
41)int init()
42)
43)int ChartID;
44)  ChartSetInteger(ChartID,CHART_MODE,CHART_CANDLES);
45) 
46){
47)   IndicatorBuffers(4);
48)   SetIndexBuffer(0,bartu); SetIndexStyle(0,DRAW_HISTOGRAM);
49)   SetIndexBuffer(1,bartd); SetIndexStyle(1,DRAW_HISTOGRAM);
50)   SetIndexBuffer(2,vel);
51)   SetIndexBuffer(3,trend);
52)   return(0);
53)}
54)int deinit() { return(0); }
55)
56) //-------------------------------------------------------------------
 
Volcom:

Hi @GrumpyDuckMan,

I get three errors when placing this code and still can't quite figure it out. I scanned through the Cart Properties manual and still unsure. Any ideas?

Line 43) 'int' - semicolon expected

Line 44) 'ChartSetInteger' - declaration without type

Line 46) '{' function definition unexpected


//+------------------------------------------------------------------+
//|                                                    Open.mq4 |
//|                                                              CK1 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Lime
#property indicator_color2  Red
 
//
//
//
//
//
 
//extern int Length = 8;
//extern int Price  = PRICE_CLOSE;
 
//
//
//
//
//
 
double bartu[];
double bartd[];
double trend[];
double vel[];

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
 
int init()
{
   int Chart_ID;
   ChartSetInteger(Chart_ID,CHART_MODE,CHART_CANDLES);
   IndicatorBuffers(4);
      SetIndexBuffer(0,bartu); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,bartd); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,vel);
      SetIndexBuffer(3,trend);
   return(0);
}
int deinit() { return(0); }
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
 
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
 
   //
   //
   //
   //
   //
   
   for(int i=limit; i>=0; i--)
   {
      //vel[i] = iVelocity(iMA(NULL,0,1,0,MODE_SMA,Price,i),Length,i);
      vel[i] = iMA(NULL,0,8,0,MODE_EMA,0,i);
         bartu[i] = EMPTY_VALUE;
         bartd[i] = EMPTY_VALUE;
            if (iClose(NULL,0,i )> vel[i]) trend[i] =  1;
            if (iClose(NULL,0,i) < vel[i]) trend[i] = -1;
               if (trend[i]== 1) { bartu[i] = High[i]; bartd[i] = Low[i]; }
               if (trend[i]==-1) { bartd[i] = High[i]; bartu[i] = Low[i]; }
   }
   return(0);
}
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
This error should have given it away    Line 46) '{' function definition unexpected
 

Thanks @GrumpyDuckMan,

The code came back with no errors which is great. However, it still reflects bars only instead of candlesticks.

 
Volcom:

Thanks @GrumpyDuckMan,

The code came back with no errors which is great. However, it still reflects bars only instead of candlesticks.

I edited your code, here it is.  It works for me


//+------------------------------------------------------------------+
//|                                                    Open.mq4 |
//|                                                              CK1 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Lime
#property indicator_color2  Red
#property indicator_maximum 1
#property indicator_minimum 0 
double bartu[];
double bartd[];
double trend[];
double vel[];

int init()
{
   int Chart_ID;
      ChartSetInteger(Chart_ID,CHART_SCALE,2);
   ChartSetInteger(Chart_ID,CHART_SHIFT,1);
   ChartSetInteger(Chart_ID,CHART_MODE,CHART_CANDLES);
    ChartSetInteger(Chart_ID,CHART_SHOW_GRID,false);
   IndicatorBuffers(4);
      SetIndexBuffer(0,bartu); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,bartd); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,vel);
      SetIndexBuffer(3,trend);
   return(0);
}
int deinit()
 { return(0);
  }
 

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
   
   for(int i=limit; i>0; i--)
   {      vel[i] = iMA(NULL,0,8,0,MODE_EMA,0,i);

            if (iClose(NULL,0,i)> vel[i]) trend[i] =  0;
            if (iClose(NULL,0,i) < vel[i]) trend[i] = 1;
              if (trend[i]== 1) { bartu[i] = High[i]; bartd[i] = Low[i]; }

   }
   return(0);
}
 
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
 
GrumpyDuckMan:

I edited your code, here it is.  It works for me

@GrumpyDuckMan - Can you attach a screenshot of what your chart looks like? When I applied the MT4 code, all I got was lines (attached).

Files:
Capture.PNG  106 kb
Reason: