261

Dear Expert,

In the attached EA, there are only 3 lines containing KL:

input double KL   = 2;    // увеличение лота

if(OrderProfit()<0) lot=OrderLots()*KL;

if(profit<0) lot=LastLot*KL;

How come when KL is given a value like 1, 2 or 3, the EA works fine; but if KL is given a value like 1.3, 1.5, etc. the EA become abnormal?  What can I change so that the EA works still with KL set to 1.3, 1.5, etc?

Best,

Jim

Files:
MT45.mq5 11 kb
1898

EAgreat:

Dear Expert,

In the attached EA, there are only 3 lines containing KL:

input double KL   = 2;    // увеличение лота

if(OrderProfit()<0) lot=OrderLots()*KL;

if(profit<0) lot=LastLot*KL;

How come when KL is given a value like 1, 2 or 3, the EA works fine; but if KL is given a value like 1.3, 1.5, etc. the EA become abnormal?  What can I change so that the EA works still with KL set to 1.3, 1.5, etc?

Best,

Jim

It's because the lots function fails to return the correctly rounded value. It should instead do this.

```double Lot()
{
double lot=LT;
//---  MQL4
#ifdef __MQL4__
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()>0) lot=LT;
if(OrderProfit()<0) lot=OrderLots()*KL;
}
#endif

//---  MQL5
#ifdef __MQL5__
if(HistorySelect(0,TimeCurrent()))
{
double profit=HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_PROFIT);
double LastLot=HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_VOLUME);
if(profit>0) lot=LT;
if(profit<0) lot=LastLot*KL;
}
#endif

if(lot>ML)lot=LT;
double step = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
return step * floor(lot / step);
}```
261

nicholishen:

It's because the lots function fails to return the correctly rounded value. It should instead do this.

Dear Expert Nicholishen,

Thank you so much for your advice!  I will put your correction lines into the EA and report the result soon!

Merci Beaucoup!

Jim

17942

`if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))`
1. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

2. Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum
261

nicholishen:

It's because the lots function fails to return the correctly rounded value. It should instead do this.

Dear Mr. Nicholishen,

When I set KL to 1.5, I expect

position 1 lot: 0.01

position 2 lot: 0.02 (0.01x1.5)

position 3 lot: 0.02 (0.02x1.5)

position 4 lot: 0.03 (0.02x1.5)

position 5 lot: 0.05 (0.03x1.5)

position 6 lot: 0.08 (0.05x1.5)

position 7 lot: 0.11 (0.08x1.5)

etc.

Your correction makes all position lots to be 0.01.  The lots do not increase by KL.  Could you kindly further advise me?

Many thanks.

Jim

261

whroeder1:
1. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

2. Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

Dear whroeder1,

Thank you for your advice.  I am no programmer and I don't know how to turn your advice to mt5 language to solve my problem.  Could you please advise me the way nicholishen does?

Many thanks.

Jim

17942

EAgreat: I am no programmer and I don't know how
You have only four choices:
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
No free help
urgent help.
1898

EAgreat:

Dear Mr. Nicholishen,

When I set KL to 1.5, I expect

position 1 lot: 0.01

position 2 lot: 0.02 (0.01x1.5)

position 3 lot: 0.02 (0.02x1.5)

position 4 lot: 0.03 (0.02x1.5)

position 5 lot: 0.05 (0.03x1.5)

position 6 lot: 0.08 (0.05x1.5)

position 7 lot: 0.11 (0.08x1.5)

etc.

Your correction makes all position lots to be 0.01.  The lots do not increase by KL.  Could you kindly further advise me?

Many thanks.

Jim

That's because the floor function in this formula rounds down to the nearest lot-step. Rounding down to step ensures that you will not exceed your calculated risk. This is only really an issue when working with micro lots... if you want to round to the nearest lot-step change floor to round.

261

whroeder1:
You have only four choices:
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
No free help
urgent help.

Dear whroeder1,

Thanks for pointing out the ways.

Jim

261

nicholishen:

That's because the floor function in this formula rounds down to the nearest lot-step. Rounding down to step ensures that you will not exceed your calculated risk. This is only really an issue when working with micro lots... if you want to round to the nearest lot-step change floor to round.

Dear Nicholishen,

Very grateful to you for your guidance!  After I change "floor" in your lines to "round", the EA behaves as expected now.

Deeply appreciate your teaching and helping!

Best,

Jim