Archivio blog

giovedì 25 luglio 2013

Expert Advisor BreakOutClassic

//+------------------------------------------------------------------+
//|                                              BreakOutClassic.mq4 |
//|                                              Riccardo Placitelli |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Riccardo Placitelli"
#property link      "http://forexsmartsharing.blogspot.it/"

#include <stdlib.mqh>
#include <stderror.mqh>

extern double     Lots = 0.1; // Initial Lot Size (Minimum Lot Size)
extern double     MaxR = 0.02; // Maximum % Equity Risk 
extern double     NAtr = 1; // N° of ATR Dinamic StopLoss
extern int        MAGICMA = 1985; // Magic Number
extern int        pAtr = 14; // ATR Periods
extern int        DPeriod = 20; // Main Price Channel 
extern int        DPeriod_2 = 10; // Minor Price Channel (StopLoss) 
extern int        Dpp = 100000; // Dollar per Point
extern bool       isFloatLots = true; // Dinamic Lot Size

//---Global Variable
double Atr;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//-----------------------------+
// Calculate open positions    |
//-----------------------------+

int CalculateCurrentOrders()
   {
    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 order Volume
      if(buys>0)return(buys);
      else      return(sells);
   }
//-----------------------------+
// Calculate optimal lot size  |
//-----------------------------+
double LotsOptimized()
   {
    double lot=Lots;
      if(isFloatLots==true)
         {
          //---get Atr value
          Atr=iATR(NULL,0,pAtr,0);
          //---get Optimal Lot
          lot=NormalizeDouble((AccountFreeMargin()*MaxR)/(Atr*Dpp),2);
         }
//---return lot size
          if(lot<0.1)lot=0.1;
          return(lot);
   }
//-----------------------------------+
// Check for open order conditions   |
//-----------------------------------+  
  void CheckForOpen()
  {
      double MainDonchianUp,MainDonchianDown;
      int res;
      
//---get Channel Resistence and Support
   double Resist=        High[iHighest(NULL,0,MODE_HIGH,DPeriod,1)];
   double Support=       Low[iLowest(NULL,0,MODE_LOW,DPeriod,1)];
         
//---go trading only for first tick of new Bars
      if(Volume[0]>1)
      return;

//---get Atr
      Atr= iATR(NULL,0,pAtr,0);

//---get Donchian_1
      MainDonchianUp=iCustom(NULL,0,"Donchian",DPeriod,0,0);
      MainDonchianDown=iCustom(NULL,0,"Donchian",DPeriod,1,0);
     
//---sell conditions      
               if(Low[1]<=Support)
                 {
                  res = OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+(NAtr*Atr),0,"",MAGICMA,0,Red);
                  if(res>=1)
                     {
                     if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
                     Print("Sell order opened: " + OrderOpenPrice());
       
                     else
            
                     Print("Error opening Sell Order: " + GetLastError());
                     }
                     return;
                 } 
//----buy conditions
               if(High[1]>=Resist)
                 {
                  res = OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-(NAtr*Atr),0,"",MAGICMA,0,Green);
                  if(res>=1)
                     {
                     if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
                     Print("Buy order opened: " + OrderOpenPrice());
       
                     else
      
                     Print("Error opening Buy Order: " + GetLastError());
                     }
                     return; 
                  }
                   
   }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
   void CheckForClose()
      {
      double SecondDonchianUp,SecondDonchianDown;
//----go trading only for first ticks of new Bars
      if(Volume[0]>1)
      return;
      
//---get Donchian_2
      SecondDonchianUp=iCustom(NULL,0,"Donchian",DPeriod_2,0,0);
      SecondDonchianDown=iCustom(NULL,0,"Donchian",DPeriod_2,1,0);
      
//---get Channel Resistence and Support
      double Resist2=        High[iHighest(NULL,0,MODE_HIGH,DPeriod_2,1)];
      double Support2=        Low[iLowest(NULL,0,MODE_LOW,DPeriod_2,1)]; 
      
//---
      for(int i=0; i<OrdersTotal();i++)
         {
         if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES)==false)   break;
         if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol())   continue;
       
            //---check order type
            if(OrderType()==OP_BUY && OrderMagicNumber()==MAGICMA)
               { 
               if(Low[1]<=Support2)
               OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
               break;
               }
            if(OrderType()==OP_SELL && OrderMagicNumber()==MAGICMA)
               {
               if(High[1]>=Resist2)
               OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
               break;
               }
      }
//---
  }
  

//+------------------+
//| Start function   |
//+------------------+
   void start()
      {
      
      //---check for history and trading
      if(Bars<100 || IsTradeAllowed()==false)
      return;

//---calculate open orders by current Symbol
      if(CalculateCurrentOrders()==0)
      CheckForOpen();
      else
      CheckForClose();
      
           
      //----
      int Error=GetLastError();
        if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
        if(Error==133){Alert("Trading prohibited.");}
        if(Error==2){Alert("Common error.");}
        if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}
      //----  
        
                return(0);
      }
//+------------------------------------------------------------------+

Nessun commento:

Posta un commento

condividi la tua opinione...