Okay, here's a breakdown of what this MetaTrader script does, explained in a way that doesn't require any programming knowledge:
Overall Goal:
This script is designed to automatically place buy and sell orders in the Forex market based on signals from a specific technical indicator called the "Traders Dynamic Index" (TDI). Think of it as an automated trading assistant that tries to identify potentially profitable trading opportunities.
Here's how it works step-by-step:
-
Setup and Configuration:
- The script starts by reading in some settings that you, the user, can customize. These settings control how the TDI indicator is calculated and how the script should behave. Key settings include:
Lots
: The size of the trades it will place (e.g., 0.01 means a small trade).RSI_Period
,Volatility_Band
, etc.: These are settings that fine-tune the TDI indicator itself. The TDI indicator consists of the RSI combined with bands to identify the volatility of the market, which in this case is named Volatility band.slippage
: A buffer to allow for slight price changes when placing an order.magic
: a unique identification number so the script only manages its own trades.
- The script starts by reading in some settings that you, the user, can customize. These settings control how the TDI indicator is calculated and how the script should behave. Key settings include:
-
Checking the Environment:
- The script makes sure it's allowed to trade and that it's not trying to do things too quickly. It only runs once per price update to avoid overloading the system.
-
Reading the TDI Indicator:
-
The heart of the script is using the Traders Dynamic Index. This indicator generates several lines and levels based on historical price data and calculations. The script retrieves these values:
RSI
: The main RSI line of the TDI.Signal
: a Signal Line of the TDI to check crossover events.HighLine
,LowLine
: These are upper and lower bands or levels that the TDI indicator uses.BLine
: another line from the TDI indicator to compare against the RSI line.
-
The script retrieves the current value of these lines and also the value of the previous time period using RSI1,Signal1, HighLine1, LowLine1, BLine1.
-
-
Generating Buy/Sell Signals:
-
The script compares the current values of the TDI lines to the previous ones and to each other. It is looking for specific patterns where the RSI line crosses above or below other lines of the TDI.
-
It then assigns a score to either a "Buy" or "Sell" indicator depending on the crosses.
-
-
Managing Existing Trades (if any):
- The script checks if it already has any open trades. If the "Buy" or "Sell" scores reach a certain level (5), it will close the existing trade.
-
Placing New Trades:
- If the "Buy" score reaches 5 and there are no open trades, the script will automatically place a "Buy" order.
- If the "Sell" score reaches 5 and there are no open trades, the script will automatically place a "Sell" order.
- After placing a trade, the "Buy" and "Sell" scores are reset to zero.
In Simple Terms:
The script watches a special indicator (TDI). When the TDI lines create certain crossing patterns that suggest a potential upward or downward price movement, the script either buys or sells automatically. It also closes any existing trades if the indicator suggests the market is moving in the opposite direction.
Important Note: This script is based on a specific trading strategy (TDI indicator) and relies on certain assumptions about market behavior. It's crucial to understand the TDI indicator and the risks involved before using this script to trade with real money.
//+------------------------------------------------------------------+
//| TDI.mq4 |
//| Copyright © 2009, WalkMan. |
//| |
//+------------------------------------------------------------------+
//| Price & Line Type settings: |
//| RSI Price settings |
//| 0 = Close price [DEFAULT] |
//| 1 = Open price. |
//| 2 = High price. |
//| 3 = Low price. |
//| 4 = Median price, (high+low)/2. |
//| 5 = Typical price, (high+low+close)/3. |
//| 6 = Weighted close price, (high+low+close+close)/4. |
//| |
//| RSI Price Line & Signal Line Type settings |
//| 0 = Simple moving average [DEFAULT] |
//| 1 = Exponential moving average |
//| 2 = Smoothed moving average |
//| 3 = Linear weighted moving average |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, WalkMan."
#property link ""
bool Work=true; // Ýêñïåðò áóäåò ðàáîòàòü.
extern double Lots = 0.01;
extern int RSI_Period = 13; //8-25
extern int RSI_Price = 0; //0-6
extern int Volatility_Band = 34; //20-40
extern int RSI_Price_Line = 2;
extern int RSI_Price_Type = 0; //0-3
extern int Trade_Signal_Line = 7;
extern int Trade_Signal_Type = 0; //0-3
extern int slippage = 0;
extern int magic = 123456;
static int prevtime = 0;
static int Sell = 0;
static int Buy = 0;
int init()
{
prevtime = Time[0];
return(0);
}
int deinit()
{
return(0);
}
int start()
{
if (! IsTradeAllowed()) {
return(0);
}
if(Work==false) // Êðèòè÷åñêàÿ îøèáêà
{
Alert("Êðèòè÷åñêàÿ îøèáêà. Ýêñïåðò íå ðàáîòàåò.");
return; // Âûõîä èç start()
}
if (Time[0] == prevtime) {
return(0);
}
prevtime = Time[0];
/*
Print("Buy =", Buy);
Print("Sell =", Sell);
*/
double RSI = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 4, 0);
double RSI1 = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 4, 1);
double HighLine = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 1, 0);
double HighLine1 = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 1, 1);
double LowLine = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 3, 0);
double LowLine1 = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 3, 1);
double BLine = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 2, 0);
double BLine1 = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 2, 1);
double Signal = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 5, 0);
double Signal1 = iCustom(NULL, 0, "Traders_Dynamic_Index", RSI_Period, RSI_Price, Volatility_Band, RSI_Price_Line, RSI_Price_Type, Trade_Signal_Line, Trade_Signal_Type, 5, 1);
//
if(RSI1>HighLine1 && RSI<HighLine && OrderType()!=OP_SELL && OrdersTotal()<=1)
{
Sell=Sell+3;
}
if(RSI1<LowLine1 && RSI>LowLine && OrderType()!=OP_BUY && OrdersTotal()<=1)
{
Buy=Buy+3;
}
if(RSI1<Signal1 && RSI>Signal && OrderType()!=OP_BUY && OrdersTotal()<=1)
{
Buy=Buy+2;
}
if(RSI1>Signal1 && RSI<Signal && OrderType()!=OP_SELL && OrdersTotal()<=1)
{
Sell=Sell+2;
}
if(RSI1<BLine1 && RSI>BLine && OrderType()!=OP_BUY && OrdersTotal()<=1)
{
Buy=Buy+3;
}
if(RSI1>BLine1 && RSI<BLine && OrderType()!=OP_SELL && OrdersTotal()<=1)
{
Sell=Sell+3;
}
int ticket = -1;
int total = OrdersTotal();
for (int i = total - 1; i >= 0; i--) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == magic)) {
int prevticket = OrderTicket();
if (Buy >= 5)
{
OrderClose(prevticket,OrderLots(),Ask,3,Red);
}
if (Sell>= 5)
{
OrderClose(prevticket,OrderLots(),Bid,3,Blue);
}
return(0);
}
}
if (Sell >= 5 && OrdersTotal()==0)
{
ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,0,0, WindowExpertName(), magic, 0, Red);
Sell=0;
}
if (Buy >= 5 && OrdersTotal()==0)
{
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,0,0,WindowExpertName(), magic, 0, Blue);
Buy=0;
}
return(0);
}
Comments