Please use this to post code . . . it makes it easier to read.
- Use SRC
double sLimit2 = NormalizeDouble((Ask - 2),Digits); //2.5
On EURUSD Ask is something like 1.2345 so sLimit2 is a negative number - Bogus. You probably mean Ask - 2 pips but aren't converting pips to price, and arenn't adjusting for 4/5 digit brokers.double thisDigits; if (Digits <= 3) thisDigits = 0.01; else thisDigits = 0.0001; double spread = NormalizeDouble((Ask - Bid) / thisDigits, 1);
Hard coding numbers is never good. This will work for EURUSD like pairs and JPY. It will fail on metals. EA's must adjust for 4/5 digit brokers (TP, SL, AND SLIPPAGE)//++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.015 0.0150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) int init(){ if (Digits % 2 == 1){ // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262 pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //---- These are adjusted for 5 digit brokers. /* On ECN brokers you must open first and THEN set stops int ticket = OrderSend(..., 0,0,...) if (ticket < 0) Alert("OrderSend failed: ", GetLastError()); else if (!OrderSelect(ticket, SELECT_BY_TICKET)) Alert("OrderSelect failed: ", GetLastError()); else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0) Alert("OrderModify failed: ", GetLastError()); */
You do NOT need to use normalize, EVER. Comparing for near equality Normalize(a-b,x)==0 same as MathAbs(a-b) < Point.- When MarketInfo(pair, MODE_TICKSIZE) != Point (metals only) then the pending price must be adjusted
double TS=MarketInfo(pair, MODE_TICKSIZE); sLimit1=MathRound(sLimit1/TS)*TS;
Hi Raptor and WHRoeder. I really appreciate your replys. I was wondering how to show the SRC code properly - so thanks also for pointing this out. Below it is attached properly.
WHRoeder, thanks so much for your time. Here are my responses to your points:
2.)I am coding only for myself, my broker and 1 instrument SP500, so I hardcoded the values as mentioned. I realise this isnt best practise, so I will improve on this.
3.) As per point 2.
4.) Thanks for bringing this out! I found though that when i didnt normalise, I had errors sending values to modify orders: Stoploss and TP for example. But I think your point 5 answers this problem
5.) THanks!
Please let me know if you spot anything else that I can improve on.
Once again really appriated. I will apply above changes and test my new code over a week, and see if any random orders are entered.
//+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD); //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start(){ //---- if(Bars < 1){ Alert("Not enough bars in the window. EA doesn't work."); return; // Exit start() } double thisDigits; if (Digits <= 3) thisDigits = 0.01; else thisDigits = 0.0001; double spread = NormalizeDouble((Ask - Bid) / thisDigits, 1); //MA 1 - SHORT 5 MIN if(GetCurrentShortOrders(Symbol(),201105) < 1){ if(Ask > iMA(NULL, PERIOD_M5, 20,MODE_EMA,PRICE_CLOSE,0)&& spread <= 2){ double sLimit1 = NormalizeDouble((Ask - 2),Digits); //2.5 double sSLoss1 = NormalizeDouble((Ask + 5),Digits); //7.5 double sTP1 = NormalizeDouble((Ask - 5),Digits); //6.75 double slot1 = 1; OrderSend(Symbol(),OP_SELLSTOP,slot1,sLimit1,3,sSLoss1,sTP1,"S_1",201105,TimeCurrent()+3601,Green); }//end if ask }else{ //already have 1 short - dont add any more } //END MA 1 SHORT //MA 2 - SHORT 30 MIN if(GetCurrentShortOrders(Symbol(),201105) < 1){ if(Ask > iMA(NULL, PERIOD_M30, 20,MODE_EMA,PRICE_CLOSE,0)&& spread <= 2){ double sLimit2 = NormalizeDouble((Ask - 2),Digits); //2.5 double sSLoss2 = NormalizeDouble((Ask + 5),Digits); //7.5 double sTP2 = NormalizeDouble((Ask - 5),Digits); //6.75 double slot1 = 1; OrderSend(Symbol(),OP_SELLSTOP,slot1,sLimit1,3,sSLoss1,sTP1,"S_2",201130,TimeCurrent()+3601,Green); }//end if ask }else{ //already have 1 short - dont add any more } //END MA 2 SHORT //---- return(0); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, I have built an EA I am testing.
I have noticed that occasionally, I get a random trade placed, that doesnt match my defined criteria for a trade. The random trades only happen when:
a.) I re-attach the EA to the chart for the first time. It then (only sometimes) opens a new trade even if conditions are not met. (Ie price is WELL below MA)
or
b.) There is a break (ie Weekend) and as soon as it opens, the EA may place a trade - again even if conditions are not met. (Ie price is WELL below MA)
Please see an example of my code below, and please - ANY suggestions are welcome! I am desperate for help.
Thanks kindly in advance.
Dale
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start(){
//----
if(Bars < 1){
Alert("Not enough bars in the window. EA doesn't work.");
return; // Exit start()
}
double thisDigits;
if (Digits <= 3) thisDigits = 0.01;
else thisDigits = 0.0001;
double spread = NormalizeDouble((Ask - Bid) / thisDigits, 1);
//MA 1 - SHORT 5 MIN
if(GetCurrentShortOrders(Symbol(),201105) < 1){
if(Ask > iMA(NULL, PERIOD_M5, 20,MODE_EMA,PRICE_CLOSE,0)&& spread <= 2){
double sLimit1 = NormalizeDouble((Ask - 2),Digits); //2.5
double sSLoss1 = NormalizeDouble((Ask + 5),Digits); //7.5
double sTP1 = NormalizeDouble((Ask - 5),Digits); //6.75
double slot1 = 1;
OrderSend(Symbol(),OP_SELLSTOP,slot1,sLimit1,3,sSLoss1,sTP1,"S_1",201105,TimeCurrent()+3601,Green);
}//end if ask
}else{
//already have 1 short - dont add any more
}
//END MA 1 SHORT
//MA 2 - SHORT 30 MIN
if(GetCurrentShortOrders(Symbol(),201105) < 1){
if(Ask > iMA(NULL, PERIOD_M30, 20,MODE_EMA,PRICE_CLOSE,0)&& spread <= 2){
double sLimit2 = NormalizeDouble((Ask - 2),Digits); //2.5
double sSLoss2 = NormalizeDouble((Ask + 5),Digits); //7.5
double sTP2 = NormalizeDouble((Ask - 5),Digits); //6.75
double slot1 = 1;
OrderSend(Symbol(),OP_SELLSTOP,slot1,sLimit1,3,sSLoss1,sTP1,"S_2",201130,TimeCurrent()+3601,Green);
}//end if ask
}else{
//already have 1 short - dont add any more
}
//END MA 2 SHORT
//----
return(0);
}
//+------------------------------------------------------------------+