Stefano Cerbioni:
hi guys i have this part of code , i call it from button
but when i try to run return me always 10013 i insert 0.10 lot size and stay so far from market price but nothing anyone can help me ? thanks
Hi , you can use trade.SellStop
or even better
this with request and result
int OnInit() { return(INIT_SUCCEEDED); } void OnTick() { static bool PLACED=false;//ignore if(!PLACED){//ignore PLACED=true;//ignore //----------------------------------- /* your user provides 4 values SL TP LOT ENTRY users are blessed with the magical ability to go STRAIGHT to an issue in the code the first time they use your product , so , you have to anticipate that. */ string targetSymbol="EURUSD"; double userLotSize=0.01; double userSL=1.09; double userTP=1.06; double userEntry=1.07; ENUM_ORDER_TYPE userType=ORDER_TYPE_SELL_STOP; MqlTick TICK; if(SymbolInfoTick(targetSymbol,TICK)){ //1. get the symbol specs ResetLastError(); int errors=0; double symbolLimitDistance=(double)SymbolInfoInteger(targetSymbol,SYMBOL_TRADE_STOPS_LEVEL);errors+=GetLastError();ResetLastError(); int symbolDigits=(int)SymbolInfoInteger(targetSymbol,SYMBOL_DIGITS);errors+=GetLastError();ResetLastError(); double symbolMinVolume=(double)SymbolInfoDouble(targetSymbol,SYMBOL_VOLUME_MIN);errors+=GetLastError();ResetLastError(); double symbolMaxVolume=(double)SymbolInfoDouble(targetSymbol,SYMBOL_VOLUME_MAX);errors+=GetLastError();ResetLastError(); double symbolPoint=(double)SymbolInfoDouble(targetSymbol,SYMBOL_POINT);errors+=GetLastError();ResetLastError(); symbolLimitDistance*=symbolPoint; //2. if there are 0 errors if(errors==0){ userEntry=NormalizeDouble(userEntry,symbolDigits); userSL=NormalizeDouble(userSL,symbolDigits); userTP=NormalizeDouble(userTP,symbolDigits); //2A : Has the user provided a proper lot size ? if(userLotSize>=symbolMinVolume&&userLotSize<=symbolMaxVolume){ //2B : Has the user provided a proper entry price ? bool propperEntry=false; if(userEntry>0.0){ propperEntry=true; if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_SELL_STOP){ /*sell stop and buy limit the price must be away from the current price at least symbolLimitDistance points In this iteration we will not be extending the price to the valid position but we'll straight out reject it. You can correct it if you want but the user wont like it as they 100% are unaware of the broker limitation being there.(if it is) */ double mustBeBelow=TICK.bid-symbolLimitDistance; if(userEntry>=mustBeBelow){propperEntry=false;Print("2B:Invalid price 1");} } else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_BUY_STOP){ //same here but above price double mustBeAbove=TICK.ask+symbolLimitDistance; if(userEntry<=mustBeAbove){propperEntry=false;Print("2B:Invalid price 2");} } else{ propperEntry=false; Print("We don't sell these hot dogs here"); } //--- if the entry is propper if(propperEntry){ Print("2A+2B passed"); //--- stop loss checks bool propperSL=false; if(userSL>=0.0){ propperSL=true; if(userSL>0.0){ //for buy types the sl must be below the open price by distance if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_BUY_STOP){ double mustBeBelow=userEntry-symbolLimitDistance; if(userSL>=mustBeBelow){propperSL=false;Print("Invalid SL 1");} } //for sell types the sl must be above the open price by distance else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_SELL_STOP){ double mustBeAbove=userEntry+symbolLimitDistance; if(userSL<=mustBeAbove){propperSL=false;Print("Invalid SL 2");} } } } //--- stop loss checks end here //--- take profit checks bool propperTP=false; if(userTP>=0.0){ propperTP=true; if(userTP>0.0){ //for buy types the tp must be above the open price by distance if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_BUY_STOP){ double mustBeAbove=userEntry+symbolLimitDistance; if(userTP<=mustBeAbove){propperTP=false;Print("Invalid TP 1");} } //for sell types the tp must be below the open price by distance else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_SELL_STOP){ double mustBeBelow=userEntry-symbolLimitDistance; if(userTP>=mustBeBelow){propperTP=false;Print("Invalid TP 2");} } } } //--- take profit checks end here //--- valid stops if(propperSL&&propperTP){ Print("Passed TP + SL checks"); ResetLastError(); MqlTradeRequest req={}; MqlTradeResult res={}; req.type_filling=ORDER_FILLING_RETURN;//not req.action=TRADE_ACTION_PENDING;//not req.type_time=ORDER_TIME_GTC; req.symbol=targetSymbol; req.type=userType; req.price=userEntry; req.volume=userLotSize; req.sl=userSL; req.tp=userTP; req.deviation=100; req.expiration=0; req.magic=0; req.comment=""; if(OrderSend(req,res)){ Print("Done Ticket #ORDER "+IntegerToString(res.order)+" #DEAL "+IntegerToString(res.deal)); }else{ Print("Cannot send #"+IntegerToString(res.retcode)+" "+res.comment); } } //--- valid stops } //--- if the entry is propper ends here } //2B : ends here }else{Print("Wrong lot size");} //2A : ends here }else{Print("errors in fetching specs from broker");} }else{ Print("Cannot get tick"); } ExpertRemove(); //----------------------------------- }//ignore this its for testing }
thanks so much

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
hi guys i have this part of code , i call it from button
but when i try to run return me always 10013 i insert 0.10 lot size and stay so far from market price but nothing anyone can help me ? thanks