error code 4107

 

Could someone please tell me what is wrong with this attempt to send an order? In backtest it places no orders and the Journal returns error 4107.

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,OrderOpenPrice()-25*Point,OrderOpenPrice()+25*Point,"newenv2",21,0,Green);

Any help would be greatly appreciated.

 
dun4fun42:

Could someone please tell me what is wrong with this attempt to send an order? In backtest it places no orders and the Journal returns error 4107.

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,OrderOpenPrice()-25*Point,OrderOpenPrice()+25*Point,"newenv2",21,0,Green);


Error 4107 is "invalid price". I suspect you think that "OrderOpenPrice()-25*Point" means "25 pips below the price at which the order is filled". It doesn't. It means 25 pips below the opening price of an existing order which you have previously selected using OrderSelect(). If you haven't previously done an OrderSelect(), then OrderOpenPrice() will return 0, and you will be trying to place the stop at a price such as -0.0025. Leading to error 4107.


If you want a stop 25 pips from the open price of this new order then you can use Ask-25*Point. However, any slippage on the opening fill will not be reflected in this stop; you could get filled at Ask+2 while still having your stop at Ask-25. If you want the stop to be 25 pips from whatever price you actually get filled at, then you need to place the order without a stop, and then use OrderModify() to put in the stop afterwards.


(All this with the usual caveat that it's now post-midnight in the UK. Apologies for any dumb errors which have crept in.)

 
jjc:

Error 4107 is "invalid price". I suspect you think that "OrderOpenPrice()-25*Point" means "25 pips below the price at which the order is filled". It doesn't. It means 25 pips below the opening price of an existing order which you have previously selected using OrderSelect(). If you haven't previously done an OrderSelect(), then OrderOpenPrice() will return 0, and you will be trying to place the stop at a price such as -0.0025. Leading to error 4107.


If you want a stop 25 pips from the open price of this new order then you can use Ask-25*Point. However, any slippage on the opening fill will not be reflected in this stop; you could get filled at Ask+2 while still having your stop at Ask-25. If you want the stop to be 25 pips from whatever price you actually get filled at, then you need to place the order without a stop, and then use OrderModify() to put in the stop afterwards.


(All this with the usual caveat that it's now post-midnight in the UK. Apologies for any dumb errors which have crept in.)

Many thanks. It now works perfectly.

Reason: