Incorporating trader inputs in algorithmic systems – MQL4 for Complete Beginners Tutorial Part 14

Welcome back! In this tutorial we will take one of the strategies we discussed last time and turn into into an MQL4 program.

Also, another important aspect of today’s lesson is – incorporating trader input. We will work with extern variables, which will allow us to create parameters for the trader. In that way the trader can control the script directly from the MetaTrader 4 Forex Platform.

Extern variables will come in very handy when we start coding our expert advisor. So pay attention today! 🙂

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

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial14.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;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

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

 

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial14.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;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

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

 

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: