Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
3---MA
//+------------------------------------------------------------------+
//|                                             3#MA.mq4             |
//|                               Copyright © 2012, Dima Sko (emonh) |
//|                                                oneforex.my1.ru   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, http://oneforex.my1.ru"
#property link      "fin3@bk.ru"
//--------------------------------------------------------------------




extern string  MA1="";     
extern int     period_1             = 5,           
               ma_shift_1           = 0,           
               ma_method_1          = MODE_EMA,    
               applied_price_1      = PRICE_OPEN,  
                                                   
               timeframe_1          = 0;           
extern string  MA2="";     
extern int     period_2             = 30,          
               ma_shift_2           = 0,           
               ma_method_2          = MODE_LWMA,   
               applied_price_2      = PRICE_OPEN,  
               timeframe_2          = 0;           
extern string  MA3="";     
extern int     period_3             = 100,         
               ma_shift_3           = 0,           
               ma_method_3          = MODE_LWMA,   
               applied_price_3      = PRICE_OPEN,  
               timeframe_3          = 0;           
extern string  îáùèå.ïàðàìåòðû="";     
extern int     stoploss             = 0,           //ñòîïëîññ
               takeprofit           = 0,           //òåéêïðîôèò
               TrailingStop         = 0,           //òðåéëèíãñòîï, åñëè 0, òî íåò òðåéëèíãà
               NoLoss               = 0,           //ïåðåâîä â áåçóáûòîê, åñëè 0, òî íåò ïåðåâîäà â áåçóáûòîê
               MaxOrders            = 1;           //ìàêñèìàëüíîå êîë-âî îðäåðîâ îäíîâðåìåííî íà ñ÷åòå
extern double  LOT                  = 0.1;         //îáúåì îðäåðà
extern bool    CloseRevers          = true;        //çàêðûâàòü îðäåðà ïðè âñòðå÷íîì ñèãíàëå

extern string           _1_        =                            "Äîáàâèë îò ñåáÿ (íå îáÿçàòåëüíî åñëè ñëåäîâàòü òîëüêî ñòðàòåãèè)" ;
extern string           __1_        =                           "ÌÀÐÒÈÍÃÅÉË Risk > 0 (Ðàáîòàåò),Risk < èëè = 0 (Íåðàáîòàåò)" ;
extern double     risk = 0.01 ;  //
extern int     MAgic = 100500;//

//--------------------------------------------------------------------
int TimeBar,STOPLEVEL,Magic;
//--------------------------------------------------------------------
int start()
{
   if (risk > 0.01) double lot = NormalizeDouble(AccountBalance() * risk/100 / 1000.0, 2);
   if (risk < 0.01) lot = LOT;
   
   
   MAgic = Magic;
   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (TrailingStop>=STOPLEVEL) TrailingStop();
   if (NoLoss>=STOPLEVEL) NoLoss();
   if (TimeBar==Time[0]) return(0);

   double MA10 = iMA(NULL,timeframe_1,period_1,ma_shift_1,ma_method_1,applied_price_1,0);
   double MA11 = iMA(NULL,timeframe_1,period_1,ma_shift_1,ma_method_1,applied_price_1,1);
   double MA20 = iMA(NULL,timeframe_2,period_2,ma_shift_2,ma_method_2,applied_price_2,0);
   double MA21 = iMA(NULL,timeframe_2,period_2,ma_shift_2,ma_method_2,applied_price_2,1);
   double MA3  = iMA(NULL,timeframe_3,period_3,ma_shift_3,ma_method_3,applied_price_3,0);
   
   double SL,TP;
   if (MA10>MA20&&MA11<MA21&&MA20>MA3)
   {
      if (CloseRevers) CLOSEORDER(OP_SELL);
      if (MaxOrders>OrdersTotal())       
      {
         if (takeprofit!=0) TP  = NormalizeDouble(Ask + takeprofit*Point,Digits); else TP = 0;
         if (stoploss!=0)   SL  = NormalizeDouble(Bid - stoploss*Point,Digits); else SL = 0;
         if (OrderSend(Symbol(),OP_BUY, lot,NormalizeDouble(Ask,Digits),2,SL,TP,"ïðîñòîé ñîâåòíèê",Magic,3)!=-1) TimeBar=Time[0]; 
         else Print("OrderSend BUY Error ",GetLastError(),"  SL ",SL,"  TP ",TP);
      }
   }
   if (MA10<MA20&&MA11>MA21&&MA20<MA3)
   {
      if (CloseRevers) CLOSEORDER(OP_BUY);
      if (MaxOrders>OrdersTotal()) 
      {
         if (takeprofit!=0) TP = NormalizeDouble(Bid - takeprofit*Point,Digits); else TP = 0;
         if (stoploss!=0)   SL = NormalizeDouble(Ask + stoploss*Point,Digits); else SL = 0;
         if (OrderSend(Symbol(),OP_SELL,lot,NormalizeDouble(Bid,Digits),2,SL,TP,"ïðîñòîé ñîâåòíèê",Magic,3)!=-1) TimeBar=Time[0]; 
         else Print("OrderSend SELL Error ",GetLastError(),"  SL ",SL,"  TP ",TP);
      }
   }
return(0);
}
//--------------------------------------------------------------------
void CLOSEORDER(int ord)
{
   for (int i=0; i<OrdersTotal(); i++)
   {                                               
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            if (OrderType()==OP_BUY && ord==OP_BUY)
               OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,CLR_NONE);
            if (OrderType()==OP_SELL && ord==OP_SELL)
               OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,CLR_NONE);
         }
      }   
   }
}
//--------------------------------------------------------------------
void TrailingStop()
{
   int tip,Ticket;
   double StLo,OSL,OOP;
   for (int i=0; i<OrdersTotal(); i++) 
   {  if (OrderSelect(i, SELECT_BY_POS))
      {  tip = OrderType();
         if (tip<2 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            OSL   = NormalizeDouble(OrderStopLoss(),Digits);
            OOP   = NormalizeDouble(OrderOpenPrice(),Digits);
            Ticket = OrderTicket();
            if (tip==OP_BUY)             
            {
               StLo = NormalizeDouble(Bid-TrailingStop*Point,Digits);
               if (StLo > OSL && StLo > OOP)
               {  if (!OrderModify(Ticket,OOP,StLo,OrderTakeProfit(),0,White))
                     Print("TrailingStop Error ",GetLastError()," buy SL ",OSL,"->",StLo);
               }
            }                                         
            if (tip==OP_SELL)        
            {
               StLo = NormalizeDouble(Ask+TrailingStop*Point,Digits);
               if (StLo > OOP || StLo==0) continue;
               if (StLo < OSL || OSL==0 )
               {  if (!OrderModify(Ticket,OOP,StLo,OrderTakeProfit(),0,White))
                     Print("TrailingStop Error ",GetLastError()," sell SL ",OSL,"->",StLo);
               }
            } 
         }
      }
   }
}
//--------------------------------------------------------------------
void NoLoss()
{
   int tip,Ticket;
   double StLo,OSL,OOP;
   for (int i=0; i<OrdersTotal(); i++) 
   {  if (OrderSelect(i, SELECT_BY_POS))
      {  tip = OrderType();
         if (tip<2 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            OSL   = NormalizeDouble(OrderStopLoss(),Digits);
            OOP   = NormalizeDouble(OrderOpenPrice(),Digits);
            Ticket = OrderTicket();
            if (tip==OP_BUY)             
            {
               if (OSL>OOP) continue;
               StLo = NormalizeDouble(OOP+Point,Digits);
               if (NormalizeDouble((Bid-OOP)/Point,0)>=NoLoss)
               {  if (!OrderModify(Ticket,OOP,StLo,OrderTakeProfit(),0,White))
                     Print("NoLoss Error ",GetLastError()," buy SL ",OSL,"->",StLo);
               }
            }                                         
            if (tip==OP_SELL)        
            {
               if (OSL<OOP) continue;
               StLo = NormalizeDouble(OOP-Point,Digits);
               if (NormalizeDouble((OOP-Ask)/Point,0)>=NoLoss)
               {  if (!OrderModify(Ticket,OOP,StLo,OrderTakeProfit(),0,White))
                     Print("NoLoss Error ",GetLastError()," sell SL ",OSL,"->",StLo);
               }
            } 
         }
      }
   }
}
//--------------------------------------------------------------------

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