Author: kwadrad
Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedChecks for the total of closed ordersIt 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
eTonnel_V
//+------------------------------------------------------------------+
//|                                                    eTonnel_V.mq4 |
//|                                                          kwadrad |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "kwadrad"
#property link      ""
extern string ñ1= "Ìóâèíãè";
extern int       perSlowMA=169;
extern int       perFastMA=144;
extern string ñ2= "Ðèñêè, ëîòû, òðåéëèíã-ñòîï, áåçóáûòîê";
extern double    Lot=0;
extern double    Risk=0.05;
extern int       Breakeven=0;
extern int       TrailingStop=0;
extern double    DecreaseFactor= 13.0;
extern string ñ3= "Âðåìÿ âûñòàâëåíèÿ îðäåðîâ";
extern int       begin=7;
extern int       end=23;
extern string ñ5= "Äîïîëíèòåëüíî";
extern double    Fibo= 89;
extern double    MinDepo=30.0;
int       TrailingStep=2;
int    Mn=123;
int    slippage=10;
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
bool signalTrendBuy()
 {
 if (MA(NULL,perFastMA,0,2)>MA(NULL,perSlowMA,0,2))
  {if(iClose(NULL,0,2)>MA(NULL,perFastMA,0,2)&& iOpen(NULL,0,2)<MA(NULL,perFastMA,0,2)&& iOpen(NULL,0,1)<iClose(NULL,0,1))
  return(true);}else 
 if (MA(NULL,perFastMA,0,2)<MA(NULL,perSlowMA,0,2))
  {if(iClose(NULL,0,2)>MA(NULL,perSlowMA,0,2)&& iOpen(NULL,0,2)<MA(NULL,perSlowMA,0,2)&& iOpen(NULL,0,1)<iClose(NULL,0,1))
  return(true);}else return(false);
}
//--------------------------------------------------------------------
bool signalTrendSell()
 {
if (MA(NULL,perFastMA,0,2)<MA(NULL,perSlowMA,0,2))
{if(iClose(NULL,0,2)<MA(NULL,perFastMA,0,2)&& iOpen(NULL,0,2)>MA(NULL,perFastMA,0,2)&& iOpen(NULL,0,1)>iClose(NULL,0,1))
   return(true);}
   else
 if (MA(NULL,perFastMA,0,2)>MA(NULL,perSlowMA,0,2))
{if(iClose(NULL,0,2)<MA(NULL,perSlowMA,0,2)&& iOpen(NULL,0,2)>MA(NULL,perSlowMA,0,2)&& iOpen(NULL,0,1)>iClose(NULL,0,1))
   return(true);}  
   
   else return(false);
 }
 //---------------------------------------------------------------
 double MA(int x1,int x2,int x3,int x4)
{
double  ma=iMA(NULL,x1,x2,x3,MODE_EMA,PRICE_CLOSE,x4);
return(ma);
 }
  //------------------------------------------------------------------------
double OtschetUp()
{double cross;
if (MA(NULL,perFastMA,0,2)>MA(NULL,perSlowMA,0,2))
 cross=MA(NULL,perFastMA,0,2);else
 cross=MA(NULL,perSlowMA,0,2);
return (cross);
}
  //------------------------------------------------------------------------+
double OtschetDn()
{double cross;
if (MA(NULL,perFastMA,0,2)<MA(NULL,perSlowMA,0,2))
 cross=MA(NULL,perSlowMA,0,2);else
 cross=MA(NULL,perFastMA,0,2);
return (cross);
}
  //------------------------------------------------------------------------+
double SLup()
{double cross;
if (MA(NULL,perFastMA,0,2)>MA(NULL,perSlowMA,0,2))
 cross=MA(NULL,perSlowMA,0,2);else
 cross=MA(NULL,perFastMA,0,2);
return (cross);
}
  //------------------------------------------------------------------------+
double SLdn()
{double cross;
if (MA(NULL,perFastMA,0,2)<MA(NULL,perSlowMA,0,2))
 cross=MA(NULL,perSlowMA,0,2);else
 cross=MA(NULL,perFastMA,0,2);
return (cross);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if (OrdersTotal()>0 && MA(NULL,perFastMA,0,0)>MA(NULL,perSlowMA,0,0))
  {
  if (iClose(NULL,0,0)<MA(NULL,perFastMA,0,0)&& iClose(NULL,0,0)>MA(NULL,perSlowMA,0,0)) CloseOrders(Mn);
  }
  else
  {if (iClose(NULL,0,0)>MA(NULL,perFastMA,0,0)&& iClose(NULL,0,0)<MA(NULL,perSlowMA,0,0)) CloseOrders(Mn);
  }
//----
  int total=CountTrades(Mn); 
if (AccountFreeMargin()>=MinDepo && OrdersTotal()==0){ 
int    ticket;
     if(signalTrendBuy()&& time())
         {
             ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,slippage,SLup()-2*MarketInfo(Symbol(),MODE_SPREAD)*fPointX(),OtschetUp()+Fibo*Point-2*MarketInfo(Symbol(),MODE_SPREAD)*fPointX(),"3535",Mn,0,Blue);
                     
                  }
     if(signalTrendSell()&& time())
         {
             ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,slippage,SLdn()+2*MarketInfo(Symbol(),MODE_SPREAD)*fPointX(),OtschetDn()-Fibo*Point+2*MarketInfo(Symbol(),MODE_SPREAD)*fPointX(),"3535",Mn,0,Red);
                } 
//----
  }
Bezubytok();
TrailingStop();

   return(0);
  }
//+------------------------------------------------------------------+
 //-----------------------------------------------------
double LotsOptimized()
  {
   double lot_;
   int    orders=OrdersHistoryTotal();  
   int    losses=0;                  
 if(Lot ==0)
     {
   lot_=NormalizeDouble(AccountFreeMargin()*Risk/1000.0,2);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot_=NormalizeDouble(lot_-lot_*losses/DecreaseFactor,2);
     }}
  
     else lot_=Lot;
   if(lot_<MarketInfo(Symbol(), MODE_MINLOT) ) lot_=MarketInfo(Symbol(), MODE_MINLOT);
   if(lot_> MarketInfo( Symbol(), MODE_MAXLOT) ) {lot_= MarketInfo(Symbol(), MODE_MAXLOT);}
   return(lot_);
  }
  //--------------------------------------------------------------------------|
 double fPointX()
 {
   double PointX = 0;
   if(Digits==5 || Digits==3) 
     {
      PointX = Point * 10;
     }
   if(Digits==4 || Digits==2) 
     {
      PointX = Point;
     }
   return(PointX);
 }
    //-------------------------------------------------------------+
   bool time()
 {
  if(Hour()>=begin && Hour()<=end)
 return(true);else return(false);
 }
 
 //+------------------------------------------------------------------+
int CountTrades(int m) {
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol()|| OrderMagicNumber() != m) continue;
      if (OrderSymbol() == Symbol()&& OrderMagicNumber() == m)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++;
   }
   return (count);
}

//-------------------------------------------------------------------+
int CloseOrders(int mmmm) //  ïðîöåäóðà çàêðûòèÿ âñåõ îðäåðîâ
{
  int total  = OrdersTotal();
  
  for (int cnt = total-1 ; cnt >= 0 ; cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == mmmm && OrderSymbol()==Symbol())
    {
    //Print(cnt);
      if (OrderType()==OP_BUY)
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
      }
      
      if (OrderType()==OP_SELL)
      {
        OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
      }
      if (OrderType()==OP_BUYSTOP)
      {
        OrderDelete(OrderTicket());
      }
      
      if (OrderType()==OP_SELLSTOP)
      {
        OrderDelete(OrderTicket());
      }      
    }
  }
  return(0);
}
//+------------------------------------------------------------------+
void Bezubytok()
 {
 int cnt,total;
 total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Mn)  
        {
         if(OrderType()==OP_BUY) 
           {
            if((Bid-OrderOpenPrice())> Breakeven*fPointX()&& Breakeven>0)  
              {  
               if(OrderStopLoss() < OrderOpenPrice())  
                 {

                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
                     return(0);
                 } 
              }
           }
         else 
           {
           if((OrderOpenPrice()-Ask)/fPointX() > Breakeven && Breakeven>0)  
              {                 
            if( OrderStopLoss() > OrderOpenPrice()) { 

                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
                     return(0);
              }}
           }
        }
     }  
   return(0); 
}
//+------------------------------------------------------------------+
void TrailingStop()
 {
 int cnt,total;
 total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==Mn)  
        {
         if(OrderType()==OP_BUY) 
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>fPointX()*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep-1)*fPointX())
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask-TrailingStop*fPointX(),OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(fPointX()*TrailingStop))
                 {
                  if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*fPointX() || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid+TrailingStop*fPointX(),OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }  
   return(0); 
}

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