//+------------------------------------------------------------------+
//| MACD Turbo v2.0a.mq4 |
//| for GBPUSD H1 re-coded 2013 |
//| Free EA based on |
//| |
//| MACD Sample.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//| |
//| MACD Sample - Turbo v1.0 to v2.0 |
//| by XeroFX - http://www.xerofx.com/ |
//| |
//| MACD Sample - Turbo v1.1 |
//| re-coded by Tjipke de Vries |
//| |
//| Free Sample Code. We disclaim all copyright interest. Thank you. |
//+------------------------------------------------------------------+
#property copyright "Copyleft - Free Sample Code - www.xerofx.com/free-ea.html - We disclaim all copyright interest."
extern string s1 = "== Basic Settings ==";
extern double StopLoss = 255;
extern double TakeProfit = 100;
extern double TrailingStop = 25;
extern double TrailingStep = 5;
extern string s2 = "== Trade Lots Size ==";
extern double Lots = 0.1;
extern string s3 = "== Signal Logic ==";
extern double MACDOpenLevel = 3;
extern double MACDCloseLevel = 2;
extern double MATrendPeriod = 24;
extern string s4 = "== Time Filters == ";
extern string UseTradingHours = "Time Control ( 1 = True, 0 = False )";
extern int TimeControl = 1;
extern string TimeZone = "Adjust ServerTimeZone if Required";
extern int ServerTimeZone = 0;
extern string TradingTimes = "HourStopGMT > HourStartGMT";
extern int HourStartGMT = 8;
extern int HourStopGMT = 20;
extern string DontTradeFriday = "Dont Trade After FridayFinalHourGMT";
extern bool UseFridayFinalTradeTime = TRUE;
extern int FridayFinalHourGMT = 6;
extern string s5 = "== Extra Settings ==";
extern int Slippage = 3;
extern double MaxSpread = 4.0;
extern int MagicNumber = 10042;
extern string TradeComment = "MACD Turbo v2.0a";
int checkorder=1;
int SlipPage;
double Points,SL,TP;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// 4 or 5 Digit Broker Account Recognition
void HandleDigits()
{
// Automatically Adjusts to Full-Pip and Sub-Pip Accounts
if (Digits == 4 || Digits == 2)
{
SlipPage = Slippage;
Points = Point;
}
if (Digits == 5 || Digits == 3)
{
SlipPage = Slippage*10;
Points = Point*10;
}
}
//----------------------- PRINT COMMENT FUNCTION
void subPrintDetails()
{
string sComment = "";
string SP = " ----------------------------------------\n";
string NL = "\n";
string SessionActive,CurrentSpread,spread;
TradeSession();
MaxSpreadFilter();
if(Digits == 4 || Digits == 5){spread = DoubleToStr(10000.0 * (Ask - Bid),1);}
if(Digits == 2 || Digits == 3){spread = DoubleToStr(100.0 * (Ask - Bid),1);}
if (TradeSession())SessionActive = "Trading...";
else SessionActive = "Non-Trading Time";
if (MaxSpreadFilter())CurrentSpread = " is to High";
else CurrentSpread = " is OK";
sComment = "MACD Turbo v2.0a" + NL;
sComment = sComment + NL;
sComment = sComment + "Magic Number " + DoubleToStr(MagicNumber,0) + NL;
sComment = sComment + "StopLoss " + DoubleToStr(StopLoss,0) + " | ";
sComment = sComment + "TakeProfit " + DoubleToStr(TakeProfit,0) + " | ";
sComment = sComment + "TrailingStop " + DoubleToStr(TrailingStop,0) + NL;
sComment = sComment + "Date: " + Month() +"-"+Day()+"-"+Year()+" Server Time: " + TimeToStr(TimeCurrent(), TIME_SECONDS) + NL;
sComment = sComment + "GMT Time: " + TimeToStr((TimeCurrent()+ (( 0 - ServerTimeZone) * 3600)), TIME_SECONDS) + NL;
sComment = sComment + "ServerTimeZone: " + ServerTimeZone + " (TimeZone)" + NL;
sComment = sComment + SP;
sComment = sComment + "Lot Size " + DoubleToStr(Lots,2) + NL;
sComment = sComment + SessionActive + NL;
sComment = sComment + "Spread: " + spread + CurrentSpread + NL;
Comment(sComment);
}
//+------------------------------------------------------------------+
bool TradeSession() {
int HourStartTrade;
int HourStopTrade;
HourStartTrade = HourStartGMT + ServerTimeZone;
HourStopTrade = HourStopGMT + ServerTimeZone;
if (HourStartTrade < 0)HourStartTrade = HourStartTrade + 24;
if (HourStartTrade >= 24)HourStartTrade = HourStartTrade - 24;
if (HourStopTrade > 24)HourStopTrade = HourStopTrade - 24;
if (HourStopTrade <= 0)HourStopTrade = HourStopTrade + 24;
if ((UseFridayFinalTradeTime && (Hour()>=FridayFinalHourGMT + ServerTimeZone) && DayOfWeek()==5)||DayOfWeek()==0)return (FALSE); // Friday Control
if((TimeControl(HourStartTrade,HourStopTrade)!=1 && TimeControl==1 && HourStartTrade<HourStopTrade)
|| (TimeControl(HourStopTrade,HourStartTrade)!=0 && TimeControl==1 && HourStartTrade>HourStopTrade)
||TimeControl==0)return (TRUE); // "Trading Time";
return (FALSE); // "Non-Trading Time";
}
//+------------------------------------------------------------------+
int TimeControl(int StartHour, int EndHour)
{
if (Hour()>=StartHour && Hour()< EndHour)
{
return(0);
}
return(1);
}
//+------------------------------------------------------------------+
bool MaxSpreadFilter()
{
RefreshRates();
if ((Digits == 4 || Digits == 5)&&(10000.0 * (Ask - Bid) > MaxSpread))
{return(true);}
if ((Digits == 2 || Digits == 3)&&(100.0 * (Ask - Bid) > MaxSpread))
{return(true);}
else return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
HandleDigits();
subPrintDetails();
total=OrdersTotal();
if(checkorder<1)
{
// if no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("You dont have enough money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(!MaxSpreadFilter() && MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Points) && MaCurrent>MaPrevious && TradeSession())
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,SlipPage,0,0,TradeComment,MagicNumber,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
Print("BUY order opened : ",OrderOpenPrice());
checkorder=checkorder+1;
}
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(!MaxSpreadFilter() && MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Points) && MaCurrent<MaPrevious && TradeSession())
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,SlipPage,0,0,TradeComment,MagicNumber,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{
Print("SELL order opened : ",OrderOpenPrice());
checkorder=checkorder+1;
}
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
if(checkorder>0)
{
checkorder=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()&& // check for symbol
OrderMagicNumber()==MagicNumber) // check the Magic Number
{
if(OrderType()==OP_BUY) // go to long position
{
checkorder=checkorder+1;
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Points*TrailingStop)
{
if(OrderStopLoss()<Bid-Points*(TrailingStop+TrailingStep))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Points*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
// modify OrderStopLoss and OrderTakeProfit for Buy
if(OrderStopLoss()==0 && OrderTakeProfit()==0 && (StopLoss>0||TakeProfit>0))
{
SL = OrderStopLoss();
if (StopLoss>0) {SL =(OrderOpenPrice() - (StopLoss*Points));}
TP = OrderTakeProfit();
if (TakeProfit>0) {TP =(OrderOpenPrice() + (TakeProfit*Points));}
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Green);
return(0);
}
}
if(OrderType()==OP_SELL) // go to short position
{
checkorder=checkorder+1;
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Points*TrailingStop))
{
if((OrderStopLoss()>(Ask+Points*(TrailingStop+TrailingStep))) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Points*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
// modify OrderStopLoss and OrderTakeProfit for Sell
if(OrderStopLoss()==0 && OrderTakeProfit()==0 && (StopLoss>0||TakeProfit>0))
{
SL = OrderStopLoss();
if (StopLoss>0) {SL =(OrderOpenPrice() + (StopLoss*Points));}
TP = OrderTakeProfit();
if (TakeProfit>0) {TP =(OrderOpenPrice() - (TakeProfit*Points));}
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red);
return(0);
}
}
}
}
}
return(0);
}
// the end.