Author: Copyright � 2011, Serg Deev
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 HistogramFractals
0 Views
0 Downloads
0 Favorites
ma-shift
//+------------------------------------------------------------------+
//|                                                       Candle.mq4 |
//|                                      Copyright © 2011, Serg Deev |
//|                                            http://www.work2it.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Serg Deev"
#property link      "http://www.work2it.ru"

#define MAGIC  20050610

double MaxRisk            = 0.0; // ïðîöåíò ðèñêà äëÿ âû÷èñëåíèÿ ðàçìåðà ëîòà (åñëè 0 - íå ðàáîòàåò)
extern double Lots               = 0.1; // ôèêñèðîâàííûé ëîò (ïðè MaxRisk=0)
extern int StepProfit            = 300; // øàã ïðîôèòà
extern int StopLost              = 500; // íà÷àëüíûé ñòîïëîñò
extern int TrailingStop          = 600; // òðàë
extern int NumOrders             = 3;

extern int ma_fast               = 8;
extern int ma_slow               = 80;

extern int shift_min             = 20; // êðóòèçíà õàðàêòåðèñòèêè â ïèïñàõ

extern int macd_fast             = 4;
extern int macd_slow             = 76;

double MinLot;
double MaxLot;
double LotStep;
int Spread;
int StopLevel;
double StartLots;
double NextProfit;

extern int slippage = 5;
extern bool UseAllTicks = false;
extern bool UseFractalTrail = false;
string sx = "";

//+------------------------------------------------------------------+
int init() {
 StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL);
 Spread = MarketInfo(Symbol(),MODE_SPREAD);
 MinLot = MarketInfo(Symbol(),MODE_MINLOT);
 MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);
 LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);

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

double LotNormalize(double x) {
   x=MathFloor(x*100)/100;
   if (x > MaxLot) x = MaxLot;
   if (x < MinLot) x = MinLot;
   return(x);
}

//+------------------------------------------------------------------+
double Get_Lots() {
   double lot=Lots;
   if (MaxRisk > 0) {
    double RiskSumm = AccountFreeMargin()*MaxRisk;
    lot=RiskSumm/StopLost/100;
   }
   return(LotNormalize(lot));
}


//+------------------------------------------------------------------+
bool Signal_Stop_Buy() {
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Stop_Sell() {
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Buy() {
 int x1,x2;
 if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1))
 if (iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,3))
 if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) > iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))
 if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,1) > 0.0)
 if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,3) < 0.0) {
  x1 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))/Point;
  x2 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,3))/Point;
  if (x1 > shift_min) {
   if (x1 >= x2) return(true);
   if (x2 <= 0) return(true);
  }
 }

 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Sell() {
 int x1,x2;
 if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1))
 if (iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_slow,0,MODE_EMA,PRICE_CLOSE,3))
 if (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1) < iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))
 if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,1) < 0.0)
 if (iMACD(NULL,0,macd_fast,macd_slow,1,PRICE_CLOSE,MODE_MAIN,3) > 0.0) {
  x1 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,1))/Point;
  x2 = (iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,3) - iMA(NULL,0,ma_fast,0,MODE_EMA,PRICE_CLOSE,2))/Point;
  if (x1 > shift_min) {
   if (x1 >= x2) return(true);
   if (x2 <= 0) return(true);
  }
 }

 return(false);
}

//+------------------------------------------------------------------+
void CheckForOpen(int mode) {
   int res,i;
   
   if (Volume[0]>1) return;

   if ((mode == OP_SELL) && Signal_Sell()) {
      StartLots = Get_Lots();
      for (i=0; i<NumOrders; i++) res=OrderSend(Symbol(),OP_SELL,StartLots,Bid,slippage,0,0,sx,MAGIC+i,0,Red);
      return;
   }

   if ((mode == OP_BUY) && Signal_Buy()) {
      StartLots = Get_Lots();
      for (i=0; i<NumOrders; i++) res=OrderSend(Symbol(),OP_BUY,StartLots,Ask,slippage,0,0,sx,MAGIC+i,0,Blue);
      return;
   }
}
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double SL,TP,fx;
   int x,profit,num;

 if ((!UseAllTicks) && (Volume[0]>NumOrders)) return;
 
   for(int i=0;i<OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(!((OrderMagicNumber() >= MAGIC) && (OrderMagicNumber() < (MAGIC+NumOrders))) || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY) {
         if (Signal_Stop_Buy()) OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
         else if (OrderStopLoss() == 0.0) {
          SL = NormalizeDouble((Bid-StopLost*Point),Digits);
          TP = NormalizeDouble(Ask + (OrderMagicNumber()-MAGIC+1)*StepProfit*Point,Digits);
          if ((OrderMagicNumber()-MAGIC) == (NumOrders-1)) TP = 0.0;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else if ((TrailingStop > 0) || (UseFractalTrail)) {
          profit = (Bid-OrderOpenPrice())/Point;
          if (profit > 0.95*StepProfit) {
           if (UseFractalTrail) {
            fx = iFractals(NULL,0,MODE_LOWER,3);
            if ((fx > 0) && (fx > OrderStopLoss())) {
             OrderModify(OrderTicket(),OrderOpenPrice(),fx,OrderTakeProfit(),0,Blue);
            }
           }
           else if (TrailingStop > 0) {
            SL = NormalizeDouble((Bid-TrailingStop*Point),Digits);
            if (SL > OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue);
           }
          }
         }
      }
      if(OrderType()==OP_SELL) {
         if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
         else if (OrderStopLoss() == 0.0) {
          SL = NormalizeDouble((Ask+StopLost*Point),Digits);
          TP = NormalizeDouble(Bid - (OrderMagicNumber()-MAGIC+1)*StepProfit*Point,Digits);
          if ((OrderMagicNumber()-MAGIC) == (NumOrders-1)) TP = 0.0;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red);
         }
         else if ((TrailingStop > 0) || (UseFractalTrail)) {
          profit = (OrderOpenPrice()-Ask)/Point;
          if (profit > 0.95*StepProfit) {
           if (UseFractalTrail) {
            fx = iFractals(NULL,0,MODE_UPPER,3);
            if ((fx > 0) && (fx < OrderStopLoss())) {
             OrderModify(OrderTicket(),OrderOpenPrice(),fx,OrderTakeProfit(),0,Red);
            }
           }
           else if (TrailingStop > 0) {
            SL = NormalizeDouble((Ask+TrailingStop*Point),Digits);
            if (SL < OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Red);
           }
          }
         }
      }
   }
}

//+------------------------------------------------------------------+
int CalculateCurrentOrders(int mode)
  {
   int num=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if ((OrderMagicNumber() >= MAGIC) && (OrderMagicNumber() < (MAGIC+NumOrders)))
        {
         if ((OrderType()==OP_BUY) && (mode==OP_BUY))  num++;
         if ((OrderType()==OP_SELL) && (mode==OP_SELL)) num++;
        }
     }
   return(num);
  }

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

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 ---