Created
August 14, 2011 15:32
-
-
Save rmoriz/1144989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| LiveAlligator.mq4 | | |
//| Copyright © 2011, MetaQuotes Software Corp. | | |
//| http://www.mql4.com/ru/users/rustein | | |
//-------------------------------------------------------------------+ | |
// Trend Detection function | |
#define BULL 1 | |
#define BEAR 2 | |
// Input variables | |
//+------------------------------------------------------------------+ | |
extern int Slippage = 5; | |
//-------------------------------------------------------------------+ | |
extern int Magic = 55; | |
//-------------------------------------------------------------------+ | |
extern int StopLoss = 75; | |
//-------------------------------------------------------------------+ | |
extern bool MoneyMangement = true; | |
extern double Lots = 0.01; | |
extern double MaximumRisk = 0.50; //- Alter to suit your account | |
//+------------------------------------------------------------------+ | |
extern int LivePeriod = 46; | |
//-------------------------------------------------------------------+ | |
extern int TrailPeriod = 113; | |
//-------------------------------------------------------------------+ | |
extern int TotalOrders = 2; | |
//-------------------------------------------------------------------+ | |
string OrderComments = "LiveAlligator"; | |
/////////////////////////////////////////////////////////////////////////////////// | |
extern int GMT.Offset = 0; // Broker timezone offset - Alter to suit you broker | |
extern int GMT.OpenHour = 1; // Earliest hour to enter the market | |
extern int GMT.CloseHour = 21; // No orders after Open of this bar | |
/////////////////////////////////////////////////////////////////////////////////// | |
int FridayCloseHour = 25; | |
extern int LastDayOfYearToTrade = 345; | |
extern int FirstDayOfYearToTrade = 5; | |
extern string BacktestSettings = "=== Backtest Settings ==="; | |
extern bool Backtest.Auto.Clock.Change=true; | |
extern int Backtest.GMT.Offset.Summer = 2; //- Alter to suit you broker | |
extern int Backtest.GMT.Offset.Winter = 1; //- Alter to suit you broker | |
//----- | |
color BuyColor = CLR_NONE; | |
color SellColor = CLR_NONE; | |
// Global variables | |
int Cnt = 0; // counter variable, used in for() loops | |
bool InitVariables; // init variable when program starts | |
datetime PreviousBar; // record the candle/bar time | |
int LastTrendDirection; // record the previous trends direction | |
int FileHandle; // variable for file handling | |
int OpenHour, CloseHour, iHourOffset; | |
static double sdPoint; | |
/* | |
1.01 BarrowBoy www.selectfx.net and on Twitter as BarrowBoy66 | |
Added trading hours approximately suitable for this system pair EURUSD | |
- Unless your system is <always on, stop and reverse> you are very likely to need to filter entry signals by time-of-day | |
Added handling for full & subpip accounts automatically | |
1.01a | |
One TrailingStop bug fixed - original was placing stop too close to market | |
Another bug remains in the OrderModify section... | |
*/ | |
//-------------------------------------------------------------------+ | |
//-------------------------------------------------------------------+ | |
int init() | |
{ | |
HandleDigits(); | |
GetHourOffSet(iHourOffset); | |
OpenHour = iHourOffset+GMT.OpenHour; | |
CloseHour = iHourOffset+GMT.CloseHour; | |
if(IsTesting()==false) | |
{ | |
InitVariables = true; // Allows us to init variables in the start function because | |
// we cannot do this in the init function because the variables | |
// use values from the chart | |
//create data file if it does not exist and load variables if file does exist | |
FileHandle=FileOpen("LiveAlligator.dat",FILE_BIN | FILE_READ); | |
if(FileHandle<1) | |
{ | |
Print("LiveAlligator.dat not found."); | |
FileHandle=FileOpen("LiveAlligator.dat",FILE_BIN | FILE_WRITE); | |
if(FileHandle>0) | |
{ | |
LastTrendDirection=0; | |
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE); | |
FileClose(FileHandle); | |
Print("LiveAlligator.dat has been successfully created."); | |
} | |
else | |
{ | |
FileClose(FileHandle); | |
Print("Failed to create LiveAlligator.dat file."); | |
} | |
} | |
else | |
{ | |
LastTrendDirection=FileReadInteger(FileHandle,SHORT_VALUE); | |
Print("LiveAlligator variables loaded from file."); | |
FileClose(FileHandle); | |
} | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//|-----------------------// Save Variables //---------------------| | |
//+------------------------------------------------------------------+ | |
int SaveVariables() | |
{ | |
if(IsTesting()==false) | |
{ | |
//save variables to file | |
FileHandle=FileOpen("LiveAlligator.dat",FILE_BIN | FILE_WRITE); | |
if(FileHandle<1) | |
{ | |
Print("LiveAlligator.dat not found."); | |
FileHandle=FileOpen("LiveAlligator.dat",FILE_BIN | FILE_WRITE); | |
if(FileHandle>0) | |
{ | |
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE); | |
FileClose(FileHandle); | |
Print("LiveAlligator.dat has been successfully created."); | |
} | |
else | |
{ | |
FileClose(FileHandle); | |
Print("Failed to create LiveAlligator.dat file."); | |
} | |
} | |
else | |
{ | |
FileWriteInteger(FileHandle,LastTrendDirection,SHORT_VALUE); | |
FileClose(FileHandle); | |
Print("LiveAlligator Variables successfully saved to file."); | |
} | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
int deinit() | |
{ | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
int start() | |
{ | |
GetHourOffSet(iHourOffset); | |
OpenHour = iHourOffset+GMT.OpenHour; | |
CloseHour = iHourOffset+GMT.CloseHour; | |
//----- | |
if(MoneyMangement == true) | |
Lots = NormalizeDouble(AccountBalance()*MaximumRisk/100.00/100.00,1); | |
else Lots = Lots; | |
//----- | |
// init variables when the expert advisor first starts running | |
if(InitVariables == true) | |
{ | |
PreviousBar = Time[0]; // record the current canle/bar open time | |
// place code here that you only wnat to run one time | |
InitVariables = false; // change to false so we only init variable once | |
} | |
// record trends direction | |
if(LastTrendDirection==0) | |
{ | |
if(TrendDetection()==BULL) | |
{ | |
LastTrendDirection=BULL; | |
} | |
if(TrendDetection()==BEAR) | |
{ | |
LastTrendDirection=BEAR; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|------------------------// Trail By MA //-------------------------| | |
//+------------------------------------------------------------------+ | |
double SL = NormalizeDouble(iMA(NULL,0,TrailPeriod,0,MODE_SMMA,PRICE_CLOSE,1),Digits); | |
double dNearest = MarketInfo(Symbol(), MODE_STOPLEVEL); | |
//----- | |
int Orders = OrdersTotal(); | |
for (int i=0; i<Orders; i++) | |
{ | |
if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue; | |
if(OrderSymbol() != Symbol()) continue; | |
{ | |
if(OrderType() == OP_BUY && OrderMagicNumber()==Magic && SL > OrderOpenPrice() && NormalizeDouble(Ask,Digits) > SL | |
&& OrderStopLoss() != SL && SL > 0 && SL!=EMPTY_VALUE && SL > OrderStopLoss()) | |
{ | |
if ((SL > Ask+dNearest*sdPoint) || (SL < Bid-dNearest*sdPoint)) | |
OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,CLR_NONE); | |
} | |
if(OrderType() == OP_SELL && OrderMagicNumber()==Magic && SL < OrderOpenPrice() && NormalizeDouble(Bid,Digits) < SL | |
&& OrderStopLoss() != SL && SL > 0 && SL!=EMPTY_VALUE && SL < OrderStopLoss()) | |
{ | |
if ((SL > Ask+dNearest*sdPoint) || (SL < Bid-dNearest*sdPoint)) | |
OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,CLR_NONE); | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|------------------------// Open Orders //-----------------------| | |
//+------------------------------------------------------------------+ | |
// perform analysis and open orders on new candle/bar | |
if(NewBar() == true) | |
{ | |
HandleDigits(); | |
Print ("Lots ", Lots); | |
// only perform analysis and open new order if we have not reached our TotalOpenOrders max | |
if(TotalOpenOrders() < TotalOrders) | |
{ | |
//----- // Open BUY | |
if(TrendDetection() == BULL && LastTrendDirection == BEAR) | |
{ | |
if(StopLoss>0) | |
{ | |
if (TradingHours()) OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slippage*sdPoint,NormalizeDouble(Ask,Digits)-(StopLoss*sdPoint),0,OrderComments,Magic,0,BuyColor); | |
} | |
if(StopLoss==0) | |
{ | |
OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slippage*sdPoint,0,0,OrderComments,Magic,0,BuyColor); | |
} | |
LastTrendDirection=BULL; | |
// save variables to file | |
SaveVariables(); | |
} | |
//----- // Open SELL | |
if(TrendDetection() == BEAR && LastTrendDirection == BULL) | |
{ | |
if(StopLoss>0) | |
{ | |
if (TradingHours()) OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slippage*sdPoint,NormalizeDouble(Bid,Digits)+(StopLoss*sdPoint),0,OrderComments,Magic,0,SellColor); | |
} | |
if(StopLoss==0) | |
{ | |
OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slippage*sdPoint,0,0,OrderComments,Magic,0,SellColor); | |
} | |
LastTrendDirection=BEAR; | |
// save variables to file | |
SaveVariables(); | |
} | |
} | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//|-----------------------// Orders Count //-----------------------| | |
//+------------------------------------------------------------------+ | |
// This function returns the total amount of orders the expert advisor has open | |
int TotalOpenOrders() | |
{ | |
Cnt=OrdersTotal(); | |
int TotalOpenOrders = 0; | |
if(Cnt==0) | |
{ | |
return(0); | |
} | |
else | |
{ | |
for(;Cnt>=0;Cnt--) | |
{ | |
RefreshRates(); | |
OrderSelect(Cnt,SELECT_BY_POS); | |
if(OrderMagicNumber()==Magic) | |
{ | |
TotalOpenOrders++; | |
} | |
} | |
} | |
return(TotalOpenOrders); | |
} | |
//+------------------------------------------------------------------+ | |
//|--------------------------// New Bar //-------------------------| | |
//+------------------------------------------------------------------+ | |
// This function return the value true if the current bar/candle was just formed | |
bool NewBar() | |
{ | |
if(PreviousBar<Time[0]) | |
{ | |
PreviousBar = Time[0]; | |
return(true); | |
} | |
else | |
{ | |
return(false); | |
} | |
return(false); // in case if - else statement is not executed | |
} | |
//+------------------------------------------------------------------+ | |
//|---------------------// Trend Detection //----------------------| | |
//+------------------------------------------------------------------+ | |
int TrendDetection() | |
{ | |
//-------------------------------------------------------------------+ | |
double LipsNow = iAlligator(NULL,0,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0); | |
double LipsPre = iAlligator(NULL,0,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,1); | |
double JawsNow = iAlligator(NULL,0,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0); | |
double JawsPre = iAlligator(NULL,0,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,1); | |
double TeethNow = iAlligator(NULL,0,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0); | |
//----- | |
double MA1 = iMA(NULL,0,LivePeriod,0,MODE_EMA,PRICE_CLOSE,0); | |
double MA2 = iMA(NULL,0,LivePeriod,0,MODE_EMA,PRICE_WEIGHTED,0); | |
double MA3 = iMA(NULL,0,LivePeriod,0,MODE_EMA,PRICE_TYPICAL,0); | |
double MA4 = iMA(NULL,0,LivePeriod,0,MODE_EMA,PRICE_MEDIAN,0); | |
double MA5 = iMA(NULL,0,LivePeriod,0,MODE_EMA,PRICE_OPEN,0); | |
//-------------------------------------------------------------------+ | |
//BULL trend | |
if(LipsNow > JawsNow && TeethNow < JawsNow && LipsPre < JawsPre && MA1 > MA2 && MA2 > MA3 && MA3 > MA4 && MA4 > MA5) | |
{ | |
return(BULL); | |
} | |
//BEAR trend | |
if(LipsNow < JawsNow && TeethNow > JawsNow && LipsPre > JawsPre && MA1 < MA2 && MA2 < MA3 && MA3 < MA4 && MA4 < MA5) | |
{ | |
return(BEAR); | |
} | |
return(0); | |
} | |
bool TradingHours() | |
{ | |
// Check if OK to Open Orders - quit if not | |
if (Hour() < OpenHour) return (false); | |
if (Hour() >= CloseHour) return (false); | |
if (DayOfYear() > LastDayOfYearToTrade) return (false); // Not the trading day | |
if (DayOfYear() < FirstDayOfYearToTrade) return (false); // Not the trading day | |
return (true); | |
} | |
void GetHourOffSet(int &iHourOffset) | |
{ | |
int iMonth, iDayOfWeek, iDayOfMonth; | |
if (IsLiveTrading()) | |
{ | |
iHourOffset = GMT.Offset; | |
return; | |
} | |
// if here we are in one of the test modes | |
if (Backtest.Auto.Clock.Change==false) | |
{ | |
iHourOffset = GMT.Offset; | |
return; | |
} | |
// if here we are in one of the test modes and want to finesse the offset | |
iMonth = Month(); | |
switch(iMonth) | |
{ | |
// Set Winter values | |
case 1: | |
iHourOffset = Backtest.GMT.Offset.Winter; | |
break; | |
case 2: | |
iHourOffset = Backtest.GMT.Offset.Winter; | |
break; | |
case 11: | |
iHourOffset = Backtest.GMT.Offset.Winter; | |
break; | |
case 12: | |
iHourOffset = Backtest.GMT.Offset.Winter; | |
break; | |
// Set Summer values | |
case 4: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
case 5: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
case 6: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
case 7: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
case 8: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
case 9: | |
iHourOffset = Backtest.GMT.Offset.Summer; | |
break; | |
} | |
if (iMonth==3 || iMonth==10) // Check further | |
{ | |
iDayOfMonth = Day(); | |
iDayOfWeek = DayOfWeek(); | |
if (iDayOfMonth < 25) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
if (iDayOfMonth==25) | |
if (iDayOfWeek > 0) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==26) | |
if (iDayOfWeek > 1) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==27) | |
if (iDayOfWeek > 2) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==28) | |
if (iDayOfWeek > 3) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==29) | |
if (iDayOfWeek > 4) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==30) | |
if (iDayOfWeek > 5) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Winter; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Summer; | |
return; | |
} | |
else | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
if (iDayOfMonth==31) | |
{ | |
if (iMonth==3) iHourOffset = Backtest.GMT.Offset.Summer; | |
if (iMonth==10) iHourOffset = Backtest.GMT.Offset.Winter; | |
return; | |
} | |
} // if (iMonth==3 || iMonth==10) // Check further | |
return; | |
} | |
bool IsLiveTrading() | |
{ | |
if (IsTesting()) return (false); | |
if (IsOptimization()) return (false); | |
if (IsVisualMode()) return (false); | |
return (true); | |
} | |
void HandleDigits() | |
{ | |
if (Digits == 4 || Digits == 2) // Full pip pair | |
{ | |
sdPoint = Point; | |
} | |
if (Digits == 5 || Digits == 3) // Sub-pip pair | |
{ | |
sdPoint = Point*10.0000; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|---------------------------// END //------------------------------| | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment