Slingshot_1.5

Author: Copyright � 2009, Orest
Profit factor:
0.00
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Slingshot_1.5
//+------------------------------------------------------------------+
//|                                                 Slingshot EA.mq4 |
//|                                          Copyright © 2009, Orest |
//|                                 based on template by Ryan Klefas |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Orest"
#property link      "olponom@yahoo.com"

extern string    basic="==== Basic Settings ====================";
extern double    TakeProfit=0;
extern double    Stoploss=0;
extern double    Lots=0.1;

extern string    manage="==== Order Management ====================";
extern int       BreakEvenAtProfit=0;
extern int       BreakEvenShift=0;               // Move the breakeven point up or down around the order open price
extern int       TrailingStop=0;
extern bool      OnlyTrailAfterProfit = false;   // Trailing Stop will only trails when order is profitable
extern bool      Move_SL_to_Prev_Hi_Lo = true;   


extern string    account="==== Account Settings ====================";
extern int       MinimumEquity=0;                // New orders will be disabled if equity drops below minimum
extern int       MinimumBalance=0;               // New orders will be disabled if balance drops below minimum

extern string    activity="==== EA Activity Settings ====================";
extern int       ConcurrentOrdersMax=1;

extern string    id="==== Identity Settings ====================";
extern int       ExpertID=999999;               // Magic number: for identifying an EA's orders
extern bool      TimeSpecific=false;            // If true, Time frames are considered in determining
                                                // whether an order belongs to the EA or not
extern string    ExpertName="Slingshot 1.5, by Orest";        // Expert name: for aesthetic purposes


extern int TimeToTrade1 = 6;
extern int TimeToTrade2 = 16;

extern string    macd="==== MACD Colored Parameters ====================";

extern int FastEMA       = 12;
extern int SlowEMA       = 26;
extern int SignalSMA     = 9;
extern int applied_price = 0;

extern string    money="==== Money Management ====================";
extern bool Money_Management = false;
extern double Risk = 0.01;
extern int Max_StopLoss = 100;


bool      DisableClosingOrders=false;    // If true, the order closing section of the EA will not run
bool      DisableNewOrders=false;        // If true, the order creating section of the EA will not run

bool belowLowPreviousBar    = false;
bool aboveClosePreviousBar  = false;

bool aboveHighPreviousBar   = false;
bool belowClosePreviousBar  = false;
datetime barTime=0; 
bool BUY_SIGNAL = false;
bool SELL_SIGNAL = false;
int trend = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   Comment(ExpertName + " is waiting for the next tick to begin.");
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   string comment = "";
   Comment(ExpertName + " has started.");

   int buyOrders=0, sellOrders=0, buyPending=0, sellPending=0;
   checkOrderStatus(buyOrders, sellOrders, buyPending, sellPending);
   int instantOrders = buyOrders + sellOrders;
   int pendingOrders = buyPending + sellPending;
   int allOrders     = instantOrders + pendingOrders;
   advancedStopManager(instantOrders);

//+------------------------------------------------------------------+
//| variable and indicator declaration                               |
//+------------------------------------------------------------------+
int total = 0;

    if( barTime < Time[0] ) {
        // we have a new bar opened
        barTime = Time[0]; // keep the new bar open time
        belowLowPreviousBar    = false;
        aboveClosePreviousBar  = false;

        aboveHighPreviousBar   = false;
        belowClosePreviousBar  = false;
        BUY_SIGNAL = false;
        SELL_SIGNAL = false;

        total = OrdersTotal();
        if (total > 0 && Move_SL_to_Prev_Hi_Lo)
            moveStopLosses();   
    }

//+------------------------------------------------------------------+
//| close existing orders under apprpriate conditions                |
//+------------------------------------------------------------------+
if (DisableClosingOrders==false && allOrders>0 && barTime == Time[0])  {
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

   int cnt;
   total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if (orderBelongsToMe() && OrderOpenTime()< Time[0])
      {
         if (OrderType()==OP_BUY)
         {
            if (OrderProfit() > 0)   //   Bid-OrderOpenPrice()>Point*TrailingStop)
            {
                  closeBuyOrder();
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if (OrderProfit() > 0) //OrderOpenPrice()-Ask>Point*TrailingStop)
            {
                  closeSellOrder();
            }
         }
      }  
   }

 //+------------------------------------------------------------------+
 }

  if (! IsTimeToTrade() || BUY_SIGNAL || SELL_SIGNAL)
        return(0);

   Trend(trend);

   if (trend==1)
      comment = "Trend is BUY-- ";
   else if (trend==-1)
      comment = "Trend is SELL-- ";
      
   // if no open orders then start looking for setup
   if (instantOrders == 0)
   { 
     int TimeFrame =0;
     if (trend == 1) 
     {
         // Looking for Buy Signal
             if (Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits))    // High[0]
             {     
                 comment = comment + "Above Close of Prev. Bar. ***** Opening BUY";
                 BUY_SIGNAL = true;
                 sendBuyOrder(); 
             }
      }     
      else if (trend==-1)
      {
             if (High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point,Digits))    // Low[0]
             {     
                 comment = comment + "Below Close of Prev. Bar. ***** Opening SELL";
                 SELL_SIGNAL = true;
                 sendSellOrder();
              }
      }
   }

  Comment(comment);
  return(0);
}
  
//+------------------------------------------------------------------+
//| middle-man modules                                               |
//+------------------------------------------------------------------+

void sendBuyOrder() // Standard Version 1.71
{ versatileOrderTaker(1, Ask); }
   
//+------------------------------------------------------------------+

void sendSellOrder() // Standard Version 1.71
{ versatileOrderTaker(2, Bid); }

//+------------------------------------------------------------------+

void sendBuyPending(double entryPrice) // Standard Version 1.71
{ versatileOrderTaker(3, entryPrice); }

//+------------------------------------------------------------------+

void sendSellPending(double entryPrice) // Standard Version 1.71
{ versatileOrderTaker(4, entryPrice); }

//+------------------------------------------------------------------+

void closeSellOrder() // Standard Version 1.71
{ versatileOrderCloser(2); }

//+------------------------------------------------------------------+

void closeBuyOrder() // Standard Version 1.71
{ versatileOrderCloser(1); }

//+------------------------------------------------------------------+

void deletePending() // Standard Version 1.71
{ versatileOrderCloser(3); }

//+------------------------------------------------------------------+




//+------------------------------------------------------------------+
//| modules and functions                                            |
//+------------------------------------------------------------------+

void trailingStopManager() // Standard Version 1.71
{
   
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if (TrailingStop>0 && orderBelongsToMe())
      {
         if (OrderType()==OP_BUY)
         {
            if ((Bid-OrderOpenPrice()>Point*TrailingStop) || OnlyTrailAfterProfit==false)
            {
               if (OrderStopLoss()<Bid-Point*TrailingStop)
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if ((OrderOpenPrice()-Ask>Point*TrailingStop) || OnlyTrailAfterProfit==false)
            {
               if ((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
            }
         }
      }  
   }
}


void moveStopLosses() 
{
   int cnt, total = OrdersTotal();
    double dblStop, dblTightestStop;

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      int index = iBarShift(NULL, 0 , OrderOpenTime()); 
      if (index==-1) return; 
     
      if (orderBelongsToMe() && OrderProfit() < 0)
      {
         if (OrderType()==OP_BUY)
         {
          dblStop = Low[index]-(MarketInfo(Symbol(),MODE_SPREAD)*Point)-1*Point;
          dblTightestStop = Bid - MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
          if ( dblStop > dblTightestStop && Stoploss>0)
               dblStop = OrderOpenPrice()-Stoploss*Point;
          
          if (dblStop < OrderOpenPrice() && dblStop != OrderStopLoss() && dblStop < dblTightestStop)           
               OrderModify(OrderTicket(),OrderOpenPrice(),dblStop,OrderTakeProfit(),0,Green); 
         }
         else if (OrderType()==OP_SELL)
         {
         //Print (MarketInfo(Symbol(),MODE_SPREAD)*Point);
         dblStop = High[index]+(MarketInfo(Symbol(),MODE_SPREAD)*Point)+1*Point;   //MarketInfo(Symbol(),MODE_SPREAD)*Point+
         dblTightestStop = Ask + MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
         if ( dblStop < dblTightestStop  && Stoploss>0)
               dblStop = OrderOpenPrice()+Stoploss*Point;
               
         if (dblStop > OrderOpenPrice() && dblStop != OrderStopLoss() && dblStop > dblTightestStop)           
             OrderModify(OrderTicket(),OrderOpenPrice(),dblStop,OrderTakeProfit(),0,Red); 
         }
      }  
   }
}

//+------------------------------------------------------------------+

void breakEvenManager() // Standard Version 1.71
{

   for(int cnt=0;cnt<OrdersTotal();cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if (BreakEvenAtProfit>0 && orderBelongsToMe())
      {
         if (OrderType()==OP_BUY)
         {
            if (Bid-OrderOpenPrice()>=Point*BreakEvenAtProfit)
            {
               if (OrderStopLoss()!=OrderOpenPrice() + BreakEvenShift*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+ BreakEvenShift*Point,OrderTakeProfit(),0,LimeGreen); 
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if (OrderOpenPrice()-Ask>=Point*BreakEvenAtProfit)
            {
               if (OrderStopLoss()!=OrderOpenPrice() - BreakEvenShift*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()- BreakEvenShift*Point,OrderTakeProfit(),0,Tomato); 
            }
         }
      }
   }
}
   
//+------------------------------------------------------------------+

 // Standard Version 1.71
void checkOrderStatus(int& buyOrders,int& sellOrders,int& buyPending, int& sellPending)
{
   for(int cnt=OrdersTotal();cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if ( orderBelongsToMe() )
      {  
         if (OrderType()==OP_BUY)
            {buyOrders++;}
         else if (OrderType()==OP_SELL)
            {sellOrders++;}
         else if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
            {buyPending++;}
         else if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
            {sellPending++;}
      }
   }
}

//+------------------------------------------------------------------+

void advancedStopManager(int instantOrders) // Standard Version 1.71
{
    
   if (instantOrders>=1)
   {
      if (BreakEvenAtProfit>0)
         breakEvenManager();
      if (TrailingStop>0)
         trailingStopManager();
    }
}

//+------------------------------------------------------------------+

bool orderBelongsToMe() // Standard Version 1.71
{
   if (OrderSymbol()==Symbol() && OrderMagicNumber()==simpleMagicGenerator() )
       return (true);
   else
      return (false);
}

//+------------------------------------------------------------------+

int simpleMagicGenerator() // Standard Version 1.71
{

   int truMagic;
   
   if (TimeSpecific)
      truMagic = ExpertID + (Period()*17 + 203);
   else
      truMagic = ExpertID;      

   return (truMagic);

}

//+------------------------------------------------------------------+

string commentString() // Standard Version 1.71
{
   string tfString, fullComment;
   
   switch (Period())  
   {   
      case 1:      tfString = "1 Min";    break;
      case 5:      tfString = "5 Min";    break;
      case 15:     tfString = "15 Min";   break;  
      case 30:     tfString = "30 Min";   break;  
      case 60:     tfString = "1 Hour";   break;  
      case 240:    tfString = "4 Hour";   break;  
      case 1440:   tfString = "Daily";    break;  
      case 10080:  tfString = "Weekly";   break;  
      case 40320:  tfString = "Monthly";  break;  
      default:     tfString = "Unknown";   
   }

   fullComment=StringConcatenate(ExpertName, ": ", tfString);
   return (fullComment);
}

//+------------------------------------------------------------------+

double lotMaker() // Standard Version 1.71 Simplified
{
   if (! Money_Management)
         return (Lots);        

	double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
	double lots = AccountFreeMargin() * Risk / (Max_StopLoss * pipValue);
	
	double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
	int digits = 0;
	if(lotStep <= 0.01)
		digits = 2;
	else if(lotStep <= 0.1)
		digits = 1;
	lots = NormalizeDouble(lots, digits);
  	
  	double minLots = MarketInfo(Symbol(), MODE_MINLOT);
  	if(lots < minLots)
  		lots = minLots;
  	
  	double maxLots = MarketInfo(Symbol(), MODE_MAXLOT);
  	if(lots > maxLots)
  		lots = maxLots;
  	
  	return (lots);
}


//+------------------------------------------------------------------+

bool newOrderPrevention(int allOrders) // Standard Version 1.71
{

   if (DisableNewOrders)
      { Comment("New Orders Disabled:  User option"); return (true); }
   else if (AccountEquity()<MinimumEquity)
      { Comment("New Orders Disabled:  Equity too low"); return (true);}
   else if (AccountBalance()<MinimumBalance)
      { Comment("New Orders Disabled:  Balance too low"); return (true);}
   else if (allOrders>=ConcurrentOrdersMax)
      { Comment("New Orders Disabled:  Too many existing orders"); return (true);}
   else
      return (false);

}

//+------------------------------------------------------------------+

void versatileOrderTaker(int simpleType, double entryPrice) // Standard Version 1.71
{

   // Variable calculations

   double Stopvalue=0, TPvalue=0;
   int ticket, oType, slippage=3;
   string typeName;
   color c_color;
   
   c_color = Green;

   switch (simpleType)
   {
      case 4: 
      if (entryPrice<Bid)  { oType=OP_SELLSTOP;  typeName="SELLSTOP"; }
      if (entryPrice>Bid)  { oType=OP_SELLLIMIT; typeName="SELLLIMIT"; }
      if (Stoploss>0)      {Stopvalue = entryPrice+Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice-TakeProfit*Point;}
      break;
         
      case 3:
      if (entryPrice>Ask)  { oType=OP_BUYSTOP;  typeName="BUYSTOP"; }
      if (entryPrice<Ask)  { oType=OP_BUYLIMIT; typeName="BUYLIMIT"; }
      if (Stoploss>0)      {Stopvalue = entryPrice-Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice+TakeProfit*Point;}
      break;

      case 2:
      oType=OP_SELL; typeName="SELL"; 
      c_color = Red;
      if (Stoploss>0)      {Stopvalue = entryPrice+Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice-TakeProfit*Point;}
      break;

      case 1:
      oType=OP_BUY; typeName="BUY"; 
      if (Stoploss>0)      {Stopvalue = entryPrice-Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice+TakeProfit*Point;}
      break;
      
      default:
      Print ("versatileOrderTaker has been passed an invalid SimpleType parameter: " + simpleType);
   }

   // Send Order
   
   ticket=OrderSend(Symbol(),oType,lotMaker(),entryPrice,slippage,Stopvalue,TPvalue,commentString(),simpleMagicGenerator(),0,c_color);
   if (ticket>0)
     {if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(ExpertName + " " + typeName + " order at ",OrderOpenPrice()); }
   else 
      Print("Error opening " + typeName + " order: ",GetLastError());
}

//+------------------------------------------------------------------+

void versatileOrderCloser(int simpleType) // Standard Version 1.71
{
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      if ( orderBelongsToMe() )
      {
         switch (simpleType)
         {
            case 4:
            case 3:
               if (OrderType()>OP_SELL)   OrderDelete(OrderTicket());
            break;
         
            case 2:
               if (OrderType()==OP_SELL)  OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE); 
            break;
         
            case 1:
               if (OrderType()==OP_BUY)   OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE); 
            break;
         
            default:
            Print ("versatileOrderCloser has been passed an invalid SimpleType parameter: " + simpleType);
         }
      }
   }
}

//+------------------------------------------------------------------+

void Trend(int& trend)
{
      int TimeFrame =0;

      int bluemarker1 = 0, bluemarker2 = 0; 
      int redmarker1 = 0, redmarker2 = 0;
      double bluenew = 0.0, blueold = 0.0;
      double rednew = 0.0, redold = 0.0;
      
      for (int i=1; i<=100; i++)
      {
         double macd3 =  iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,i); 
         double macd33 = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,i+1);
         double macd4 =  iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,i);
         double macd44 = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,i+1);
         
         if (bluemarker1 == 0)   // testing for first blue set
         {
            if (macd3 > 0 && macd33 == 0)  //ox    start of blueset1
            { 
               bluemarker1 = 1; 
               if (macd3 > bluenew) bluenew = macd3;
            }
            if (macd3 > 0 && macd33 > 0)    // xx  values within blueset1
            {
               if (macd3 >= macd33 && macd3 >= bluenew) bluenew = macd3;
               else if (macd33 > bluenew) bluenew = macd33;
            }    
            if (macd3 == 0 && macd33 > 0)       // xo   end of blueset1
            {
               if (macd33 > bluenew) bluenew = macd33;
            }
         }
   
         else if (bluemarker1 == 1)   // testing for second blue set
         {
            if (macd3 > 0 && macd33 == 0)  //ox    start of blueset2
            { 
               bluemarker2 = 1; 
               if (macd3 > blueold) blueold = macd3;
            }
            if (macd3 > 0 && macd33 > 0)    // xx  values within blueset2
            {
               if (macd3 >= macd33 && macd3 >= blueold) blueold = macd3;
               else if (macd33 > blueold) blueold = macd33;
            }    
            if (macd3 == 0 && macd33 > 0)       // xo   end of blueset2
            {
               if (macd33 > blueold) blueold = macd33;
            }
         }                                           
         
         if (redmarker1 == 0)   // testing for first red set
         {
            if (macd4 < 0 && macd44 == 0)  //ox    start of redset1
            { 
               redmarker1 = 1; 
               if (macd4 < rednew) rednew = macd4;
            }
            if (macd4 < 0 && macd44 < 0)    // xx  values within redset1
            {
               if (macd4 <= macd44 && macd4 <= rednew) rednew = macd4;
               else if (macd44 < rednew) rednew = macd44;           
            }    
            if (macd4 == 0 && macd44 < 0)       // xo   end of redset1
            {
               if (macd44 < rednew) rednew = macd44;
            }
         }
            
         else if (redmarker1 == 1)   // testing for second red set
         {
            if (macd4 < 0 && macd44 == 0)  //ox    start of redset2
            { 
               redmarker2 = 1; 
               if (macd4 < redold) redold = macd4;
            }
            if (macd4 < 0 && macd44 < 0)    // xx  values within redset2
            {
               if (macd4 <= macd44 && macd4 <= redold) redold = macd4;
               else if (macd44 < redold) redold = macd44;
            }    
            if (macd4 == 0 && macd44 < 0)       // xo   end of redset2
            {
               if (macd44 < redold) redold = macd44;
            }
         }
         
         if (bluemarker1 == 1 && bluemarker2 == 1 && redmarker1 == 1 && redmarker2 ==1) // = 2 sets each of the most recent blue and red bars have been analyzed
         {         
            double macdblue = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,1); 
            double macdred = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,1); 
      
            //Print (bluenew,blueold);
      
            if ((trend == 0 || trend == -1) && macdblue > 0 && bluenew > blueold)
            {
               trend = 1;
            }
            else if ((trend == 0 || trend == 1) && macdred < 0 && rednew < redold)   
            {
               trend = -1;
            }
            
            //Print(trend);
            
            break;
         }       
      }           
}  

bool IsTimeToTrade()
{
bool res = false;
      if (Hour() >= TimeToTrade1 && Hour() <= TimeToTrade2 && DayOfWeek()!=0 && DayOfWeek()!=6)
      {
         res = true;  
      }
return(res);
}


Comments