new to coding, need help

 

Ive just started coding. Ive been watching loads of videos.   Im trying to code an expert advisor but im having trouble.  The current thing that im working on that im strugling with is trying to make a counter that will count how many bars its been since price closed above a moving average.   This is what i have so far,   I'm using the ordersend just to see if its working or not(is there a better way to check if conditions are happening?)   Im currently getting orders on every single candle when price is over the moving average


int BaselineCounterBull=0;

if (Close[1]>Baseline)

   {

   BaselineCounterBull++;}

   else {BaselineCounterBull =0;}

   

if (BaselineCounterBull==1)

   OrderSend(Symbol(), OP_BUY,LotSize,Ask,3,Ask-(StopLoss*ATR), Ask+(TakeProfit*ATR),NULL,1234,0, Green);


 

Each bar has time property.  You can check for example

  datetime newcandletime=TimeCurrent()

   book isnewcandle()

   if(newcandletime==iTime(Symbol(),0,0)) 

    return false

   else

   newcandletime=iTime(Symbol(), 0,0))

  return true
 

This is an example of how bars can be counted

   double ma;
   int count = 0;
   
   for(int i = 0; i < Bars - 1 && !_StopFlag; i++){
      ma = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE, i);
      
      if(Close[i] > ma)
         count++;
         
      if(Close[i] < ma) break;
      
   }
   Print("total bars = ", count);
 
That worked, but the count starts off at 2.  
 
J4str:
That worked, but the count starts off at 2.  

the search starts with a zero bar

perhaps you need other conditions for counting

 
i think i got it by changing the i from 0 to 1.     Is this correct? 
double ma;
   int count = 0;
   
   for(int i = 1; i < Bars - 1 && !_StopFlag; i++){
      ma = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE, i);
      
      if(Close[i] > ma)
         count++;
         
      if(Close[i] < ma) break;
      
   }
   Print("total bars = ", count);
 
J4str:
i think i got it by changing the i from 0 to 1.     Is this correct? 

You can set any value to i, your countdown starts from the first bar, if you visually look at the chart it is the second bar, in programs, any report starts from zero

 

something else that is giving me trouble.....  Im using icustom to call an indicator.  The indicator has 2 lines and im trying to use when those two lines to cross as one of my conditions for entry.  I though i had it, but when i started really looking,  it seems like its only working some of the time.  Almost at random of whether it would enter or not.    


double GreenLine = iCustom(NULL, 0,"icustom\\AbsoluteStrenghtHisto_v1.ex4", 2,1);
double GreenLine1 = iCustom(NULL, 0, "icustom\\AbsoluteStrenghtHisto_v1.ex4", 2, 2);

double RedLine = iCustom(NULL, 0, "icustom\\AbsoluteStrenghtHisto_v1.ex4",3, 1);
double RedLine1 = iCustom(NULL, 0, "icustom\\AbsoluteStrenghtHisto_v1.ex4",3, 2);

int PrimaryEntry;
   if (GreenLine>RedLine)
        {PrimaryEntry=1;}
   else {PrimaryEntry=0;}


int PrimaryEntry1;
   if (GreenLine1>RedLine1)
        {PrimaryEntry1=1;}
   else {PrimaryEntry1=0;}


int S3BullishEntry=0;
      if (PrimaryEntry==1)
      if (PrimaryEntry1==0)     
      S3BullishEntry =1;
      else
      {S3BullishEntry=0;}

int S3BearishEntry=0;
      if (PrimaryEntry1==1)      
      S3BearishEntry =1;
      else
      {S3BearishEntry=0;}




 
i think its a problem with icustom.  It seems to be giving the wrong info sometimes.    Im not sure how to solve this
 
double GreenLine = iCustom(NULL, 0,"icustom\\AbsoluteStrenghtHisto_v1.ex4", 2,1);
  1. You can drop the .ex4 as the terminal knows the type.
  2. Did you really create a icustom folder below <data folder>\Indicators and put that indicator there?
              Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 2014.02.03
  3. Does that indicator have three buffers?
 

1.  Ok, good to know

2. Yes.  Im trying to sort through a ton of indicators and im using that folder for ones currently working with and not the ones that can be deleted.  This EA im trying to use for a test bench for sorting through indicators and comparing them

3.  As far as I can tell, it has 4 buffers.   0 and 2 being the same, and  1 and 3 being the same. One being a histogram and one being a line of the same information.  You can tell the buffer from looking at the "colors" tab in the indicator properties, right?





i think i got that part working.  I don't know what the problem was, but i just started a new EA and started adding pieces one at a time and seeing if something would cause it to print 0 for those values.  Nothing ever did.  I wonder if i just needed to restart MT4 or something.   It was weird.