Author: Copyright � 2011, Serg Deev
Profit factor:
0.00
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
Moving average indicatorMACD Histogram
0 Views
0 Downloads
0 Favorites
puria-m15
//+------------------------------------------------------------------+
//|                                                   Puaria-m15.mq4 |
//|                                      Copyright © 2011, Serg Deev |
//|                                            http://www.work2it.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Serg Deev"
#property link      "http://www.work2it.ru"

#define MAGICMA  20050610

extern string Trades             = "Íàñòðîéêè òîðãîâûõ îïåðàöèé";
extern double Lots               = 0.05;
extern int TakeProfit            = 40;
extern int MinProfit             = 25;
extern int TrailingStop          = 15;
extern int StopLost              = 55;

extern string Frames             = "Íàñòðîéêè îñíîâíîãî è âñïîìîãàòåëüíîãî òàéìôðåéìîâ";
extern int Main_TimeFrame        = 15; // 1-PERIOD_M1, 5-PERIOD_M5, 15-PERIOD_M15, 30-PERIOD_M30, 60-PERIOD_H1, 240-PERIOD_H4, 1440-PERIOD_D1, 10080-PERIOD_W1, 43200-PERIOD_MN1
extern int Slow_TimeFrame        = 30; // 1-PERIOD_M1, 5-PERIOD_M5, 15-PERIOD_M15, 30-PERIOD_M30, 60-PERIOD_H1, 240-PERIOD_H4, 1440-PERIOD_D1, 10080-PERIOD_W1, 43200-PERIOD_MN1

extern string FastMA             = "Íàñòðîéêè ïåðâîé ìåäëåííîé MA";
extern int ma0_period            = 85;
extern int ma0_shift             = 4;
extern int ma0_method            = 3; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
extern int ma0_price             = 3; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern string SlowMA1            = "Íàñòðîéêè âòîðîé ìåäëåííîé MA";
extern int ma1_period            = 70;
extern int ma1_shift             = 3;
extern int ma1_method            = 1; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
extern int ma1_price             = 5; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern string SlowMA2            = "Íàñòðîéêè áûñòðîé MA";
extern int ma2_period            = 14;
extern int ma2_shift             = 0;
extern int ma2_method            = 2; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
extern int ma2_price             = 4; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern string MACD_1             = "Íàñòðîéêè MACD íà áûñòðîì òàéìôðåéìå";
extern int macd_fast             = 18;
extern int macd_slow             = 28;
extern int macd_signal           = 1;
extern int macd_price            = 2; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int macd_open             = 2;

extern string MACD_2             = "Íàñòðîéêè MACD íà ìåäëåííîì òàéìôðåéìå";
extern int macd1_fast            = 7;
extern int macd1_slow            = 36;
extern int macd1_signal          = 1;
extern int macd1_price           = 1; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int macd1_open            = 7;

int GAP_Level                    = 20;
int GAP_TimeOUT                  = 10;
int GAP_Timer                    = 0;

//+------------------------------------------------------------------+
int init() {
   return(0);
}
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}

//+------------------------------------------------------------------+
bool Signal_Stop_Buy() {
 double ma0 = iMA(NULL,Main_TimeFrame,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,Main_TimeFrame,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,Main_TimeFrame,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 if ((ma1 > ma2) || (ma0 > ma2)) return(true);
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Stop_Sell() {
 double ma0 = iMA(NULL,Main_TimeFrame,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,Main_TimeFrame,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,Main_TimeFrame,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 if ((ma1 < ma2) || (ma0 < ma2)) return(true);
 return(false);
}

//+------------------------------------------------------------------+
bool macd_up(int timeframe, int fast, int slow, int signal, int price, int num) {
 double y;
 double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
 for (int i=1; i<num; i++) {
  y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
  if (y > x) return(false);
  else x = y;
 }
 return(true);
}

//+------------------------------------------------------------------+
bool macd_down(int timeframe, int fast, int slow, int signal, int price, int num) {
 double y;
 double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
 for (int i=1; i<num; i++) {
  y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
  if (y < x) return(false);
  else x = y;
 }
 return(true);
}

//+------------------------------------------------------------------+
bool Signal_Buy() {
 double ma0 = iMA(NULL,Main_TimeFrame,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,Main_TimeFrame,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,Main_TimeFrame,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 double macd = iMACD(NULL,Main_TimeFrame,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
 double macd1 = iMACD(NULL,Slow_TimeFrame,macd1_fast,macd1_slow,macd1_signal,macd1_price,MODE_MAIN,0);
 
 if (macd_up(Slow_TimeFrame,macd1_fast,macd1_slow,macd1_signal,macd1_price,macd1_open))
// if (macd1 > 0)
 if (macd_up(Main_TimeFrame,macd_fast,macd_slow,macd_signal,macd_price,macd_open))
 if (macd > 0)
 if ((ma1 < ma2) && (ma0 < ma2)) return(true);
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Sell() {
 double ma0 = iMA(NULL,Main_TimeFrame,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,Main_TimeFrame,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,Main_TimeFrame,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 double macd = iMACD(NULL,Main_TimeFrame,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
 double macd1 = iMACD(NULL,Slow_TimeFrame,macd1_fast,macd1_slow,macd1_signal,macd1_price,MODE_MAIN,0);
 
 if (macd_down(Slow_TimeFrame,macd1_fast,macd1_slow,macd1_signal,macd1_price,macd1_open))
// if (macd1 < 0)
 if (macd_down(Main_TimeFrame,macd_fast,macd_slow,macd_signal,macd_price,macd_open))
 if (macd < 0)
 if ((ma1 > ma2) && (ma0 > ma2)) return(true);
 return(false);
}

//+------------------------------------------------------------------+
void CheckForOpen() {
   int res;

   if(Volume[0]>1) return;

   int gap = MathAbs(Open[0]-Close[1])/Point; 
   if (gap > GAP_Level) GAP_Timer = GAP_TimeOUT;

   if (GAP_Timer > 0) {
    GAP_Timer--;
    if (GAP_Timer > 0) return;
   }

   if (Signal_Sell()) {
      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,Red);
      return;
   }

   if (Signal_Buy()) {
      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
   }
}
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double SL,TP;
   int profit;

 if(Volume[0]>1) return;

   for(int i=0;i<OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY) {
         if (Signal_Stop_Buy()) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         else if (OrderStopLoss() == 0.0) {
          SL = Ask - StopLost*Point;
          TP = Bid + TakeProfit*Point;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          profit = (Bid - OrderOpenPrice())/Point;
          if (profit > MinProfit) {
           SL = Bid - TrailingStop*Point;
           if (OrderStopLoss() < SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue);
          }
         }
         break;
      }
      if(OrderType()==OP_SELL) {
         if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         else if (OrderStopLoss() == 0.0) {
          SL = Bid + StopLost*Point;
          TP = Ask - TakeProfit*Point;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          profit = (OrderOpenPrice() - Ask)/Point;
          if (profit > MinProfit) {
           SL = Ask + TrailingStop*Point;
           if (OrderStopLoss() > SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue);
          }
         }
         break;
      }
   }
}

//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
void start()
  {
   if(Bars<100 || IsTradeAllowed()==false) return;
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else CheckForClose();
  }
//+------------------------------------------------------------------+

Comments