Skydart_v1[1].1

Author: Copyright � 2007, Robert Hill
Price Data Components
Series array that contains close prices for each bar
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
MACD HistogramBollinger bands indicatorMoving average indicatorCommodity channel indexParabolic Stop and Reverse system
0 Views
0 Downloads
0 Favorites
Skydart_v1[1].1
//+------------------------------------------------------------------+
//|                                                      Skydart.mq4 |
//|                                                Copyright © 2006  |
//|                                                                  |
//|    Written by Robert Hill aka MrPip for Forex-tsd group          |
//|                                                                  |
//| Version 1.0                                                      |
//|  Indicators                                                      |
//|     Heiken_Ashi_Ma, Fisher_m11, BBands_Stop_v2                   |
//|                                                                  |
//|  Includes 7 different types of trailing stops                    |
//|  Code from IgorAD, KimIV and MrPip modified to be functions      |
//|                                                                  |
//|  Added Damiani_volatmeter for filter                             |
//|                                                                  |
//| 5/6/2007 Added Check of 4 hr trend                               |
//|          Uses HMA3 indicator                                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Robert Hill"
#property link      "http://www.forex-tsd.com/"
#include <stdlib.mqh>

#define UP 1
#define DOWN 2

//+---------------------------------------------------+
//|Account functions                                  |
//+---------------------------------------------------+
extern bool    AccountIsMini = true;      // 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  TradeSizePercent = 10;  // Change to whatever percent of equity you wish to risk.
extern double  Lots = 0.1;             // standard lot size. 
extern double  MaxLots = 100;

//+---------------------------------------------------+
//|Indicator Variables                                |
//| Change these to try your own system               |
//| or add more if you like                           |
//+---------------------------------------------------+

extern string  Expert_Name    = "---- Skydart_EA_v1.1 ----";
extern int     SignalCandle = 1;
extern string  sw="--Switches--";
extern string  sw1=" 0 = OFF, 1 = ON";
extern int     useXpMA = 1;
extern int     useCCI = 1;
extern int     useWaddah = 1;
extern int     useDamianiFilter = 1;
extern int     use4HrTrendEntry = 1;
extern int     useDailyTrendEntry = 1;


extern int     useXpMAExit = 0;
extern int     useCCIExit = 0;
extern int     useWaddahExit = 1;

extern string  s1 = "xpMA Settings";
extern int     MA_Period  = 34;
extern int     MA_Type    = MODE_EMA;
extern int     MA_Applied = PRICE_CLOSE;

extern string  s2 = "CCI Settings";
extern int     TrendCCI_Period = 34;

extern string  s3 = "Waddah Settings";
extern int     Sensetive = 100;
extern int     DeadZonePip = 40;
extern bool    usePower = true;
extern int     ExplosionPower = 5;
extern int     TrendPower = 5;

extern string  s5="--Damiani Filter settings--";
extern int     Viscosity=10;
extern int     Sedimentation=60;
extern double  Threshold_level=1.4;
extern bool    lag_supressor=true;

extern string  ss="-- Trend HMA settings--";
extern int     T4_Period=12;
extern double  T4_Threshold=5;
extern int     T4_PrevShift=3;
extern int     T4_CurShift=1;
extern int     T4_TimeFrame = 240;
extern int     T1_Period=12;
extern double  T1_Threshold=5;
extern int     T1_PrevShift=3;
extern int     T1_CurShift=1;
extern int     T1_TimeFrame = 1440;
extern int     HMA_method = 0;
extern int     HMA_price = 0;

extern string  s44="--Signal_TimeFrame--";
extern string  s54=" 15, 30 or 60 minute";
extern int     Signal_TimeFrame = 30;
extern string  sm="--Trading Hours--";
extern bool    UseTradingHours = true;
extern bool    TradeAsianMarket = true;
extern int     StartHour1 = 0;       // Start trades after time
extern int     StopHour1 = 3;      // Stop trading after time
extern bool    TradeEuropeanMarket = true;
extern int     StartHour2 = 9;       // Start trades after time
extern int     StopHour2 = 11;      // Stop trading after time
extern bool    TradeNewYorkMarket = true;
extern int     StartHour3 = 15;       // Start trades after time
extern int     StopHour3 = 17;      // Stop trading after time

extern string  hd = " --Delay time--";
extern int     useDelay = 1;
extern int     useDelayAfterWin = 1;
extern int     MinutesToDelay = 180;

//+---------------------------------------------------+
//|Profit controls                                    |
//+---------------------------------------------------+
extern string  st6 = "--Profit Controls--";
extern double  StopLoss = 100;          // Maximum pips willing to lose per position.
extern int     TakeProfit=15;
extern int     Slippage = 3;          // Possible fix for not getting filled or closed 
extern string  tsp0 = "Trailing Stop Types";
extern string  tsp1 = " 1 = Trail immediately";
extern string  tsp2 = " 2 = Wait to trail";
extern string  tsp3 = " 3 = Uses 3 levels before trail";
extern string  tsp4 = " 4 = Breakeven + Lockin";
extern string  tsp5 = " 5 = Step trail";
extern string  tsp6 = " 6 = EMA trail";
extern string  tsp7 = " 7 = pSAR trail";
extern bool    UseTrailingStop = true;
extern int     TrailingStopType = 1;

extern string  ts2 = "Settings for Type 2";
extern double  TrailingStop = 25;      // Change to whatever number of pips you wish to trail your position with.

extern string  ts3 = "Settings for Type 3";
extern double  FirstMove = 20;        // Type 3  first level pip gain
extern double  FirstStopLoss = 50;    // Move Stop to Breakeven
extern double  SecondMove = 30;       // Type 3 second level pip gain
extern double  SecondStopLoss = 30;   // Move stop to lock is profit
extern double  ThirdMove = 40;        // type 3 third level pip gain
extern double  TrailingStop3 = 20;    // Move stop and trail from there

extern string  ts4 = "Settings for Type 4";
extern double  BreakEven = 25;
extern int     LockInPips = 1;        // Profit Lock in pips

extern string  ts5 = "Settings for Type 5";
extern int     eTrailingStop   = 10;
extern int     eTrailingStep   = 2;

extern string  ts6 = "Settings for Type 6";
extern int     EMATimeFrame    =  30;
extern int     Price           =  0;
extern int     EMAPeriod       = 13;
extern int     EMAShift        =  2;    
extern int     InitialStop     =  0; 

extern string  ts7 = "Settings for Type 7";
extern double  StepParabolic = 0.02;
extern double  MaxParabolic  = 0.2;
extern int     Interval      = 5;

   
//+---------------------------------------------------+
//|General controls                                   |
//+---------------------------------------------------+
string setup;
double lotMM;
int TradesInThisSymbol;
int MagicNumber;
bool YesStop;
datetime StopTime;                 // Time to wait after a trade is stopped out
bool StoppedOut=false;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
	MagicNumber = 3000 + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period()); 

   setup = Expert_Name + Symbol() + "_" + func_TimeFrame_Val2String(func_TimeFrame_Const2Val(Period()));
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  

//===================================================================
//                                                    EMA_Angle.mq4 
//                                                          jpkfox
//
// You can use this indicator to measure when the EMA angle is
// "near zero". AngleTreshold determines when the angle for the
// EMA is "about zero": This is when the value is between
// [-AngleTreshold, AngleTreshold] (or when the histogram is red).
//   EMAPeriod: EMA period 
//   AngleTreshold: The angle value is "about zero" when it is
//     between the values [-AngleTreshold, AngleTreshold].
//   StartEMAShift: The starting point to calculate the 
//     angle. This is a shift value to the left from the
//     observation point. Should be StartEMAShift > EndEMAShift.
//   StartEMAShift: The ending point to calculate the
//     angle. This is a shift value to the left from the
//     observation point. Should be StartEMAShift > EndEMAShift.
//
//  Return 1 for up OK
//  Return -1 for down OK
//  Return 0 for too flat
//===================================================================
double MA_Angle( int PrevShift, int CurShift, double fPrevMA, double fCurMA)
{
   double fAngle, mFactor;
   string Sym;
   int ShiftDif;
   
   mFactor = 100000.0;
   Sym = StringSubstr(Symbol(),3,3);
   if (Sym == "JPY") mFactor = 1000.0;
   ShiftDif = PrevShift-CurShift;
   mFactor /= ShiftDif; 

    fAngle = mFactor * (fCurMA - fPrevMA)/2.0;

    return(fAngle);
}

bool CheckDailyTrend(int cmd)
{
   double MACurrent, MAPrevious;
	double maAngle;
	
   MACurrent=iCustom(NULL,T1_TimeFrame,"HMA3",T1_Period,HMA_method,HMA_price,0,T1_CurShift);
   MAPrevious=iCustom(NULL,T1_TimeFrame,"HMA3",T1_Period,HMA_method,HMA_price,0,T1_PrevShift);
	maAngle = MA_Angle(T1_PrevShift, T1_CurShift, MAPrevious, MACurrent);
   switch (cmd)
   {
       case OP_BUY : if (maAngle > T1_Threshold) return(true);
                     break;
       case OP_SELL : if (maAngle < -T1_Threshold) return(true);
   }
   
   return(false);
}

bool Check4HrTrend (int cmd)
{
   double MACurrent, MAPrevious;
	double maAngle;

   MACurrent=iCustom(NULL,T4_TimeFrame,"HMA3",T4_Period,HMA_method,HMA_price,0,T4_CurShift);
   MAPrevious=iCustom(NULL,T4_TimeFrame,"HMA3",T4_Period,HMA_method,HMA_price,0,T4_PrevShift);
   
	maAngle = MA_Angle(T4_PrevShift, T4_CurShift, MAPrevious, MACurrent);
   switch (cmd)
   {
      case OP_BUY : if (maAngle > T4_Threshold) return(true);
                    break;
      case OP_SELL : if (maAngle < -T4_Threshold) return(true);
   }
   
   return(false);
 }
 
// Code taken from Waddah_Attr_Explosion indicator 
bool Check_WaddahEnter(int cmd)
  {
   double Trend1, Trend2, Explo1, Explo2, Dead;
   double pwrt, pwre;
   
//  double  sig1=iCustom(NULL,0,"Explosion_Gold",0,SignalCandle);
//  double sig2=iCustom(NULL,0,"Explosion_Gold",1,SignalCandle);
//  double  sig3=iCustom(NULL,0,"Explosion_Gold",2,SignalCandle);
//  double sig4=iCustom(NULL,0,"Explosion_Gold",2,SignalCandle+1);

   Trend1 = (iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle) - 
             iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 1))*Sensetive;
   Trend2 = (iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 2) - 
             iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 3))*Sensetive;
   Explo1 = (iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, SignalCandle) - 
             iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, SignalCandle));
   Explo2 = (iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, SignalCandle + 1) - 
             iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, SignalCandle + 1));
   Dead = Point * DeadZonePip;
       
/*
   if(Trend1 >= 0) ind_buffer1[i] = Trend1;
   if(Trend1 < 0) ind_buffer2[i] = (-1*Trend1);
   ind_buffer3[i] = Explo1;
   ind_buffer4[i] = Dead;
*/
       
   switch (cmd)
   {
   case OP_BUY :
       if(Trend1 > 0 && Trend1 > Explo1 && Trend1 > Dead && 
          Explo1 > Dead && Explo1 > Explo2 && Trend1 > Trend2 )
       {
           if (usePower)
           {
              pwrt = 100*(Trend1 - Trend2) / Trend1;
              pwre = 100*(Explo1 - Explo2) / Explo1;
              if(pwre >= ExplosionPower && pwrt >= TrendPower)
              {
// Was give an alert to Buy
                 return(true);
              }
           }
           else
              return(true);
       }
       break;
   case OP_SELL : 
       if(Trend1 < 0 && MathAbs(Trend1) > Explo1 && MathAbs(Trend1) > Dead && 
          Explo1 > Dead && Explo1 > Explo2 && MathAbs(Trend1) > MathAbs(Trend2))
       {
           if (usePower)
           {
              pwrt = 100*(MathAbs(Trend1) - MathAbs(Trend2)) / MathAbs(Trend1);
              pwre = 100*(Explo1 - Explo2) / Explo1;
              if(pwre >= ExplosionPower && pwrt >= TrendPower)
              {
// Was give an alert to Sell
                return(true);
              }
           }
           else
               return(true);
       }
   }
   
   return(false);
}

// uses OP_BUY to check exit sell
// uses OP_SELL to check exit buy
bool Check_WaddahExit(int cmd)
  {
   double Trend1, Trend2, Explo1, Explo2, Dead;
   double pwrt, pwre;
   
//  double  sig1=iCustom(NULL,0,"Explosion_Gold",0,SignalCandle);
//  double sig2=iCustom(NULL,0,"Explosion_Gold",1,SignalCandle);
//  double  sig3=iCustom(NULL,0,"Explosion_Gold",2,SignalCandle);
//  double sig4=iCustom(NULL,0,"Explosion_Gold",2,SignalCandle+1);
   Trend1 = (iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle) - 
             iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 1))*Sensetive;
   Trend2 = (iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 2) - 
             iMACD(NULL, Signal_TimeFrame, 20, 40, 9, PRICE_CLOSE, MODE_MAIN, SignalCandle + 3))*Sensetive;
   Explo1 = (iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, SignalCandle) - 
             iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, SignalCandle));
   Explo2 = (iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, SignalCandle + 1) - 
             iBands(NULL, Signal_TimeFrame, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, SignalCandle + 1));
   Dead = Point * DeadZonePip;
   
/*
   if(Trend1 >= 0) ind_buffer1[i] = Trend1;
   if(Trend1 < 0)  ind_buffer2[i] = (-1*Trend1);
   ind_buffer3[i] = Explo1;
   ind_buffer4[i] = Dead;
*/
       
   switch (cmd)
   {
   case OP_SELL :
       if(Trend1 > 0 && Trend1 < Explo1 && Trend1 < Trend2 && Trend2 > Explo2 && 
          Trend1 > Dead && Explo1 > Dead)
       {
// Was give an alert to exit buy
             return(true);
       }
       break;
   case OP_BUY : 
       if(Trend1 < 0 && MathAbs(Trend1) < Explo1 && 
          MathAbs(Trend1) < MathAbs(Trend2) && MathAbs(Trend2) > Explo2 && 
          Trend1 > Dead && Explo1 > Dead)
       {
// Was give an alert to Exit Sell
             return(true);
       }
   }
   return(false);
 }

bool Check_XpMA(int cmd)
{
//  double sig5=iCustom(NULL,0,"xpMA",1,shift);
//  double sig6=iCustom(NULL,0,"xpMA",2,shift);
//  double sig7=iCustom(NULL,0,"xpMA",4,shift);
// Only code needed from xpMA
  double sig7 = iMA(NULL,Signal_TimeFrame,MA_Period,0,MA_Type,MA_Applied,SignalCandle);

  switch (cmd)
  {
  case OP_BUY : if (iClose(NULL, Signal_TimeFrame, SignalCandle) > sig7) return(true);
                break;
  case OP_SELL : if (iClose(NULL, Signal_TimeFrame, SignalCandle) < sig7) return(true);
  }
  
  return(false);
  
}

bool Check_CCI(int cmd)
{
//  double sig8=iCustom(NULL,0,"DoubleCCI_Woodies",4,shift);
// Only code needed from DoubleCCI_Woodies
  double sig8 = iCCI(NULL, Signal_TimeFrame, TrendCCI_Period, PRICE_TYPICAL, SignalCandle);
  
  switch(cmd)
  {
  case OP_BUY : if (sig8 > 0) return(true);
                break;
  case OP_SELL : if (sig8 < 0) return(true);
  }
  return(false);

}

bool CheckDamianiFilter()
{
   double DamianiGreen, DamianiGray;

   DamianiGray = iCustom(NULL, Signal_TimeFrame, "Damiani_volatmeter", Viscosity, Sedimentation, Threshold_level,lag_supressor, 0, SignalCandle);
   DamianiGreen = iCustom(NULL, Signal_TimeFrame, "Damiani_volatmeter", Viscosity, Sedimentation, Threshold_level,lag_supressor, 2, SignalCandle);
   if (DamianiGreen > DamianiGray)
   {
     return (true);
   }
   
   return(false);
}

//+------------------------------------------------------------------+
//| CheckExitCondition                                               |
//| Uses OP_BUY as cmd to check exit sell                            |
//| Uses OP_SELL as cmd to check exit buy                            |
//+------------------------------------------------------------------+
bool CheckExitCondition(int cmd)
{
   bool rule1, rule2, rule3;
   
   rule1 = true;
   rule2 = true;
   rule3 = true;
   
// The rules are checked this way for speed

   if (useWaddahExit == 1) rule1 = Check_WaddahExit(cmd);
   if (rule1)
   {
       if (useXpMAExit == 1) rule2 = Check_XpMA(cmd);
       if (rule2)
       {
         if (useCCIExit == 1) rule3 = Check_CCI(cmd);
         if (rule3)
         {
            return (true);
         }
       }
   }
   
   return (false);
}

//+------------------------------------------------------------------+
//| CheckEntryCondition                                              |
//+------------------------------------------------------------------+
bool CheckEntryCondition(int cmd)
{
   bool rule1, rule2, rule3, rule4, rule5, rule6;
   
   rule1 = true;
   rule2 = true;
   rule3 = true;
   rule4 = true;
   rule5 = true;
   rule6 = true;
   
// The rules are checked this way for speed
  
     if (useWaddah == 1) rule1 = Check_WaddahEnter(cmd);
     if (rule1)
     {
       if (useXpMA == 1) rule2 = Check_XpMA(cmd);
       if (rule2)
       {
          if (useCCI == 1) rule3 = Check_CCI(cmd);
          if (rule3)
          {
            if (use4HrTrendEntry == 1) rule5 = Check4HrTrend(cmd);
            if (rule5)
            {
              if (useDailyTrendEntry == 1) rule6 = CheckDailyTrend(cmd);
              if (rule6)
              {
                if (useDamianiFilter == 1) rule4 = CheckDamianiFilter();
                if (rule4) return(true);
              }
            }
          }
       }
     }
   
   return (false);
}
  
//+------------------------------------------------------------------+
//| LastTradeStoppedOut                                              |
//| Check History to see if last trade stopped out                   |
//| Return Time for next trade                                       |
//+------------------------------------------------------------------+
  
datetime LastTradeStoppedOut()
{
   int cnt, total;
   datetime NextTime;
   bool Stopped;
   
   NextTime = 0;
   
   total = HistoryTotal();
   for (cnt = total - 1; cnt >= 0; cnt--)
   {
      OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);
      
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
      {
        Stopped = false;
        if (OrderType() == OP_BUY)
        {
          if (OrderClosePrice() - OrderOpenPrice() < 0)
          {
             Stopped = true;
          }
          if(useDelayAfterWin == 1) Stopped = true;
          cnt = 0;
        }
        if (OrderType() == OP_SELL)
        {
          if (OrderOpenPrice() - OrderClosePrice() < 0)
          {
             Stopped = true;
          }
          if(useDelayAfterWin == 1) Stopped = true;
          cnt = 0;
        }
      }
   }
   
   if (Stopped)
   {
      StopTime = OrderCloseTime() + MinutesToDelay*60;
   }
   
   return (Stopped);
}

bool CheckTradingTimes()
{
   bool StopTrading;
   
     StopTrading = true;
// Check trading Asian Market
     if (TradeAsianMarket)
     {
        if (StartHour1 > 18)
        {
// Check broker that uses Asian open before 0:00

          if (Hour() >= StartHour1) StopTrading = false;
          if (!StopTrading)
          {
            if (StopHour1 < 24)
            {
              if ( Hour() <= StopHour1) StopTrading = false;
            }
// These cannot be combined even though the code looks the same
            if (StopHour1 >=0)
            {
              if ( Hour() <= StopHour1) StopTrading = false;
            }
          }
        }
        else
        {
          if (Hour() >= StartHour1 && Hour() <= StopHour1) StopTrading = false;
        }
     }
     if (StopTrading)
     {
// Check trading European Market
       if (TradeEuropeanMarket)
       {
         if (Hour() >= StartHour2 && Hour() <= StopHour2) StopTrading = false;
       }
     }
     if (StopTrading)
     {
// Check trading European Market
       if (TradeNewYorkMarket)
       {
         if (Hour() >= StartHour3 && Hour() <= StopHour3) StopTrading = false;
       }
     }
     
     return(StopTrading);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//| Code taken from custom indicators                                |
//+------------------------------------------------------------------+
int start()
  {

//+------------------------------------------------------------------+
//| Check for Open Position                                          |
//+------------------------------------------------------------------+

     HandleOpenPositions();
     
// Check if any open positions were not closed

     TradesInThisSymbol = CheckOpenPositions();
     
//+------------------------------------------------------------------+
//| Check if OK to make new trades                                   |
//+------------------------------------------------------------------+

// Check if last trade stopped out

   if (useDelay == 1)
   {
     StoppedOut = LastTradeStoppedOut();
     if  (CurTime() < StopTime)
     {
//    Comment("No Trades Until : " + TimeToStr(StopTime,TIME_DATE|TIME_MINUTES));
      return(0);
     }
   }

   
   YesStop = false;
   if (UseTradingHours) YesStop = CheckTradingTimes();
   
   if (YesStop)
   {
//      Comment ("Trading has been stopped as requested - wrong time of day");
       return (0);
   }
     
// Only allow 1 trade per Symbol

     if(TradesInThisSymbol > 0) {
       return(0);}
   

   lotMM = GetLots();
   
   
	if(CheckEntryCondition(OP_BUY))
	{
		   OpenBuyOrder();
		   return(0);
	}

   
	if(CheckEntryCondition(OP_SELL))
	{
         OpenSellOrder();
	}
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| OpenBuyOrder                                                     |
//| If Stop Loss or TakeProfit are used the values are calculated    |
//| for each trade                                                   |
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
   int err,ticket, digits;
   double myPrice, myStopLoss = 0, myTakeProfit = 0;
   
   myPrice = MarketInfo(Symbol(), MODE_ASK);
   myStopLoss = 0;
   if ( StopLoss > 0 ) myStopLoss = myPrice - StopLoss * Point ;
   myTakeProfit = 0;
   if (TakeProfit>0) myTakeProfit = myPrice + TakeProfit * Point;
 // Normalize all price / stoploss / takeprofit to the proper # of digits.
   digits = MarketInfo(Symbol( ), MODE_DIGITS) ;
   if (digits > 0) 
   {
     myPrice = NormalizeDouble( myPrice, digits);
     myStopLoss = NormalizeDouble( myStopLoss, digits);
     myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
   }
   ticket=OrderSend(Symbol(),OP_BUY,lotMM,myPrice,Slippage,myStopLoss,myTakeProfit,setup,MagicNumber,0,Green); 
   if (ticket > 0)
   {
    if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) 
     {
      Print("BUY order opened : ", OrderOpenPrice( ));
//      ModifyOrder(ticket,OrderOpenPrice( ), OrderStopLoss(), myTakeProfit);
    }
   }
   else
   {
      err = GetLastError();
      Print("Error opening BUY order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); 
   }
}

//+------------------------------------------------------------------+
//| OpenSellOrder                                                    |
//| If Stop Loss or TakeProfit are used the values are calculated    |
//| for each trade                                                   |
//+------------------------------------------------------------------+
void OpenSellOrder()
{
   int err, ticket, digits;
   double myPrice, myStopLoss = 0, myTakeProfit = 0;
   
   myPrice = MarketInfo(Symbol( ), MODE_BID);
   myStopLoss = 0;
   if ( StopLoss > 0 ) myStopLoss = myPrice + StopLoss * Point;
   myTakeProfit = 0;
   if (TakeProfit > 0) myTakeProfit = myPrice - TakeProfit * Point;
 // Normalize all price / stoploss / takeprofit to the proper # of digits.
   digits = MarketInfo(Symbol( ), MODE_DIGITS) ;
   if (digits > 0) 
   {
     myPrice = NormalizeDouble( myPrice, digits);
     myStopLoss = NormalizeDouble( myStopLoss, digits);
     myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
   }
   ticket=OrderSend(Symbol(),OP_SELL,lotMM,myPrice,Slippage,myStopLoss,myTakeProfit,setup,MagicNumber,0,Red); 
   if (ticket > 0)
   {
     if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) 
     {
      Print("Sell order opened : ", OrderOpenPrice());
//      ModifyOrder(ticket,OrderOpenPrice( ), OrderStopLoss(), myTakeProfit);
     }
   }
   else
   {
      err = GetLastError();
      Print("Error opening Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); 
   }
}

//+------------------------------------------------------------------+
//| Check Open Position Controls                                     |
//+------------------------------------------------------------------+
  
int CheckOpenPositions()
{
   int cnt, total, NumPositions;
   int NumBuyTrades, NumSellTrades;   // Number of buy and sell trades in this symbol
   
   NumBuyTrades = 0;
   NumSellTrades = 0;
   total=OrdersTotal();
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol() != Symbol()) continue;
      if ( OrderMagicNumber() != MagicNumber)  continue;
      
      if(OrderType() == OP_BUY )  NumBuyTrades++;
      if(OrderType() == OP_SELL ) NumSellTrades++;
             
     }
     NumPositions = NumBuyTrades + NumSellTrades;
     return (NumPositions);
  }

int CloseOrder(int ticket,double numLots,int cmd)
{
   int CloseCnt, err, digits;
   double myPrice;
   
   if (cmd == OP_BUY) myPrice = MarketInfo(Symbol( ), MODE_BID);
   if (cmd == OP_SELL) myPrice = MarketInfo(Symbol( ), MODE_ASK);
   digits = MarketInfo(Symbol( ), MODE_DIGITS) ;
   if (digits > 0)  myPrice = NormalizeDouble( myPrice, digits);
   // try to close 3 Times
      
    CloseCnt = 0;
    while (CloseCnt < 3)
    {
       if (!OrderClose(ticket,numLots,myPrice,Slippage,Violet))
       {
         err=GetLastError();
         Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
         if (err > 0) CloseCnt++;
       }
       else
       {
         CloseCnt = 3;
       }
    }
}

int ModifyOrder(int ord_ticket,double op, double price,double tp, color mColor)
{
    int CloseCnt, err;
    
    CloseCnt=0;
    while (CloseCnt < 3)
    {
       if (OrderModify(ord_ticket,op,price,tp,0,mColor))
       {
         CloseCnt = 3;
       }
       else
       {
          err=GetLastError();
          Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err));
         if (err>0) CloseCnt++;
       }
    }
}

double ValidStopLoss(int type, double price, double SL)
{

   double minstop, pp;
   
   pp = MarketInfo(Symbol(), MODE_POINT);
   minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (type == OP_BUY)
   {
		 if((price - SL) < minstop*pp) SL = price - minstop*pp;
   }
   if (type == OP_SELL)
   {
       if((SL-price) < minstop*pp)  SL = price + minstop*pp;  
   }

   return(SL);   
}
//+------------------------------------------------------------------+
//|                                           BreakEvenExpert_v1.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
void BreakEven_TrailingStop(int type, int ticket, double op, double os, double tp)
{

   int digits;
   double pBid, pAsk, pp, BuyStop, SellStop;

   pp = MarketInfo(Symbol(), MODE_POINT);
   digits = MarketInfo(Symbol(), MODE_DIGITS);
   
  if (type==OP_BUY)
  {
    pBid = MarketInfo(Symbol(), MODE_BID);
    if ( pBid-op > pp*BreakEven ) 
    {
       BuyStop = op + LockInPips * pp;
       if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
		 BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);   
       if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
       if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
		 return;
	 }
  }
  if (type==OP_SELL)
  {
    pAsk = MarketInfo(Symbol(), MODE_ASK);
    if ( op - pAsk > pp*BreakEven ) 
    {
       SellStop = op - LockInPips * pp;
       if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
       SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
       if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
       if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
		 return;
    }
  }   

}

//+------------------------------------------------------------------+
//|                                                   e-Trailing.mq4 |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//| 12.09.2005 Àâòîìàòè÷åñêèé Trailing Stop âñåõ îòêðûòûõ ïîçèöèé    |
//|            Âåøàòü òîëüêî íà îäèí ãðàôèê                          |
//+------------------------------------------------------------------+
void eTrailingStop(int type, int ticket, double op, double os, double tp)
{

  int digits;
  double pBid, pAsk, pp, BuyStop, SellStop;

  pp = MarketInfo(Symbol(), MODE_POINT);
  digits = MarketInfo(Symbol(), MODE_DIGITS) ;
  if (type==OP_BUY)
  {
    pBid = MarketInfo(Symbol(), MODE_BID);
    if ((pBid-op)>eTrailingStop*pp)
    {
      if (os<pBid-(eTrailingStop+eTrailingStep-1)*pp)
      {
        BuyStop = pBid-eTrailingStop*pp;
        if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
		  BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
        if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
        ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
        return;
      }
    }
  }
  if (type==OP_SELL)
  {
    pAsk = MarketInfo(Symbol(), MODE_ASK);
    if (op - pAsk > eTrailingStop*pp)
    {
      if (os > pAsk + (eTrailingStop + eTrailingStep-1)*pp || os==0)
      {
        SellStop = pAsk + eTrailingStop * pp;
        if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
        SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
        if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
        ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
        return;
      }
    }
  }

}

//+------------------------------------------------------------------+
//|                                           EMATrailingStop_v1.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
void EMA_TrailingStop(int type, int ticket, double op, double os, double tp)
{
   int digits;
   double pBid, pAsk, pp, BuyStop, SellStop, ema;

   pp = MarketInfo(Symbol(), MODE_POINT);
   digits = MarketInfo(Symbol(), MODE_DIGITS) ;
   ema = iMA(Symbol(),EMATimeFrame,EMAPeriod,0,MODE_EMA,Price,EMAShift);
   
   if (type==OP_BUY) 
   {
	   BuyStop = ema;
      pBid = MarketInfo(Symbol(),MODE_BID);
		if(os == 0 && InitialStop>0 ) BuyStop = pBid-InitialStop*pp;
		if (digits > 0) BuyStop = NormalizeDouble(SellStop, digits); 
		BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
		if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
		Print("MA=",ema," BuyStop=",BuyStop);
		if ((op <= BuyStop && BuyStop > os) || os==0) 
		{
          ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
			 return;
      }
   }   

   if (type==OP_SELL)
   {
	   SellStop = ema;
      pAsk = MarketInfo(Symbol(),MODE_ASK);
      if (os==0 && InitialStop > 0) SellStop = pAsk+InitialStop*pp;
		if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
		SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
		if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
      Print("MA=",ema," SellStop=",SellStop);   
      if( (op >= SellStop && os > SellStop) || os==0) 
      {
          ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
			 return;
      }
   }
}

//+------------------------------------------------------------------+
//|                                                b-TrailingSAR.mqh |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//|    21.11.2005  Áèáëèîòåêà ôóíêöèé òðàëà ïî ïàðàáîëèêó.           |
//|  Äëÿ èñïîëüçîâàíèÿ äîáàâèòü ñòðîêó â ìîäóëå start                |
//|  if (UseTrailing) TrailingPositions();                           |
//+------------------------------------------------------------------+
void pSAR_TrailingStop(int type, int ticket, double op, double os, double tp)
{
   int digits;
   double pBid, pAsk, pp, BuyStop, SellStop, spr;
   double sar1, sar2;
  
   pp = MarketInfo(Symbol(), MODE_POINT);
   digits = MarketInfo(Symbol(), MODE_DIGITS) ;
   pBid = MarketInfo(Symbol(), MODE_BID);
   pAsk = MarketInfo(Symbol(), MODE_ASK);
   sar1=iSAR(NULL, 0, StepParabolic, MaxParabolic, 1);
   sar2=iSAR(NULL, 0, StepParabolic, MaxParabolic, 2);
   spr = pAsk - pBid;
   if (digits > 0) spr = NormalizeDouble(spr, digits); 
   
   if (type==OP_BUY)
   {
     pBid = MarketInfo(Symbol(), MODE_BID);
     if (sar2 < sar1)
     {
        BuyStop = sar1-Interval*pp;
        if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
	     BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
        if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
        if (os<BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
     }
   }
   if (type==OP_SELL)
   {
     if (sar2 > sar1)
     {
        SellStop = sar1 + Interval * pp + spr;
        if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
	     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);   
        if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
        if (os>SellStop || os==0) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
     }
   }
}

//+------------------------------------------------------------------+
//|                                      ThreeLevel_TrailingStop.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by MrPip,robydoby314@yahoo.com   |   
//|                                                                  |
//| Uses up to 3 levels for trailing stop                            |
//|      Level 1 Move stop to 1st level                              |
//|      Level 2 Move stop to 2nd level                              |
//|      Level 3 Trail like type 1 by fixed amount other than 1      |
//+------------------------------------------------------------------+
void ThreeLevel_TrailingStop(int type, int ticket, double op, double os, double tp)
{

   int digits;
   double pBid, pAsk, pp, BuyStop, SellStop;

   pp = MarketInfo(Symbol(), MODE_POINT);
   digits = MarketInfo(Symbol(), MODE_DIGITS) ;

   if (type == OP_BUY)
   {
      pBid = MarketInfo(Symbol(), MODE_BID);
      if (pBid - op > FirstMove * pp)
      {
         BuyStop = op + FirstMove*pp - FirstStopLoss * pp;
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
		   BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
         if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
      }
              
      if (pBid - op > SecondMove * pp)
      {
         BuyStop = op + SecondMove*pp - SecondStopLoss * pp;
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
		   BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
         if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
      }
                
      if (pBid - op > ThirdMove * pp)
      {
         BuyStop = pBid  - ThirdMove*pp;
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
		   BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);   
         if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits); 
         if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
      }
   }
       
    if (type ==  OP_SELL)
    {
        pAsk = MarketInfo(Symbol(), MODE_ASK);
        if (op - pAsk > FirstMove * pp)
        {
           SellStop = op - FirstMove * pp + FirstStopLoss * pp;
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
		     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);   
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
           if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
        }
        if (op - pAsk > SecondMove * pp)
        {
           SellStop = op - SecondMove * pp + SecondStopLoss * pp;
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
		     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);   
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
           if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
        }
        if (op - pAsk > ThirdMove * pp)
        {
           SellStop = pAsk + ThirdMove * pp;               
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
		     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);   
           if (digits > 0) SellStop = NormalizeDouble(SellStop, digits); 
           if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
        }
    }

}

//+------------------------------------------------------------------+
//|                                       Immediate_TrailingStop.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by MrPip,robydoby314@yahoo.com   |
//|                                                                  |   
//| Moves the stoploss without delay.                                |
//+------------------------------------------------------------------+
void Immediate_TrailingStop(int type, int ticket, double op, double os, double tp)
{

   int digits;
   double pt, pBid, pAsk, pp, BuyStop, SellStop;

   pp = MarketInfo(Symbol(), MODE_POINT);
   digits = MarketInfo(Symbol( ), MODE_DIGITS);
   
   if (type==OP_BUY)
   {
     pBid = MarketInfo(Symbol(), MODE_BID);
     pt = StopLoss * pp;
     if(pBid-os > pt)
     {
       BuyStop = pBid - pt;
       if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
		 BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);   
       if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
       if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
		 return;
	  }
   }
   if (type==OP_SELL)
   {
     pAsk = MarketInfo(Symbol(), MODE_ASK);
     pt = StopLoss * pp;
     if(os - pAsk > pt)
     {
       SellStop = pAsk + pt;
       if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
       SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
       if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
       if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
		 return;
     }
   }   
}

//+------------------------------------------------------------------+
//|                                         Delayed_TrailingStop.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by MrPip,robydoby314@yahoo.com   |
//|                                                                  |   
//| Waits for price to move the amount of the TrailingStop           |
//| Moves the stoploss pip for pip after delay.                      |
//+------------------------------------------------------------------+
void Delayed_TrailingStop(int type, int ticket, double op, double os, double tp)
{
   int digits;
   double pt, pBid, pAsk, pp, BuyStop, SellStop;

   pp = MarketInfo(Symbol(), MODE_POINT);
   pt = TrailingStop * pp;
   digits = MarketInfo(Symbol(), MODE_DIGITS);
   
   if (type==OP_BUY)
   {
     pBid = MarketInfo(Symbol(), MODE_BID);
     BuyStop = pBid - pt;
     if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
	  BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);   
     if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
     if (pBid-op > pt && os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen);
	  return;
   }
   if (type==OP_SELL)
   {
     pAsk = MarketInfo(Symbol(), MODE_ASK);
     pt = TrailingStop * pp;
     SellStop = pAsk + pt;
     if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
     if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
     if (op - pAsk > pt && os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange);
	  return;
   }   
}
//+------------------------------------------------------------------+
//| HandleTrailingStop                                               |
//| Type 1 moves the stoploss without delay.                         |
//| Type 2 waits for price to move the amount of the trailStop       |
//| before moving stop loss then moves like type 1                   |
//| Type 3 uses up to 3 levels for trailing stop                     |
//|      Level 1 Move stop to 1st level                              |
//|      Level 2 Move stop to 2nd level                              |
//|      Level 3 Trail like type 1 by fixed amount other than 1      |
//| Type 4 Move stop to breakeven + Lockin, no trail                 |
//| Type 5 uses steps for 1, every step pip move moves stop 1 pip    |
//| Type 6 Uses EMA to set trailing stop                             |
//+------------------------------------------------------------------+
int HandleTrailingStop(int type, int ticket, double op, double os, double tp)
{
   switch (TrailingStopType)
   {
     case 1 : Immediate_TrailingStop (type, ticket, op, os, tp);
              break;
     case 2 : Delayed_TrailingStop (type, ticket, op, os, tp);
              break;
     case 3 : ThreeLevel_TrailingStop (type, ticket, op, os, tp);
              break;
     case 4 : BreakEven_TrailingStop (type, ticket, op, os, tp);
              break;
	  case 5 : eTrailingStop (type, ticket, op, os, tp);
              break;
	  case 6 : EMA_TrailingStop (type, ticket, op, os, tp);
              break;
	  case 7 : pSAR_TrailingStop (type, ticket, op, os, tp);
              break;
	}
   return(0);
}


//+------------------------------------------------------------------+
//| Handle Open Positions                                            |
//| Check if any open positions need to be closed or modified        |
//+------------------------------------------------------------------+
int HandleOpenPositions()
{
   int cnt;
   
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol() != Symbol()) continue;
      if ( OrderMagicNumber() != MagicNumber)  continue;
      
      if(OrderType() == OP_BUY)
      {
            
         if (CheckExitCondition(OP_SELL))
          {
              CloseOrder(OrderTicket(),OrderLots(),OP_BUY);
          }
          else
          {
            if (UseTrailingStop)
            {
               HandleTrailingStop(OP_BUY,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
            }
          }
      }

      if(OrderType() == OP_SELL)
      {
          if (CheckExitCondition(OP_BUY))
          {
             CloseOrder(OrderTicket(),OrderLots(),OP_SELL);
          }
          else
          {
            if (UseTrailingStop)
            {
               HandleTrailingStop(OP_SELL,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
            }
          }
      }
   }
}

     
//+------------------------------------------------------------------+
//| Get number of lots for this trade                                |
//+------------------------------------------------------------------+
double GetLots()
{
   double lot;
   
   if(MoneyManagement)
   {
     lot = LotsOptimized();
   }
   else
   {
     lot = Lots;
   }
   
   if(AccountIsMini)
   {
     if (lot < 0.1) lot = 0.1;
   }
   else
   {
     if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0;
   }
   if (lot > MaxLots) lot = MaxLots;
   
   return(lot);
}

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+

double LotsOptimized()
  {
   double lot=Lots;
//---- select lot size
   lot=NormalizeDouble(MathFloor(AccountFreeMargin()*TradeSizePercent/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;
    
   }
   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)
 {
   string mySymbol = StringSubstr(symbol,0,6);
	if(mySymbol=="AUDCAD") return(1);
	if(mySymbol=="AUDJPY") return(2);
	if(mySymbol=="AUDNZD") return(3);
	if(mySymbol=="AUDUSD") return(4);
	if(mySymbol=="CHFJPY") return(5);
	if(mySymbol=="EURAUD") return(6);
	if(mySymbol=="EURCAD") return(7);
	if(mySymbol=="EURCHF") return(8);
	if(mySymbol=="EURGBP") return(9);
	if(mySymbol=="EURJPY") return(10);
	if(mySymbol=="EURUSD") return(11);
	if(mySymbol=="GBPCHF") return(12);
	if(mySymbol=="GBPJPY") return(13);
	if(mySymbol=="GBPUSD") return(14);
	if(mySymbol=="NZDUSD") return(15);
	if(mySymbol=="USDCAD") return(16);
	if(mySymbol=="USDCHF") return(17);
	if(mySymbol=="USDJPY") return(18);
   Comment("unexpected Symbol");
	return(19);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---