Home Trading Sistematico Post

AK-47 Scalper EA: La Guida Definitiva per MetaTrader 4

Allegato
42236.zip (5.76 KB, Scarica 0 volte)

1. Parametri di Input

#define ExtBotName "AK-47 Scalper EA" //Nome del Bot
#define  Version "1.00"

//--- parametri di input
extern string  EASettings        = "---------------------------------------------"; //-------- <Impostazioni EA> --------
input int      InpMagicNumber    = 124656;   //Numero Magico

extern string  TradingSettings   = "---------------------------------------------"; //-------- <Impostazioni di Trading> --------
input double   Inpuser_lot       = 0.01;     //Lotti
input double   InpSL_Pips        = 3.5;      //Stoploss (in Pips)
input double   InpMax_spread     = 0.5;      //Spread massimo consentito (in Pips) (0 = fluttuante)

extern string  MoneySettings     = "---------------------------------------------"; //-------- <Impostazioni di Denaro> --------
input bool     isVolume_Percent  = true;     //Consenti Percentuale di Volume
input double   InpRisk           = 3;        //Percentuale di Rischio sul Saldo (%)

input string   TimeSettings      = "---------------------------------------------"; //-------- <Impostazioni di Tempo Trading> --------
input bool     InpTimeFilter     = true      //Filtro di Tempo Trading
input int      InpStartHour      = 2;         //Ora di Inizio
input int      InpStartMinute    = 30;        //Minuto di Inizio
input int      InpEndHour        = 21;        //Ora di Fine
input int      InpEndMinute      = 0         //Minuto di Fine

2. Inizializzazione delle variabili locali

//--- Variabili
int      Pips2Points;               // slippage  3 pips    3=points    30=points
double   Pips2Double;               // Stoploss 15 pips    0.015      0.0150
int      InpMax_slippage   = 3;     // Massimo slippage consentito.
bool     isOrder           = false; // apri solo 1 ordine
int      slippage;
string   strComment        = "";

3. Codice Principale

a/ Funzione di inizializzazione dell'Expert

int OnInit()
  {
//---   
   //Rilevamento a 3 o 5 cifre
   //Pip e punto
   if (Digits % 2 == 1)
   {
      Pips2Double  = _Point*10; 
      Pips2Points  = 10;
      slippage = 10* InpMax_slippage;
   } 
   else
   {    
      Pips2Double  = _Point;
      Pips2Points  =  1;
      slippage = InpMax_slippage;
   }
   
//---
   return(INIT_SUCCEEDED);
  }

b/ Funzione di tick dell'Expert

void OnTick()
  {
//---
     if(IsTradeAllowed() == false)
     {
      Comment("AK-47 EA\nTrading non consentito.");
      return;
     }
     
       MqlDateTime structTime;
       TimeCurrent(structTime);
       structTime.sec = 0;
       
       //Imposta ora di inizio
       structTime.hour = InpStartHour;
       structTime.min = InpStartMinute;       
       datetime timeStart = StructToTime(structTime);
       
       //Imposta ora di fine
       structTime.hour = InpEndHour;
       structTime.min = InpEndMinute;
       datetime timeEnd = StructToTime(structTime);
       
       double acSpread = MarketInfo(Symbol(), MODE_SPREAD);
       StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
      
      strComment = "\n" + ExtBotName + " - v." + (string)Version;
      strComment += "\nOra GMT = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS);
      strComment += "\nTempo di Trading = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " +  (string)InpEndHour + "h" + (string)InpEndMinute + "]";
      
      strComment += "\nSpread Corrente = " + (string)acSpread + " Punti";
      strComment += "\nLivello di stop corrente = " + (string)StopLevel + " Punti";
      
      Comment(strComment);
   
      //Aggiorna Valori
      UpdateOrders();
      
      TrailingStop();
      
      //Controlla il tempo di trading
      if(InpTimeFilter)
      {
         if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd)
         {
            if(!isOrder) OpenOrder();
         }
      }
      else
      {
         if(!isOrder) OpenOrder();
      }
  }

3.1 Calcola il segnale per inviare ordini

void OpenOrder(){
   
   //int OrdType = OP_SELL;//-1;

   double TP = 0;
   double SL = 0;
   string comment = ExtBotName;

   //Calcola Lotti
   double lot1 = CalculateVolume();
   
   //if(OrdType == OP_SELL){
      double OpenPrice = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
      SL = NormalizeDouble(Ask + StopLevel * _Point + InpSL_Pips/2 * Pips2Double, Digits);
         
      if(CheckSpreadAllow())                                    //Controlla Spread
      {
         if(!OrderSend(_Symbol, OP_SELLSTOP, lot1, OpenPrice, slippage, SL, TP, comment, InpMagicNumber, 0, clrRed))
         Print(__FUNCTION__,"--> Errore nell'invio dell'ordine ",GetLastError());
      }
   //}
}

3.2 Calcola Volume

double CalculateVolume()
  {
   double LotSize = 0;

   if(isVolume_Percent == false)
     {
      LotSize = Inpuser_lot;
     }
   else
     {

      LotSize = (InpRisk) * AccountFreeMargin();
      LotSize = LotSize /100000;
      double n = MathFloor(LotSize/Inpuser_lot);
      //Comment((string)n);
      LotSize = n * Inpuser_lot;

      if(LotSize < Inpuser_lot)
         LotSize = Inpuser_lot;

      if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT))
         LotSize = MarketInfo(Symbol(),MODE_MAXLOT);

      if(LotSize < MarketInfo(Symbol(),MODE_MINLOT))
         LotSize = MarketInfo(Symbol(),MODE_MINLOT);
     }

   return(LotSize);
  }

3.3 L'EA ha la funzione "trailing Stop", SL cambierà ogni volta che il prezzo cambia (in basso)

void TrailingStop()
  {
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if((OrderMagicNumber() == InpMagicNumber) && (OrderSymbol() == Symbol()))   //_Symbol))
           {

            //Per Ordine di Vendita
            if(OrderType() == OP_SELL)
              {
                  //--Calcola SL quando il prezzo cambia
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  if(SL_in_Pip > InpSL_Pips){
                        double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + InpSL_Pips * Pips2Double, Digits);
                        if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed))
                        {
                           Print(__FUNCTION__,"--> Errore nell'OrderModify ",GetLastError());
                        }
                          }
            }
             
            //Per Ordine di Vendita Stop
            else if(OrderType() == OP_SELLSTOP)
              {
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  
                  if(SL_in_Pip < InpSL_Pips/2){
                     double newOP = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
                     double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + (InpSL_Pips/2) * Pips2Double, Digits);
                     
                     if(!OrderModify(OrderTicket(), newOP, newSL, OrderTakeProfit(), 0, clrRed))
                         {
                           Print(__FUNCTION__,"--> Errore nel Modificare l'Ordine Pendiente!", GetLastError());
                        continue;
                         }
                          }
              }
          }
      }
     }
  }


Post correlati

Commento (0)