Se sei un trader alla ricerca di un sistema di trading automatizzato efficace, l'AK-47 Scalper EA potrebbe fare al caso tuo. In questo post, esploreremo i parametri di input e come configurare questo Expert Advisor per massimizzare le tue operazioni.
1. Parametri di input
#define ExtBotName "AK-47 EA" //Nome del Bot #define Version "1.00" //Importa classi necessarie #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> #include <Trade\AccountInfo.mqh> #include <Trade\OrderInfo.mqh> //--- Introduzione di variabili predefinite per migliorare la leggibilità del codice #define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK) #define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID) //--- Parametri di input input string EASettings = "---------------------------------------------"; //-------- <Impostazioni EA> -------- input int InpMagicNumber = 124656; //Numero Magico input string MoneySettings = "---------------------------------------------"; //-------- <Impostazioni Finanziarie> -------- input bool isVolume_Percent = true; //Consenti Percentuale di Volume input double InpRisk = 3; //Percentuale di Rischio sul Saldo (%) input string TradingSettings = "---------------------------------------------"; //-------- <Impostazioni di Trading> -------- input double Inpuser_lot = 0.01; //Lotti input double InpSL_Pips = 3.5 //Stoploss (in Pips) input double InpTP_Pips = 7 //TP (in Pips) (0 = No TP) input int InpMax_slippage = 3 //Massimo slippage consentito in Pips. input double InpMax_spread = 5 //Massimo spread consentito (in Punti) (0 = flottante) input string TimeSettings = "---------------------------------------------"; //-------- <Impostazioni Orarie di Trading> -------- input bool InpTimeFilter = true; //Filtro Orario di 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 bool isOrder = false; int slippage; long acSpread; string strComment = ""; CPositionInfo m_position; // oggetto posizione di trading CTrade m_trade; // oggetto di trading CSymbolInfo m_symbol; // oggetto info simbolo CAccountInfo m_account; // wrapper info account COrderInfo m_order; // oggetto ordini in attesa
3. Codice Principale
a/ Funzione di inizializzazione dell'Expert
//+------------------------------------------------------------------+ //| Funzione di inizializzazione dell'Expert | //+------------------------------------------------------------------+ int OnInit() { //Rilevamento di 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; } if(!m_symbol.Name(Symbol())) // Imposta nome simbolo return(INIT_FAILED); RefreshRates(); //--- m_trade.SetExpertMagicNumber(InpMagicNumber); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(slippage); //--- return(INIT_SUCCEEDED); }
b/ Funzione di tick dell'Expert
//+------------------------------------------------------------------+ //| Funzione di tick dell'Expert | //+------------------------------------------------------------------+ void OnTick() { if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) == false) { Comment("LazyBot\nTrading non consentito."); return; } MqlDateTime structTime; TimeCurrent(structTime); structTime.sec = 0; //Imposta l'ora di inizio structTime.hour = InpStartHour; structTime.min = InpStartMinute; datetime timeStart = StructToTime(structTime); //Imposta l'ora di fine structTime.hour = InpEndHour; structTime.min = InpEndMinute; datetime timeEnd = StructToTime(structTime); acSpread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD); strComment = "\n" + ExtBotName + " - v." + (string)Version; strComment += "\nOra server = " + TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS) + " - " + DayOfWeekDescription(structTime.day_of_week); strComment += "\nOrario di trading = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " + (string)InpEndHour + "h" + (string)InpEndMinute + "]"; strComment += "\nSpread attuale = " + (string)acSpread + " Punti"; Comment(strComment); //Aggiorna valori UpdateOrders(); TrailingStop(); //Condizioni di trading secondo l'orario if(InpTimeFilter) { if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) { if(!isOrder) OpenOrder(); } } else { if(!isOrder) OpenOrder(); } } //---Fine della funzione
3.1 Calcola il segnale per inviare ordini
//+------------------------------------------------------------------+ //| CALCOLA IL SEGNALE E INVIA L'ORDINE | //+------------------------------------------------------------------+ void OpenOrder(){ ENUM_ORDER_TYPE OrdType = ORDER_TYPE_SELL;//-1; double TP = 0; double SL = 0; string comment = ExtBotName; //Calcola Lotti double lot1 = CalculateVolume(); if(OrdType == ORDER_TYPE_SELL) { double OpenPrice = Bid - NormalizeDouble(InpSL_Pips/2 * Pips2Double, _Digits); TP = OpenPrice - NormalizeDouble(InpTP_Pips * Pips2Double, _Digits); SL = Ask + NormalizeDouble(InpSL_Pips/2 * Pips2Double, _Digits); if(CheckSpreadAllow() //Controlla Spread && CheckVolumeValue(lot1) //Controlla volume && CheckOrderForFREEZE_LEVEL(ORDER_TYPE_SELL_STOP, OpenPrice) //Controlla distanza tra openPrice e Bid && CheckStopLoss(OpenPrice, SL, TP) //Controlla distanza da SL, TP a OpenPrice && CheckMoneyForTrade(m_symbol.Name(), lot1, ORDER_TYPE_SELL)) //Controlla bilancio quando il comando viene eseguito { if(!m_trade.SellStop(lot1, OpenPrice, m_symbol.Name(), SL, TP, ORDER_TIME_GTC, 0, comment)) Print(__FUNCTION__,"--> Errore OrderSend ", m_trade.ResultComment()); } } else if(OrdType == ORDER_TYPE_BUY) { double OpenPrice = Ask + NormalizeDouble(InpSL_Pips/2 * Pips2Double, _Digits); SL = Bid - NormalizeDouble(InpSL_Pips/2 * Pips2Double, _Digits); if(CheckSpreadAllow() //Controlla Spread && CheckVolumeValue(lot1) //Controlla volume && CheckOrderForFREEZE_LEVEL(ORDER_TYPE_BUY_STOP, OpenPrice) //Controlla distanza tra openPrice e Bid && CheckStopLoss(OpenPrice, SL, TP) //Controlla distanza da SL, TP a OpenPrice && CheckMoneyForTrade(m_symbol.Name(), lot1, ORDER_TYPE_BUY)) //Controlla bilancio quando il comando viene eseguito { if(!m_trade.BuyStop(lot1, OpenPrice, m_symbol.Name(), SL, TP, ORDER_TIME_GTC, 0, comment))// usa "ORDER_TIME_GTC" quando la data di scadenza = 0 Print(__FUNCTION__,"--> Errore OrderSend ", m_trade.ResultComment()); } } }
3.2 Calcola il Volume
//+------------------------------------------------------------------+ //| CALCOLA IL VOLUME | //+------------------------------------------------------------------+ // Definiamo la funzione per calcolare la dimensione della posizione e restituire il lotto da ordinare. double CalculateVolume() { double LotSize = 0; if(isVolume_Percent == false) { LotSize = Inpuser_lot; } else { LotSize = (InpRisk) * m_account.FreeMargin(); 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 > m_symbol.LotsMax()) LotSize = m_symbol.LotsMax(); if(LotSize < m_symbol.LotsMin()) LotSize = m_symbol.LotsMin(); } //--- return(LotSize); }3.3 Funzione di "Trailing Stop" dell'EA, SL cambierà ogni volta che il prezzo cambia (in giù)
//+------------------------------------------------------------------+ //| TRAILING STOP | //+------------------------------------------------------------------+ void TrailingStop() { double SL_in_Pip = 0; for(int i = PositionsTotal() - 1; i >= 0; i--) { if(m_position.SelectByIndex(i)) { // seleziona gli ordini per indice per accedere ulteriormente alle sue proprietà if((m_position.Magic() == InpMagicNumber) && (m_position.Symbol() == m_symbol.Name())) { // Per ordine di acquisto if(m_position.PositionType() == POSITION_TYPE_BUY) { //--Calcola SL quando il prezzo cambia SL_in_Pip = NormalizeDouble(Bid - m_position.StopLoss(), _Digits) / Pips2Double; if(SL_in_Pip > InpSL_Pips) { double newSL = NormalizeDouble(Bid - InpSL_Pips * Pips2Double, _Digits); if(!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) { Print(__FUNCTION__,"--> Errore OrderModify ", m_trade.ResultComment()); continue } } } //Per ordine di vendita else if(m_position.PositionType() == POSITION_TYPE_SELL) { //--Calcola SL quando il prezzo cambia SL_in_Pip = NormalizeDouble(m_position.StopLoss() - Bid, _Digits) / Pips2Double; if(SL_in_Pip > InpSL_Pips){ double newSL = NormalizeDouble(Bid + (InpSL_Pips) * Pips2Double, _Digits); if(!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) { Print(__FUNCTION__,"--> Errore OrderModify ", m_trade.ResultComment()); //continue; } } } }//--- Modifica ordini in attesa for(int i=OrdersTotal()-1; i>=0; i--) {// restituisce il numero di ordini correnti if(m_order.SelectByIndex(i)) { // seleziona l'ordine in attesa per indice per accesso successivo alle sue proprietà if(m_order.Symbol() == m_symbol.Name() && m_order.Magic()==InpMagicNumber) { if(m_order.OrderType() == ORDER_TYPE_BUY_STOP) { SL_in_Pip = NormalizeDouble(Bid - m_order.StopLoss(), _Digits) / Pips2Double; if(SL_in_Pip < InpSL_Pips/2) { double newOP = NormalizeDouble(Bid + (InpSL_Pips/2) * Pips2Double, _Digits); double newTP = NormalizeDouble(newOP + InpTP_Pips * Pips2Double, _Digits); double newSL = NormalizeDouble(Bid - (InpSL_Pips/2) * Pips2Double, _Digits); if(!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC,0)) { Print(__FUNCTION__,"--> Errore Modifica Ordine in Attesa!", m_trade.ResultComment()); continue } } } else if(m_order.OrderType() == ORDER_TYPE_SELL_STOP) { SL_in_Pip = NormalizeDouble(m_order.StopLoss() - Ask, _Digits) / Pips2Double; if(SL_in_Pip < InpSL_Pips/2){ double newOP = NormalizeDouble(Ask - (InpSL_Pips/2) * Pips2Double, _Digits); double newTP = NormalizeDouble(newOP - InpTP_Pips * Pips2Double, _Digits); double newSL = NormalizeDouble(Ask + (InpSL_Pips/2) * Pips2Double, _Digits); if(!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC,0)) { Print(__FUNCTION__,"--> Errore Modifica Ordine in Attesa!", m_trade.ResultComment()); //continue } } } }//--- Fine della funzione } }
Post correlati
- MQL5 Wizard: Creare Trading Signals con Morning/Evening Stars e MFI per MetaTrader 5
- MQL5 Wizard: Strategia di Trading con Dark Cloud Cover/Piercing Line e RSI
- MQL5 Wizard: Creare Trade Signals con Hammer/Hanging Man e RSI
- MQL5 Wizard: Crea Expert Advisor per segnali di trading basati su Dark Cloud Cover e Piercing Line con CCI
- MQL5 Wizard: Crea Trading Signals con 3 Corvi Neri/3 Soldati Bianchi e RSI