How can open trade by using marketinfo() wtih a stoploss (''marketinfo(symbal,MODE_ASK)-stoploss*Point '')

 

hello, Dear all

I'm a new learner for MQL4 and now got problem with coding .

from this forum i found that trading multip pair in the same char is using Marketinfo(), it does work really, but can't open a trade with stoploss by coding ''''marketinfo(symbal,MODE_ASK)-stoploss*Point ''or ''''marketinfo(symbal,MODE_BID)+stoploss*Point '' . But a manual way is "marketinfo(symbal,MODE_ASK)-0.0020 " .

I have try on that way(marketinfo(symbal,MODE_BID)+stoploss*Point) for a dazen times,it still can not work .

you reply will be highly appricated .

int start()
  {
double TP, SL;
     double myBid, myAsk,myPoint;
RefreshRates();
myAsk = MarketInfo(symbol1,MODE_ASK);myPoint = MarketInfo(symbol1,MODE_POINT);
   SL = myAsk - StopLoss * myPoint;
   Print(MarketInfo(symbol1,MODE_POINT));
if (wannaBuy) 
{   
    int ticket1;
   ticket1 =OrderSend(symbol1,OP_BUY,LotSize,myAsk,Slippage,myAsk-0.002,0,0,0,Magicnumber1,Blue);
   if (ticket1 <0 )
   {
   Print ("OrderSend failed with error #", GetLastError());
   return(0);
   }
   wannaBuy = false;
   }

//----
   return(0);
  }

this way can solve the problem. but if the symbal become "USDJPY" OR "EURJPY",it must to shift 0.20 ..

 

Following your coding style:


int myDigits;

myDigits = MarketInfo(symbol1,MODE_DIGITS);

SL = NormalizeDouble(myAsk-StopLoss*myPoint,myDigits);


CB

 
cloudbreaker wrote >>

Following your coding style:

int myDigits;

myDigits = MarketInfo(symbol1,MODE_DIGITS);

SL = NormalizeDouble(myAsk-StopLoss*myPoint,myDigits);

CB

oh ~~ thank you so much for your idea . but I could not totally understand the mind of coding like this way .

especially, when coded as below, it can open the order with a stoploss,but the order shows me that buy GBPUSD at 1.62803-- stoploss at "0.0020"

int start()
  {
double TP, SL;
     double myBid, myAsk,myPoint;
     int myDigits;
RefreshRates();
myAsk = MarketInfo(symbol1,MODE_ASK);myPoint = MarketInfo(symbol1,MODE_POINT);
   myDigits = MarketInfo(symbol1,MODE_DIGITS);
   SL = NormalizeDouble(myAsk-StopLoss*myPoint,myDigits);
   Print(SL);
if (wannaBuy) 
{   
    int ticket1;
   ticket1 =OrderSend(symbol1,OP_BUY,LotSize,myAsk,Slippage,myAsk-SL,0,0,0,Magicnumber1,Blue);
   if (ticket1 <0 )
   {
   Print ("OrderSend failed with error #", GetLastError());
   return(0);
   }
   wannaBuy = false;
   }
It's being millons appreciation that you could give a perfect sample !!!
 

You really need to clean up your code..

int start()
{
   double TP, SL;
   double myBid, myAsk,myPoint;
   int myDigits;
   
   RefreshRates();
   
   /* If you're using a different symbol than current one, use this:
   myAsk = MarketInfo(symbol1,MODE_ASK);
   myPoint = MarketInfo(symbol1,MODE_POINT);
   myDigits = MarketInfo(symbol1,MODE_DIGITS);
   SL = NormalizeDouble(myAsk-StopLoss*myPoint,myDigits);
   */
   
   /* If you're using current symbol, use this: (it's always better to use a variable than a function when they have the same value) */
   myAsk = Ask;
   myPoint = Point;
   myDigits = Digits;
   SL = NormalizeDouble(myAsk-StopLoss*myPoint,myDigits);
   
   Print(SL);
   
   if(wannaBuy){   
      int ticket1;
      
      ticket1 = OrderSend(symbol1,OP_BUY,LotSize,myAsk,Slippage,myAsk-SL,0,0,0,Magicnumber1,Blue);
      
      if(ticket1 < 0 ){
         Print ("OrderSend failed with error #", GetLastError());
         return(0);
      }
      
      wannaBuy = false;
   }
}
Jon
 

In the code I gave you (which I structured to make it familiar to your own code) SL (as you can clearly see) already has already factored in the subtraction from the ask price.

Why would you then subtract it from the ask price?

SL plugs directly into the OrderSend() command.

Have you been reading any of the documentation or are you shooting in the dark with someone else's code?


CB

 
cloudbreaker wrote >>

In the code I gave you (which I structured to make it familiar to your own code) SL (as you can clearly see) already has already factored in the subtraction from the ask price.

Why would you then subtract it from the ask price?

SL plugs directly into the OrderSend() command.

Have you been reading any of the documentation or are you shooting in the dark with someone else's code?

CB

thanks for Jon and CB ... so i should directly coding as :

OrderSend(symbol1,OP_BUY,LotSize,myAsk,Slippage,SL,0,0,0,Magicnumber1,Blue)

right ?

 

any one pls help me


iam using 5 th digit broker

i use below command to print bid value in my chart


Comment("Slave BID : ",MarketInfo(Symbol(),MODE_BID));


but the result always getting 4 digit value


pls help me


Rgds

Mohammed Rafi kp

 
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
but the result always getting 4 digit value
Print defaults to 4
string  PriceToStr(double p){
    string pFrc = DoubleToStr(p, Digits);       if(Digits.pips==0) return(pFrc);
    string pPip = DoubleToStr(p, Digits-1);
    if (pPip+"0" == pFrc)       return(pPip);           return(pFrc);          }
string  DeltaToPips(double d){
    if (d > 0)  string sign = "+";  else    sign = "";
    double pips = d / pips2dbl;
    string dPip = sign + DoubleToStr(pips, 0);  if(Digits.pips==0) return(dPip);
    string dFrc = sign + DoubleToStr(pips, Digits.pips);
    if (dPip+".0" == dFrc)      return(dPip);           return(dFrc);          }
 
efmrafi786:


Comment("Slave BID : ",MarketInfo(Symbol(),MODE_BID));

but the result always getting 4 digit value

Use this: https://docs.mql4.com/convert/DoubleToStr

to do this . . .

Comment("Slave BID : ", DoubleToStr (MarketInfo(Symbol(),MODE_BID), Digits);

 
RaptorUK:

Use this: https://docs.mql4.com/convert/DoubleToStr

to do this . . .

Thanks, its working


i need to compare one bid value of with this bid ......... its retun 5th digit value but i can't compare difference



 

Show your code.

Comment only outputs doubles as 4 digits . . . to get more you convert to a string . . . if you want to compare the original double then use the original double . . not a string.