//+------------------------------------------------------------------+
//|                                             SELL SELL MY WAY.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+


#property copyright "Copyright © 2013, Pro4x"
#property link      "pro4x@ymail.com"
#include <stdlib.mqh>

#define vers     "24.Sep.2013"
#define Major    "1"
#define Minor    "0"
#define name     "BUY 200 20"

#property show_inputs //Comment out if you want it to execute with out showing inputs

//NOTE - to use the script to place a market Order as the first, and then pending orders for the secondaries
//set the _OrderMethod to 2, then set the Order1PipSpace to 0. If it is greater than 2 pips then it place a pending
//order as the primary order.

extern bool   DisplayModeOnly     = False;
extern bool   ExecuteBuyOders     = TRUE; //If Set to False will execute Sell Orders
extern string  _OrderMethod       = "1=UseStartPrice,2=X Pips from current price";
extern int    OrderMethod         = 2;
extern double StartPrice          = 0;
extern double Order1PipSpace      = 0; //Pips from current price

extern int    NumSecondaryOrders  = 0; //Number of secondary orders to place.Not used
extern double SecondaryPipSpace   = 0; //Pips spacing between secondary orders.Not used

extern double PrimaryTP           = 20;
extern double PrimarySL           = 10;
extern double SecondaryTP         = 0;// Not used
extern double SecondarySL         = 0;// Not used

extern string MM = "===== MONEY MANAGEMENT ====="; 
extern bool   UseMM              = FALSE;
extern double Primary_ManLots    = 0.02;
extern double Secondary_ManLots  = 0.01;//Not used

extern double Primary_MMPcnt     = 0.75;//Not used
extern double Secondary_MMPcnt   = 0.10;//Not used

extern double MinMarginPcnt      = 150;

extern string Gen = "===== GENERAL SETTINGS ====="; 
extern int    MagicNo = 227722;
extern int    MaxOrderAttempts = 5; //Max No of Order Attempts.  
extern int    MaxPriceDeviation = 3; //Maximum allowable Deviation price can differ from current market price when order is first attempted to be placed.
extern bool   UseAlerts = True; //Alert on errors, if not using alerts errors are added to journal
extern bool   TakeScreenShot = True;
extern string Note = "ScreenShot Sizes: 1-Small,2-Med,3-Large";
extern int    ScreenShotSize = 3;

color  EnterLongColor = Lime;
color  EnterShortColor = Red;
string NL = "\n";
double PipPoint;
string sError = "";
string sCom;

color Ord_Color;
double PriceNow;
double EntryPrice;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{ 
   //"1=UseStartPrice,2=X Pips from current price";
   OrderMethod = MathMax(MathMin(OrderMethod,2),1);
   if(OrderMethod==1 && StartPrice<=0)
   {
      Alert("StartPrice must be greater than 0");
      return(0);
   }

   if(NumSecondaryOrders>20) Print("Scale In Scipt - Max allowed orders is 20");
   NumSecondaryOrders = MathMax(MathMin(NumSecondaryOrders,20),0);

   bool MicroLotsAllowed = false;
   bool MicroLotStepsAllowed = False;
   int Ord_Type=OP_BUYSTOP;
   Ord_Color = EnterLongColor;

   if(!ExecuteBuyOders) 
   {
      Ord_Type=OP_SELLSTOP;
      Ord_Color = EnterShortColor;
   }   

   int DimX, DimY;
   ScreenShotSize = MathMin(ScreenShotSize,3);
   ScreenShotSize = MathMax(ScreenShotSize,1);
   switch (ScreenShotSize)
   {
      case 1: DimX = 640; DimY=480; break;
      case 2: DimX = 1024; DimY=768; break;
      case 3: DimX = 1920; DimY=1080; break; 
   }
      
   PipPoint = GetPointSize(Symbol());
   Order1PipSpace *= PipPoint;
   SecondaryPipSpace *= PipPoint;

   PrimaryTP *= PipPoint;
   PrimarySL *= PipPoint;
   SecondaryTP *= PipPoint;
   SecondarySL *= PipPoint;
   
   PriceNow = GetBidAsk(Ord_Type==OP_BUYSTOP,Symbol());

   //"1=UseStartPrice,2=X Pips from current price";
   if(OrderMethod==1) EntryPrice = StartPrice;
   else 
   {
      if(Ord_Type==OP_BUYSTOP) EntryPrice = PriceNow+Order1PipSpace;
      else EntryPrice = PriceNow-Order1PipSpace;
   }
   
   bool PlaceMarketOrder = False;
   if(MathAbs(EntryPrice-PriceNow)<=2*PipPoint)
   {
      //Alert("Order 1 entry cannot be within 2 pips of current price");
      PlaceMarketOrder = True;
      //return;
   }
   
   double TP = 0;
   double SL = 0;       
   if(PrimaryTP>0 && Ord_Type==OP_BUYSTOP) TP = NormalizeDouble(EntryPrice+PrimaryTP,Digits);
   if(PrimaryTP>0 && Ord_Type==OP_SELLSTOP) TP = NormalizeDouble(EntryPrice-PrimaryTP,Digits);
   
   if(PrimarySL>0 && Ord_Type==OP_BUYSTOP) SL = NormalizeDouble(EntryPrice-PrimarySL,Digits);
   if(PrimarySL>0 && Ord_Type==OP_SELLSTOP) SL = NormalizeDouble(EntryPrice+PrimarySL,Digits);      
      
   double PriLots=GetPositionSize(True,PrimarySL); //,False); 
   if(PriLots<0) 
   {
      Alert(name + ": Error with lot sizing."); 
      return(0); //Error raised during lot sizing insufficient funds or margin
   }
   
   if(UseMM)
   {
      sCom = "Use MM Pcnt: TRUE";
      sCom = sCom + NL + "Primary MM Pcnt: "+ DoubleToStr(Primary_MMPcnt,1);
      sCom = sCom + NL + "Secondary MM Pcnt: " + DoubleToStr(Secondary_MMPcnt,1);

   } else {
      sCom = "Use MM Pcnt: FALSE";
   }      
   sCom = sCom + NL + "Primary Lots: " + DoubleToStr(PriLots,3) + ", SL Size: " + DoubleToStr(PrimarySL/PipPoint,0);

   double SecLots=GetPositionSize(False,SecondarySL); //,False); 
   sCom = sCom + NL + "Secondary Lots: " + DoubleToStr(SecLots,3) + ", SL Size: " + DoubleToStr(SecondarySL/PipPoint,0);
   
   if(DisplayModeOnly)
   {
      Comment(sCom);
      return(0);
   }
        
   string Com = name + "-Pri-" +  TF2Str(Period());
   int Ticket;
   
   if(PlaceMarketOrder) 
   {
      int PriOrdType = OP_BUY;
      if (Ord_Type == OP_SELLSTOP) PriOrdType = OP_SELL;
      SL = 0; TP = 0;
      if(PrimarySL>0) SL = ValidSLTP(False,PriOrdType,EntryPrice,PrimarySL);
      if(PrimaryTP>0) TP = ValidSLTP(True,PriOrdType,EntryPrice,PrimaryTP);
      Ticket = PlaceMarketOrder(PriOrdType,PriLots,SL,TP,Com); //Place Primary Trade   
   } else { 
      Ticket = PlaceTrade(Ord_Type, PriLots, EntryPrice, SL, TP, Com); //Place Primary Trade   
   }
   
   if(Ticket==0)
   {
      int Error=GetLastError();
      if(Error>0)
      {
         Print(name + " Order Error: " + Error + ", " + ErrorDescription(Error));
         Alert(name + ": Error placing Primary Trade script exiting. Try Again!"); 
      }
      return(0);
   }

   if(NumSecondaryOrders>0)
   {
      //Entry price of the first secondary order
      if(Ord_Type==OP_BUYSTOP) EntryPrice += SecondaryPipSpace;
      else EntryPrice -= Order1PipSpace;
            
      if(SecLots<0) 
      {
         Alert(name + ": Error with lot sizing for secondary trades. No secondary trades placed."); 
         return(0); //Error raised during lot sizing insufficient funds or margin
      }
   
      Com = name + "-Sec-" +  TF2Str(Period());

      for(int x = 1; x<=NumSecondaryOrders; x++)
      {
         SL = 0;
         TP = 0;
         if(SecondaryTP>0 && Ord_Type==OP_BUYSTOP) TP = NormalizeDouble(EntryPrice+SecondaryTP,Digits);
         if(SecondaryTP>0 && Ord_Type==OP_SELLSTOP) TP = NormalizeDouble(EntryPrice-SecondaryTP,Digits);

         if(SecondarySL>0 && Ord_Type==OP_BUYSTOP) SL = NormalizeDouble(EntryPrice-SecondarySL,Digits);
         if(SecondarySL>0 && Ord_Type==OP_SELLSTOP) SL = NormalizeDouble(EntryPrice+SecondarySL,Digits);

         Ticket = PlaceTrade(Ord_Type, SecLots, EntryPrice, SL, TP, Com); //Place Secondary Trade
         if(Ord_Type==OP_BUYSTOP) EntryPrice += SecondaryPipSpace;
         else EntryPrice -= SecondaryPipSpace;
      }
   }
      
   Sleep(2000); 
   if(TakeScreenShot) 
   {
      string Mth = Month();
      if(StringLen(Mth)==1) Mth = "0" + Mth;
   
      string fName = name + "_" + Mth + "_("+Day()+DateToStr(TimeCurrent(),False)+")_" + Symbol() +"_["+Ticket+"]"+ ".gif";
      TakeTheScreenShot(fName,DimX,DimY);
   }
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int PlaceMarketOrder(int Ord_Type, double Lots, double StopLoss, double TakeProfit, string Com)
{      
   for (int c=1; c<=MaxOrderAttempts; c++)
   {
      RefreshRates();
      int PriceDev = (GetBidAsk(Ord_Type==OP_BUY,Symbol())-PriceNow)/PipPoint;

      if(MaxPriceDeviation>0 && PriceDev > MaxPriceDeviation)
      {
          sError = sError + NL + "Order Error. Max price deivation of " + MaxPriceDeviation + " pips exceeded.";
          Print(sError); //PriceDev + " pips from initial point of trying to place order.";         
          break;
      }

      if(IsTradeContextBusy()) 
      {
         Sleep(500);
         continue;
      }

      int Ticket = OrderSend(Symbol(), Ord_Type, Lots, PriceNow, 2, StopLoss, TakeProfit, Com, MagicNo, 0, Ord_Color);        
      bool SetTPSL = False; 
      if(Ticket<0)
      {
         int err=GetLastError();
         //error 130 = Invalid stops, this normally occurs at brokers who don't allow SL, TP to be sent with the initial market order.
         //If this happens, then try this method. The Stops and or Take Profit values are set after the order has been confirmed as placed.
         if(err==130) 
         {
            Print("Executing Order without SL/TP. Completing in 2 stages.");
            Ticket=OrderSend(Symbol(), Ord_Type, Lots, PriceNow, 2, 0, 0, Com, MagicNo, 0, Ord_Color);  
            SetTPSL = True;
         }
      }   

      if (Ticket>0) 
      {                        
         Sleep(3000); 
         //Now add in the TP/SL levels 
         string result = "";
         if(SetTPSL) 
         {
            Print("Setting SL/TPmfor Primary Order");
            result = ModifyOrder(Ticket, StopLoss, TakeProfit);  
         }
         if(StringLen(result)>0) sError = sError + NL + result;

         return(Ticket);
      }
      else  
      {
         int Error=GetLastError();
         Print(name + " Order Error: " + Error + ", " + ErrorDescription(Error));

         if(Error==134) 
         {
            sError = sError + NL + "Order Error. Insufficient funds to place order.";
            break; 
         }
         Sleep(500); //recoverable error so wait 1/2 a second
      }
   
      if( Ticket == 0 ) 
      {
         sError = sError + NL + "Order Error. Failed to place order after " + MaxOrderAttempts + " attempts.";
      }   
   }
   
   if(StringLen(sError)>0 && UseAlerts) 
   {
      Alert("Errors were raised during execution. Please see on screen summary.");
      Comment(name + " Scale In Script" + NL + sError);
      Print(name + " Scale In Script");
      Print(sError);
   }
   return(0);
}

//+------------------------------------------------------------------+
//| Ensures that the SL/TP entered by user meets broker minimum      |
//+------------------------------------------------------------------+
double ValidSLTP(bool IsTakeProfit, int OrdType, double Price, double SLTP)
{   
   double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL);     
   if(IsGoldSilver(Symbol())) StopLevel = 300; 
   double BrokerMinstop = MathMax(StopLevel*Point, SLTP);

   if(IsTakeProfit)
   {
      if(OrdType == OP_BUY) return(NormalizeDouble(Price+BrokerMinstop,Digits));
      else return(NormalizeDouble(Price-BrokerMinstop,Digits));      
   }
   else
   {
      if(OrdType == OP_BUY) return(NormalizeDouble(Price-BrokerMinstop,Digits));
      else return(NormalizeDouble(Price+BrokerMinstop,Digits));      
   }
}  

//+------------------------------------------------------------------+
//| Modify Open Orders TP/SL values                                  |
//+------------------------------------------------------------------+
string ModifyOrder(int TicketNo, double Stop, double TakeProf)
{
   string result = "";
   bool OrderModified = false;
   int ModifyCount = 0;
   int MaxOrderTries = 500;
   int MinMillisecondsBetweenTries = 1000;
   int MaxMillisecondsBetweenTries = 10000;
   
   while( !OrderModified && ModifyCount < MaxOrderTries )
   {
      if(OrderSelect(TicketNo,SELECT_BY_TICKET))
      {
         OrderModified = OrderModify(TicketNo, OrderOpenPrice(), NormalizeDouble(Stop,Digits), NormalizeDouble(TakeProf,Digits), 0);
      }   
      if( !OrderModified )
         Sleep( MinMillisecondsBetweenTries + ( ( MathRand() * MathRand() ) % ( MaxMillisecondsBetweenTries - MinMillisecondsBetweenTries ) ) );
      ModifyCount++;
   }
   
   if(!OrderModified)
   {
      int err=GetLastError();
      if(err!=0) result = "Error Setting TP/SL Values for Ticket: " + TicketNo;
   }
   return(result);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int PlaceTrade(int Ord_Type, double Lots, double EntPrice, double StopLoss, double TakeProfit, string Com)
{      
   for (int c=1; c<=MaxOrderAttempts; c++)
   {
      if(IsTradeContextBusy()) 
      {
         Sleep(500);
         continue;
      }

      int Ticket = OrderSend(Symbol(), Ord_Type, Lots, EntPrice, 1, StopLoss, TakeProfit, Com, MagicNo, 0, Ord_Color);        
      bool SetTPSL = False; 
      if(Ticket<0)
      {
         int err=GetLastError();
         //error 130 = Invalid stops, this normally occurs at brokers who don't allow SL, TP to be sent with the initial market order.
         //If this happens, then try this method. The Stops and or Take Profit values are checked in OpenOrderManagement, this ensures that they get set.
         if(err==130) 
         {
            Ticket=OrderSend(Symbol(), Ord_Type, Lots, EntPrice, 2, 0, 0, Com, MagicNo, 0, Ord_Color);  
            SetTPSL = True;
         }
      }   

      if (Ticket>0) 
      {                        
         Sleep(3000); 
         //Now add in the TP/SL levels 
         string result = "";
         if(SetTPSL) result = ModifyOrder(Ticket, StopLoss, TakeProfit);  
         if(StringLen(result)>0) sError = sError + NL + result;

         return(Ticket);
      }
      else  
      {
         int Error=GetLastError();
         if(Error>0)
         {
            Print(name + " Order Error: " + Error + ", " + ErrorDescription(Error));

            if(Error==134) 
            {
               sError = sError + NL + "Order Error. Insufficient funds to place order.";
               break; 
            }
         }
         Sleep(500); //recoverable error so wait 1/2 a second
      }
   
      if( Ticket == 0 ) 
      {
         sError = sError + NL + "Order Error. Failed to place order after " + MaxOrderAttempts + " attempts.";
      }   
   }
   
   if(StringLen(sError)>0 && UseAlerts) 
   {
      Alert("Errors were raised during execution. Please see on screen summary.");
      Comment(name + " Market Entry Script" + NL + sError);
   }
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetBidAsk(bool ask, string Sym)
{
   RefreshRates();
   if(ask) return(NormalizeDouble(MarketInfo(Sym,MODE_ASK),Digits));
   else return(NormalizeDouble(MarketInfo(Sym,MODE_BID),Digits));
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsGoldSilver(string Symb)
{
     
   string sUpper = StringUpperCase(Symb);
   return(StringFind(sUpper, "GOLD", 0) != -1 || StringFind(sUpper, "GLD", 0) != -1 || StringFind(sUpper, "XAU", 0) != -1 ||
   StringFind(sUpper, "SILVER", 0) != -1 || StringFind(sUpper, "XAG", 0) != -1);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetPointSize(string Sym)
{
   if(IsGoldSilver(Sym)) return(0.1);
   if(StringFind(Sym, "JPY", 0) != -1) return(0.01);  

   double Pt = 0.0001;
   //Dont check for USD as a US based futures may have these three letters in it.
   //if we check on all other cross pairs then this we should be able to get all main fx pairs
   if(StringFind(Sym, "EUR", 0) != -1) return(Pt);     
   if(StringFind(Sym, "GBP", 0) != -1) return(Pt);  
   if(StringFind(Sym, "AUD", 0) != -1) return(Pt);  
   if(StringFind(Sym, "CAD", 0) != -1) return(Pt);  
   if(StringFind(Sym, "CHF", 0) != -1) return(Pt);  
   if(StringFind(Sym, "NZD", 0) != -1) return(Pt);  
   if(StringFind(Sym, "SGD", 0) != -1) return(Pt);  
   if(StringFind(Sym, "SEK", 0) != -1) return(Pt);  
   if(StringFind(Sym, "DKK", 0) != -1) return(Pt);  
   if(StringFind(Sym, "HKD", 0) != -1) return(Pt);  
   if(StringFind(Sym, "ZAR", 0) != -1) return(Pt);  
   return(MarketInfo(Sym,MODE_POINT)); //Unknown so return symbol point
}  

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string StringUpperCase(string str)
{
   string   s = str;
   int      length = StringLen(str) - 1;
   int      ichar;

   while(length >= 0)
   {
      ichar = StringGetChar(s, length);
      if((ichar > 96 && ichar < 123) || (ichar > 223 && ichar < 256))
         s = StringSetChar(s, length, ichar - 32);
       else
         if(ichar > -33 && ichar < 0)
            s = StringSetChar(s, length, ichar + 224);
      length--;
   }
   return(s);
}

/*//+------------------------------------------------------------------+
//| Convert Order Type integer value into a descriptive string       |
//+------------------------------------------------------------------+
string OrdType2Str(int type) 
{
	if (type == OP_BUY) 			    return("BUY");
	else if (type == OP_SELL) 		 return("SELL");
	else if (type == OP_BUYSTOP) 	 return("BUY STOP");
	else if (type == OP_SELLSTOP)	 return("SELL STOP");
	else if (type == OP_BUYLIMIT)  return("BUY LIMIT");
	else if (type == OP_SELLLIMIT) return("SELL LIMIT");
	return("NONE");
}*/

//+------------------------------------------------------------------+
//| Calculate the number of lots to place per trade                  |
//+------------------------------------------------------------------+
double GetPositionSize(bool Primary, double dStopLoss=0.0, bool SL_Adjusted_LotSz=True)
{
   //dStopLoss must be passed in as full point value
   //eg High[1]-Low[1], NOT the pip value eg 51.1

   double LotSize = Primary_ManLots;
   if(!Primary) LotSize = Secondary_ManLots;
   
   bool MicroLotsAllowed = False;
   bool MicroLotStepsAllowed = False;

   double MaxLots = MarketInfo( Symbol(), MODE_MAXLOT );
   double MinLots = MarketInfo( Symbol(), MODE_MINLOT );
   LotSize = MathMax(MathMin(LotSize,MaxLots),MinLots);

   double LotStep = MarketInfo( Symbol(), MODE_LOTSTEP );

   if( MinLots == 0.01 ) MicroLotsAllowed = true;
   if( LotStep == 0.01 ) MicroLotStepsAllowed = true;

   double MMPercent = Primary_MMPcnt;
   if(!Primary) MMPercent = Secondary_MMPcnt;

   if( UseMM ) 
   {
      double MoneyToRisk = AccountBalance() * (MMPercent / 100.0);
      double TickValue = 1/MarketInfo(Symbol(), MODE_TICKVALUE);
   
      if (SL_Adjusted_LotSz && dStopLoss > 0.0)
      { 
         LotSize = MoneyToRisk / dStopLoss / (TickValue / Point * PipPoint ) / 10;
         if(Digits>3) LotSize/=1000;
         else LotSize/=10;
      }
      else 
         LotSize = MoneyToRisk / (TickValue / Point * PipPoint ) / 100;
         
   }

   if( MicroLotsAllowed ) LotSize = NormalizeDouble( LotSize, 2 );
   else if( MicroLotStepsAllowed && LotSize > 0.1 ) LotSize = NormalizeDouble( LotSize, 2 );
   else if( MinLots < 0.5 ) LotSize = NormalizeDouble( LotSize, 1 );
   else LotSize = NormalizeDouble( LotSize, 0 );
   
   LotSize = MathMin(LotSize,MaxLots);
   LotSize = MathMax(LotSize,MinLots);

   //Multiplier is used for returning a negative lot size (ie detected as error by EA) if insufficient margin
   double Multiplier = 1;
   //Check we have sufficient margin to take new position
   if(AccountFreeMarginCheck(Symbol(), OP_BUY, LotSize) <= 0.0) 
   {
      Alert("Insufficient Free Margin to Open New Trade!!");
      Multiplier = -1;	
   }

   int MarginPcnt = (AccountEquity()/MathMax(AccountMargin(),1))*100;
   if(MarginPcnt<MinMarginPcnt)
   {
      Alert("Available Margin below safe limit of " + DoubleToStr(MinMarginPcnt,0) + "%");
      Multiplier = -1;
   }

   return (LotSize*Multiplier);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
string TF2Str(int period) 
{
  switch (period) 
  {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN");
  }
  return (Period());
} 

//+------------------------------------------------------------------+
//| Take Screen Shot                                                 |
//+------------------------------------------------------------------+
void TakeTheScreenShot(string fName, int Dim1, int Dim2)
{   
   if(!WindowScreenShot(fName,Dim1,Dim2))
   {
      int Err = GetLastError();
      string sErr = "Screen shot Error: " + Err + ", " + ErrorDescription(Err);
      Print(name + ": " + sErr);
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string DateToStr(datetime DT, bool ReturnDay)
{
   string sDay, sMonth;

   switch(TimeDayOfWeek(DT))
   {
      case 0: sDay = "Sunday"; break;
      case 1: sDay = "Monday"; break;
      case 2: sDay = "Tuesday"; break;
      case 3: sDay = "Wednesday"; break;
      case 4: sDay = "Thursday"; break;
      case 5: sDay = "Friday"; break;
      case 6: sDay = "Saturday"; break;
   }
   
   switch(TimeMonth(DT))
   {
      case 1:  sMonth = "Jan"; break;
      case 2:  sMonth = "Feb"; break;
      case 3:  sMonth = "Mar"; break;
      case 4:  sMonth = "Apr"; break;
      case 5:  sMonth = "May"; break;
      case 6:  sMonth = "Jun"; break;
      case 7:  sMonth = "Jul"; break;
      case 9:  sMonth = "Sep"; break;
      case 8:  sMonth = "Aug"; break;
      case 10: sMonth = "Oct"; break;
      case 11: sMonth = "Nov"; break;
      case 12: sMonth = "Dec"; break;
   }
   if(ReturnDay) return(sDay);
   else return(sMonth);
}
//+------------------------------------------------------------------+


