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 indicatorStandard Deviation indicator
0 Views
0 Downloads
0 Favorites
mom-std-ma
//+------------------------------------------------------------------+
//|                                                   mom-std-ma.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 double MaxRisk            = 0.0; // ïðîöåíò ðèñêà äëÿ âû÷èñëåíèÿ ðàçìåðà ëîòà (åñëè 0 - íå ðàáîòàåò)
extern double Lots               = 0.1; // ôèêñèðîâàííûé ëîò (ïðè MaxRisk=0)
extern int TakeProfit            = 80; // êîíå÷íûé ïðîôèò
extern int MinProfit             = 40;  // ìèíèìàëüíûé ïðîôèò
extern int ProfitStep            = 20;  // øàã ïðîôèòà
extern double MinProfitPercent   = 0.6; // ïðîöåíò âçÿòèÿ ïðè ìèíèìàëüíîì ïðîôèòå
extern double ProfitPercent      = 0.1; // ïðîöåíò ïðîôèòà áîëüøå ìèíèìàëüíîãî
extern int TrailingStop          = 15;  // ðàáîòàåò ïîñëå âçÿòèÿ MinProfit
int StopLost                     = 15;  // ðàáîòàåò äî âçÿòèÿ MinProfit

extern int ma_period             = 30;
extern int mo_period             = 16;
int mo_ma_period                 = 3;
int mo_ma_method                 = 2;

extern int std_period            = 15;
extern int std_min               = 13;

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

int slippage = 3;
bool UseAllTicks                 = false;

//+------------------------------------------------------------------+
int init() {
 Spread = MarketInfo(Symbol(),MODE_SPREAD);
 MinLot = MarketInfo(Symbol(),MODE_MINLOT);
 MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);
 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() {
 double LoMa = iMA(NULL,0,ma_period,0,MODE_EMA,PRICE_LOW,0);
 double HiMa = iMA(NULL,0,ma_period,0,MODE_EMA,PRICE_HIGH,0);
 double mom_now = iCustom(NULL,0,"MomentumMX",mo_period,mo_ma_period,mo_ma_method,1,0);
 double mom_pre = iCustom(NULL,0,"MomentumMX",mo_period,mo_ma_period,mo_ma_method,1,1);
 double std_now = iStdDev(NULL,0,std_period,0,MODE_OPEN,PRICE_WEIGHTED,0)*10000;
 double std_pre = iStdDev(NULL,0,std_period,0,MODE_OPEN,PRICE_WEIGHTED,1)*10000;
 if (Close[1] > Open[1])
 if (Close[1] > HiMa)
 if (std_now > std_pre)
 if (std_now > std_min) 
 if (mom_now > 100)
 if (mom_now > mom_pre) {
  StopLost = (Open[0]-LoMa)/Point;
  return(true);
 }
 
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Sell() {
 double LoMa = iMA(NULL,0,ma_period,0,MODE_EMA,PRICE_LOW,0);
 double HiMa = iMA(NULL,0,ma_period,0,MODE_EMA,PRICE_HIGH,0);
 double mom_now = iCustom(NULL,0,"MomentumMX",mo_period,mo_ma_period,mo_ma_method,1,0);
 double mom_pre = iCustom(NULL,0,"MomentumMX",mo_period,mo_ma_period,mo_ma_method,1,1);
 double std_now = iStdDev(NULL,0,std_period,0,MODE_OPEN,PRICE_WEIGHTED,0)*10000;
 double std_pre = iStdDev(NULL,0,std_period,0,MODE_OPEN,PRICE_WEIGHTED,1)*10000;
 if (Close[1] < Open[1])
 if (Close[1] < LoMa)
 if (std_now > std_pre)
 if (std_now > std_min)
 if (mom_now < 100)
 if (mom_now < mom_pre) {
  StopLost = (HiMa-Open[0])/Point;
  return(true);
 }
 
 return(false);
}

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

   if (Volume[0]>1) return;

   if (Signal_Sell()) {
      StartLots = Get_Lots();
      NextProfit = Bid - MinProfit*Point;
      res=OrderSend(Symbol(),OP_SELL,StartLots,Bid,slippage,0,0,"",MAGICMA,0,Red);
      return;
   }

   if (Signal_Buy()) {
      StartLots = Get_Lots();
      NextProfit = Ask + MinProfit*Point;
      res=OrderSend(Symbol(),OP_BUY,StartLots,Ask,slippage,0,0,"",MAGICMA,0,Blue);
      return;
   }
}
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double SL,TP,lx;
   int profit;

 if ((!UseAllTicks) && (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,slippage,White);
         else if (OrderStopLoss() == 0.0) {
          SL = NormalizeDouble(Bid - StopLost*Point,Digits);
          TP = NormalizeDouble(Ask + TakeProfit*Point,Digits);
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          if (Bid > NextProfit) {
           if (Bid < (OrderOpenPrice()+(MinProfit+ProfitStep/2)*Point)) lx = LotNormalize(StartLots*MinProfitPercent);
           else lx = LotNormalize(StartLots*ProfitPercent);
           OrderClose(OrderTicket(),lx,Bid,slippage,White);
           NextProfit = Bid + ProfitStep*Point;
          }
          else if (NextProfit > (OrderOpenPrice()+MinProfit*Point)) {
           if (TrailingStop > 0) {
            SL = NormalizeDouble((Bid - TrailingStop*Point),Digits);
            if (OrderStopLoss() < SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue);
           }
          }
         }
         break;
      }
      if(OrderType()==OP_SELL) {
         if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
         else if (OrderStopLoss() == 0.0) {
          SL = NormalizeDouble(Bid + StopLost*Point,Digits);
          TP = NormalizeDouble(Ask - TakeProfit*Point,Digits);
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          if (Ask < NextProfit) {
           if (Ask > (OrderOpenPrice()-(MinProfit+ProfitStep/2)*Point)) lx = LotNormalize(StartLots*MinProfitPercent);
           else lx = LotNormalize(StartLots*ProfitPercent);
           OrderClose(OrderTicket(),lx,Ask,slippage,White);
           NextProfit = Bid - ProfitStep*Point;
          }
          else if (NextProfit < (OrderOpenPrice()-MinProfit*Point)) {
           if (TrailingStop > 0) {
            SL = NormalizeDouble(Ask + TrailingStop*Point,Digits);
            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

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