newbee1713006039 Posted September 28, 2010 Report Share Posted September 28, 2010 (edited) Hello, what could be wrong inside the code, when the EA always starts with the lowest lotsize (0.01), after MT4 has been restarted, although MM is enabled on real money account? But after the first trade the following orders are opened with the correct lotsize. But as soon as MT4 is restarted again, the EA opens the first order with the lowest lotsize again. here is my statement http://www.mt4i.com/users/trendonfbs #property copyright "" #include <stderror.mqh> #include <stdlib.mqh> extern int StopLoss = 6; extern int TrailingStop = 6; extern int TimesPriceMustGoInThisDirection = 5; //should probably introduce something like how far it must go extern double InHowManySeconds = 3; //in how many seconds extern double Lots = 0; extern double MaxLot = 10000000000000; extern bool MoneyManagement = TRUE; extern int Risk = 5; extern int Magic = 11111111; //internal variables int total; int upCnt, dnCnt, TimeStartedGoingUp, TimeStartedGoingDown; bool AmIGoingUp,AmIGoingDown, NeedToSetStop; double priceLastUp, priceLastDown; int PipMultiplier=1; int Dig = 2; //this is used to ensure on 5 digit brokers that the price moves a whole pip, not a fraction of a pip int init() { if(Digits==2 || Digits==3) Dig = 2; else Dig = 4; if(Digits==3 || Digits==5) PipMultiplier=10; return(0); } int deinit() { return(0); } void openOrders() { int try; if(upCnt < dnCnt){ Print("Down... Check if moved far enough"); if(TimeCurrent() - TimeStartedGoingDown <= InHowManySeconds){ Print("Moved far enough, lets go"); for(try = 1; try <= 2; try++){ while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if(OrderSend(Symbol(),OP_SELL,GetLots(),Bid,10,0,0,"",Magic,0,Red) < 0){ Print("Error ", ErrorDescription(GetLastError())); Print("This was attempt: ", try); Sleep(1000); } else { NeedToSetStop = true; break; } } } } else { Print("Up... Check if moved far enough"); if(TimeCurrent() - TimeStartedGoingUp <= InHowManySeconds){ Print("Moved far enough, lets go"); for(try = 1; try <= 2; try++){ while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if (OrderSend(Symbol(),OP_BUY,GetLots(),Ask,10,0,0,"",Magic,0,Green) < 0){ Print("Error ", ErrorDescription(GetLastError())); Print("This was attempt: ", try); Sleep(1000); } else { NeedToSetStop = true; break; } } } } priceLastUp = 0; priceLastDown = 0; upCnt = 0; dnCnt = 0; AmIGoingUp = false; AmIGoingDown = false; TimeStartedGoingUp = 0; TimeStartedGoingDown = 0; } int start(){ double ask = StrToDouble(DoubleToStr(Ask,Dig)); double bid = StrToDouble(DoubleToStr(Bid,Dig)); total = OrdersTotal(); int i, currentSymbolOrderPos = -1; //check if in a trade for (i = 0; i < total; i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { currentSymbolOrderPos = i; break; } } else { Print("Error ", ErrorDescription(GetLastError())); } } if (currentSymbolOrderPos < 0) {//no orders, count if(priceLastUp < ask){ upCnt = upCnt + 1; priceLastUp = ask; if(AmIGoingUp == false) { TimeStartedGoingUp = TimeCurrent(); AmIGoingUp = true; } } else { priceLastUp = 0; upCnt = 0; AmIGoingUp = false; TimeStartedGoingUp = 0; } if(priceLastDown > ask) { dnCnt = dnCnt + 1; priceLastDown = ask; if(AmIGoingDown == false) { TimeStartedGoingDown = TimeCurrent(); AmIGoingDown = true; } } else { priceLastDown = 0; dnCnt = 0; AmIGoingDown = false; TimeStartedGoingDown = 0; } //check open conditions if(upCnt == TimesPriceMustGoInThisDirection || dnCnt == TimesPriceMustGoInThisDirection) { openOrders(); } if(priceLastUp == 0) { priceLastUp = ask; } if(priceLastDown == 0) { priceLastDown = ask; } } else {// have order, need to set stop or check trail if(!OrderSelect(currentSymbolOrderPos, SELECT_BY_POS, MODE_TRADES)) { Print("Error ", ErrorDescription(GetLastError())); } else { if (OrderType() == OP_BUY) { if(NeedToSetStop == true) { while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid - StopLoss*Point*PipMultiplier,Digits),'', 0, Red)) { Print("Error ", ErrorDescription(GetLastError())); } else { NeedToSetStop = false; } } //otherwise do trail if (Bid-OrderOpenPrice()>TrailingStop*Point*PipMultiplier) { if (OrderStopLoss()<Bid-(TrailingStop)*Point*PipMultiplier){ while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrailingStop)*Point*PipMultiplier,'', 0, Red)) { Print("Error ", ErrorDescription(GetLastError())); } } } } if(OrderType() == OP_SELL){ if(NeedToSetStop == true){ while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + StopLoss*Point*PipMultiplier, Digits),'', 0, Red)) { Print("Error ", ErrorDescription(GetLastError())); } else { NeedToSetStop = false; } } //otherwisedo trail if(OrderOpenPrice()-Ask > TrailingStop*Point*PipMultiplier){ if(OrderStopLoss() > Ask +(TrailingStop)*Point*PipMultiplier) { while(IsTradeContextBusy()) Sleep(10); RefreshRates(); if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (TrailingStop)*Point*PipMultiplier,'', 0, Red)) { Print("Error ", ErrorDescription(GetLastError())); } } } } } } return(0); } double GetLots() { int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);//Get digits size double MinlotTmp = MarketInfo(Symbol(), MODE_MINLOT); //What's the minimum possible lot size double MaxlotTmp = MarketInfo(Symbol(), MODE_MAXLOT); //What's the maximum possible lot size double Leverage = AccountLeverage(); //How much can you use depending on your account ballance and what the broker allowes double LotSize = MarketInfo(Symbol(), MODE_LOTSIZE); //What is the allowed lot size double lotsTmp = MathMin(MaxlotTmp, MathMax(MinlotTmp, Lots)); if(MoneyManagement && Risk > 0.0 && AccountFreeMargin() > Ask * lotsTmp * LotSize / Leverage) lotsTmp = NormalizeDouble(AccountFreeMargin() * Risk / LotSize, dig); else lotsTmp = MinlotTmp; lotsTmp = MathMax(MinlotTmp, MathMin(MaxlotTmp, NormalizeDouble(lotsTmp / MinlotTmp, 0) * MinlotTmp)); //lotsTmp = NormalizeDouble(lotsTmp / Max_BuySell, LotDecimal); if (lotsTmp > MaxLot) lotsTmp = MaxLot; //if (AccountFreeMargin() < Ask * lotsTmp * LotSize / Leverage) { //Print("Low Account Balance. Lots = ", lotsTmp, " , Free Margin = ", AccountFreeMargin()); //Comment("Low Account Balance. Lots = ", lotsTmp, " , Free Margin = ", AccountFreeMargin()); // return; return (lotsTmp); } Any help is appreciated. newbee Edited September 28, 2010 by newbee Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.