Failsafe programming techniques – MQL4 for Complete Beginners Tutorial Part 17

Have you ever had problems with computer software? Perhaps a program crashed while you were working in it, and you lost a lot of information or had to redo work? Well, can you imagine if that happens to an FX-Robot? If a program (script / expert advisor) crashes while trading on the Forex market, the consequences can be catastrophic! That’s why in today’s tutorial we will learn how to create fail-safe blocks of code which will protect the trader using our programs in the event of a malfunction. Today we will discuss the minimal error-handling that you would want to add to your programs. For more on this topic, check out my premium course Algorithmic Trading For Success!

Enrol in the full course here: https://www.forexboat.com/learn-mql4

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial17.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

TakeProfitLevel = Bid + TakeProfit*Point; //0.0001
StopLossLevel = Bid - StopLoss*Point;

/*
OrderSend can return:
ticket #; OR
-1 (if OrderSend failed)
*/

int ticket;
ticket = OrderSend("EURUSD", OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, "My 1st Order!");

if(ticket < 0)
{
Alert("Error!");
}
else
{
Alert("Your ticket # is: " + string(ticket));
}

}
//+------------------------------------------------------------------+

 

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial17.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

//here we are assuming that the TakeProfit and StopLoss are entered in Pips
TakeProfitLevel = Bid + TakeProfit*Point*10; //0.00001 * 10 = 0.0001
StopLossLevel = Bid - StopLoss*Point*10;

/*
OrderSend can return:
ticket #; OR
-1 (if OrderSend failed)
*/

int ticket;
ticket = OrderSend("EURUSD", OP_BUY, 1.0, Ask, 10*10, StopLossLevel, TakeProfitLevel, "My 1st Order!"); //notice that slippage also has to be multiplied by 10

if(ticket < 0)
{
Alert("Error!");
}
else
{
Alert("Your ticket # is: " + string(ticket));
}

}
//+------------------------------------------------------------------+

What are you waiting for?

START LEARNING FOREX TODAY!

share This:
Team ForexBoat
Team ForexBoat

Our goal is to share this passion with others and guide newbies to avoid costly mistakes. Today we want to share all our knowledge and insights, so you can take your trading skills to the next level.

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: