1. Invoerparameters
#define ExtBotName "AK-47 Scalper EA" //Naam van de bot #define Version "1.00" //--- invoerparameters extern string EASettings = "---------------------------------------------"; //-------- <EA Instellingen> -------- input int InpMagicNumber = 124656; //Magisch Nummer extern string TradingSettings = "---------------------------------------------"; //-------- <Handelsinstellingen> -------- input double Inpuser_lot = 0.01; //Lots input double InpSL_Pips = 3.5 //Stoploss (in Pips) input double InpMax_spread = 0.5 //Maximaal toegestane spread (in Pips) (0 = zwevend) extern string MoneySettings = "---------------------------------------------"; //-------- <Geldinstellingen> -------- input bool isVolume_Percent = true; //Sta Volume Percentage toe input double InpRisk = 3 //Risico Percentage van Balans (%) input string TimeSettings = "---------------------------------------------"; //-------- <Handeltijdinstellingen> -------- input bool InpTimeFilter = true //Handeltijdfilter input int InpStartHour = 2 //Startuur input int InpStartMinute = 30 //Startminuut input int InpEndHour = 21 //Einduur input int InpEndMinute = 0 //Eindminuut
2. Initialisatie van lokale variabelen
//--- Variabelen int Pips2Points; // slippage 3 pips 3=punten 30=punten double Pips2Double; // Stoploss 15 pips 0.015 0.0150 int InpMax_slippage = 3; // Maximaal toegestane slippage in Pips. bool isOrder = false; // slechts 1 order openen int slippage; string strComment = "";
3. Hoofdcode
a/ Initialisatiefunctie voor de Expert
int OnInit() { //--- //3 of 5 cijfers detectie //Pip en punt 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/ Expert tick-functie
void OnTick() { //--- if(IsTradeAllowed() == false) { Comment("AK-47 EA\nHandel niet toegestaan."); return; } MqlDateTime structTime; TimeCurrent(structTime); structTime.sec = 0; //Stel starttijd in structTime.hour = InpStartHour; structTime.min = InpStartMinute; datetime timeStart = StructToTime(structTime); //Stel eindtijd in 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 += "\nGMT tijd = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS); strComment += "\nHandeltijd = [" + (string)InpStartHour + "u" + (string)InpStartMinute + " --> " + (string)InpEndHour + "u" + (string)InpEndMinute + "]"; strComment += "\nHuidige Spread = " + (string)acSpread + " Punten"; strComment += "\nHuidige stopniveau = " + (string)StopLevel + " Punten"; Comment(strComment); //Update Waarden UpdateOrders(); TrailingStop(); //Controleer Handeltijd if(InpTimeFilter) { if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) { if(!isOrder) OpenOrder(); } } else { if(!isOrder) OpenOrder(); } }
3.1 Bereken signaal om orders te verzenden
void OpenOrder(){ //int OrdType = OP_SELL;//-1; double TP = 0; double SL = 0; string comment = ExtBotName; //Bereken Lots 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()) //Controleer Spread { if(!OrderSend(_Symbol, OP_SELLSTOP, lot1, OpenPrice, slippage, SL, TP, comment, InpMagicNumber, 0, clrRed)) Print(__FUNCTION__,"--> OrderSend fout ",GetLastError()); } //} }
3.2 Bereken 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 EA heeft een functie "trailing Stop", SL wijzigt elke keer dat de prijs verandert (naar beneden)
void TrailingStop() { for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if((OrderMagicNumber() == InpMagicNumber) && (OrderSymbol() == Symbol())) //_Symbol)) { //Voor Verkooporder if(OrderType() == OP_SELL) { //--Bereken SL wanneer prijs verandert 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__,"--> OrderModify fout ",GetLastError()); { continue; } } } //Voor SellStop Order 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__,"--> Wijzig PendingOrder fout!", GetLastError()); continue; } } } } } }