Hi everyone!
I wasted the last couple of hours to find an error in my code. See the pic below:
Suddenly, in the strategy tester, it buys and sells where it shouldnt. I mean its the right bar, but at a price level, price never saw. I took a look into the logs for some errors, but it says that the price level it opened a position at was the actual ask/bid price. Even when price never got there, like in the pic. THere is just one error in the log, when it fails to set proper sl/tp because of this behavior. At this point there is just an open position, which will be closed before the end of test. Take a look on the pics margin area in the end and you know what i mean.
I cant get behind it, any ideas?
Thanks for the reply angevoyageur!
So you say he is buying at bid, where he should buy at ask. But if buy conditions in this ea are met he buys at ask i think.
if(Cexpert.checkBuy()==true) { //--- do we already have an opened buy position if(Buy_opened || Sell_opened) { // Print("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 Print("buy ",last_tick.ask); // place order Cexpert.openBuy(ORDER_TYPE_BUY,aprice,stl,tkp,mdev); }
He only checks for buy conditions at open of current bar and buys at last_tick.ask
Sometimes he buys/sells 4.5 pips from price away and sometimes even 7 pips, because of this i thought i would be no spread or ask/bid problem. Or something went terribly wrong :)
Thanks for the reply angevoyageur!
So you say he is buying at bid, where he should buy at ask. But if buy conditions in this ea are met he buys at ask i think.
He only checks for buy conditions at open of current bar and buys at last_tick.ask
Sometimes he buys/sells 4.5 pips from price away and sometimes even 7 pips, because of this i thought i would be no spread or ask/bid problem. Or something went terribly wrong :)
No, a buy is always at ask price. Can you show the code of Cexpert.openBuy ?
You can print ask/bid price to check.
sure. here you go
void ExpertA::openBuy(ENUM_ORDER_TYPE otype,double askprice,double SL, double TP, int dev,string comment="") { if(Chk_Margin==1) { if(MarginOK()==false) { Errormsg= "You do not have enough money to open this Position!!!"; Errcode =GetLastError(); showError(Errormsg,Errcode); } else { trequest.action=TRADE_ACTION_DEAL; trequest.type=otype; trequest.volume=LOTS; trequest.price=askprice; trequest.sl=0; trequest.tp=0; trequest.deviation=dev; trequest.magic=Magic_No; trequest.symbol=symbol; trequest.type_filling=ORDER_FILLING_FOK; //--- send OrderSend(trequest,tresult); //--- check result if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed { Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!"); } else { Errormsg= "The Buy order request could not be completed"; Errcode =GetLastError(); showError(Errormsg,Errcode); } } } else { // Alert("test"); trequest.action=TRADE_ACTION_DEAL; trequest.type=otype; trequest.volume=LOTS; //Alert("volume",trequest.volume); trequest.price=askprice; trequest.sl=0; trequest.tp=0; trequest.deviation=dev; trequest.magic=Magic_No; trequest.symbol=symbol; trequest.type_filling=ORDER_FILLING_FOK; //--- send OrderSend(trequest,tresult); //--- check result if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed { Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!"); } else { Errormsg= "The Buy order request could not be completed"; Errcode =GetLastError(); showError(Errormsg,Errcode); } Sleep(10); trequest.action = TRADE_ACTION_SLTP; trequest.symbol = _Symbol; trequest.sl = SL; //NormalizeDouble( trequest.price + Stop*_Point,_Digits); // Stop Loss trequest.tp = TP; //NormalizeDouble( trequest.price + Take*_Point,_Digits); // Stop Loss trequest.deviation = Slippage; OrderSend(trequest, tresult); if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed { Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!"); } else { Errormsg= "The Buy order request could not be completed"; Errcode =GetLastError(); showError(Errormsg,Errcode); } // } } }
sure. here you go
All seems correct, although a little weird (why are repeating the same code twice).
Anyway why are you thinking the difference isn't from ask/bid ? You can check the spread on chart by displaying Data Window (Ctrl+D) and moving your mouse on the chart at the date of interest. Obviously you can also check it by code.
maybe i got it all wrong. But i show you another pic
lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.
Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?
edit: if i print ask, it tells me this is the ask price. is that possible?
lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.
Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?
You have been given good advice, take it and you will know the answer instead of asking other people to guess . . . .
You can print ask/bid price to check.
When your EA places a trade Print(), Bid, Ask, OpenPrice of the trade and anything else that will help you, make sure to use DoubleToStr(value, _Digits) to get the correct number of decimals for the symbol you are using.
maybe i got it all wrong. But i show you another pic
lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.
Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?
edit: if i print ask, it tells me this is the ask price. is that possible?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone!
I wasted the last couple of hours to find an error in my code. See the pic below:
Suddenly, in the strategy tester, it buys and sells where it shouldnt. I mean its the right bar, but at a price level, price never saw. I took a look into the logs for some errors, but it says that the price level it opened a position at was the actual ask/bid price. Even when price never got there, like in the pic. THere is just one error in the log, when it fails to set proper sl/tp because of this behavior. At this point there is just an open position, which will be closed before the end of test. Take a look on the pics margin area in the end and you know what i mean.
I cant get behind it, any ideas?