cm tral limit order MA

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
cm tral limit order MA
ÿþ//+---------------------------------------------+

//|     Copyright © 2019, Vladimir Hlystov      |

//|     cmillion@narod.ru                       |

//|     Skype: mqlcmillion                      |

//|     WhatsApp +79283690333                   |

//|     Telegram: mqlcmillion                   |

//|                                             |

//|     https://cmillion.ru/                    |

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

#property copyright "Copyright © 2019, https://cmillion.ru"

#property link      "cmillion@narod.ru"

#property version   "1.00"

#property strict

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

#property description "!>25B=8: >B:@K205B 8 B@0;8B ;8<8B=K9 >@45@ ?> 8=48:0B>@C "

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

extern int     period_MA    = 34;     //5@8>4 MA.

input ENUM_MA_METHOD ma_method=MODE_SMMA;

input ENUM_APPLIED_PRICE   applied_price = PRICE_OPEN;

extern double  Lot          = 0.01;   //;>B

extern int     Stoploss     = 50; 

extern int     Takeprofit   = 100; 

extern int     Magic        = 100;    //C=8:0;L=K9 =><5@ >@45@0

//--------------------------------------------------------------------

void OnTick()

{

   double MINLOT = MarketInfo(Symbol(),MODE_MINLOT);

   double MAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);

   if (Lot>MAXLOT) Lot = MAXLOT;

   if (Lot<MINLOT) Lot = MINLOT;

   int b=0,s=0,i,OT,TicketB=0,TicketS=0;

   double OOP;

   double Price = NormalizeDouble(iMA(NULL,0,period_MA,0,ma_method,applied_price,0),Digits);

   double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;

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

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      {

         if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())

         { 

            OT = OrderType(); 

            OOP = NormalizeDouble(OrderOpenPrice(),Digits);

            if (OT==OP_BUY) b++; 

            if (OT==OP_SELL) s++;

            if (OT==OP_BUYLIMIT) 

            {  

               TicketB=OrderTicket();

               if (OOP != Price && Price<NormalizeDouble(Bid-StopLevel,Digits))

               {  

                  if (!OrderModify(TicketB,Price,Stoploss==0?0:NormalizeDouble(Price-Stoploss*Point,Digits),

                  Takeprofit==0?0:NormalizeDouble(Price+Takeprofit*Point,Digits),0,clrBlue)) 

                     Comment("Error ",GetLastError(),"   Order Modify BUYLIMIT ",DoubleToStr(OOP,Digits),"->",DoubleToStr(Price,Digits));

                  else Comment("Order ",TicketB," BUYLIMIT BUYLIMIT ",DoubleToStr(OOP,Digits),"->",DoubleToStr(Price,Digits));

                  ObjectsDeleteAll(0,OBJ_ARROW);

               }

            }                                         

            if (OT==OP_SELLLIMIT) 

            {

               TicketS=OrderTicket();

               if (OOP != Price && Price>NormalizeDouble(Ask+StopLevel,Digits))

               {  

                  if (!OrderModify(TicketS,Price,Stoploss==0?0:NormalizeDouble(Price+Stoploss*Point,Digits),

                  Takeprofit==0?0:NormalizeDouble(Price-Takeprofit*Point,Digits),0,clrBlue)) 

                     Comment("Error ",GetLastError(),"   Order SELLLIMIT SellStop ",DoubleToStr(OOP,Digits),"->",DoubleToStr(Price,Digits));

                  else Comment("Order ",TicketS," SELLLIMIT Modify ",DoubleToStr(OOP,Digits),"->",DoubleToStr(Price,Digits));

                  ObjectsDeleteAll(0,OBJ_ARROW);

               }

            }                                         

         }

      }

   }

   if(!IsNewOrderAllowed()) return;

   double LOT=CheckVolumeValue(Lot);

   if(b+TicketB==0 && Price<NormalizeDouble(Bid-StopLevel,Digits)) 

   {

      if (AccountFreeMarginCheck(Symbol(),OP_BUY,LOT)<0) return;

      if (OrderSend(Symbol(),OP_BUYLIMIT,LOT,Price,0,Stoploss==0?0:NormalizeDouble(Price-Stoploss*Point,Digits),

      Takeprofit==0?0:NormalizeDouble(Price+Takeprofit*Point,Digits),NULL,Magic,0,clrBlue)==-1) 

         Comment("Error ",GetLastError()," OrderSend BUYLIMIT ->",DoubleToStr(Price,Digits));

      else Comment("Send Order BUYLIMIT->",DoubleToStr(Price,Digits));

   }

   if (s+TicketS==0 && Price>NormalizeDouble(Ask+StopLevel,Digits))

   {

      if (AccountFreeMarginCheck(Symbol(),OP_SELL,LOT)<0) return;

      if (OrderSend(Symbol(),OP_SELLLIMIT,LOT,Price,0,Stoploss==0?0:NormalizeDouble(Price+Stoploss*Point,Digits),

      Takeprofit==0?0:NormalizeDouble(Price-Takeprofit*Point,Digits),NULL,Magic,0,clrRed)==-1) 

         Comment("Error ",GetLastError()," OrderSend SELLLIMIT ->",DoubleToStr(Price,Digits));

      else Comment("Send Order SELLLIMIT->",DoubleToStr(Price,Digits));

   }

}

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

bool IsNewOrderAllowed()

  {

   int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

   if(max_allowed_orders==0)

      return(true);

   if(OrdersTotal()<max_allowed_orders)

      return(true);

   return(false);

  }

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

double CheckVolumeValue(double volume)

  {

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      return(min_volume);

     }



   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      return(max_volume);

     }



   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      return(ratio*volume_step);

     }

   return(volume);

  }

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

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