First EA- Simple example --phase 1

 

Hi,

I'm about to write my first EA and I was hoping for guidance and perhaps someone could point me towards a similar simple example. I hope to write a simple EA that just calculates and prints values for now and then build on it. As a training project I'm going to use Ichimoku... 

Phase 1.

Simple script that starts and has two 'private' functions calculateTekanSan, printValues. The pseudo code I have in my head is below. I think the structure is OK. I am unsure how to implement calculateTekanSan. Basically I want to calculate tekanSen value for two different time frames.

 

//global variables

tekanSenPeriod=9;
tekanSenFrequency1=DAILY;
tekanSenFrequency2=HOURLY;
//initialise
tekanSenFrequency1_value=0; 
tekanSenFrequency2_value=0;

//maybe this taken from the chart EA is attached to and not required?
currency=EURUSD

start()

{
 //improve with for loop at later stage
 calculateTekanSan(tekanSenPeriod, tekanSenFrequency1, currency) 
 calculateTekanSan(tekanSenPeriod, tekanSenFrequency2, currency)


 //print 
 printGlobalValues

} 
values
 
//-------------UNSURE-----------------
//--------------------------------------
void calculateTekanSan(tekanSanPeriod,  tekanSenFrequency, currency)
{

  int localtekansenValue=0 

  //   (tekanSanPeriod high+ tekanPeriodLow)/2 
  //   how ?????????  :)
 
  if tekanSenFrequency==DAILY
  {
   tekanSenFrequency1_value=localtekanSenValue;

 
  }
  else  
  {

   tekanSenFrequency2_value=localtekanSenValue;
  }
 

void printGlobalValues()
{

 //print tekanSenFrequency1_value and tekanSenFrequency2_value

} 

 

 Any help much appreciated..

 

What IDE are you using?  Usually they come with more up-to-date examples than what you have.  I'm using FXCM's MetaTrader 4 / MetaEditor and it has a wizard that lays out the basic EA template for you.

Here's a basic EA template:  You're mainly interested in OnInit and OnTick:

//+------------------------------------------------------------------+
//|                                                  MyExampleEA.mq4 |
//|                                  (Copyright 2015) <Your Company> |
//|                                         <Your web addr or email> |
//+------------------------------------------------------------------+
#property copyright "<You>"
#property link      "<Your Email>"
#property version   "1.00"
#property strict
//--- input parameters
input int      Input1=2;
input color    Input2=clrDarkOrchid;
input string   Input3="hello";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
 

Hi @enjoysmath

 

Many thanks for your reply updated template. I was just basing mine on what I had read on book.mql4.com. I am using (...eh looking at is a more accurate description!) the same IDE, I have MT4 from FXCM.

  1. Based on your template I assume my calculateTekanSen method is replaced with / superseded by  OnChartEvent?
  2. My main question was how to implement the formula, how do I/should I find/ he highest higest over last 9 periods and how do i find lowest low?. 

For example I don;t want to wait for 9 periods on the hourly chart before I have 9 highs and lows. I assume there is a way to read the previous 9 periods from when I start my EA.

 

Thanks again for the reply... 

 

Assuming that you mean the bars' highs and lows

Use iHighest() to find the index of the bar with the highest high

then use High[Highest index] to find the actual value 

Similarly use iLowest() and Low[] for the lowest. 

 
GumRai:

Assuming that you mean the bars' highs and lows

Use iHighest() to find the index of the bar with the highest high

then use High[Highest index] to find the actual value 

Similarly use iLowest() and Low[] for the lowest. 

Many thanks for the tip trying it out now....
 

Hi @Gumral, @enjoysmath

 

Many thanks to you both for your help. That works well...

 

One thing I noticed that is very important, especially in global variables, is you need to specify the type before you can use them in any way (like give them a value).  The types I used are examples only, they should be whatever your program needs them to be.

// not like this
//global variables

tekanSenPeriod=9;
tekanSenFrequency1=DAILY;
tekanSenFrequency2=HOURLY;
//initialise
tekanSenFrequency1_value=0; 
tekanSenFrequency2_value=0;

// like this
//global variables

int tekanSenPeriod=9;
int tekanSenFrequency1=DAILY;
int tekanSenFrequency2=HOURLY;
//initialise
float tekanSenFrequency1_value=0; 
float tekanSenFrequency2_value=0;
Reason: