Author: Copyright 2017, Maksim Neimerik
Price Data Components
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
Commodity channel index
0 Views
0 Downloads
0 Favorites
CCI Expert
//+------------------------------------------------------------------+
//|                                                   CCI Expert.mq4 |
//|                                  Copyright 2017, Maksim Neimerik |
//|                                              Deilyplanet@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Maksim Neimerik"
#property link      "Deilyplanet@ukr.net"
#property version   "1.00"
#property strict

extern double lot=0.1;//Lot
extern int magic=2508;//Magic
extern int Slippage=2;//Slippage
extern double mnog=1.5;//Multiplier
extern bool UseReverse=true;//Use Reverse Exit
extern bool UseTrailing=true;//Use Trailing
extern int TrailingStop=15;//Trailing Stop Start
extern int TrailingStep=0;//Trailing Stop Distance
extern bool UseDrawdown=true;//Use Drawdown
extern int Drawdown=30;//Drawdown (%)
input int InpCCIPeriod=14; // CCI Period

int HighIndex=0,LowIndex=0;
double SL=0,StopLevel=0,TStop=0,TStep=0,Depo=0,Bal=0,CCI1=0,CCI2=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits()==3||Digits()==5)
      {
         TrailingStop*=10;
         TrailingStep*=10;
      }  
   StopLevel=NormalizeDouble(MarketInfo(Symbol(),MODE_STOPLEVEL)*Point,Digits);
   TStop=NormalizeDouble(TrailingStop*Point,Digits());
   TStep=NormalizeDouble(TrailingStep*Point,Digits());
   if(mnog<1)
   {
      mnog=1;
      Comment("The multiplier is less than 1. Installed 1 !!!");
   }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnTick()
  {
//--Check Lot------------------------------------------------------
   if(lot*MarketInfo(Symbol(),MODE_MARGINREQUIRED)>AccountEquity())
     {Print("Trade stop is not enough free margin to begin");Comment("Trade stop is not enough free margin to begin"); return;}

   if(lot<MarketInfo(Symbol(),MODE_MINLOT))
     {Print("Trade stop invalid lot size");Comment("Trade stop invalid lot size"); return;}

   if(UseTrailing)
   {
      Trailing();
   }
//------------------------------------------------------  
   CCI1=NormalizeDouble(iCCI(NULL,PERIOD_CURRENT,InpCCIPeriod,PRICE_CLOSE,1),Digits());
   CCI2=NormalizeDouble(iCCI(NULL,PERIOD_CURRENT,InpCCIPeriod,PRICE_CLOSE,2),Digits());
   Depo=(AccountBalance()/100)*Drawdown; 
   Bal=AccountBalance();
   if(UseDrawdown==true)
   {
      if(Bal-AccountEquity()>Depo)
      {
         CloseOrders(OP_BUY);
         CloseOrders(OP_SELL);
         ObjectsDeleteAll(0,OBJ_ARROW);
         Comment("Equity is less than the permissible level !!!");
      }
   } 
   if(UseReverse==true)
   {
      if(CCI1>-100&&CCI2<-100)
      {
         CloseOrders(OP_SELL);
      }
      if(CCI1<100&&CCI2>100)
      {
         CloseOrders(OP_BUY);
      }
   }
   if(CountOrders()==0&&CCI1>-100&&CCI2<-100)
   {
      LowIndex=iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,10,0);
      OrderOpen(OP_BUY,lot);
   }
   if(CountOrders()==0&&CCI1<100&&CCI2>100)
   {
      HighIndex=iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,10,0);
      OrderOpen(OP_SELL,lot);
   }
  }
//----------------------------------------------
int CountOrders()
{
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderSymbol()==Symbol() &&OrderMagicNumber()==magic)
         count++;
      }
   }
   return(count);
}
//+------------------------------------------------------------------+
bool OrderOpen(int cmd, double volume)
{
   double price=0;
   color clr;
   if(cmd==OP_BUY)
   {
      price=Ask;
      SL=Low[LowIndex];
      clr=Blue;  
      if(Ask-SL<StopLevel)
      {
         SL=0;
      }
   }
   else 
   {
      price=Bid;
      SL=High[HighIndex];
      clr=Red;  
      if(SL-Bid<StopLevel)
      {
         SL=0;
      }
   }
   if(!OrderSend(Symbol(),cmd,volume,price,Slippage,SL,0,"",magic,0,clr))
   Print("Order Send ERROR ",GetLastError());
   return (true);   
}
//+-------------------------------------------------------------------------------------
bool CloseOrders(int cmd)
{
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
         {
            if(OrderType()==OP_BUY&&cmd==OP_BUY)
            {
               if(OrderClose(OrderTicket(),OrderLots(),Bid,0))
               return(true);
            }
            if(OrderType()==OP_SELL&&cmd==OP_SELL)
            {
               if(OrderClose(OrderTicket(),OrderLots(),Ask,0))
               return(true);
            }            
         }
      }
   }
   return(false);
}
//+------------------------------------------------------------------+
void Trailing()
  {
   datetime t=0;
   double SLoss=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid-OrderOpenPrice()>TStop)
                 {
                  if(OrderStopLoss()<Bid-(TStop+TStep))
                    {
                     SLoss=NormalizeDouble(Bid-TStop,Digits());
                     if(OrderStopLoss()!=SLoss)
                       {
                        if(OrderOpenTime()>t)
                        {
                           t=OrderOpenTime();
                           if(!OrderModify(OrderTicket(),OrderOpenPrice(),SLoss,0,0,Blue))
                             {
                              Print("BUY Trailing Stop False! Error = ",GetLastError());
                             }
                        }     
                       }
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if(OrderOpenPrice()-Ask>TStop)
                 {
                  if(OrderStopLoss()>Ask+(TStop+TStep) || OrderStopLoss()==0)
                    {
                     SLoss=NormalizeDouble(Ask+TStop,Digits());
                     if(OrderStopLoss()!=SLoss)
                       {
                        if(OrderOpenTime()>t)
                           {
                              t=OrderOpenTime();
                              if(!OrderModify(OrderTicket(),OrderOpenPrice(),SLoss,0,0,Red))
                                {
                                 Print("SELL Trailing Stop False! Error = ",GetLastError());
                                }
                           }     
                       }
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume)
  {
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(volume<min_volume)
     {
      Print("Volume is less than the minimum");
      return(false);
     }

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(volume>max_volume)
     {
      Print("Volume is greater than the maximum");
      return(false);
     }

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

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      Print("Wrong lot size");
      return(false);
     }
   return(true);
  }
//+------------------------------------------Maksim Neimerik---------+

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