Lot size

To add comments, please log in or register
Luciole
121
Luciole  

Hi everyone,

I aim to code something saying that only 80% of the position will be closed when reaching TP1.

I am scared that the value will not accepted by the broker.

Ho to tell the EA to choose a value around 80%..

Do you have any suggestions? or link to help me through this step.

Thanks for you help !

Luciole

Mohamad Zulhairi Baba
16042
Mohamad Zulhairi Baba  
Luciole:

Hi everyone,

I aim to code something saying that only 80% of the position will be closed when reaching TP1.

I am scared that the value will not accepted by the broker.

Ho to tell the EA to choose a value around 80%..

Do you have any suggestions? or link to help me through this step.

Thanks for you help !

Luciole

0.8 * currentLotSize, then normalize against minlot,maxlot.

alphatrading
87
alphatrading  
Mohamad Zulhairi Baba:

0.8 * currentLotSize, then normalize against minlot,maxlot.

That's not correct and produces invalid values as it ignores MODE_LOTSTEP.

Following is the correct approach in pure MQL4. After that you still need to check the returned value against violations of MODE_MINLOT/MODE_MAXLOT. However that's a different story:

/**
 * Round a lot size according to the specified symbol's lot step value (MODE_LOTSTEP).
 *
 * @param  double lots              - lot size
 * @param  string symbol [optional] - symbol (default: the current symbol)
 *
 * @return double - rounded lot size
 */
double NormalizeLots(double lots, string symbol = "") {
   if (!StringLen(symbol))
      symbol = Symbol();
   double lotstep = MarketInfo(symbol, MODE_LOTSTEP);
   return(NormalizeDouble(MathRound(lots/lotstep) * lotstep, 2));
}

Regards

PS: You not only use this function to calculate 80% of something. You also use it for calculating 100% (i.e. your initial order lotsize) as your money management (risk, leverage, SL) will almost always calculate "something" for you which doesn't match the broker's expectations.

Mohamad Zulhairi Baba
16042
Mohamad Zulhairi Baba  
alphatrading:

That's not correct and produces invalid values as it ignores MODE_LOTSTEP.

Following is the correct approach in pure MQL4. After that you still need to check the returned value against violations of MODE_MINLOT/MODE_MAXLOT. However that's a different story:

Regards

double NormLot(double lot) {
   double maxLot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
   double minLot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   double lotStep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

   double useLot  = minLot;
   if(lot>useLot) {
      if(lot>maxLot) useLot = maxLot;
      else useLot = floor(lot/lotStep)*lotStep;
   }
   return useLot;
}
but of course you need to loop OrderSelect to get CurrentLotSize first, 
then * 0.8
alphatrading
87
alphatrading  
Mohamad Zulhairi Baba:
Sorry, the usage of floor() is again wrong. I would expect somebody with a rating of 15k to be more exact.
Mohamad Zulhairi Baba
16042
Mohamad Zulhairi Baba  
alphatrading:
Sorry, the usage of floor() is again wrong. I would expect somebody with a rating of 15k to be more exact.

rating means nothing brother..
this is my approach, you can use ceil, round or whatever method you like.
i'm just try to help.

alphatrading
87
alphatrading  
Very appreciated. I understand your intention with the code to play safe. Thought about it before and came to the conclusion to not post that part as it depends on the user's context. Especially in the context of this thread (I want to close 80% of an open position) the safe way to play would indeed be to round up. But as we both know that's not correct in all cases either. So, I left this to the user to decide. We just need to make him aware of the fact that rounding up/down cannot be a general advice as it heavily depends on context.
Luciole
121
Luciole  

I really want to thank you both for your help.

I do not have enough experience to fully understand the problem,

but I will work on it this week-end in order to understand the situation.

Have a great day !

Luciole

nicholi shen
1916
nicholi shen  

You could also use the CDouble library to make your life easier. 


double tp_lots = CDouble::RoundToLots(OrderLots() * 0.80);
alphatrading
87
alphatrading  
Luciole:

I really want to thank you both for your help.

I do not have enough experience to fully understand the problem,

but I will work on it this week-end in order to understand the situation.

Have a great day !

Luciole

The issue becomes important the smaller the lot sizes are you trade. Imagine you have an open position of 0.08 lot and want to close 80% => 0.08 * 0.8 = 0.064. Your broker will only accept 0.06 or 0.07, not the exact calculated value. The question now is: To be on the safe side do you have to round your calculation result up or do have to round it down? Or will it not matter?
  • If you want to close 80% the safe way would probably be to round up, so you close 0.07 lots.
  • If you have no open position, intend to open a new one and your money management calculates 0.064 lots you probably want to round down, so you only open 0.06 lots.

This is what I mean with context. Usually people don't want to increase their intended risk, so they round in the direction of the smaller risk. This rounding direction cannot be known without knowing the trader's context. Are you opening a position (you increase your overall risk) or are you closing (part of) a position (you lower your overall risk)?

Imagine your lotstep is 0.1, you have an open position of 0.8 lots and  want to close 70%. What do you do?

I hope that explains it better.
Luciole
121
Luciole  

Good morning everyone


thanks for your help

the CDouble library seems to be very easy to use.


I have a question. what is lotStep?

why do you divide the lot by the lotStep and then multiply by the lot step again?

I understand that mathfloor() or mathround() only applied the first division

how much will be lot step if i want to close 80%? 

A short example with number will definitely help..


I may ask a lot, sorry and thanks for your help

Have a great Monday

Luciole

123
To add comments, please log in or register