Author: Copyright 2017, Andrey Minaev
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
Indicators Used
Moving Average of Oscillator
0 Views
0 Downloads
0 Favorites
EA_OsMA
ÿþ//+-----------------------------------------------------------------------------------------------+

//|                                                                                   EA_OsMA.mq4 |

//|                                                                 Copyright 2017, Andrey Minaev |

//|                                                     https://www.mql5.com/ru/users/id.scorpion |

//|                                                                     Mail: id.scorpion@mail.ru |

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

#property copyright "Copyright 2017, Andrey Minaev"

#property link      "https://www.mql5.com/ru/users/id.scorpion"

#property version   "1.00"

#property strict



enum CONTINUE_TRADING

{

   YES,   // Yes

   NO     // No

};



extern string             paramEA     = "";               // Parameters EA

extern double             fixLot      = 0.01;             // FixLot

extern int                stopLoss    = 50;               // StopLoss

extern int                takeProfit  = 50;               // TakeProfit

extern int                breakEven   = 20;               // BreakEven

extern int                magic       = 123;              // Magic

extern CONTINUE_TRADING   contTrading = YES;              // ContinueTrading



extern string             paramInd    = "";               // Parameters OsMA

extern ENUM_TIMEFRAMES    timeFrames  = PERIOD_CURRENT;   // TimeFrames

extern int                fastEMA     = 12;               // FastEMA

extern int                slowEMA     = 26;               // SlowEMA

extern int                macdSMA     = 9;                // MACDSMA

extern ENUM_APPLIED_PRICE calcPrice   = PRICE_CLOSE;      // CalcPrice



datetime newBar;

bool openBuy, openSell;

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

int OnInit()

{

   return(INIT_SUCCEEDED);

}

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

void OnDeinit(const int reason)

{

   

}

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

void OnTick()

{

   if(newBar != Time[0])

   {

      double val_1 = iOsMA(_Symbol, timeFrames, fastEMA, slowEMA, macdSMA, calcPrice, 1);

      double val_2 = iOsMA(_Symbol, timeFrames, fastEMA, slowEMA, macdSMA, calcPrice, 2);

      

      if(val_1 > 0 && val_2 < 0) { openBuy  = true; openSell = false; }

      if(val_1 < 0 && val_2 > 0) { openSell = true; openBuy  = false; }

      

      if(openBuy)

      {

         if(CountNumberOrders() >  0) CloseMarketOrder();

         if(CountNumberOrders() == 0) OpenMarketOrder(OP_BUY, fixLot, Ask);

      }

      if(openSell)

      {

         if(CountNumberOrders() >  0) CloseMarketOrder();

         if(CountNumberOrders() == 0) OpenMarketOrder(OP_SELL, fixLot, Bid);

      }

   }

   newBar = Time[0];

   

   if(breakEven > 0) SetBreakEven();

   

   if(contTrading == YES && CountNumberOrders() == 0)

   {

      double val_0 = iOsMA(_Symbol, timeFrames, fastEMA, slowEMA, macdSMA, calcPrice, 0);

      if(val_0 > 0) OpenMarketOrder(OP_BUY,  fixLot, Ask);

      if(val_0 < 0) OpenMarketOrder(OP_SELL, fixLot, Bid);

   }

}

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

// $C=:F8O >B:@K205B @K=>G=K9 >@45@                                                               |

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

void OpenMarketOrder(int type, double lot, double price)

{

   int ticket = OrderSend(_Symbol, type, lot, price, 0, 0, 0, "", magic, 0);

   

   if(ticket > 0 && (stopLoss > 0 || takeProfit > 0)) SetSLTP(ticket);

   

   if(type == OP_BUY)  openBuy  = false;

   if(type == OP_SELL) openSell = false;

}

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

// $C=:F8O CAB0=02;8205B StopLoss 2 157C1KB>:                                                     |

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

void SetBreakEven(void)

{

   double stopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);

   double be = breakEven;

   

   if(be < stopLevel) be = stopLevel;

   

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic)

         {

            int    ticket = OrderTicket();

            int    type   = OrderType();

            double oop    = NormalizeDouble(OrderOpenPrice(), _Digits);

            double sl     = NormalizeDouble(OrderStopLoss(), _Digits);

            double tp     = NormalizeDouble(OrderTakeProfit(), _Digits);

            

            if(type == OP_BUY  && oop != sl) if(Bid >= oop + be * _Point) if(OrderModify(ticket, oop, oop, tp, 0)) continue;

            if(type == OP_SELL && oop != sl) if(Ask <= oop - be * _Point) if(OrderModify(ticket, oop, oop, tp, 0)) continue;

         }

}

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

// $C=:F8O CAB0=02;8205B StopLoss 8 TakeProfit                                                    |

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

void SetSLTP(int ticket)

{

   double stopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);

   double sl = stopLoss;

   double tp = takeProfit;

   

   if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

   {

      int    type = OrderType();

      double oop  = OrderOpenPrice();

      

      if(stopLoss > 0)

      {

         if(stopLoss < stopLevel) sl = stopLevel;

         if(type == OP_BUY)  sl = NormalizeDouble(Bid - sl * _Point, _Digits);

         if(type == OP_SELL) sl = NormalizeDouble(Ask + sl * _Point, _Digits);

      }

      if(takeProfit > 0)

      {

         if(takeProfit < stopLevel) tp = stopLevel;

         if(type == OP_BUY)  tp = NormalizeDouble(Ask + tp * _Point, _Digits);

         if(type == OP_SELL) tp = NormalizeDouble(Bid - tp * _Point, _Digits);

      }

      if(OrderModify(ticket, oop, sl, tp, 0)) return;

   }

}

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

// $C=:F8O 70:@K205B 2A5 @K=>G=K5 >@45@0                                                          |

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

void CloseMarketOrder(void)

{

   for(int i = OrdersTotal(); i >= 0; i--)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic)

         {

            if(OrderType() == OP_BUY)  if(OrderClose(OrderTicket(), OrderLots(), Bid, 0)) continue;

            if(OrderType() == OP_SELL) if(OrderClose(OrderTicket(), OrderLots(), Ask, 0)) continue;

         }

}

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

// $C=:F8O ?>4AG8BK205B :>;8G5AB2> @K=>G=KE 8 >B;>65==KE >@45@>2                                  |

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

int CountNumberOrders(void)

{

   int number = 0;

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic)

            number++;

   return(number);

}

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

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