Okay, here's a breakdown of what this MetaTrader script does, explained in a way that's easy to understand, even if you don't know anything about programming:
Overall Goal:
This script is designed to automatically trade on the Forex market based on something called "divergence." Divergence, in trading, is when the price of an asset and a technical indicator move in opposite directions. The script aims to identify these divergence patterns and then automatically place buy or sell orders, manage those orders, and close them out based on pre-defined rules.
Key Components and Logic:
-
User Settings (External Variables):
- The script starts by defining a bunch of settings that you, the user, can adjust. These settings control how the script behaves. Examples include:
Lots
: How much of the currency to trade each time (the size of the trade).Slippage
: A small tolerance for price changes when placing an order. Forex prices move fast, so this allows for some wiggle room.Fast_Period
,Slow_Period
: These settings define how the "divergence" indicator is calculated. They control the sensitivity of the indicator.DVBuySell
,DVStayOut
: Thresholds for divergence values that trigger buy or sell signals.ProfitMade
,LossLimit
: How much profit you want to make or how much loss you're willing to tolerate on each trade.TrailStop
: A "trailing stop" that moves along with the price to lock in profits.PLBreakEven
: A "break even" level, that places a stop loss at the trade's open price, once the trade is in profit.StartHour
,StopHour
: The hours of the day when the script is allowed to trade.BasketProfit
,BasketLoss
: Close all active trades if the total profit of all open trades reach this level (or loss).UseDir
: use the direction of a fast moving average (MA) to validate tradesCloseOnCross
: Close any open trades when the fast MA crosses a slow MA.
- The script starts by defining a bunch of settings that you, the user, can adjust. These settings control how the script behaves. Examples include:
-
Initialization (init() function):
- When the script is first loaded, this part runs once.
- It clears away any old "drawings" the script might have made on the chart in a previous run.
-
Deinitialization (deinit() function):
- When the script is removed from the chart, this part runs once.
- It displays some simple statistics about the trades, such as the maximum number of orders placed, the highest equity, and the lowest equity.
-
Main Trading Logic (start() function):
- This is the heart of the script. It runs every time the price changes (every "tick") or when a new bar is formed on the chart.
- Detecting New Bars: The script keeps track of time to know when a new bar has formed on the price chart. When a new bar appears, it resets a "trade allowed" flag.
- Checking Open Orders: The script checks how many buy and sell orders it already has open for the current currency pair.
- Divergence Calculation:
- The script calls a function (more on that below) to calculate the "divergence" value. This function uses two moving averages (Fast and Slow) to determine the divergence.
- Based on whether the divergence value is above or below certain thresholds (set by
DVBuySell
andDVStayOut
), the script decides whether to trigger a buy or sell.
- Placing Orders:
- If the script decides to buy or sell, it calculates the stop-loss (the maximum loss it will allow) and take-profit (the target profit).
- It then sends an order to the broker to buy or sell the specified amount of currency (
Lots
).
- Managing Open Orders:
- The script constantly monitors any open trades.
- Break-Even: If a trade becomes profitable by a certain amount (
PLBreakEven
), the script moves the stop-loss to the trade's open price to eliminate the risk of losing money on that trade. - Trailing Stop: If the trade continues to be profitable, the script moves the stop-loss up (for a buy order) or down (for a sell order) to lock in profits as the price moves in a favorable direction.
- Closing Orders: The script checks if a trade has reached either the profit target (
ProfitMade
) or the loss limit (LossLimit
). If so, it closes the trade. Also if the basket profit or loss has been reached then all the trades are closed.
- Basket Management: The script constantly monitors the overall equity (total value of the account) and if that equity reaches a specified amount of profit or loss then all the trades are closed.
-
divergence()
Function:- This is a separate part of the script that calculates the divergence value.
- It calculates moving averages using the
iMA()
function. Moving averages smooth out price data to show the overall trend. - It compares the moving averages to determine if there's divergence between the price and the indicator.
- It sets the values of the
cross
,divup
anddivdn
variables based on the state of the moving averages.
-
CloseEverything()
Function:- This function closes all open orders (both winning and losing) and deletes any pending orders for the symbol being traded.
-
CloseMySymbol()
Function:- This function closes all open orders (both winning and losing) and deletes any pending orders only for the symbol being traded on.
In Simple Terms:
Imagine this script as a robot trader that:
- Watches the market for specific divergence patterns.
- Follows your rules for how much to trade, when to buy/sell, and how much risk to take.
- Adjusts its strategy while trades are open to protect profits.
- Closes trades when they hit your profit targets or loss limits.
- Can close ALL trades when a total profit or loss target is reached.
The key is that you need to configure the settings carefully to match your trading style and risk tolerance. The quality of these settings will be the main decider on if the script becomes profitable or not.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
/*
+--------+
|Divergence Trader
+--------+
*/
// variables declared here are GLOBAL in scope
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex"
// user input
extern double Lots=0.01; // how many lots to trade at a time
extern int Slippage=2; // how many pips of slippage can you tolorate
extern int Fast_Period=7;
extern int Fast_Price = PRICE_OPEN;
extern int Fast_Mode = MODE_EMA;
extern int Slow_Period=88;
extern int Slow_Price = PRICE_OPEN;
extern int Slow_Mode = MODE_EMA;
extern double DVBuySell=0.0011;
extern double DVStayOut=0.0079;
extern double ProfitMade=0; // how much money do you expect to make
extern double LossLimit=0; // how much loss can you tolorate
extern double TrailStop=9999; // trailing stop (999=no trailing stop)
extern int PLBreakEven=9999; // set break even when this many pips are made (999=off)
extern int StartHour=0; // your local time to start making trades
extern int StopHour=24; // your local time to stop making trades
extern int BasketProfit=10; // if equity reaches this level, close trades
extern int BasketLoss=9999; // if equity reaches this negative level, close trades
extern bool UseDir=true;
extern bool CloseOnCross=false;
bool cross=false;
bool divup=false;
bool divdn=false;
extern bool FileData=false;
// naming and numbering
int MagicNumber = 200601182020; // allows multiple experts to trade on same account
string TradeComment = "Divergence_4y_";
// Bar handling
datetime bartime=0; // used to determine when a bar has moved
int bartick=0; // number of times bars have moved
int objtick=0; // used to draw objects on the chart
int tickcount=0;
// Trade control
bool TradeAllowed=true; // used to manage trades
// Min/Max tracking
double maxOrders;
double maxEquity;
double minEquity;
//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled
int init()
{
int i;
string o;
//remove the old objects
for(i=0; i<Bars; i++)
{
o=DoubleToStr(i,0);
ObjectDelete("myx"+o);
ObjectDelete("myz"+o);
}
objtick=0;
ObjectDelete("Cmmt");
ObjectCreate("Cmmt", OBJ_TEXT, 0, Time[20], High[20]+(5*Point()));
ObjectSetText("Cmmt","Divergence=0.0020",10,"Arial",White);
Print("Init happened ",CurTime());
Comment(" ");
}
//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart
int deinit()
{
int i;
string o;
//remove the old objects
for(i=0; i<Bars; i++)
{
o=DoubleToStr(i,0);
ObjectDelete("myx"+o);
ObjectDelete("myz"+o);
}
objtick=0;
Print("MAX number of orders ",maxOrders);
Print("MAX equity ",maxEquity);
Print("MIN equity ",minEquity);
Print("DE-Init happened ",CurTime());
Comment(" ");
}
//+-----------+
//| Main |
//+-----------+
// Called EACH TICK and each Bar[]
int start()
{
double p=Point();
double spread=Ask-Bid;
int cnt=0;
int gle=0;
int OrdersPerSymbol=0;
int OrdersBUY=0;
int OrdersSELL=0;
int iFileHandle;
// stoploss and takeprofit and close control
double SL=0;
double TP=0;
double CurrentProfit=0;
double CurrentBasket=0;
// direction control
bool BUYme=false;
bool SELLme=false;
// Trade stuff
double diverge;
// bar counting
if(bartime!=Time[0])
{
bartime=Time[0];
bartick++;
objtick++;
TradeAllowed=true;
}
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
OrdersPerSymbol++;
if(OrderType()==OP_BUY) {OrdersBUY++;}
if(OrderType()==OP_SELL){OrdersSELL++;}
}
}
if(OrdersPerSymbol>maxOrders) maxOrders=OrdersPerSymbol;
//+-----------------------------+
//| Insert your indicator here |
//| And set either BUYme or |
//| SELLme true to place orders |
//+-----------------------------+
cross=false;
divup=false;
divdn=false;
diverge=divergence(Fast_Period,Slow_Period,Fast_Price,Slow_Price,Fast_Mode,Slow_Mode,0);
ObjectDelete("Cmmt");
ObjectCreate("Cmmt", OBJ_TEXT, 0, Time[10], High[0]+(10*p));
ObjectSetText("Cmmt","Divergence="+DoubleToStr(diverge,4),10,"Arial",White);
if( diverge>=DVBuySell && diverge<=DVStayOut ) BUYme=true;
if( diverge<=(DVBuySell*(-1)) && diverge>=(DVStayOut*(-1)) ) SELLme=true;
//if( diverge>=DVBuySell ) BUYme=true;
//if( diverge<=(DVBuySell*(-1)) ) SELLme=true;
if(CloseOnCross && cross) CloseEverything();
if(FileData)
{
tickcount++;
iFileHandle = FileOpen("iDivergence", FILE_CSV|FILE_READ|FILE_WRITE, ",");
FileSeek(iFileHandle, 0, SEEK_END);
FileWrite(iFileHandle, bartick, " ", tickcount, " ", diverge);
FileFlush(iFileHandle);
FileClose(iFileHandle);
}
//+------------+
//| End Insert |
//+------------+
//ENTRY LONG (buy, Ask)
if(TradeAllowed && BUYme && divup)
{
//Ask(buy, long)
if(LossLimit ==0) SL=0; else SL=Ask-((LossLimit+7)*Point() );
if(ProfitMade==0) TP=0; else TP=Ask+((ProfitMade+7)*Point() );
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,TradeComment,MagicNumber,White);
gle=GetLastError();
if(gle==0)
{
Print("BUY Ask=",Ask," bartick=",bartick);
ObjectCreate("myx"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], High[0]+(5*p));
ObjectSetText("myx"+DoubleToStr(objtick,0),"B",15,"Arial",Red);
bartick=0;
TradeAllowed=false;
}
else
{
Print("-----ERROR----- BUY Ask=",Ask," error=",gle," bartick=",bartick);
}
}
//ENTRY SHORT (sell, Bid)
if(TradeAllowed && SELLme && divdn)
{
//Bid (sell, short)
if(LossLimit ==0) SL=0; else SL=Bid+((LossLimit+7)*Point() );
if(ProfitMade==0) TP=0; else TP=Bid-((ProfitMade+7)*Point() );
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,TradeComment,MagicNumber,Red);
gle=GetLastError();
if(gle==0)
{
Print("SELL Bid=",Bid," bartick=",bartick);
ObjectCreate("myx"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], High[0]+(5*p));
ObjectSetText("myx"+DoubleToStr(objtick,0),"S",15,"Arial",Red);
bartick=0;
TradeAllowed=false;
}
else
{
Print("-----ERROR----- SELL Bid=",Bid," error=",gle," bartick=",bartick);
}
}
//Basket profit or loss
CurrentBasket=AccountEquity()-AccountBalance();
if(CurrentBasket>maxEquity) maxEquity=CurrentBasket;
if(CurrentBasket<minEquity) minEquity=CurrentBasket;
// actual basket closure
if( CurrentBasket>=BasketProfit || CurrentBasket<=(BasketLoss*(-1)) )
{
CloseEverything();
}
// CLOSE order if profit target made
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber )
{
if(OrderType()==OP_BUY)
{
CurrentProfit=Bid-OrderOpenPrice() ;
// modify for break even
if (CurrentProfit >= PLBreakEven*p && OrderOpenPrice()>OrderStopLoss())
{
SL=OrderOpenPrice()+(spread*2);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
Print("MODIFY BREAKEVEN BUY Bid=",Bid," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"BE",15,"Arial",White);
}
else
{
Print("-----ERROR----- MODIFY BREAKEVEN BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
// modify for trailing stop
if(CurrentProfit >= TrailStop*p )
{
SL=Bid-(TrailStop*p);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
Print ("MODIFY TRAILSTOP BUY StopLoss=",SL," bartick=",bartick,"OrderTicket=",OrderTicket()," CurrProfit=",CurrentProfit);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"TS",15,"Arial",White);
}
else
{
Print("-----ERROR----- MODIFY TRAILSTOP BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
// did we make our desired BUY profit
// or did we hit the BUY LossLimit
if((ProfitMade>0 && CurrentProfit>=(ProfitMade*p)) || (LossLimit>0 && CurrentProfit<=((LossLimit*(-1))*p)) )
{
OrderClose(OrderTicket(),Lots,Bid,Slippage,White);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE BUY Bid=",Bid," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"C",15,"Arial",White);
}
else
{
Print("-----ERROR----- CLOSE BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
} // if BUY
if(OrderType()==OP_SELL)
{
CurrentProfit=OrderOpenPrice()-Ask;
// modify for break even
if (CurrentProfit >= PLBreakEven*p && OrderOpenPrice()<OrderStopLoss())
{
SL=OrderOpenPrice()-(spread*2);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
Print("MODIFY BREAKEVEN SELL Ask=",Ask," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"BE",15,"Arial",Red);
}
else
{
Print("-----ERROR----- MODIFY BREAKEVEN SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
// modify for trailing stop
if(CurrentProfit >= TrailStop*p)
{
SL=Ask+(TrailStop*p);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
Print ("MODIFY TRAILSTOP SELL StopLoss=",SL," bartick=",bartick,"OrderTicket=",OrderTicket()," CurrProfit=",CurrentProfit);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"TS",15,"Arial",Red);
}
else
{
Print("-----ERROR----- MODIFY TRAILSTOP SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
// did we make our desired SELL profit?
if( (ProfitMade>0 && CurrentProfit>=(ProfitMade*p)) || (LossLimit>0 && CurrentProfit<=((LossLimit*(-1))*p)) )
{
OrderClose(OrderTicket(),Lots,Ask,Slippage,Red);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE SELL Ask=",Ask," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"C",15,"Arial",Red);
}
else
{
Print("-----ERROR----- CLOSE SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
} //if SELL
} // if(OrderSymbol)
} // for
} // start()
//+-----------------+
//| CloseEverything |
//+-----------------+
// Closes all OPEN and PENDING orders
int CloseEverything()
{
double myAsk;
double myBid;
int myTkt;
double myLot;
int myTyp;
int i;
bool result = false;
for(i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);
myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
switch( myTyp )
{
//Close opened long positions
case OP_BUY :result = OrderClose(myTkt, myLot, myBid, Slippage, Red);
break;
//Close opened short positions
case OP_SELL :result = OrderClose(myTkt, myLot, myAsk, Slippage, Red);
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT:
case OP_SELLSTOP :result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Print("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
Sleep(1000);
} //for
} // closeeverything
//+----------------+
//| CloseMySymbol |
//+----------------+
// Closes all OPEN and PENDING orders
// only on the attached symbol
int CloseMySymbol()
{
double myAsk;
double myBid;
int myTkt;
double myLot;
int myTyp;
int i;
bool result = false;
for(i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if(OrderSymbol()==Symbol())
{
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);
myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
switch( myTyp )
{
//Close opened long positions
case OP_BUY :result = OrderClose(myTkt, myLot, myBid, Slippage, Red);
break;
//Close opened short positions
case OP_SELL :result = OrderClose(myTkt, myLot, myAsk, Slippage, Red);
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT:
case OP_SELLSTOP :result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Print("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
Sleep(1000);
} //for
} // closemysymbol
double divergence(int F_Period, int S_Period, int F_Price, int S_Price, int F_Mode, int S_Mode, int mypos)
{
int i;
double maF1, maF2, maS1, maS2;
double dv1, dv2;
maF1=iMA(Symbol(),0,F_Period,0,F_Mode,F_Price,mypos);
maS1=iMA(Symbol(),0,S_Period,0,S_Mode,S_Price,mypos);
dv1=(maF1-maS1);
maF2=iMA(Symbol(),0,F_Period,0,F_Mode,F_Price,mypos+1);
maS2=iMA(Symbol(),0,S_Period,0,S_Mode,S_Price,mypos+1);
dv2=((maF1-maS1)-(maF2-maS2));
// either cross
if(maF1<maS1 && maF2>maS2) cross=true;
if(maF1>maS1 && maF2<maS2) cross=true;
// Fast MA direction
if(maF2>maF1) divup=true;
if(maF2<maF1) divdn=true;
if(!UseDir) { divup=true; divdn=true; }
return(dv1-dv2);
}
Comments