User login

  

Sample Trading Sistem for cross 2MA & CCI

Display/hide source code
#define MAGICMA  20050610
 
//---- input parameters
extern int       FMa=4; // быстрый РјСѓРІРёРЅРі
extern int       SMa=8; // медленный РјСѓРІРёРЅРі
extern int       PCCi=4; // период CCI
extern int       pATR=4; //Период ATR для стоп/лосса
extern double    Lots=10.0; // лот
extern double    DcF = 3.0;// Фактор оптимизации 
extern double    MaxR = 0.02; // Максимальный СЂРёСЃРє
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) 
         break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  
            buys++;
         if(OrderType()==OP_SELL) 
           sells++;
        }
     }
//---- return orders volume
      if(buys>0) 
         return(buys);
      else
        return(-sells);
  }
  
  //+------------------------------------------------------------------+
//| Расчет оптимальной величины лота                                 |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=OrdersHistoryTotal();     // history orders total
   int    losses=0;                        // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaxR/1000.0,1);
//---- calculate number of losses orders without a break
   if(DcF>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DcF, 1);
     }
//---- return lot size
   if(lot<1 ||lot>Lots)
     lot=Lots;
   return(lot);
}
  
void CheckForOpen()
{
   double mas;
   double maf;
   double mas_p;
   double maf_p;
   double Atr;
   double icc;
   double icc_p;
   int    res;
 
//---- начинаем торговлю только с первым тиком нового бара
//   if(Volume[0]>1) return;
//---- определяем Moving Average 
   mas=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,1); // динный РјСѓРІРёРЅРі 1 период назад
   maf=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,1);// короткий РјСѓРІРёРЅРі 1 период назад
   mas_p=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,2); // динный РјСѓРІРёРЅРі 2 периода назад
   maf_p=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,2);// короткий РјСѓРІРёРЅРі 2 периода назад
   Atr = iATR(NULL,0,pATR,0);
   icc = iCCI(NULL,0,PCCi,PRICE_CLOSE,1);// CCI 1 период назад
   icc_p = iCCI(NULL,0,PCCi,PRICE_CLOSE,2);// CCI 2 периода назад
 //---- Условие продажи
   if ( (maf<mas && maf_p>=mas_p)&&(icc<0 && icc_p >=0 )) 
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Ask+Atr,0,"",MAGICMA,0,CLR_NONE);
      return;
     }
//---- Условие покупки
   if ((maf>mas && maf_p<=mas_p)&& (icc > 0 && icc_p <=0 ))  
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Bid-Atr,0,"",MAGICMA,0,CLR_NONE);
      return;
     }
}  
 
void CheckForClose()
{
   double mas;
   double maf;
   double mas_p;
   double maf_p;
   bool CloseOrd;
//---- 
   //if(Volume[0]>1) return;
//----  
   mas=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,1); // динный РјСѓРІРёРЅРі 1 период назад
   maf=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,1);// короткий РјСѓРІРёРЅРі 1 период назад
   mas_p=iMA(NULL,0,SMa,0,MODE_SMA,PRICE_CLOSE,2); // динный РјСѓРІРёРЅРі 2 периода назад
   maf_p=iMA(NULL,0,FMa,0,MODE_SMA,PRICE_CLOSE,2);// короткий РјСѓРІРёРЅРі 2 периода назад
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //----  
      if(OrderType()==OP_BUY)
        {
         if(maf<mas && maf_p>=mas_p) CloseOrd=OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(maf>mas && maf_p<=mas_p) OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
         break;
        }
     }
//----
}  
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   if(Bars<10)
      return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
   return(0);
  }
//+------------------------------------------------------------------+
12345

Comments