Ben je op zoek naar een slimme manier om je trading te automatiseren? Dan is de Lazy Bot MT5, ook wel bekend als de Daily Breakout EA, iets voor jou! Deze Expert Advisor is ontworpen om de hoogste en laagste waarden van de vorige dag te analyseren en automatisch twee pending orders te plaatsen: een BUY_STOP en een SELL_STOP. Laten we eens dieper ingaan op de instellingen en functies van deze handige tool.
1. Invoeren van parameters
- Input parameters
//Importeer externe classes #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> #include <Trade\AccountInfo.mqh> #include <Trade\OrderInfo.mqh> //--- Definieer vooraf gedefinieerde variabelen voor leesbaarheid van de code #define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK) #define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID) //--- Invoeren van parameters input string EASettings = "---------------------------------------------"; //-------- <EA Instellingen> -------- input int InpMagicNumber = 123456; //Magic Number input string InpBotName = "LazyBot_V1"; //Bot Naam input string TradingSettings = "---------------------------------------------"; //-------- <Trading Instellingen> -------- input double Inpuser_lot = 0.01; //Lots input double Inpuser_SL = 5.0; //Stoploss (in Pips) input double InpAddPrice_pip = 0; //Afstand van [H], [L] naar OP_Price (in Pips) input int Inpuser_SLippage = 3; // Maximale slippage toegestaan (in Pips). input double InpMax_spread = 0; //Maximale toegestane spread (in Pips) (0 = variabel) input string TimeSettings = "---------------------------------------------"; //-------- <Handel Tijd Instellingen> -------- input bool isTradingTime = true; //Sta handelstijd toe input int InpStartHour = 7; //Begin Uur input int InpEndHour = 22; //Eind Uur input string MoneyManagementSettings = "---------------------------------------------"; //-------- <Geld Instellingen> -------- input bool isVolume_Percent = false; //Sta volume percentage toe input double InpRisk = 1; //Risico percentage van balans (%)
2. Initialisatie van lokale variabelen
//Lokale parameters datetime last; int totalBars; int Pips2Points; // slippage 3 pips 3=punten 30=punten double Pips2Double; // Stoploss 15 pips 0.015 0.0150 double slippage; double acSpread; string strComment = ""; CPositionInfo m_position; // trade positie object CTrade m_trade; // trading object CSymbolInfo m_symbol; // symbool info object CAccountInfo m_account; // account info wrapper COrderInfo m_order; // openstaande orders object
3. Hoofdcode
De strategie van deze bot is vrij simpel. Elke dag verwijdert de bot alle oude orders, zoekt de hoogste en laagste waarde van de vorige dagelijkse kaars en plaatst vervolgens twee pending orders: een BUY_STOP en een SELL_STOP. Er is geen TakeProfit ingesteld.
4. Functie voor Expert initialisatie
int OnInit() { //--- //Detectie van 3 of 5 cijfers //Pip en punt if(_Digits % 2 == 1) { Pips2Double = _Point*10; Pips2Points = 10; slippage = 10* Inpuser_SLippage; } else { Pips2Double = _Point; Pips2Points = 1; slippage = Inpuser_SLippage; } if(!m_symbol.Name(Symbol())) // stelt de naam van het symbool in return(INIT_FAILED); RefreshRates(); //--- m_trade.SetExpertMagicNumber(InpMagicNumber); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(slippage); //--- return(INIT_SUCCEEDED); }
5. Functie voor Expert tick
void OnTick() { if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) == false) { Comment("LazyBot\nHandel niet toegestaan."); return; } //Verkrijg handelstijd, // Opening sectie // Londen 14h - 23h GMT Vietnam // New York 19h - 04h GMT Vietnam MqlDateTime timeLocal; MqlDateTime timeServer; TimeLocal(timeLocal); TimeCurrent(timeServer); // werkt niet op feestdagen. if(timeServer.day_of_week == 0 || timeServer.day_of_week == 6) return; int hourLocal = timeLocal.hour;//TimeHour(TimeLocal()); int hourCurrent = timeServer.hour;//TimeHour(TimeCurrent()); acSpread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD); strComment = "\nLokale Uur = " + hourLocal; strComment += "\nHuidige Uur = " + hourCurrent; strComment += "\nSpread = " + (string)acSpread; strComment += "\nTotaal Bars = " + (string)totalBars; Comment(strComment); //Controleer Trailing TrailingSL(); //--- if(last != iTime(m_symbol.Name(), PERIOD_D1, 0))// && hourCurrent > InpStartHour) { //Controleer Handelstijd if(isTradingTime) { if(hourCurrent >= InpStartHour) // && hourCurrent < InpEndHour){ { DeleteOldOrds(); //Verzend Order BUY_STOP en SELL_STOP OpenOrder(); last = iTime(m_symbol.Name(), PERIOD_D1, 0); } } else { DeleteOldOrds(); //Verzend Order BUY_STOP en SELL_STOP OpenOrder(); last = iTime(m_symbol.Name(), PERIOD_D1, 0); } } }
6. Bereken signaal en verzend orders
//+------------------------------------------------------------------+ //| BEREKEN SIGNAALE EN VERSTUUR ORDER | //+------------------------------------------------------------------+ void OpenOrder() { double TP_Buy = 0, TP_Sell = 0; double SL_Buy = 0, SL_Sell = 0; //Controleer Maximale Spread if(InpMax_spread != 0){ if(acSpread > InpMax_spread){ Print(__FUNCTION__," > huidige Spread is groter dan gebruiker Spread!..."); return; } } double Bar1High = m_symbol.NormalizePrice(iHigh(m_symbol.Name(), PERIOD_D1, 1)); double Bar1Low = m_symbol.NormalizePrice(iLow(m_symbol.Name(), PERIOD_D1, 1)); //Bereken Lots double lot1 = CalculateVolume(); double OpenPrice = m_symbol.NormalizePrice(Bar1High + InpAddPrice_pip * Pips2Double);// + NormalizeDouble((acSpread/Pips2Points) * Pips2Double, Digits); //Voor BUY_STOP -------------------------------- TP_Buy = 0;//Bar1High + NormalizeDouble(min_sl* Pips2Double, Digits); SL_Buy = m_symbol.NormalizePrice(OpenPrice - Inpuser_SL * Pips2Double); totalBars = iBars(m_symbol.Name(), PERIOD_D1); string comment = InpBotName + ";" + m_symbol.Name() + ";" + totalBars; if(CheckVolumeValue(lot1) && CheckOrderForFREEZE_LEVEL(ORDER_TYPE_BUY_STOP, OpenPrice) && CheckMoneyForTrade(m_symbol.Name(),lot1, ORDER_TYPE_BUY) && CheckStopLoss(OpenPrice, SL_Buy)) { if(!m_trade.BuyStop(lot1, OpenPrice, m_symbol.Name(), SL_Buy, TP_Buy, ORDER_TIME_GTC, 0, comment))// gebruik "ORDER_TIME_GTC" wanneer vervaldatum = 0 Print(__FUNCTION__, "--> Koop Fout"); } //Voor SELL_STOP -------------------------------- OpenPrice = m_symbol.NormalizePrice(Bar1Low - InpAddPrice_pip * Pips2Double);// - NormalizeDouble((acSpread/Pips2Points) * Pips2Double, Digits); TP_Sell = 0;//Bar1Low - NormalizeDouble(min_sl* Pips2Double, Digits); SL_Sell = m_symbol.NormalizePrice(OpenPrice + Inpuser_SL * Pips2Double); if(CheckVolumeValue(lot1) && CheckOrderForFREEZE_LEVEL(ORDER_TYPE_SELL_STOP, OpenPrice) && CheckMoneyForTrade(m_symbol.Name(),lot1, ORDER_TYPE_SELL) && CheckStopLoss(OpenPrice, SL_Sell)) { if(!m_trade.SellStop(lot1, OpenPrice, m_symbol.Name(), SL_Sell, TP_Sell, ORDER_TIME_GTC, 0, comment)) Print(__FUNCTION__, "--> Verkoop Fout"); } }
7. Nieuw dag, verwijder alle oude orders
//+------------------------------------------------------------------+ //| Verwijder Oude Orders | //+------------------------------------------------------------------+ void DeleteOldOrds() { string sep=";"; // Een scheidingsteken als een karakter ushort u_sep; // De code van het scheidingsteken karakter string result[]; // Een array om strings op te slaan for(int i = OrdersTotal() - 1; i >= 0; i--) // retourneert het aantal huidige orders { if(m_order.SelectByIndex(i)) // selecteert de openstaande order op index voor verdere toegang tot zijn eigenschappen { //--- Verkrijg de code van het scheidingsteken u_sep = StringGetCharacter(sep, 0); string Ordcomment = m_order.Comment(); //Splits OrderComment (EAName;Symbool;totaalBar) om Ordersymbool te krijgen int k = StringSplit(Ordcomment, u_sep, result); if(k > 2) { string sym = m_symbol.Name(); if((m_order.Magic() == InpMagicNumber) && (sym == result[1])) { m_trade.OrderDelete(m_order.Ticket()); } } } } }
Reactie 0