Hello everyone!
I am new to mql5 programmig and after some reading and tutorials i took this article as basis for simple EA. Nothing fancy. I just used price bars to entry. After some ECN and Mqltraderequest related problems i got it work in demo and strategy tester. Now another problem encountered with checkSell and checkBuy. It seems the simple signal i programmed doesnt work always. below is the checkSell func and i attached a pic with the behavior. The white arrows are showing some of the bars, were the EA should have given a signal. Any ideas? I feel like missing something obvious, but i dont find it. There are now errors, checkSell and checkBuy just give no signal sometimes.
void OnTick() { int Mybars=Bars(_Symbol,_Period); if(Mybars<60) { Alert("We have less than 60 bars, EA will now exit!!"); return; } MqlTick last_tick; MqlRates rates[]; ArraySetAsSeries(rates,true); if(!SymbolInfoTick(_Symbol,last_tick)) { Alert("Error getting the latest price quote - error:",GetLastError(),"!!"); return; } if(CopyRates(_Symbol,_Period,0,3,rates)<0) { Alert("Error copying rates/history data - error:",GetLastError(),"!!"); return; } static datetime Prev_time; datetime Bar_time[1]; Bar_time[0]=rates[0].time; if(Prev_time==Bar_time[0]) { return; } Prev_time=Bar_time[0]; bool Buy_opened=false,Sell_opened=false; if(PositionSelect(_Symbol)==true) { if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { Buy_opened=true; } else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) { Sell_opened=true; } } Cexpert.setCloseprice(rates[1].close); if(Cexpert.checkBuy()==true) { //--- do we already have an opened buy position if(Buy_opened) { Alert("We already have a Buy Position!!!"); return; // Don't open a new Buy Position } double aprice = NormalizeDouble(last_tick.ask,_Digits); // current Ask price double stl = NormalizeDouble(last_tick.ask - STP*_Point,_Digits); // Stop Loss double tkp = NormalizeDouble(last_tick.ask + TKP*_Point,_Digits); // Take profit int mdev = 100; // Maximum deviation // place order Cexpert.openBuy(ORDER_TYPE_BUY,aprice,stl,tkp,mdev); } //--- check for any Sell position if(Cexpert.checkSell()==true) { //--- do we already have an opened Sell position if(Sell_opened) { Alert("We already have a Sell position!!!"); return; // Don't open a new Sell Position } double bprice=NormalizeDouble(last_tick.bid,_Digits); // Current Bid price double bstl = NormalizeDouble(last_tick.bid + STP*_Point,_Digits); // Stop Loss double btkp = NormalizeDouble(last_tick.bid - TKP*_Point,_Digits); // Take Profit int bdev=100; // Maximum deviation // place order Cexpert.openSell(ORDER_TYPE_SELL,bprice,bstl, btkp, bdev); // Cexpert.takeStop(TP1,SL1, bdev); } return; }I thought so, because checkSell and checkBuy called in the OnTick() function. Like above.
No, this is what I thought, your EA trade on new bar, and not every tick :
static datetime Prev_time; datetime Bar_time[1]; Bar_time[0]=rates[0].time; if(Prev_time==Bar_time[0]) { return; }It's better to understand a code when you copy it


- 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 everyone!
I am new to mql5 programmig and after some reading and tutorials i took this article as basis for simple EA. Nothing fancy. I just used price bars to entry. After some ECN and Mqltraderequest related problems i got it work in demo and strategy tester. Now another problem encountered with checkSell and checkBuy. It seems the simple signal i programmed doesnt work always. below is the checkSell func and i attached a pic with the behavior. The white arrows are showing some of the bars, were the EA should have given a signal. Any ideas? I feel like missing something obvious, but i dont find it. There are now errors, checkSell and checkBuy just give no signal sometimes.