//#property copyright "Copyright © 2012, " //#property link "" extern string _OrderTags = "--------------------"; extern string OrdComment = "Mast_ea1"; // is used here as a variable string EAName = "_ea1"; extern int MagicNumber = 8326; extern string _Signals = "---------------"; extern int SignalShift = 0; //extern bool TurnPosition = true; extern string _Entry = "--------------------"; extern int MeasureBars = 10; extern double MeasurePips = 10; extern int DelayAfterClose = 2; extern bool BarsShiftSignal = true; extern int EntryTime = 30; extern int Grouping = 2; extern bool GroupingStepMode = true; extern double GroupingStep = 1; extern string _Exit = "--------------------"; extern double TakeProfit = 0; extern double StopLoss = 0; extern string _TrailingStop = "..............."; extern bool TrailingStopUse = true; extern double TrailingStopSL = 10; extern double TrailingStopStart = 12; extern double TrailingStep = 1; extern string _Money = "--------------------"; extern double Lots = 0.1; extern string _OrderErrorHandling = "--------------------"; extern bool ECN = true; extern double SlippageOpen = 6; extern double SlippageClose = 6; extern int MaxOrderTrials = 100; extern int OrderWaitTime = 100; // ms extern double MinStopAdd = 1.0; // points extern bool SimulateErrors = false; extern double ErrorRate = 10; // weg! extern double ErrorRateOpen = 10; extern double ErrorRateModify = 10; extern double ErrorRateClose = 10; bool OrdAutoForget = true; //depending on EA extern bool InfiniteTrialsOpen = false; //!!! important must be false . otherwise problem with recognition of proper mighty trade ( checkForOpen ) extern bool InfiniteTrialsMod = true; extern bool InfiniteTrialsClose = true; double NumericTolerance = 0.000001; int BackTestVisualSlowDown = 10000; extern bool DeleteGraphics = false; bool DrawClosedTrades = true; extern bool DEBUG = false; #include #include #include //adjusted!! #include #include #include #include #include //#include #include //#include #include /* struct SubEA // #################################################################################### { //public: */ int id; int magic; bool stopped; bool justStarted2; int nbTradesOld,nbTrades,nbBuys,nbBuysOld,nbSells,nbSellsOld; bool firstTrades; bool firstUp,lastUp; int tradeNb; int tradeStep; double ma1_old,ma2_old; double ordersHistoryTotal_old,ordersHistoryTotal; double profitTotal; // cumulative losses in a chain . reset when brought back inside double lotsCur; bool allowTrades; bool closeBuysFlag; bool closeSellsFlag; bool openBuyFlag2,openSellFlag2; int barsLastClose; //#include "saveState2.mqh" int barsOld; double volumeOld; datetime timeOld; bool firstStart = true; bool justStarted; double closeOld,bidOld,askOld; //int ordersHistoryTotal_old; int subEACnt; double spread; bool buyOld,sellOld; bool groupBuy; bool groupClosed; datetime groupStartTime; double groupLastOpenPrice; bool buy,sell; bool groupStarted; int groupsThisBar; // for bars Shift Signal double priceCloseLast; //double lotsCur; //bool movedUp,movedDown; //bool adaptTPSLFlag; int init() { int i; i = 0; stopped = false; if (firstStart) { allowTrades = false; tradeNb = 0; lotsCur = Lots; profitTotal = 0; barsLastClose = 0; } openBuyFlag2 = false; openSellFlag2 = false; justStarted = true; justStarted2 = true; graphicsInit(); if (firstStart) { ordersInit(); //ordersHistoryTotal_old = OrdersHistoryTotal(); } buy = false; sell = false; groupClosed = true; firstStart = false; return(0); } int deinit() { graphicsDeinit(); } int start() { // Print("id ",id," start"); int i, id, ticket,n; double entryLev,t2; double lots,tp; updateOrders(); ordersHistoryTotal = OrdersHistoryTotal(); nbTrades = CalcCurActiveTrades(); nbBuys = CalcCurActiveBuys(); nbSells = CalcCurActiveSells(); // closing price of the last trade for (i = OrdersHistoryTotal(); i >= ordersHistoryTotal_old; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) { if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) { priceCloseLast = OrderClosePrice(); if (DEBUG) Print(" priceCloseLast ",priceCloseLast); break; } } } if (Bars > barsOld || justStarted) { groupsThisBar = 0; } if (nbTrades == 0 && nbTradesOld > 0 && !justStarted) { barsLastClose = Bars; } if (nbTrades < nbTradesOld && !justStarted) { groupClosed = true; } groupStarted = false; if ((SignalShift == 0 || Bars > barsOld) && !justStarted) { int y; y = SignalShift; // measure,Measurement Period int shiftMin,shiftMax; double min,max; min = 999999999; max = 0; // buy = false; // sell = false; for (i = y; i <= y + MeasureBars - 1; i++) { // from right to left if (Low[i] < min) { min = Low[i]; shiftMin = i; } if (High[i] > max) { max = High[i]; shiftMax = i; } sell = max - Close[y] >= MeasurePips * PointX; // max comes later - > upward . break because only the last one trend buy = Close[y] - min >= MeasurePips * PointX; } // start group if (nbTrades == 0 && Bars - barsLastClose >= DelayAfterClose // max a 2nd time open group if price returns to the closing price of the last trades && ( (BarsShiftSignal && (groupsThisBar == 0 || (groupsThisBar == 1 && ((buy && Ask >= priceCloseLast) || (sell && Bid <= priceCloseLast))))) || (!BarsShiftSignal && groupsThisBar == 0) ) ) { bool groupStarted = true; groupClosed = false; groupStartTime = Time[0]; if (buy) groupBuy = true; if (sell) groupBuy = false; groupsThisBar++; if (DEBUG) Print("start group. group "+groupsThisBar+" in bar ",Bars," priceCloseLast ",priceCloseLast); } sellOld = sell; buyOld = buy; justStarted2 = false; } // open if ( nbTrades < Grouping && ( groupStarted || (!groupStarted && (!GroupingStepMode || ((groupBuy && Ask > groupLastOpenPrice + GroupingStep * PointX) || (!groupBuy && Bid < groupLastOpenPrice - GroupingStep * PointX)))) // mindestsabstand (staffelung) ) && TimeCurrent() - groupStartTime < EntryTime // nur innnerhalb der ersten x sek der kerze des signals einsteigen && !groupClosed // wenn noch kein trade der gruppe geschlossen wurde ) { if (buy && groupBuy) groupLastOpenPrice = Ask; if (sell && !groupBuy) groupLastOpenPrice = Bid; lots = Lots; if (!justStarted2) { if (buy && groupBuy) { OpenBuy(lots,StopLoss,TakeProfit); } if (sell && !groupBuy) { OpenSell(lots,StopLoss,TakeProfit); } } } nbTrades = CalcCurActiveTrades(); nbBuys = CalcCurActiveBuys(); nbSells = CalcCurActiveSells(); CheckForClose(); nbTradesOld = nbTrades; nbBuysOld = nbBuys; nbSellsOld = nbSells; barsOld = Bars; justStarted = false; ordersHistoryTotal_old = ordersHistoryTotal; } void CheckForClose() { // if (closeAllFlag) CloseAllTrades(); int ownOpenTrades,nb,i,ticket,k; bool res=false,close; double price; int nb1=OrdersTotal(); ownOpenTrades=1; while(ownOpenTrades>0 && nb1>0) { nb1--; ownOpenTrades=0; nb=OrdersTotal(); for (i=0; i