Rookie Code - What am I doing wrong?

 

I'm new to programming.  MQL4 is my first coding language (and only for that matter).  I'm trying to write a basic code that sends an alert when either a shooting star or a hammer is formed.  I have two questions after putting this through the strategy tester on MT4.  First, no alerts have been sent to I just want to make sure, alerts still get sent on the strategy tester, right?  Assuming they do then something must just be missing from my code.  The second question is, once is able to send the alert, is it going to send the alert on every single tick over and over?  Is there something I'm supposed to write in there to make sure it only sends the alert once?

Anyways, here's my code.  Thank you in advance. The code is simply meant to send an alert when the long wick is three times the length of the body or more and the short wick is 1/5 the length of the long wick or less. It's also worth noting, I've only written scripts before, so I'm not even sure I'm writing this correctly for an EA.  Your help is greatly appreciated!

#property strict
#include <CustomFunctions01.mqh>
#property show_inputs

// Adding all the global variables:
//-----------------------------------------------

// Parts of the candle
double topWick;
double body;
double bottomWick;

// Is there a pattern
bool bIsShootingStar = FALSE;
bool bIsHammer = FALSE;

// Inputs 
input double tailSize = 3;  //multiples of body, refers to the long wick on the pattern
input double wickSize = 0.2;  //multiples of tail, refers to the short wick on the pattern

int OnInit()
{
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{ 
}

void OnTick()
{

   // Is the candle bullish or not? 
   if(Open[1]<=Close[1])  
   {
   topWick = High[1]-Close[1];
   body = Close[1]-Open[1];
   bottomWick = Open[1]-Low[1];
   }
   else
   {
   topWick = High[1]-Open[1];
   body = Open[1]-Close[1];
   bottomWick = Close[1]-Low[1];
   }
   
   // Is it a shooting star?
   if (topWick >= body * tailSize && bottomWick <= topWick * wickSize)
   {
   bIsShootingStar = TRUE;
   Alert("Shooting Star Found");
   }
   
   // Is it a hammer? 
   if (bottomWick >= body * tailSize && topWick <= topWick * wickSize)
   {
   bIsHammer = TRUE;
   Alert("Hammer Found");
   } 
   
}

 

In the strategy tester the alerts are printed in the journal, so you will need to check there.

The checks that you are doing should only be executed when a new bar opens or you will get alerts every tick.

  static datetime barTime=0;
  datetime thisBarTime=Time[0];
  if(barTime!=thisBarTime)
   {
    barTime=thisBarTime;
    //Code to be executed only once when a new bar opens
   }
 

Please don't start a new topic concerning the same code.

I will copy your other post below and delete your new topic.

I note that you have completely ignored my advice and still calculating every tick!

Except for the stochastic which for some reason you are calculating globalscope instead of in OnTick().

dasilvja:

First off, I want to point out that I'm new to coding with very limited formal training (udemy only). I wrote what I believe is a straightforward code but I'm not getting the results I expected. I wrote an EA to locate shooting stars/hammers based on some simple criteria like if the top wick is 3 x the size of the body and the bottom wick is less than 1/5 of the top wick than it's a shooting star. It's just a guideline for now. I can tweak the proportions later. I set the EA to draw a vertical line before this candle wherever it's identified. It seemed to do that much ok. Then I added a stochastic value and told the EA to only say that it's a shooting star when stochastic is over 80 and only say that it's a hammer when stochastic is below 20 but for whatever reason these instructions seem to be completely ignored. I don't understand what I'm doing wrong. I would really appreciate your help. Thanks in advance!

#property strict
#include <CustomFunctions01.mqh>
#property show_inputs

// Adding all the global variables:
//-----------------------------------------------

// Parts of the candle
double topWick;
double body;
double bottomWick;

double stochValue = iStochastic(_Symbol,_Period,14,3,3,MODE_SMA,0,MODE_SIGNAL,1);

// Is there a pattern
bool bIsShootingStar = FALSE;
bool bIsHammer = FALSE;

// Inputs 
input double tailSize = 3;  //multiples of body, refers to the long wick on the pattern
input double wickSize = 0.2;  //multiples of tail, refers to the short wick on the pattern
input double stochUpperLevel = 80; //Stochastic Upper Level
input double stochLowerLevel = 20; //Stochastic Lower Level


int OnInit()
{
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{ 
}

void OnTick()
{

   // Is the candle bullish or not? 
   if(Open[1]<=Close[1])  
   {
   topWick = High[1]-Close[1];
   body = Close[1]-Open[1];
   bottomWick = Open[1]-Low[1];
   }
   else
   {
   topWick = High[1]-Open[1];
   body = Open[1]-Close[1];
   bottomWick = Close[1]-Low[1];
   }
   
   // Is it a shooting star?
   if (topWick >= body * tailSize 
   && bottomWick <= topWick * wickSize
   && stochValue >= stochUpperLevel)
      {
      ObjectCreate(NULL,TimeCurrent(),OBJ_VLINE,0,Time[2],0);
      Alert("Shooting Star Found");
      bIsShootingStar = TRUE;  
      }
   
   // Is it a hammer? 
   if (bottomWick >= body * tailSize 
   && topWick <= topWick * wickSize
   && stochValue <= stochLowerLevel)
      {
      ObjectCreate(NULL,TimeCurrent(),OBJ_VLINE,0,Time[2],0);
      Alert("Hammer Found");
      bIsHammer = TRUE;
      } 

}
 
Keith Watford:

Please don't start a new topic concerning the same code.

I will copy your other post below and delete your new topic.

I note that you have completely ignored my advice and still calculating every tick!

Except for the stochastic which for some reason you are calculating globalscope instead of in OnTick().

 I'm not ignoring your advice. I just haven't got to it yet. I'm very slow. Honestly, I don't completely understand how to write in your code so I figured I'd tackle it when I got to that point (assuming I'm not there yet?) Sorry for reposting the same code. My new inquiry is specifically about the stochastics so I thought it was ok.

Of course! Stochastics has to be in the OnTick area. Seems so obvious. Please forgive me. I really don't mean to break the posting rules or ask foolish questions. I'm doing my best, I'm just lost with this stuff 95% of the time. I keep telling myself just to stay the course and eventually it'll get easier. 

I appreciate your help. Last time too. I have a tutor that takes forever to respond and I was going to have him help me to understand the code you provided. 

Thanks again for everything! 


Reason: