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: http://45.55.161.186/learn-mql4
Code for 4-digit brokers
//+------------------------------------------------------------------+
//| Tutorial17.mq4 |
//| Copyright 2014, ForexBoat |
//| http://45.55.161.186 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://45.55.161.186"
#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://45.55.161.186 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://45.55.161.186"
#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));
}
}
//+------------------------------------------------------------------+
September 15, 2014 at 9:22 am, tablet8 said:
hello
where I can find your premium course?
give me the link please.
I hope it not be expensive too much 🙂
September 15, 2014 at 9:54 am, Kirill said:
Hi Tablet8,
Thanks for your inquiry. I am still working on the Premium course at this stage and if all goes as planned it should be ready sometime in October this year. Let me know if I can help you out with any minor questions in the meantime.
Regards,
Kirill