Code Problems!

 

Hi All,

I am new to the Mql4 forum, but I have been using MetaStock for years.

Below is the code for an indicator that I am trying write.

Basically, I'm trying to get either a value of 1 ( when "iClose(Symbol(),PERIOD_H1,0)>iClose(Symbol(),PERIOD_H1,1)&&iClose(Symbol(),PERIOD_H1,1)>iClose(Symbol(),PERIOD_H1,2)"...should be simple)

or a value of -1 "iClose(Symbol(),PERIOD_H1,0)<iClose(Symbol(),PERIOD_H1,1)&&iClose(Symbol(),PERIOD_H1,1)<iClose(Symbol(),PERIOD_H1,2)"

If neither, then the PREVIOUS VALUE (either 1 or -1)

Please can anyone help...I'm still learning

Thank you. :)

/+------------------------------------------------------------------+
//|                                                   Up Down Me.mq4 |
//|                                                            Timbo |
//|                                                          Missing |
//+------------------------------------------------------------------+
#property copyright "Timbo"
#property link      "Missing"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LightSeaGreen
//---- buffers
double NCBuffer[];
double YCBuffer[];
double DYCBuffer[];
double BinBuffer[];
int Up[];
int Down[];
int Flat[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   IndicatorBuffers(7);
   SetIndexBuffer(1, NCBuffer);
   SetIndexBuffer(2, YCBuffer);
   SetIndexBuffer(3, DYCBuffer);
   SetIndexBuffer(4, BinBuffer);
   SetIndexBuffer(5, Up);
   SetIndexBuffer(6, Down);
   SetIndexBuffer(7, Flat);
   
//---- indicator lines
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, BinBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="Binary Close Trend";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Binary Close Trend                                       |
//+------------------------------------------------------------------+
int start()
  {
double DYCBuffer=iClose(Symbol(),PERIOD_H1,2);
double YCBuffer=iClose(Symbol(),PERIOD_H1,1);
double NCBuffer=iClose(Symbol(),PERIOD_H1,0);
int Up=1;
int Down=-1;
int Flat=0;

   if(Bars <= 3) 
       return(0);
//---- Binary
DYCBuffer=iClose(Symbol(),PERIOD_H1,2);
YCBuffer=iClose(Symbol(),PERIOD_H1,1);
NCBuffer=iClose(Symbol(),PERIOD_H1,0);
if((YCBuffer > DYCBuffer)&&(NCBuffer > YCBuffer))
{
Up
}else
{
Flat
}
if((YCBuffer < DYCBuffer)&&(NCBuffer < YCBuffer))
{
Down
}else
{
Flat
}
//----
   return(0);
  }
//+-------------------------------------------------
 
Timbo618:

Hi All,

I am new to the Mql4 forum, but I have been using MetaStock for years.

Below is the code for an indicator that I am trying write.

Basically, I'm trying to get either a value of 1 ( when "iClose(Symbol(),PERIOD_H1,0)>iClose(Symbol(),PERIOD_H1,1)&&iClose(Symbol(),PERIOD_H1,1)>iClose(Symbol(),PERIOD_H1,2)"...should be simple)

or a value of -1 "iClose(Symbol(),PERIOD_H1,0)<iClose(Symbol(),PERIOD_H1,1)&&iClose(Symbol(),PERIOD_H1,1)<iClose(Symbol(),PERIOD_H1,2)"

If neither, then the PREVIOUS VALUE (either 1 or -1)

Please can anyone help...I'm still learning

Thank you. :)

<CODE REMOVED>

Please read some other posts before posting . . .

Please edit your post above . . . please use the SRC button to post code: How to use the SRC button.

 

Sorry about that Raptor...lol...

Am I on the right track....or way off base?

Thanks for all your help.

Cheers,

Tim.

 
Timbo618:

Sorry about that Raptor...lol...

Am I on the right track....or way off base?

Get your code to compile first . . .


Currently you code is not going to process all the current bars on your chart, and for each tick it will do what it did last tick . . .

You have a buffer called DYCBuffer and a local double called DYCBuffer, why ?

Don't Indicator buffers have to be doubles ?

int Up[];
int Down[];
int Flat[];

How many Indicator Buffers do you have ?

#property indicator_buffers 1

//---- buffers
double NCBuffer[];
double YCBuffer[];
double DYCBuffer[];
double BinBuffer[];
int Up[];
int Down[];
int Flat[];


   IndicatorBuffers(7);
 

Thanks Raptor.

I thought I could use int instead of double to only get a 1,-1 or 0 as an value.

I was way off then...Damn... I could write it into MetaStock in a heartbeat, and it seems such an easy calculation in my head, but mql4 is a diiferent puppy all together.

Do I just need to add Indicator buffers to get it to work, or is my other code completely stuffed aswell...lol...I'm guessing all stuffed...but it felt so close!!! :)

I think I better start from scratch.

Thanks again for all your help Raptor.

Chat soon.

Tim.

 
Timbo618:

Thanks Raptor.

I thought I could use int instead of double to only get a 1,-1 or 0 as an value.

I was way off then...Damn... I could write it into MetaStock in a heartbeat, and it seems such an easy calculation in my head, but mql4 is a diiferent puppy all together.

Do I just need to add Indicator buffers to get it to work, or is my other code completely stuffed aswell...lol...I'm guessing all stuffed...but it felt so close!!! :)

I think I better start from scratch.

Don't throw away what you have . . . you can get the general idea of how Custom Indicators work by looking at some of the Indicators in the CodeBase, there are plenty to look at . . . then once you understand the principles you can fix your code.
 

Hi Raptor,

I Think I am almost there, but my indicator is just a flat line, instead of moving between a value of -1 and 1.

Also could you see if I have used the SetIndexShift correctly at the end of my code...I would like it to show the previous value of either -1 or 1 if the condition has not been met.

Thank you again for all you help.

Tim.

Below is the code.

//+------------------------------------------------------------------+
//|                                                       TimTry.mq4 |
//|                                                               Me |
//|                                                          Missing |
//+------------------------------------------------------------------+
#property copyright "Me"
#property link      "Missing"

#property indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color4  Silver
#property indicator_minimum -2
#property indicator_maximum 2
#property  indicator_width4  1
//---- indicator buffers
double     HOURCLOSE2[];
double     HOURCLOSE1[];
double     HOURCLOSE0[];
double     BinSig[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(3,DRAW_LINE);
   SetIndexDrawBegin(3,BinSig);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,HOURCLOSE2);
   SetIndexBuffer(1,HOURCLOSE1);
   SetIndexBuffer(2,HOURCLOSE0);
   SetIndexBuffer(3,BinSig);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Binary Trend");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Binary Close Trend                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- Close from 2 Days ago
   for(int i=0; i<limit; i++)
      HOURCLOSE2[i]=iClose(Symbol(),PERIOD_H1,2);
//---- Close from Yesterday
   for(i=0; i<limit; i++)
      HOURCLOSE1[i]=iClose(Symbol(),PERIOD_H1,1);
//---- Close from Today
   for(i=0; i<limit; i++)
      HOURCLOSE0[i]=iClose(Symbol(),PERIOD_H1,0);
//---- Binary Trend
   for(i=0; i<limit; i++)
if((HOURCLOSE1[i]>HOURCLOSE2[i])&&(HOURCLOSE0[i]>HOURCLOSE1[i]))
  {
   BinSig[i]=1;
  }
else        if((HOURCLOSE1[i]<HOURCLOSE2[i])&&(HOURCLOSE0[i]<HOURCLOSE1[i]))
  {
   BinSig[i]=-1;
  }
else        SetIndexShift(1,BinSig[i]);
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
 
  for(int i=0; i<limit; i++)
      HOURCLOSE2[i]=iClose(Symbol(),PERIOD_H1,2);
//---- Close from Yesterday
   for(i=0; i<limit; i++)
      HOURCLOSE1[i]=iClose(Symbol(),PERIOD_H1,1);
//---- Close from Today
   for(i=0; i<limit; i++)
      HOURCLOSE0[i]=iClose(Symbol(),PERIOD_H1,0);

How do the highlighted shifts relate to i ?

 

Hi GumRai,

I'm am trying to get the indicator to give a value of 1 if todays close>yesterdays close and yesterdays close>2 days ago close. and vice verse for value of -1.

And also the previous value of 1 or -1 if condtion is false.

Thanks for you help.

Newbee....but getting there.

Also, why is it just a flat line at either 1 or -1?

Thanks.

Tim.

 
   for(int i=0; i<limit; i++)
      HOURCLOSE2[i]=iClose(Symbol(),PERIOD_H1,2);
You are filling in your entire arrays with the same value. Take your shift "i" and convert them to a H1 shift
   for(int i=0; i<limit; i++){
      int iH1 = iBarShift(NULL, Period_H1, Time[i]);
      HOURCLOSE2[i]=iClose(Symbol(),PERIOD_H1,iH1+2);
   }
 

Hi WHRoeder,

It is still just giving a flat line...Not to sure why.

The code is below.

Thank you very much.

Tim.

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
///---- Close from Today
   for(int i=0; i<limit; i++)
      HOURCLOSE0[i]=iClose(Symbol(),PERIOD_H1,Time[i]);
//---- Close from Yesterday
   for(i=0; i<limit; i++)
      HOURCLOSE1[i]=iClose(Symbol(),PERIOD_H1,HOURCLOSE0[i]+1);
//---- Close from 2 Days ago
   for(i=0; i<limit; i++)
      HOURCLOSE2[i]=iClose(Symbol(),PERIOD_H1,HOURCLOSE0[i]+2);
//---- Binary Trend
   for(i=0; i<limit; i++)
if((HOURCLOSE1[i]>HOURCLOSE2[i])&&(HOURCLOSE0[i]>HOURCLOSE1[i]))
  {
   BinSig[i]=1;
  }
else        if((HOURCLOSE1[i]<HOURCLOSE2[i])&&(HOURCLOSE0[i]<HOURCLOSE1[i]))
  {
   BinSig[i]=-1;
  }
else        BinSig[i]=0;
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
Reason: