TriplePlay2





//+------------------------------------------------------------------+
//|                                                   TriplePlay.mq4 |
//|                                  Copyright © 2006, GwadaTradeBoy |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, GwadaTradeBoy"
#property link      "http://www.metaquotes.net"

//---- Include
#include <stderror.mqh>
#include <stdlib.mqh>

//---- Money Management
extern bool    MoneyManagement   = true;
extern double  Lots              = 0.1;
extern double  MaximumRisk       = 0.02;
extern double  DecreaseFactor    = 3;
double         lot;
int            orders, losses, spread;
bool           AcountIsMini      = false;
      
//---- Prise de position
int      MagicNumber             = 170107;
string   NameEA                  = "100in30.mq4";
int      ticket;
double   myPrice, myStopLoss, myTakeProfit, myLots, digits;
double   StopLoss                = 0;
double   Slippage                = 0;

//|Profit controls                                    |
//extern double StopLoss = 70;        // Maximum pips willing to lose per position.
extern int     FirstLevel        = 10;
extern int     SecondLevel       = 20;
extern double  TakeProfit1       = 30;
extern double  TakeProfit2       = 20;
extern double  TakeProfit3       = 10;

//---- Autres Variables
int      TradesInThisSymbol;

//---- Indicateurs
double   Units, LotMM;
double   MACD,RSI,MA13,MACDPrevious,RSIPrevious,MA13Previous;
int      Hours;

//---- Debugage
extern bool Debug;
int         OrderErr;

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

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

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   {
//----
//---- Verification du contexte de trade
      Units = MathFloor((AccountBalance( ) /5000)*0.5);
      if (Units < 0.05)
         Units = 0.01;
      Hours = TimeHour(TimeCurrent());
      if (Hours >= 7 && Hours <= 8)
         {
            GetIndicators();
            if(CheckEntryCondition(OP_BUY) )
               {
                  //CloseOpenOrders(OP_SELL);
                  OpenBuyOrders();
                  return(0);
               }
            if(CheckEntryCondition(OP_SELL) )
	           {
                  //CloseOpenOrders(OP_BUY);
                  OpenSellOrders();
	           }
         }
      //else 
//----*Fonctions qui serviront pour la partie si target atteind avant ou a 10H
//+------------------------------------------------------------------+
//| Check for Open Position                                          |
//+------------------------------------------------------------------+
     HandleOpenPositions();
// Check if any open positions were not closed
     TradesInThisSymbol = CheckOpenPositions();
      if (Hours <= 10 && TradesInThisSymbol == 0)
         {
            GetIndicators();
            if(CheckEntryCondition(OP_BUY) )
               {
                  //CloseOpenOrders(OP_SELL);
                  OpenBuyOrders();
                  return(0);
               }
            if(CheckEntryCondition(OP_SELL) )
	           {
                  //CloseOpenOrders(OP_BUY);
                  OpenSellOrders();
	           }
         }
//----
      return(0);
   }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| GetIndicators                                                    |
//+------------------------------------------------------------------+
void GetIndicators()
   {
MACD,RSI,MA13,MACDPrevious,RSIPrevious,MA13Previous
      MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,SignalCandle);
      MACDPrevious = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,SignalCandle + 1);
      SignalCurrent = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,SignalCandle);
      SignalPrevious = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,SignalCandle + 1);
      RSI = iRSI(NULL,0,12,PRICE_CLOSE,SignalCandle);
      RSIPrevious = iRSI(NULL,0,12,PRICE_CLOSE,SignalCandle + 1);
      MA13 = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,SignalCandle);
      MA13Previous = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,SignalCandle + 1);*/
   }
   
//+------------------------------------------------------------------+
//| CheckEntryCondition                                              |
//+------------------------------------------------------------------+
bool CheckEntryCondition(int type)
   {
      switch (type)
         {
            case OP_BUY :  if (MacdCurrent<0 && MacdCurrent>SignalCurrent &&
                              MacdPrevious<SignalPrevious &&
                              MathAbs(MacdCurrent)>(MACDOpenLevel*Point) &&
                              MaCurrent>MaPrevious)
                              {
                                 return(true);
                              }
                           break;
            case OP_SELL :    if (MacdCurrent>0 && MacdCurrent<SignalCurrent &&
                              MacdPrevious>SignalPrevious && 
                              MacdCurrent>(MACDOpenLevel*Point) &&
                              MaCurrent<MaPrevious)
                              {
                                 return(true);
                              }
         }
      return (false);
   }

//+------------------------------------------------------------------+
//| OpenOrder                                                        |
//| If Stop Loss or TakeProfit are used the values are calculated    |
//| for each trade                                                   |
//| place 3 orders :                                                 |
//| - 1 st order , 1 lot TARGET 30 pips                              |
//| - 2 nd order , 2 lot placed first order price +10 pips TARGET 20 pips
//| - 3 nd order , 3 lot placed first order price +20 pips TARGET 10 pips
//+------------------------------------------------------------------+
//---- * Buy
void OpenBuyOrders( )
   {
      myPrice = MarketInfo(Symbol( ), MODE_ASK);
      myStopLoss = 0;
      if ( StopLoss > 0 ) 
         myStopLoss = myPrice - StopLoss * Point ;
      myTakeProfit = myPrice + TakeProfit1 * 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); 
         }
// First Buy 
      ticket=OrderSend( Symbol(), OP_BUY, LotMM, myPrice, Slippage, myStopLoss, myTakeProfit, "FirstBuy", MagicNumber, 0, Green); 
      if (ticket > 0)
         {
            if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) 
               {
                  if (Debug) 
                     Print("BUY order opened : ", OrderOpenPrice( ));
                  myPrice = OrderOpenPrice() + FirstLevel*Point;
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice - StopLoss * Point ;
                  myTakeProfit = myPrice + TakeProfit2*Point;
                  if (digits > 0) 
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
                     }
                  ticket=OrderSend( Symbol(), OP_BUYSTOP, 2*LotMM, myPrice, Slippage, myStopLoss, myTakeProfit, "Second Buy", MagicNumber, 0, Green); 
                  myPrice = OrderOpenPrice() + SecondLevel*Point;
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice - StopLoss * Point ;
                  myTakeProfit = myPrice + TakeProfit3*Point;
                  if (digits > 0) 
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
                     }
                  ticket=OrderSend( Symbol(), OP_BUYSTOP, 3*LotMM, myPrice, Slippage, myStopLoss, myTakeProfit, "Third Buy", MagicNumber, 0 ,Green); 
               }
         }
      else
         {
            OrderErr = GetLastError();
            Print("Error opening BUY order : (" + OrderErr + ") " + ErrorDescription( OrderErr) );
         }
   }
//---- * Sell 
void OpenSellOrders( )
   {
      myPrice = MarketInfo(Symbol( ), MODE_BID);
      myStopLoss = 0;
      if ( StopLoss > 0 ) 
         myStopLoss = myPrice + StopLoss * Point ;
      myTakeProfit = myPrice - TakeProfit1 * 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); 
         }
// First Sell
      ticket=OrderSend( Symbol(), OP_SELL, LotMM, myPrice, Slippage, myStopLoss ,myTakeProfit, "First Sell", MagicNumber, 0, Red); 
      if (ticket > 0)
         {
            if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) 
               {
                  if (Debug) 
                     Print("Sell order opened : ", OrderOpenPrice());
                  myPrice = OrderOpenPrice() - FirstLevel*Point;
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice + StopLoss * Point ;
                  myTakeProfit = myPrice - TakeProfit2*Point;
                  if (digits > 0) 
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
                     }
                  ticket=OrderSend( Symbol(), OP_SELLSTOP, 2*LotMM, myPrice, Slippage, myStopLoss, myTakeProfit, "Second Sell", MagicNumber, 0, Red); 
                  myPrice = OrderOpenPrice() - SecondLevel*Point;
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice + StopLoss * Point ;
                  myTakeProfit = myPrice - TakeProfit3*Point;
                  if (digits > 0) 
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits); 
                     }
                  ticket=OrderSend( Symbol(), OP_SELLSTOP, 3*LotMM, myPrice, Slippage, myStopLoss, myTakeProfit, "Third Sell", MagicNumber, 0, Red); 
               }
         }
      else
         { 
            OrderErr = GetLastError();
            Print("Error opening Sell order : (" + OrderErr + ") " + ErrorDescription( OrderErr) );
         }
   }
   
//+------------------------------------------------------------------+
//| Check Open Position Controls                                     |
//+------------------------------------------------------------------+
int CheckOpenPositions()
   {
      int cnt, total, NumPositions;
      NumPositions = 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 )  
               NumPositions++;
            if(OrderType() == OP_SELL ) 
               NumPositions++;
         }
      return (NumPositions);
   }   



Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

MACD Histogram
Relative strength index
Moving average indicator


Custom Indicators Used:

Order Management characteristics:
It automatically opens orders when conditions are reached
Checks for the total of open orders

Other Features: