This script is designed to automatically trade on the Forex market based on "divergence," which it calculates using moving averages. Here's a breakdown of how it works:
1. Initial Setup:
-
User-Defined Settings: The script starts by letting you, the user, define various parameters. These include things like:
- Lot Size: How much of a currency to buy or sell in each trade.
- Slippage: The acceptable difference between the requested price and the actual price at which the trade is executed.
- Moving Average Periods: The script uses two moving averages (a "fast" one and a "slow" one) to calculate divergence. You specify the periods for these averages (e.g., how many past price points to consider when calculating the average). You can also choose which price (open, high, low or close) to base the moving average.
- Divergence Thresholds: The script will only enter trades when the calculated "divergence" reaches certain levels, which you define. There's one level to buy, and one to sell.
- Profit and Loss Limits: How much profit you want to make on each trade, and how much loss you're willing to tolerate.
- Trading Hours: The specific times of day and days of the week that the script is allowed to place new trades.
- Basket Profit/Loss: Allows you to close all open positions when the combined profit/loss reaches a certain level.
-
Initial Actions: When the script starts (or is recompiled), it removes any old visual objects it drew on the chart in previous runs, and writes "Init Happened" to the logs.
2. Core Logic (Executed Every Tick):
- Detecting New Bars: The script keeps track of time, and it detects when a new bar appears on the chart, which represents a new period of trading activity.
- Trading Time: The script looks to see if it's within the time you specified in the user defined setting.
- Checking Existing Orders: It counts how many open trades are already running on the currency pair it is attached to.
- Calculating Divergence: This is the heart of the trading strategy. The script calculates "divergence" using the two moving averages you defined. Without knowing the exact mathematical formula that is being used inside the black box named "divergence" it is not possible to determine the actual nature of the calculation. However, in laymans terms, divergence looks for the differences in the prices of the two moving averages over time. This can then be used as an indicator of when to buy or sell the currency.
- Determining Buy/Sell Signals: Based on the calculated "divergence," the script decides whether to generate a "buy" signal or a "sell" signal. If the divergence is positive, it's a buy signal, and if it's negative, it's a sell signal. However, the amount of divergence will need to be in the threshold specified in the user defined settings.
- Placing Orders (If Allowed):
- If the script is allowed to trade based on the time filter, the divergence criteria are met, it will place a trade. If the signal is "buy," the script places a "buy" order at the current ask price (the price you would pay to buy the currency). If the signal is "sell," it places a "sell" order at the current bid price (the price you would receive to sell the currency).
- When placing an order, the script also sets a stop-loss (the maximum loss it will tolerate) and a take-profit (the target profit it wants to achieve).
- Basket Profit/Loss: It checks to see if the overall profit or loss across all open trades reaches the "BasketProfit" or "BasketLoss" limit. If so, it closes all open positions.
3. Managing Existing Orders:
- Monitoring Profits: For each open trade, the script constantly monitors the profit or loss.
- Break-Even: The script is capable of modifying a stop loss to create a break even position if the profits reach a certain threshold.
- Trailing Stop: The script can implement a "trailing stop," which is a stop-loss that automatically adjusts as the price moves in a favorable direction. This helps to lock in profits.
- Closing Orders: If a trade reaches either the take-profit or stop-loss level, the script automatically closes the trade. If the profit target is made and the user set the script to create a break-even position, the trade will be automatically closed if the stop loss level is reached.
4. Time Restrictions:
- The script contains settings to only allow trading within certain days of the week or times of day.
5. Logging:
- The script writes information to the logs for debugging purposes, and can write information to a text file for later analysis.
In essence, this script automates a trading strategy based on divergence. It continuously monitors the market, calculates divergence, generates buy/sell signals, places orders, manages existing trades, and closes them when profit or loss targets are met.
/*
+--------+
|Divergence Trader
+--------+
*/
// variables declared here are GLOBAL in scope
#property copyright "Forex-tsd.com"
#property link "http://www.forex-tsd.com"
// user input
extern double Lots=0.1; // 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;
int Fast_Price = PRICE_OPEN;
extern int Slow_Period=88;
int Slow_Price = PRICE_OPEN;
extern double DVBuySell=0.0011;
extern double DVStayOut=0.0079;
extern double ProfitMade=10; // how much money do you expect to make
extern double LossLimit=40; // 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 GMTOpeningHour = 16; // First hour to open new orders (begins at :00 of specified hour)
extern int GMTClosingHour = 7; // Last hour to open new orders (ends at :00 of specified hour)
extern int GMTFridayOpeningHour = 0; // First hour to open new orders on Friday (begins at :00 of specified hour)
extern int GMTFridayClosingHour = 0; // Last hour to open new orders on Friday (ends at :00 of specified hour)
extern bool TradeOnFriday = true; // >0 trades on friday
extern bool TradeOnSunday = false; // >0 trades on friday
extern int BrokerGMTOffset = 0; // MIG is 2. Interbank FX and GMT are 0. FXDD is 3.
extern int BasketProfit=75; // if equity reaches this level, close trades
extern int BasketLoss=9999; // if equity reaches this negative level, close trades
extern bool FileData=false;
// naming and numbering
extern int MagicNumber = 200601182020; // allows multiple experts to trade on same account
string TradeComment = "Divergence";
// 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;
double CECount;
double CEProc;
double CEBuy;
double CESell;
string sComment;
//+-------------+
//| 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("Close Everything ",CECount);
Print("Close Proc ",CEProc);
Print("Proc Buy ",CEBuy);
Print("Proc Sell ",CESell);
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;
sComment = "";
// bar counting
if(bartime!=Time[0])
{
bartime=Time[0];
bartick++;
objtick++;
TradeAllowed=CheckTimeFilter();
}
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 |
//+-----------------------------+
diverge=divergence(Fast_Period,Slow_Period,Fast_Price,Slow_Price,0);
ObjectDelete("Cmmt");
ObjectCreate("Cmmt", OBJ_TEXT, 0, Time[0], 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(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)
{
//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 )
{
//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();
CECount++;
}
// 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
Comment( sComment );
} // start()
bool CheckTimeFilter( )
{
int OpenHour = 0;
int CloseHour = 0;
int GMTDay = DayOfWeek( );
int GMTHour = Hour( ) - BrokerGMTOffset;
bool bResult = true;
// Convert server's hour and day to GMT
if( GMTHour < 0 ) // Check for midnight rollover
{
GMTDay -= 1;
GMTHour += 24;
if( GMTDay < 0 ) // Check for week rollever
GMTDay = 6;
}
if( GMTHour > 23 )
{
GMTDay += 1;
GMTHour -= 24;
if( GMTDay > 6 ) // Check for week rollever
GMTDay = 0;
}
// Set the working variables
if(( GMTDay == 5 ) && TradeOnFriday && ( GMTFridayOpeningHour != GMTFridayClosingHour )) // Friday may have special trading hours
{
OpenHour = GMTFridayOpeningHour;
CloseHour = GMTFridayClosingHour;
sComment = sComment + "Friday ";
}
else
{
OpenHour = GMTOpeningHour;
CloseHour = GMTClosingHour;
}
sComment = sComment + "TimeFilter: ";
if(( GMTDay == 0 ) && !TradeOnSunday )
{
sComment = sComment + "TRADING DISABLED on Sunday\n";
bResult = false;
}
else if(( GMTHour == 5 ) && !TradeOnFriday )
{
sComment = sComment + "TRADING DISABLED on Friday\n";
bResult = false;
}
else if( OpenHour == CloseHour )
sComment = sComment + "None\n";
else
{
sComment = sComment + ( OpenHour + BrokerGMTOffset + 0 ) + ":00 to " + ( CloseHour + BrokerGMTOffset + 0 ) + ":00";
if ( BrokerGMTOffset >= 0 ) sComment = sComment + " (GMT+" + BrokerGMTOffset + ")";
else sComment = sComment + " (GMT-" + BrokerGMTOffset + ")";
CloseHour -= 1; // Subtract one so that 10 means 10:00 and not 10:59
// Adjust if we've crossed midnight
while( OpenHour < 0 ) OpenHour += 24;
while( OpenHour > 23 ) OpenHour -= 24;
while( CloseHour < 0 ) CloseHour += 24;
while( CloseHour > 23 ) CloseHour -= 24;
if((( OpenHour <= CloseHour ) && ( GMTHour < OpenHour || GMTHour > CloseHour ))
|| // Allow for Overnight Trading
(( OpenHour > CloseHour ) && ( GMTHour < OpenHour && GMTHour > CloseHour ))
)
{
sComment = sComment + " - TRADING DISABLED";
bResult = false;
}
else
sComment = sComment + " - Trading Active";
sComment = sComment + "\n";
}
return( bResult );
}
//+-----------------+
//| 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);
CEBuy++;
break;
//Close opened short positions
case OP_SELL :result = OrderClose(myTkt, myLot, myAsk, Slippage, Red);
CESell++;
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(500);
}
Sleep(1000);
CEProc++;
} //for
} // closeeverything
double divergence(int F_Period, int S_Period, int F_Price, int S_Price, int mypos)
{
int i;
double maF1, maF2, maS1, maS2;
double dv1, dv2;
maF1=iMA(Symbol(),0,F_Period,0,MODE_SMA,F_Price,mypos);
maS1=iMA(Symbol(),0,S_Period,0,MODE_SMA,S_Price,mypos);
dv1=(maF1-maS1);
maF2=iMA(Symbol(),0,F_Period,0,MODE_SMA,F_Price,mypos+1);
maS2=iMA(Symbol(),0,S_Period,0,MODE_SMA,S_Price,mypos+1);
dv2=((maF1-maS1)-(maF2-maS2));
return(dv1-dv2);
}
Comments