Hi forex trader,
In this blog I want to show you how to use TDO buffers to build a simple MT4 EA. If you want to know more about TDO then here are some important links that could be helpful for you:
Important Information
- TDO User's Manual: https://www.mql5.com/en/blogs/post/717970
- TDO F.A.Q: https://www.mql5.com/en/blogs/post/722015
- TDO video library: https://www.mql5.com/en/blogs/post/722018
The most important buffer is Buffer number seven. It represents change in trend direction.
.
.
Here is a code example about how to build an expert advisor depending on MQL4 iCustom() function.//+------------------------------------------------------------------+
//| True Direction Oscillator EA Example v2.0.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com/en/users/bermaui314"
#property version "2.00"
#property strict
#property description "TDO Important Output Buffers:"
#property description "=============================="
#property description "1)-Buffer [ 0 ] = TDO Bullish Smart Shadow."
#property description "2)-Buffer [ 1 ] = TDO Bearish Smart Shadow."
#property description "3)-Buffer [ 2 ] = TDO Line."
#property description "4)-Buffer [ 3 ] = TDO Trend."
sinput string Trading_Setup; // Trading Setup
extern int MagicNumber = 1234567890; // Magic Number
extern int TakeProfit = 500; // Take Profit (points)
extern int StopLoss = 250; // Stop Loss (points)
extern double FirstLots = 0.01; // Lot Size
sinput string MASR_Settings; // 1) >>>>>>>>>> MASR INPUTS
sinput string indAdress = "Market\\True Direction Oscillator"; // Indicator Path & Name :
extern int TDO_Bars_P1 = 10; // First TDO Period [Min =1]
//---Global Variables
int Ticket1;
int t1,t2;
datetime timeprev = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---Check trading at candle open only
if(timeprev == Time[0]) { return; }
timeprev = Time[0];
//---TDO
double TDOTernd = TDOCustom(1);
double TDOTerndPerv = TDOCustom(2);
//---Buy Order
if(MyRealOrdersTotal(MagicNumber) == 0){
if(TDOTernd > TDOTerndPerv) { OpenBUYOrder(MagicNumber);}}
//---Sell Order
if(MyRealOrdersTotal(MagicNumber) == 0){
if(TDOTernd < TDOTerndPerv) { OpenSELLOrder(MagicNumber);}}
}
//+------------------------------------------------------------------+
//| Custom functions. |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| The number of current Market orders only. |
//+------------------------------------------------------------------+
int MyRealOrdersTotal(int Magic)
{
int c = 0;
int total = OrdersTotal();
t1 = -1;
t2 = -1;
for(int cnt = 0 ; cnt < total ; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL))
{
if(t1 == -1) { t1 = OrderTicket(); }
else
{
if(t2 == -1) { t2 = OrderTicket(); }
}
c++;
}
}
return(c);
}
//+------------------------------------------------------------------+
//| Open a Buy Order Function. |
//+------------------------------------------------------------------+
void OpenBUYOrder(int Magic)
{
double en,sl,tp;
int slippage = 30;
en = Ask; //---Enter of order
sl = Ask - (StopLoss * Point); //---Stop Loss
tp = Ask + (TakeProfit * Point); //---Take Profit
if(FirstLots > 0)
{Ticket1 = OrderSend(Symbol(),OP_BUY, FirstLots, en, slippage, sl, tp, "EA Comment", Magic, 0, clrGreen);}
else
{Alert("Buying Error "," Err = ",GetLastError()," Price = ",Ask," Lots = ",FirstLots);}
}
//+------------------------------------------------------------------+
//| Open a Sell Order Function. |
//+------------------------------------------------------------------+
void OpenSELLOrder(int Magic)
{
double en,sl,tp;
int slippage = 30;
en = Bid; //---Enter of order
sl = Bid + (StopLoss * Point); //---Stop Loss
tp = Bid - (TakeProfit * Point); //---Take Profit
if(FirstLots > 0)
{Ticket1 = OrderSend(Symbol(),OP_SELL, FirstLots, en, slippage, sl, tp, "EA Comment", Magic, 0, clrRed);}
else
{Alert("Selling Error "," Err = ",GetLastError()," Price = ",Ask," Lots = ",FirstLots);}
}
//+==================================================================+
//| TDO iCustom Function. |
//+==================================================================+
double TDOCustom(int shift)
{
//+------------------------------------------------------------------+
//| Indicator Options |
//+------------------------------------------------------------------+
// Colors List Options
enum cL
{
aAlex, // 1. Alex
bLotus, // 2. Lotus
cNile, // 3. Nile
dCairo, // 4. Cairo
eDoNothing, // 5. Do Nothing
};
// Arrows & Vertical Lines
enum AW
{
aShowArrow, // Show Arrows Only
bShowArrowsVL, // Show Arrows & Vertical Lines
cShowVL, // Show Vertical Lines Only
dHideAll // Hide All
};
//+------------------------------------------------------------------+
//| iCustom Function |
//+------------------------------------------------------------------+
double TDO = iCustom(Symbol(), // Symbol
Period(), // Period
indAdress, // Indicator Path and name
">>>>> TDO SETTINGS", // 1)) TDO SETTINGS
TDO_Bars_P1, // First TDO Period [Min =1]
">>>>> TDO COLORS LIST & BUTTONS", // 2)) TDO COLORS LIST & BUTTONS
eDoNothing, // Chart Colors List:
"A", // Panel Appear [Shift + Letter]
"D", // Panel Disappear [Shift + Letter]
10, // Button Font Size:
false, // Chart on Foreground:
">>>>> TDO ARROWS & ALERT", // 3)) TDO ARROWS & ALERT
aShowArrow, // Show Arrows & Vertical Lines
1, // TDO Arrow Size
false, // Alert on New Arrow
false, // Send eMail on New Arrow
false, // Send Notification on New Arrow
">>>>> TDO PANEL ALERT", // 3)))--->>>>>>>>>>>>>>>>>>>>>>>>>>
false, // Alert if TDO Panel is in-line
false, // Send eMail if TDO Panel is in-line
false, // Send Notification if TDO Panel is in-line
3, // Calculation Buffer (3)
shift); // i Counter
return(TDO);
}
This code is just an example about how to use the indicators buffer in building an expert advisor and It was not written to test the indicator ability to trade on real accounts.
I hope True Direction Oscillator (TDO) to be a good trading tool for you. If you have any question send me a message and I will answer as soon as I am online.
Best regards
Muhammad Al Bermaui, CMT