Download MetaTrader 5

EA - seeking help writing first robot

To add comments, please log in or register
davmartin
34
davmartin  

Hi everyone, 


Practising writing EA's and want to build a simple robot and see how it works. 


I am trying to write a rule: If upper bollinger band is hit sell fixed position size and vice versa. 


My reasoning was that I should compare the current price level of the upper band with the current tick and if this is equal place a market order. 

I found that I could use MqlTick to retrieve current price data, but I don't understand how I could retrieve the current price level of the upper (or lower) band. 


Any suggestions? 


Appreciate the help! 

Thanks, 

Dave

Drazen Penic
2703
Drazen Penic  

Read iBands() function documentation - pay attention to the "mode" parameter. 

iBands - Technical Indicators - MQL4 Reference
iBands - Technical Indicators - MQL4 Reference
  • docs.mql4.com
iBands - Technical Indicators - MQL4 Reference
davmartin
34
davmartin  
Drazen Penic:

Read iBands() function documentation - pay attention to the "mode" parameter. 


Hi Drazen,


Appreciate the quick response!

Taken your suggestion into account, the code line I came up with is: 

if (iBands(NULL,0,20,2,0,PRICE_HIGH,MODE_UPPER,0) == MqlTick) Order = SIGNAL_SELL;


But how can I write MqlTick than as the current tick ask price in this case? 


Dave

whroeder1
15925
whroeder1  
davmartin:
if (iBands(NULL,0,20,2,0,PRICE_HIGH,MODE_UPPER,0) == MqlTick) Order = SIGNAL_SELL;
Doubles are rarely equal. The == operand. - MQL4 forum
Drazen Penic
2703
Drazen Penic  
davmartin:

Hi Drazen,


Appreciate the quick response!

Taken your suggestion into account, the code line I came up with is: 

if (iBands(NULL,0,20,2,0,PRICE_HIGH,MODE_UPPER,0) == MqlTick) Order = SIGNAL_SELL;


But how can I write MqlTick than as the current tick ask price in this case? 


Dave


MqlTick is a struct name. You can't compare anything with MqlTick.

Read the documentation on MqlTick, you'll find a sample code showing how to use it.

Read about price comparison problems - follow the link whroeder1 provided.

Structure for Current Prices - Data Structures - Standard Constants, Enumerations and Structures - MQL4 Reference
Structure for Current Prices - Data Structures - Standard Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Structure for Current Prices - Data Structures - Standard Constants, Enumerations and Structures - MQL4 Reference
davmartin
34
davmartin  
Drazen Penic:

MqlTick is a struct name. You can't compare anything with MqlTick.

Read the documentation on MqlTick, you'll find a sample code showing how to use it.

Read about price comparison problems - follow the link whroeder1 provided.


Hi Drazen,

I will read through the documentation, thanks.


But in the situation I am in do you suggest that I use MqlTick to compare to the upper or lower band?

Or should I use for example double bid (+ask) (with Refreshrates() in the beginning of the code)?


I assume there are many ways to achieve the same outcome, but what in your mind is the simplest? 

davmartin
34
davmartin  
whroeder1:
Doubles are rarely equal. The == operand. - MQL4 forum

Great lesson! Thanks. 

So if I am correct you suggest using the MathAbs function when it's apparent that both values will not (seldom) be exactly the same? 

Drazen Penic
2703
Drazen Penic  
davmartin:

Hi Drazen,

I will read through the documentation, thanks.


But in the situation I am in do you suggest that I use MqlTick to compare to the upper or lower band?

Or should I use for example double bid (+ask) (with Refreshrates() in the beginning of the code)?


I assume there are many ways to achieve the same outcome, but what in your mind is the simplest? 


Use whichever you like the most. 

You can access current ask value through the Ask variable and MqlTick struct.

A bid value can be accessed through Bid variable, MqlTick, Close[0], iClose function.

Then there is MarketInfo() function which can access both bid and ask. 

And I probably forgot some additional way.

Common is that all those access methods return the same value. Functions have additional parameters which allow you to access values for different symbols if needed.

Bid and Ask variables are probably the simplest in your case.


You do not need to call RefreshRates() initially. You should call it if you do some "lengthy" processing or if you execute multiple trading functions or repeat trading functions after error. 

davmartin
34
davmartin  
Drazen Penic:

Use whichever you like the most. 

You can access current ask value through the Ask variable and MqlTick struct.

A bid value can be accessed through Bid variable, MqlTick, Close[0], iClose function.

Then there is MarketInfo() function which can access both bid and ask. 

And I probably forgot some additional way.

Common is that all those access methods return the same value. Functions have additional parameters which allow you to access values for different symbols if needed.

Bid and Ask variables are probably the simplest in your case.


You do not need to call RefreshRates() initially. You should call it if you do some "lengthy" processing or if you execute multiple trading functions or repeat trading functions after error. 


Hi Drazen,


Clearly explained, thanks!! 


All the best,

Dave

To add comments, please log in or register