//+------------------------------------------------------------------+
//|                                    AngleSep_expert_v1.mq4        |
//|                                              Copyright © 2005    |
//|    Thanks to Starter, Maloma, Amir, Fukinagashi, Forex_trader,   |
//|    kmrunner, and all other strategybuilderFx members that        |
//|    contributed to the success of this expert.                    |
//|    From MrPip                                                    |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Strategybuilderfx members"
#property link      "http:/strategybuilderfx.com/"
#include <stdlib.mqh>
extern bool Debug = false;               // Change to true to allow print
//+---------------------------------------------------+
//|Account functions                                  |
//+---------------------------------------------------+
extern bool AccountIsMini = false;      // Change to true if trading mini account
//+---------------------------------------------------+
//|Money Management                                   |
//+---------------------------------------------------+
extern bool MoneyManagement = true; // Change to false to shutdown money management controls.
                                    // Lots = 1 will be in effect and only 1 lot will be open regardless of equity.
extern double Riskpercent = 10;      // Change to whatever percent of equity you wish to risk.
extern double StopLoss = 0;        // Maximum pips willing to lose per position.
extern double TrailingStop = 0;    // Change to whatever number of pips you wish to trail your position with.
extern double Margincutoff = 800;   // Expert will stop trading if equity level decreases to that level.
extern double MaxLots = 100;
//+---------------------------------------------------+
//|Profit controls                                    |
//+---------------------------------------------------+
extern int Slippage = 10;           // Possible fix for not getting filled or closed    
extern int TakeProfit = 0;
//+---------------------------------------------------+
//|Indicator Variables                                |
//| Change these to try your own system               |
//| or add more if you like                           |
//+---------------------------------------------------+
extern int LSMAShortPeriod=7;
extern int LSMALongPeriod=16;
extern int MinSeparation=0;
extern double Lots = 1;             // standard lot size. 
//+---------------------------------------------------+
//|General controls                                   |
//+---------------------------------------------------+
string OrderText = "";
double lotMM;
int TradesInThisSymbol;
datetime LastTime;
double Sl; 
double Tr;
bool OK2Buy, OK2Sell;              // determines if buy or sell trades are allowed
int NumBuys, NumSells;             // Number of buy and sell trades in this symbol
//+---------------------------------------------------+
//|  Indicator values for filters                     |
//|  Add or Change to test your system                |
//+---------------------------------------------------+
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   int handle, err;
   
    string filename=Symbol()+TimeToStr(CurTime(),TIME_DATE) + ".txt";
    
//---- 
    if (Debug)
    {
      GlobalVariableSet("MyHandle",0);
      if (!GlobalVariableCheck("MyHandle"))
      {
         err = GetLastError();
         Print("Error creating Global Variable MyHandle: (" + err + ") " + ErrorDescription(err));
         return(0); 
      }
      handle = FileOpen(filename,FILE_CSV|FILE_WRITE,"\t");
      GlobalVariableSet("MyHandle",handle); 
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
     int handle;
 
    if (Debug)
    {
      if (GlobalVariableCheck("MyHandle"))
      {
         handle = GlobalVariableGet("MyHandle");
         FileFlush(handle);
         FileClose(handle);
         GlobalVariableDel("MyHandle");  
      }
    }
   return(0);
  }
//+------------------------------------------------------------------+
//| Write - writes string to debug file                              |
//+------------------------------------------------------------------+
int Write(string str)
{
   int handle;
   
   if (GlobalVariableCheck("MyHandle"))
   {
      handle = GlobalVariableGet("MyHandle");
      FileWrite(handle,str,"\r\n");
   } 
}
  
//+------------------------------------------------------------------+
//| The functions from this point to the start function are where    |
//| changes are made to test other systems or strategies.            |
//|+-----------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom Indicators                                                |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation               |
//| LSMA_In_Color Indicator plots the end of the linear regression line    |
//+------------------------------------------------------------------------+
double LSMADaily(int Rperiod, int shift)
{
   int i;
   double sum;
   int length;
   double lengthvar;
   double tmp;
   double wt;
   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     tmp = ( i - lengthvar)*iClose(NULL,1440,length-i+shift);
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    
    return(wt);
}
//+------------------------------------------------------------------+
//| CheckExitCondition                                               |
//| Check if AngleSep cross 0 line                                   |
//+------------------------------------------------------------------+
bool CheckExitCondition(string TradeType)
{
   bool YesClose;
   double Dif;
   
   YesClose = false;
   Dif=LSMADaily(LSMAShortPeriod,1)-LSMADaily(LSMALongPeriod,1);
   
   if (TradeType == "BUY" && Dif < 0) YesClose = true;
   if (TradeType == "SELL" && Dif > 0) YesClose = true;
    
   return (YesClose);
}
//+------------------------------------------------------------------+
//| CheckEntryCondition                                              |
//| Check if separation on LSMA pair                                 |
//+------------------------------------------------------------------+
bool CheckEntryCondition(string TradeType)
{
   bool YesTrade;
   double Dif;
   
   YesTrade = false;
 
   Dif = LSMADaily(LSMAShortPeriod,1) - LSMADaily(LSMALongPeriod,1);
   if (TradeType == "BUY" && Dif > MinSeparation ) YesTrade = true;
   if (TradeType == "SELL" && Dif < -MinSeparation ) YesTrade = true;
   
   return (YesTrade);
}
  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   int Filter;
   bool ExitBuy, ExitSell, YesTrade;
   
	int MagicNumber = 3000 + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period()); 
   string setup="LSMA_Daily" + Symbol() + "_" + func_TimeFrame_Val2String(func_TimeFrame_Const2Val(Period()));
//+------------------------------------------------------------------+
//| Check for Open Position                                          |
//+------------------------------------------------------------------+
     ExitBuy = CheckExitCondition("BUY");
     ExitSell = CheckExitCondition("SELL");
     HandleOpenPositions(MagicNumber, ExitBuy, ExitSell);
     
// Check if any open positions were not closed
     TradesInThisSymbol = CheckOpenPositions(MagicNumber);
     
//+------------------------------------------------------------------+
//| Check if OK to make new trades                                   |
//+------------------------------------------------------------------+
   if(AccountFreeMargin() < Margincutoff) {
     return(0);}
     
// Only allow 1 trade per Symbol
   if(TradesInThisSymbol > 0) {
     return(0);}
   if(CurTime() < LastTime) {
     return(0);}
// Money Management
// Moved after open positions are closed for more available margin
     
   if(MoneyManagement)
   {
     lotMM = LotsOptimized();
   }
   else {
     lotMM = Lots;
     if(AccountIsMini)
     {
       if (lotMM > 1.0) lotMM = lotMM / 10;
       if (lotMM < 0.1) lotMM = 0.1;
     }
   }
   
   OrderText = ""; //Must be blank before going into the main section
   
   
	if(CheckEntryCondition("BUY") && !CheckExitCondition("BUY"))
	{
		OrderText = "BUY";
		if (Debug)
		{
         Print ("Buy  at ", TimeToStr(CurTime()), " for ", DoubleToStr(Ask,4) );
         Write ("Buy at " + TimeToStr(CurTime())+ " for " + DoubleToStr(Ask,4));
      }
		if (StopLoss>0) {
		 Sl = Ask-StopLoss*Point;
		} else {
		 Sl=0;
		}
		if (TakeProfit == 0) 
		    Tr = 0;
		else
		    Tr = Ask+TakeProfit*Point;
	}
   
	if(CheckEntryCondition("SELL") && !CheckExitCondition("SELL"))
	{
		OrderText = "SELL";
		if (Debug)
		{
         Print ("Sell at ", TimeToStr(CurTime()), " for ", DoubleToStr(Bid,4) );
         Write ("Sell at " + TimeToStr(CurTime())+ " for " + DoubleToStr(Bid,4));
      }
		if (StopLoss>0) {
		 Sl = Bid+StopLoss*Point;
		} else {
		 Sl = 0;
		}
		if (TakeProfit == 0) 
		    Tr = 0;
		else
		    Tr = Bid-TakeProfit*Point;
	}
   if(OrderText != "" && TradesInThisSymbol == 0) 
   {
	   LastTime = DoTrades(OrderText,setup,MagicNumber, lotMM,Sl,Tr,CurTime());
      return(0);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Functions beyond this point should not need to be modified       |
//| Eventually will be placed in include file                        |
//+------------------------------------------------------------------+
//+-------------------------------------------+
//| DoTrades module cut from start            |
//|  No real changes                          |
//+-------------------------------------------+
datetime DoTrades(string OrdText, string SetupStr,int MagicNum,double lotM, double SSl, double TTr, datetime LstTime)
{
   double Min_OrderPrice;
   int err,tries;
   double dtries;
   int ticket;
   datetime lsttim;
   lsttim = LstTime;
   if(OrdText == "BUY")
   {
      tries = 0;
      while (tries < 3)
      {
         ticket = OrderSend(Symbol(),OP_BUY,lotM,Ask,Slippage,SSl,TTr,SetupStr,MagicNum,0,Green);
         if (Debug)
         {
            dtries = tries;
            Print ("Buy at ",TimeToStr(CurTime())," for ",Ask, " try:",tries);
            Write ("Buy at " + TimeToStr(CurTime()) + " for " + DoubleToStr(Ask,4) + " try:" + DoubleToStr(dtries,0));
         }
         lsttim += 12;
         if (ticket <= 0) {
           tries++;
         } else tries = 3;
      }
      if(ticket<=0) {
         err = GetLastError();
         Alert("Error opening BUY order [" + SetupStr + "]: (" + err + ") " + ErrorDescription(err)); 
         if (Debug) Write("Error opening BUY order [" + SetupStr + "]: (" + err + ") " + ErrorDescription(err)); 
      }
      return(lsttim);
   }
   else if(OrdText == "SELL")
   {
     tries = 0;
     while (tries < 3)
     {
       ticket = OrderSend(Symbol(),OP_SELL,lotM,Bid,Slippage,SSl,TTr,SetupStr,MagicNum,0,Red);
       if (Debug)
       {
          dtries = tries;
          Print ("Sell at ",TimeToStr(CurTime())," for ",Bid, " try:",tries);
          Write ("Sell at " + TimeToStr(CurTime()) + " for " + DoubleToStr(Bid,4) + " try:" + DoubleToStr(dtries,0));
        }
       lsttim += 12;
       if (ticket <= 0)
       {
         tries++;
        }else tries = 3;
      }
      if(ticket<=0) {
        err = GetLastError();
        Alert("Error opening Sell order [" + SetupStr + "]: (" + err + ") " + ErrorDescription(err)); 
        if (Debug) Write("Error opening Sell order [" + SetupStr + "]: (" + err + ") " + ErrorDescription(err)); 
      }
      return(lsttim);
    }
    return(lsttim);
}
//+------------------------------------------------------------------+
//| Check Open Position Controls                                     |
//+------------------------------------------------------------------+
  
int CheckOpenPositions(int MagicNumbers)
{
   int cnt, total, NumPositions;
   int NumBuyTrades, NumSellTrades;   // Number of buy and sell trades in this symbol
   
   NumBuyTrades = 0;
   NumSellTrades = 0;
   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;
      if ( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumbers)  continue;
      
      if((OrderType() == OP_BUY || OrderType() == OP_BUYSTOP) && (OrderSymbol()==Symbol()))
          {
             NumBuyTrades++;
          }
             
      if((OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) && (OrderSymbol()==Symbol()))
          {
             NumSellTrades++;
          }
             
     }
     NumPositions = NumBuyTrades + NumSellTrades;
     return (NumPositions);
  }
//+------------------------------------------------------------------+
//| Handle Open Positions                                            |
//| Check if any open positions need to be closed or modified        |
//| Three attempts are made to close or modify                       |
//+------------------------------------------------------------------+
int HandleOpenPositions(int MagicNum, bool BuyExit, bool SellExit)
{
   int cnt, total;
   int CloseCnt, err;
   double os;
   
   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      if ( OrderSelect (cnt, SELECT_BY_POS) == false )  continue;
      if ( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNum)  continue;
      
      if((OrderType() == OP_BUY || OrderType() == OP_BUYSTOP) && (OrderSymbol()==Symbol()))
      {
            
            if (BuyExit)
            {
   // try to close 3 Times
      
              CloseCnt = 0;
              while (CloseCnt < 3)
              {
                if (!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet))
                {
                 err=GetLastError();
                 Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
                 if (err > 0) CloseCnt++;
                }
                else
                {
                 CloseCnt = 3;
                }
               }
             }
             else
             {
              if(TrailingStop>0) 
               {
                 os = OrderStopLoss();                
//                 if(Bid-os > 2*Point*TrailingStop)
                 if(Bid-os > Point*StopLoss)
                 {
                   CloseCnt=0;
                   while (CloseCnt < 3)
                   {
//                     if (!OrderModify(OrderTicket(),OrderOpenPrice(),os+Point*TrailingStop,OrderTakeProfit(),0,Aqua))
                     if (!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*StopLoss,OrderTakeProfit(),0,Aqua))
                     {
                        err=GetLastError();
                        if (err>0) CloseCnt++;
                     }
                     else
                     {
                      CloseCnt = 3;
                     }
                   }
                 }
              }
             }
          }
      if((OrderType() == OP_SELL || OrderType() == OP_SELLSTOP) && (OrderSymbol()==Symbol()))
          {
            if (SellExit)
            {
            
   // try to close 3 Times
      
               CloseCnt = 0;
               while (CloseCnt < 3)
               {
                 if (!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet))
                 {
                  err=GetLastError();
                  Print(CloseCnt," Error closing order : ",cnt," (", err , ") " + ErrorDescription(err));
                  if (err > 0) CloseCnt++;
                 }
                 else CloseCnt = 3;
               }
             }
             else
             {
  
   // try to modify 3 Times
      
                 if(TrailingStop>0)  
                 {                
                  os = OrderStopLoss();                
//                  if(os - Ask > 2*Point*TrailingStop)
                  if(os - Ask > Point*StopLoss)
                  {
                     CloseCnt = 0;
                     while (CloseCnt < 3)
                     {
//                       if (!OrderModify(OrderTicket(),OrderOpenPrice(),os-Point*TrailingStop,OrderTakeProfit(),0,Aqua))
                       if (!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*StopLoss,OrderTakeProfit(),0,Aqua))
                       {
                         err=GetLastError();
                         if (err > 0) CloseCnt++;
                       }
                       else
                       {
                        CloseCnt = 3;
                        }
                     }
                  }
                }
             }
       }
  }
}
     
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
//---- select lot size
   lot=NormalizeDouble(MathFloor(AccountBalance()*Riskpercent/10000)/10,1);
   
  
  // lot at this point is number of standard lots
  
//  if (Debug) Print ("Lots in LotsOptimized : ",lot);
  
  // Check if mini or standard Account
  
  if(AccountIsMini)
  {
    lot = MathFloor(lot*10)/10;
    
// Use at least 1 mini lot
   if(lot<0.1) lot=0.1;
  }else
  {
    if (lot < 1.0) lot = 1.0;
    if (lot > MaxLots) lot = MaxLots;
  }
   return(lot);
  }
//+------------------------------------------------------------------+
//| Time frame interval appropriation  function                      |
//+------------------------------------------------------------------+
int func_TimeFrame_Const2Val(int Constant ) {
   switch(Constant) {
      case 1:  // M1
         return(1);
      case 5:  // M5
         return(2);
      case 15:
         return(3);
      case 30:
         return(4);
      case 60:
         return(5);
      case 240:
         return(6);
      case 1440:
         return(7);
      case 10080:
         return(8);
      case 43200:
         return(9);
   }
}
//+------------------------------------------------------------------+
//| Time frame string appropriation  function                               |
//+------------------------------------------------------------------+
string func_TimeFrame_Val2String(int Value ) {
   switch(Value) {
      case 1:  // M1
         return("PERIOD_M1");
      case 2:  // M1
         return("PERIOD_M5");
      case 3:
         return("PERIOD_M15");
      case 4:
         return("PERIOD_M30");
      case 5:
         return("PERIOD_H1");
      case 6:
         return("PERIOD_H4");
      case 7:
         return("PERIOD_D1");
      case 8:
         return("PERIOD_W1");
      case 9:
         return("PERIOD_MN1");
   	default: 
   		return("undefined " + Value);
   }
}
int func_Symbol2Val(string symbol) {
	if(symbol=="AUDCAD") {
		return(1);
	} else if(symbol=="AUDJPY") {
		return(2);
	} else if(symbol=="AUDNZD") {
		return(3);
	} else if(symbol=="AUDUSD") {
		return(4);
	} else if(symbol=="CHFJPY") {
		return(5);
	} else if(symbol=="EURAUD") {
		return(6);
	} else if(symbol=="EURCAD") {
		return(7);
	} else if(symbol=="EURCHF") {
		return(8);
	} else if(symbol=="EURGBP") {
		return(9);
	} else if(symbol=="EURJPY") {
		return(10);
	} else if(symbol=="EURUSD") {
		return(11);
	} else if(symbol=="GBPCHF") {
		return(12);
	} else if(symbol=="GBPJPY") {
		return(13);
	} else if(symbol=="GBPUSD") {
		return(14);
	} else if(symbol=="NZDUSD") {
		return(15);
	} else if(symbol=="USDCAD") {
		return(16);
	} else if(symbol=="USDCHF") {
		return(17);
	} else if(symbol=="USDJPY") {
		return(18);
	} else {
		Comment("unexpected Symbol");
		return(0);
	}
}
             
            
Comments