EA startet nicht immer
Hallo,
der EA startet oft nicht, es gibt jedoch auch keine Fehlermeldung. Smile ist an, Live aktiviert, und Autotrading.
Jedoch geht keine Order vom Tisch, wo eine sein sollte. Blöderweise geht es manchmal wiederum.
Da ich zwei MT4 laufen habe und bei beiden die gleichen Einstellungen, sollte es doch gleich laufen (Demo,Live).
Ich habe die aktuelle MT4 Version.
Woher kann so ein Fehler stammen?
PHP-Code:
//+------------------------------------------------------------------+ //| SR2 | //+------------------------------------------------------------------+ #property copyright "" #property link ""
#include <stdlib.mqh>
extern string Einstellungen_Order = "..................................................................."; extern int Magic = 9001; extern double BaseLot = 0.02; extern double MaxLotSize = 0.25; extern double MinLotSize = 0.01; extern bool UseMoneyManagement = false; extern bool IsMicroAcc = true; extern double EquityPRC = 2; extern int MaxTrades = 1;
extern string Einstellungen_Exit = "..................................................................."; extern double TakeProfit = 0.0;//Pips extern int StopLoss = 0;//Pips
extern bool UseExitSignal = true;
extern bool UseTimeRestriction = false; extern int DayEnd = 5; extern int HourEnd = 12;
extern double BreakEvenTrigger = 0.0;//Pips, Wird Kaufkurs+DIESEM Wert erreicht, setzt er den SL auf 1 Pip über Kaufkurs.
extern int TrailingStopTrigger = 0;//Pips, Kaufkurs+dieser Pipzahl im Gewinn sein, um das Trailing zu aktivieren extern int TrailingStopPips = 0;//Pips, ab der Stelle der Trailingaktivierung wird der TrailingSL je nach OrderType unter oder über den Trigger gelegt. Bneötigt nicht noch zusätzlich den StopLoss //extern int TrailingStep = 0;//Pips, die Schrittgröße des Nachziehens
extern string Einstellungen_Meldungen = "..................................................................."; extern bool EmailOn = false; extern bool AlertOn = false; extern bool ScreenshotOn = false;
extern string Einstellungen_EA = "...................................................................";
//intVars string Version; int bm; int barCheck = 0; //Variablen außerhalb einer Funktion deklariert, bleiben gesetzt,leben länger bool SpreadtoHigh, Orderstop, OrderTime; int einmalKauf, einmalVerkauf; double irot, iblau, irotLast, iblauLast, irotLastNew, iblauLastNew, iblauAbweichung, irotAbweichung, iblauSL, irotSL;
//+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- Version = "SR 2"; bm = 1; if(MarketInfo(Symbol(),MODE_DIGITS) == 5 || MarketInfo(Symbol(),MODE_DIGITS) == 3) bm = 10; if(TakeProfit>0) TakeProfit = TakeProfit * bm;//auskommentiert, weil sonst TP und SL mal Faktor 10 if(TrailingStopTrigger>0) TrailingStopTrigger = TrailingStopTrigger * bm; if(TrailingStopPips>0) TrailingStopPips = TrailingStopPips * bm; if(StopLoss>0) StopLoss = StopLoss * bm; if(BreakEvenTrigger>0) BreakEvenTrigger = BreakEvenTrigger * bm; //----------------------- Spreadcalc Begin --------------------- //--- set variables type double Spread; //--- detect spread Spread=MarketInfo(Symbol(),MODE_SPREAD); //--- if(Digits==3 || Digits==5) { Spread=Spread/10; Comment("Spread: ",Spread," Pips"); //Comment("Spread: ",Spread," Pips \nPip Zeilenumbruch"); } if(Digits==2) { Spread=Spread; double Punkte = Spread/100; Comment("Spread: ",Spread," Pips oder Spot: ",Punkte," Punkt/e"); }
if( TakeProfit && Spread >= TakeProfit ) { SpreadtoHigh = True; Print("Achtung! Spread größer als TakeProfit"); Comment("Spread: ",Spread," Pips\n\n\nAchtung! Spread größer als TakeProfit"); }else{ SpreadtoHigh = false; } //----------------------- Spreadcalc Ende --------------------- return(0); }
//+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert Signaldefinitionen | //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- //Indikator abfragen iblau = iCustom(Symbol(),0,"Support_Resistance",1,0); //1 ist blau Unterstützung mit Wert if ( iblauLastNew == 0 ) { iblauLastNew = iblau; } if ( iblauLastNew != iblau ) { iblauLast = iblauLastNew; iblauLastNew = 0; }// iblau = aktueller Wert und iblauLast = vorhergehender Wert iblauAbweichung = (iblauLast / iblau * 100) - 100; if ( iblauAbweichung < 0 ) { iblauAbweichung = iblauAbweichung * -1; }//prozentuale Abweichung zwischen Last und Aktuell, negatives Vorzeichen entfernen irot = iCustom(Symbol(),0,"Support_Resistance",0,0); //0 ist rot Widerstand mit Wert if ( irotLastNew == 0 ) { irotLastNew = irot; } if ( irotLastNew != irot ) { irotLast = irotLastNew; irotLastNew = 0; }// irot = aktueller Wert und irotLast = vorhergehender Wert irotAbweichung = (irotLast / irot * 100) - 100; if ( irotAbweichung < 0 ) { irotAbweichung = irotAbweichung * -1; }
//---- bool timeCheck = true; if(UseTimeRestriction) { int stunde = TimeHour(TimeCurrent()); if( stunde == 22 || stunde == 23 || stunde == 24 || stunde == 0 || stunde == 1 || stunde == 2 || stunde == 3 || stunde == 4 || stunde == 5 ) { OrderTime = True; }else{ OrderTime = false; } timeCheck = TimeDayOfWeek(TimeCurrent()) < DayEnd; if(timeCheck) { if(TimeDayOfWeek(TimeCurrent()) == DayEnd) timeCheck = TimeHour(TimeCurrent())<=HourEnd; } } double tp = 0; double sl = 0; int orders = orderCount(-1); if(orders != 0) { if(timeCheck) { for(int pos=0;pos<OrdersTotal();pos++) { if(OrderSelect(pos,SELECT_BY_POS)==false) continue; if(OrderMagicNumber()!=getMagic())continue; //EXIT if(UseExitSignal && getExitSignal(OrderType()) && profitCount()>0) { closeAllPoisitions(OrderType()); break; } //REVERSAL if(OrderType() == OP_SELL && getSignal(OP_BUY)) { closeAllPoisitions(OP_SELL); break; } else if(OrderType() == OP_BUY && getSignal(OP_SELL)) { closeAllPoisitions(OP_BUY); break; } //BreakEven if(BreakEvenTrigger>0) { if(OrderType() == OP_BUY) { if(OrderStopLoss() < NormalizeDouble(OrderOpenPrice()+(1*bm*Point),Digits) && Ask > ((OrderOpenPrice()+(1*bm*Point))+BreakEvenTrigger*Point)) { if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(OrderOpenPrice()+(1*bm*Point),Digits)) moveSL(OrderTicket(),OrderOpenPrice()+(1*bm*Point)); } } else if(OrderType() == OP_SELL) { if(OrderStopLoss() > NormalizeDouble(OrderOpenPrice()-(1*bm*Point),Digits) && Bid < ((OrderOpenPrice()-(1*bm*Point))-BreakEvenTrigger*Point)) { if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(OrderOpenPrice()-(1*bm*Point),Digits)) moveSL(OrderTicket(),OrderOpenPrice()-(1*bm*Point)); } } } //TRAILING if(TrailingStopTrigger > 0) { if(OrderType() == OP_BUY) { if(OrderStopLoss() < Ask - (TrailingStopPips*Point)&& Ask > (OrderOpenPrice()+TrailingStopTrigger*Point)) { if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(Ask - (TrailingStopPips*Point),Digits)) moveSL(OrderTicket(),NormalizeDouble(Ask - (TrailingStopPips*Point),Digits)); } } else if(OrderType() == OP_SELL) { if(OrderStopLoss() > Bid + (TrailingStopPips*Point) && Bid < (OrderOpenPrice()-TrailingStopTrigger*Point)) { if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(Bid + (TrailingStopPips*Point),Digits)) moveSL(OrderTicket(),NormalizeDouble(Bid + (TrailingStopPips*Point),Digits)); } } } } } } //OPEN Order if( OrdersTotal() >= MaxTrades ) { Orderstop = True; }else{ Orderstop = false; } //Anzahl der Orders if( !OrderTime ) { Orderstop = True; } if(timeCheck && isOrderAllowed()/*one order per bar*/ ) { if( !Orderstop && !SpreadtoHigh && getSignal(OP_BUY)) //Long Open { if(TakeProfit>0) tp = Ask + TakeProfit * Point;
if(StopLoss>0) { sl = Ask - StopLoss*Point; } sl = iblauSL;// aktuelles SL openOrder(LotSize(OP_BUY),OP_BUY, sl, tp,"START"); // Orig: openOrder(LotSize(OP_BUY),OP_BUY, sl,tp,"START"); if(EmailOn) SendMail("Message from "+Version,"LONG TRADE TAKEN AT "+Symbol()+" PRICE "+Ask); if(AlertOn) Alert("LONG TRADE TAKEN AT "+Symbol()+" PRICE "+Ask); } if( !Orderstop && !SpreadtoHigh && getSignal(OP_SELL)) //Short Open { if(TakeProfit>0) tp = Bid - TakeProfit * Point;
if(StopLoss>0) { sl = Bid + StopLoss * Point; } sl = irotSL; openOrder(LotSize(OP_SELL),OP_SELL, sl, tp,"START"); // Orig: openOrder(LotSize(OP_SELL),OP_SELL, sl,tp,"START"); if(EmailOn) SendMail("Message from "+Version,"SHORT TRADE TAKEN AT "+Symbol()+" PRICE "+Bid); if(AlertOn) Alert("SHORT TRADE TAKEN AT "+Symbol()+" PRICE "+Ask); } }
//---- return(0); } //+------------------------------------------------------------------+
//Merken: iblauLastNew == 0 --->Neubildung einer Unterstützung //Merken: irotLastNew == 0 --->Neubildung eines Widerstandes // aa = -0.012; bb = -0.063; Erg: aa > bb // iLow(NULL,PERIOD_M1,iLowest(NULL,PERIOD_M1,MODE_LOW,10,0)); //findet Tiefs der letzten 10 Bars und 10 minuten // iHigh(NULL,PERIOD_M1,iHighest(NULL,PERIOD_M1,MODE_HIGH,10,0));
bool getSignal(int mode) //Kaufsignale definieren und starten { int SignalKauf = 0, SignalVerkauf = 0;
//KaufSignale definieren double naheiblau = (iClose(NULL, 0, 0) / iblau * 100) - 100; if ( !einmalKauf && iClose(NULL, 0, 0) > iblau && naheiblau < 0.01 ) { SignalKauf = 1; iblauSL = iblau - 0.0003; } //einmalig pro Unterstützung, Kauf nahe Unterstützung prozentuale Abweichung zwischen Kurs und Unterstützung
//VerkaufSignale definieren double naheirot = (iClose(NULL, 0, 0) / irot * 100) - 100; if ( !einmalVerkauf && iClose(NULL, 0, 0) < irot && naheirot > -0.01 ) { SignalVerkauf = 1; irotSL = irot + 0.0003; } //Verkauf nahe Widerstandes(Prozentangabe), einmalig pro Widerstand
//Rücksetzung bei neuem Bereich mit Toleranzwert in Prozentangabe, ab x Prozent Veränderung wird erst zurückgesetzt if ( iblauAbweichung > 0.05 && iblauLastNew == 0 ) { einmalKauf = 0; } if ( irotAbweichung > 0.05 && irotLastNew == 0 ) { einmalVerkauf = 0; }
//Signal zur Order geben if(mode == OP_BUY) { if( SignalKauf ) { einmalKauf = 1; return(true); } } else if(mode == OP_SELL) { if( SignalVerkauf ) { einmalVerkauf = 1; return(true); } } return(false); } //---- bool getExitSignal(int mode) { if(mode == OP_SELL) //Schließen des Put { if ( ((iClose(NULL, 0, 0) / iblau * 100) - 100) <= 0.08 ) { return(true); } else return(false); } if(mode == OP_BUY) //Schließen des Call { if ( ((iClose(NULL, 0, 0) / irot * 100) - 100) >= -0.08 ) { return(true); } else return(false); } return(false); } //---- bool moveSL(int ticket,double stoploss) { if(!IsTradeAllowed()) return (false); if(MathAbs(Ask-stoploss)/Point < MarketInfo(Symbol(),MODE_STOPLEVEL)) { Print("STOP LOSS too close ",Bid," SL ",stoploss); return(false); } int error; int MAXRETRIES = 5; int retries = 0; while(!OrderModify(ticket,OrderOpenPrice(), stoploss, OrderTakeProfit(), 0,CLR_NONE)) { error = GetLastError(); if(error>1) Print("MoveSL failed with error #",ErrorDescription(error)," CurrentSL ",OrderStopLoss()," NewSL ",stoploss); Sleep(1000); RefreshRates(); if(retries >= MAXRETRIES) return(false); else retries++; } } //---- int orderCount(int type) { int total = OrdersTotal(); int oc = 0; for(int cnt = 0 ;cnt<=total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == getMagic() && (OrderType() == type || type == -1)) oc+=1; } return(oc); } //---- double LotSize(int type) { if(!UseMoneyManagement) return(BaseLot);
double lots = BaseLot; if(IsMicroAcc) lots = NormalizeDouble(BaseLot*(orderCount(type)+1),2); else lots = NormalizeDouble(BaseLot*(orderCount(type)+1),1);
return(MaxMinLot(lots)); }
double MaxMinLot (double lots) { if(lots>MaxLotSize) lots = MaxLotSize; if(lots<MinLotSize) lots = MinLotSize; return(lots); } //---- int getMagic() { return(Magic); } //---- bool isOrderAllowed() { int totalH = OrdersHistoryTotal(); for(int cntH = 0 ; cntH<=totalH; cntH++) { OrderSelect(cntH, SELECT_BY_POS, MODE_HISTORY); if((OrderMagicNumber() == getMagic()) ) { int orderOShiftH = iBarShift(Symbol(),0,OrderOpenTime(),false); if(orderOShiftH == 0 ) return(false); } } int total = OrdersTotal(); for(int cnt = 0 ; cnt <= total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if((OrderMagicNumber() == getMagic()) ) { int orderOShift = iBarShift(Symbol(),0,OrderOpenTime(),false); if(orderOShift == 0 ) return(false); } } return(true); } //---- bool openOrder(double LTS,int type, double sl,double tp, string description = "" ) { if(!IsTradeAllowed()) { return (-1); } int error = 0; int ticket = 0; if( type == OP_SELL ) { while(true) { RefreshRates(); ticket = OrderSend(Symbol(),OP_SELL,LTS,MarketInfo(Symbol(),MODE_BID),0,0,0,StringConcatenate(Version," ",description),getMagic(),0,Pink); if(ticket<=0) { error=GetLastError(); Print("SELL ORDER ERROR:", ErrorDescription(error)); if(!ErrorBlock(error,LTS)) break; } else { if(sl>0 || tp>0) { OrderSelect(ticket,SELECT_BY_TICKET); OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green); } if(ScreenshotOn)doScreenshot(); OrderPrint(); break; } } } else if( type == OP_BUY ) { while(true) { RefreshRates(); ticket = OrderSend(Symbol(),OP_BUY,LTS,MarketInfo(Symbol(),MODE_ASK),0,0,0,StringConcatenate(Version," ",description),getMagic(),0,Lime); if(ticket<=0) { error=GetLastError(); Print("BUY ORDER ERROR:", ErrorDescription(error)); if(!ErrorBlock(error,LTS)) break; } else { if(sl>0 || tp>0) { OrderSelect(ticket,SELECT_BY_TICKET); OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green); } if(ScreenshotOn)doScreenshot(); OrderPrint(); break; } } } return (0); } //+------------------------------------------------------------------+ bool ErrorBlock(int error = 0,double lot = 0) { switch(error) { case 0: { //no error - exit from loop Print("NO ERROR"); return(false); } case 2: { Print("System failure. Reboot the computer/check the server"); return(false); } case 3: { Print("Error of the logic of the EA"); return(false); } case 4: { Print("Trading server is busy. Wait for 2 minutes."); Sleep(120000); return(true); } case 6: { bool connect = false; int iteration = 0; Print("Disconnect "); while((!connect) || (iteration > 60)) { Sleep(10000); Print("Connection not restored", iteration*10," seconds passed"); connect = IsConnected(); if(connect) { Print("Connection restored"); } iteration++; } Print("Connection problems"); return(false); } case 8: { Print("Frequent requests"); return(false); } case 64: { Print("Account is blocked!"); return(false); } case 65: { Print("Wrong account number???"); return(false); } case 128: {//???? Print("Waiting of transaction timed out"); Sleep(10000);//10 seconds RefreshRates(); return(false); } case 129: { Print("Wrong price"); RefreshRates(); return(false); } case 130: { Print("Wrong stop SLEVEL"+MarketInfo(Symbol(),MODE_STOPLEVEL)+" FZLVL "+MarketInfo(Symbol(),MODE_FREEZELEVEL)+" FZLVL "+MarketInfo(Symbol(),MODE_SPREAD)); RefreshRates(); return(false); } case 131: { Print("Wrong calculation of trade volume"); return(false); } case 132: { Print("Market closed"); return(false); } case 134: {//NOT ENOUGH CASH? Print("Lack of margin for performing operation, margin: "+AccountFreeMargin()); return(false); } case 135: { Print("Prices changed"); RefreshRates(); return(true); } case 136: { Print("No price!"); return(false); } case 138: { Print("Requote again!"); RefreshRates(); return(true); } case 139: { Print("The order is in process. Program glitch"); Sleep(10000);//10 seconds return(true); } case 141: { Print("Too many requests"); Sleep(10000);//10 seconds return(true); } case 148: { Print("Transaction volume too large"); return(false); } default: { Print("Unhandeled exception code:",error," stoplevel ",MarketInfo( Symbol(), MODE_STOPLEVEL) ," spread ",MarketInfo( Symbol(), MODE_SPREAD)+" LOTS:"+ lot); return(false); } } } void doScreenshot() { string dateMarker = TimeYear(TimeCurrent())+"_"+TimeMonth(TimeCurrent())+"_"+TimeDay(TimeCurrent())+"___"+TimeHour(TimeCurrent())+"_"+TimeMinute(TimeCurrent())+"_"+TimeSeconds(TimeCurrent()); WindowScreenShot(getMagic()+" "+Version+" "+dateMarker+".gif", 800, 600, 0, 1, 0); } void closeAllPoisitions(int type) { //close all int oc = orderCount(type); while(oc > 0) { int total = OrdersTotal(); RefreshRates(); for(int cnt = 0 ;cnt<=total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber() == getMagic()) { if(OrderType() == OP_BUY && type == OP_BUY) { OrderClose(OrderTicket(),OrderLots(), Bid, 0, Yellow); } else if(OrderType() == OP_SELL && type == OP_SELL) { OrderClose(OrderTicket(),OrderLots(), Ask, 0, Yellow); } } } oc = orderCount(type); } } double profitCount() { double oc = 0; for(int cnt = 0 ;cnt<OrdersTotal();cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == getMagic()) { oc+= OrderProfit()+OrderSwap()+OrderCommission(); } } return(oc); }
|