# OnTrade() is working weird and strange.

Hello. I wanna implement Martingale strategy in my EA, after each losing trade the risk is multiplied by 2. However, after the first losing trade the risk multiplies by 64 and the EA stop working after that. Can someone expain what might go wrong? It's very important for me. Thanks

```double risk = 0.01;

{
HistorySelect(0,TimeCurrent());
total1 = HistoryDealsTotal();
profit1=HistoryDealGetDouble(total1,DEAL_PROFIT);
if(profit1 < 0)
{
risk=risk*2;
}
else
if(profit1 >= 0)
{
risk = 0.01;
}
}```
Documentation on MQL5: Trade Functions / HistorySelect
• www.mql5.com

Launger :
Hello. I wanna implement Martingale strategy in my EA, after each losing trade the risk is multiplied by 2. However, after the first losing trade the risk multiplies by 64 and the EA stop working after that. Can someone expain what might go wrong? It's very important for me. Thanks

1. Insert the code correctly (using the button ). The first time I corrected your messages and pasted the code correctly.

2. Remember to reset the variable risk to its initial state

1. Insert the code correctly (using the button ). The first time I corrected your messages and pasted the code correctly.

2. Remember to reset the variable risk to its initial state

Thanks for correcting. The variable risk is supposed to be reset to initial state with, so after the profitable trade the risk set to 1 percent. But the problem is that it does not multiply by 2, but 8 instead.
```else
if(profit1 >= 0)
{
risk = 0.01;
}```

Launger # :
Thanks for correcting. The variable risk is supposed to be reset to initial state with, so after the profitable trade the risk set to 1 percent. But the problem is that it does not multiply by 2, but 8 instead.

It only depends on your knowledge of mathematics.

You are not listening to me and you are making the same mistake: you are not resetting the variable risk to its original state.

Add: By the way, you need to multiply not by '2', but by '2.0'!

OnTrade is called by multiple conditions.
sending, modifying or removing of a pending order; cancellation of a pending order with not enough of money or expiration; activation of a pending order; opening, adding or closing a position (or part of the position); modifying of the open position (change stops – Stop Loss and/or Take Profit).
Client Terminal Events - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
You multiply by 2 on every one, no just on trade closing.
Reason: