Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 740

 
evillive:

Thanks, it's already been explained to me :D

It is clear that for myself I write with all the checks, as it should be, there was just an outline of how to bring the lot to balance/10 ratio, but the idea was unsuccessful...

But you have a lot of text, it could be simpler:

  lotstep= MarketInfo(Symbol(),MODE_LOTSTEP);
  lotmax=MarketInfo(Symbol(), MODE_MAXLOT);
  lotmin=MarketInfo(Symbol(), MODE_MINLOT);

lot=lotstep*MathRound(AccountBalance()*0.001/lotstep);
if(lot < lotmin) lot = lotmin;
if(lot > lotmax) lot = lotmax;

It could be simpler. Only, what will happen if MarketInfo() for lotstep returns 0? What will be the consequences? The Expert Advisor will be stopped and positions will be abandoned?

And if MINLOT is set by broker to 0.03 and LOTSTEP to 0.02, i.e., the first two allowed lot values are 0.03 and 0.05, and the current balance is 41 - what will be the value of calculated lot in your code?

Why, if the lot is less than minimal value - for example, with a balance equal to 1, it will be equal to 0 after the expression is fully calculated - why in this case your code takes the lot equal to MINLOT, which can be 0.03, i.e. 30 times more than the "raw" value of "AccountBalance()*0.001", which in this case is equal to 0.001?

You can't always get a valid lot value, because conditions may be such that NONE of the valid lot values corresponds to them. Erroneous situations must all be programmed, and it must be remembered that this is at least 90% of the job.

The applied model set by MetaQuotes is that lot = MINLOT + N * LOTSTEP, where N = 0, 1, 2... M. And M is such that MINLOT + M * LOTSTEP <= MAXLOT and MINLOT + (M + 1) * LOTSTEP > MAXLOT. Accordingly, you should also calculate within the set model, i.e., lot = MINLOT + N * LOTSTEP. Then all sorts of awkward combinations of MINLOT and LOTSTEP become unimportant, because the formula works in a general way. Once you've written it properly, debugged it properly and concentrated on something else interesting and fun. And with pleasure - because you don't have to constantly come back to badly written code because again this code doesn't work, but only glitches...

 
simpleton:

It could be simpler. But what will happen if MarketInfo() returns 0 for lotstep? What will be the consequences? The Expert Advisor will be stopped and positions will be abandoned?

If the value of MINLOT is set by a broker to 0.03, and LOTSTEP to 0.02, i.e., the first two lot values are 0.03 and 0.05, and the current balance is 41, what is the value of calculated lot in your code?

Why, if the lot is less than minimal value - for example, with a balance equal to 1, after the complete calculation it will be equal to 0? Why in this case your code takes the lot equal to MINLOT, which can be, for example, 0.03, which is 30 times more than the "raw" value "AccountBalance()*0.001", which in this case is equal to 0.001?

You can't always get a valid lot value, because conditions may be such that NONE of the valid lot values corresponds to them. Erroneous situations must all be programmed, and it must be remembered that this is at least 90% of the job.

The applied model set by MetaQuotes is that lot = MINLOT + N * LOTSTEP, where N = 0, 1, 2... M. And M is such that MINLOT + M * LOTSTEP <= MAXLOT and MINLOT + (M + 1) * LOTSTEP > MAXLOT. Accordingly, you should also calculate within the set model, i.e., lot = MINLOT + N * LOTSTEP. Then all sorts of awkward combinations of MINLOT and LOTSTEP become unimportant, because the formula works in a general way. Once you've written it properly, debugged it properly and concentrated on something else interesting and fun. And with pleasure - because you don't have to constantly come back to badly written code because again this code doesn't work, but only glitches...

For such horrors, there's also a check for sufficiency of funds to open with such a lot. No one in his right mind would allow a robot to deposit 10 quid knowing that the min/lot is 0.1 and the step is 0.1, for example.

And if such a clever one were found, the robot would tell him "give me the money and goodbye in the meantime" :D.

And I've never seen or read anywhere that the lot step is zero, it's a fantasy...

 
evillive:

For such horrors, there is also a check on the sufficiency of funds to open with such a lot. No one in their right mind would allow a robot to deposit 10 quid, knowing that the min. lot is 0.1 and the step is 0.1 for example.

And if such a clever one is found, the robot will tell him "give me the money, but in the meantime, say goodbye" :D

To avoid zeros from MarketInfo in the tester, I do this:

  double AFM = AccountFreeMargin(); 
  double StopLevel,FreezeLevel,spr,prot;
  if(IsOptimization() || IsTesting() || IsVisualMode())
  {
    MinLot = 0.1;
    LotStep = 0.01;
    spr = Point*10;
    StopLevel = spr*2;
    FreezeLevel = spr*2;
  }
  else
  {
    MinLot = MarketInfo(Symbol(),MODE_MINLOT);
    LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
    spr = MarketInfo(Symbol(),MODE_SPREAD)*Point;
    StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
    FreezeLevel = MarketInfo(Symbol(),MODE_FREEZELEVEL)*Point;
  }
  if(MM == 1) 
  lot = MinLot+MathMax(LotStep*MathFloor((AFM-initdepo)*risk/100),0); 
  else if(MM == 0) lot = MinLot;
 
evillive:

For such horrors, there is also a check on the sufficiency of funds to open with such a lot. No one in their right mind would allow a robot to deposit 10 quid knowing that the minimum lot is 0.1 and the increment is 0.1, for example.

And if such a clever one were found, the robot would tell him "Give me the money and goodbye" :D

And I've never seen or read anywhere that the lot step is zero, it's an imagination...

Why is it terrible? Because it doesn't fit into the simplified model?

I meant that with MINLOT = 0.03 and LOTSTEP = 0.02 the first two allowed lot values are 0.03 and 0.05. That said, if the balance is, say, 41, then your code:

lot=lotstep*MathRound(AccountBalance()*0.001/lotstep);

Will give a lot value of 0.04, which is not allowed. The robot will say something like "the server said my lot size is wrong".

And zero lot size can be obtained when an error occurs when requesting its value through MarketInfo(). I gave you the code on how to cause this error artificially:

Print("MarketInfo(\"Фигня\", MODE_LOTSTEP) = ", MarketInfo("Фигня", MODE_LOTSTEP));

It returns 0. What kind of fantasy is it?

 
simpleton:

Why the horror? Because it doesn't fit into the simplified model?

I meant that with MINLOT = 0.03 and LOTSTEP = 0.02, the first two allowed lot values are 0.03 and 0.05. That said, if the balance is, say, 41, then your code:

Will give a lot value of 0.04, which is not allowed. The robot will say something like "the server said my lot size is wrong".

And zero lot size can be obtained when an error occurs when requesting its value through MarketInfo(). I have given you the above code on how to artificially induce this error:

It will return 0. So what's the fantasy?

lot=lotstep*MathRound(AccountBalance()*0.001/lotstep);

This formula is easily corrected to meet the requirements. You just need to consider the minimum lot

It will look a little different

lot=Min_Lot+lotstep*MathRound((AccountBalance()-X)*0.001/lotstep);

Where X - Balance for opening of minimal lot,

But the check for the minimum lot will still be needed.

 

Good afternoon!

Help an ignoramus ) In the tester no orders open error 10013 Wrong request, look at the code where the error is.

All the numbers in the journal are displaying on the order and the order does not open.

2014.10.19 12:14:34.984 2014.02.03 00:06:00 failed request buy 0.10 at 102.31100 sl: 102.27200 tp: 102.33600 [Invalid request]

2014.10.19 12:22:17.928 2014.02.03 00:06:00 ticket =0 retcode =10013

MqlTradeRequest Req;
MqlTradeResult  Res;
   string TradeSmb;
   if(TradeSmb=="") TradeSmb=_Symbol;
   Req.action=TRADE_ACTION_DEAL;
   Req.symbol=TradeSmb;
   Req.volume=0.1;
   Req.type=ORDER_TYPE_BUY;
   Req.type_filling=ORDER_FILLING_IOC;
   Req.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Req.sl= NormalizeDouble(sym_bid - (StopLoss*sym_point),sym_digits);
   Req.tp= NormalizeDouble(sym_ask + (TakeProfit*sym_point),sym_digits);
   Req.deviation=3;
   Req.comment="777";
if(OrderSend(Req,Res))
{
      Print("Sent...");
}
      Print("ticket =",Res.order,"   retcode =",Res.retcode);
      if(Res.order!=0)
     {
      datetime tm=TimeCurrent();
      //--- request all history
      HistorySelect(0,tm);
      string comment;
      bool result=HistoryOrderGetString(Res.order,ORDER_COMMENT,comment);
      if(result)
        {
         Print("ticket:",Res.order,"    Comment:",comment);
        }
Drummer has corrected your message
 

Where do you see that?!

if(OrderSend(Req,Res))

See the documentation! And use the SRC button to insert the code!

If you're too lazy, see the 1st and last time:

int  OrderSend(
   string   symbol,              // символ
   int      cmd,                 // торговая операция
   double   volume,              // количество лотов
   double   price,               // цена
   int      slippage,            // проскальзывание
   double   stoploss,            // stop loss
   double   takeprofit,          // take profit
   string   comment=NULL,        // комментарий
   int      magic=0,             // идентификатор
   datetime expiration=0,        // срок истечения ордера
   color    arrow_color=clrNONE  // цвет
   );

It's never a bad thing to learn!

 

Dear Sirs!

Please advise.....

Here is a function

OrderProfit( )

This function returns the net profit value for the selected order.

QUESTION

Which function, similarly to the above mentioned function, returns the loss value for the selected order instead of the profit value.

I.e. if an order closed on SL with a loss of $150, i need the function to returna loss of $150.

Thank you.

 
solnce600:

Dear Sirs!

Please advise.....

Here is a function

OrderProfit( )

This function returns the net profit value for the selected order.

QUESTION

Which function, like the above function, returns a loss value for the selected order instead of a profit.

I.e. if an order closed on SL with a loss of $150, i need the function to returna loss of $150.

Thank you.

It is the same! Only the amount will come out with a minus.
 
borilunad:
It's the same! Only the amount will come out with a minus.
Thank you.
Reason: