# Calculate how many lots to close in order to get a certain amount of profit/loss ?

Hello,

I am building my EA with the purpose of "not closing" positions in loss.

When the Close Conditions are met :

a) If the current chain of trades is in profit than close in profit.

b) If the current chain of trades is in loss, than hedge the loss by an opposite position with volume = to the sum of all the trades chain.

The hedged positions will wait for new position to close in profit, and with the amount of profit, close partially the trades hedged, to have ideally a net profit close to 0.

Example:

Current Trade Chain just closed 1 lot in profit of 30\$.

Hedged Trades has in total 30 lots opened (only by one side, not taking in account the position which is opposite of the trade chains) with a current stable loss of 300\$.

This in the SAME SYMBOL.

My question is:

How can I calculate accurately the amount of volume i can close from the Hedged Trades to close an amount as close as possible to 30\$ (which is the profit I just made)?

This is my solution, please let me know if you think is correct, seems to me it's not so accurate:

(Profit_Current_Trade_Chain / Current_hedged_loss) * hedged_total_volume   ===> volume_to_close_from_hedged_trades

In the previous Example:

(30/300)*30 = 3 lots to close to get a net profit of 0 \$

```      double lots_toclose = MathFloor(((PNL_current/MathAbs(PNL_Hedge))*hedge_volume)/SymbolInfoDouble(act_symbol,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(act_symbol,SYMBOL_VOLUME_STEP);

if(lots_toclose < SymbolInfoDouble(act_symbol,SYMBOL_VOLUME_STEP))
return;

Print("Pnl CHAIN: ", DoubleToString(PNL_current,2), " PNL HEDGE: ", DoubleToString(PNL_Hedge,2), " HEDGE VOLUME: ", DoubleToString(hedge_volume,2), " LOTS CALCULATED TO CLOSE: ", DoubleToString(lots_toclose,2));```

Please see the following for the maths needed to calculate the "net" outcome for a basket of positions ...

Forum on trading, automated trading systems and testing trading strategies

Looking for indicator that gives the info tp of the globality of open trades please

Fernando Carreiro, 2022.10.12 16:05

No such indicator exists as far as I known. It will have to be coded (use the Freelance section), but it will be easier to just have your existing EA be modified to show that target on the chart using the following maths:

• vi = volume of individual position
• oi = open price of individual position
• ci = close price of individual position
• Vn = total volume for a basket of positions
• On = net mean open price for a basket of positions
• Cn = net mean close price for a basket of positions
• PLn = profit/loss for a basket of positions

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 16:51

Instead of using the Tick Value directly, consider using the function OrderCalcProfit, because it will apply the correct profit calculation method depending on the CALC_MODE for that symbol.

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

```// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
* dbLotsStep, dbLotsMin ), dbLotsMax );

// the rest of the code ...
};```

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

```// Variables for Symbol Volume Conditions
double
dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );

// Variables for Geometric Progression
double
dblGeoRatio = 2.8,
dblGeoInit  = dblLotsMinimum;

// Calculate Next Geometric Element
double
dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );

// Adjust Volume for allowable conditions
double
dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
fmax( dblLotsMinimum,                                   // Prevent too smaller volume
round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value```

Forum on trading, automated trading systems and testing trading strategies

Volume Limit Reached - Validation for new Expert Advisor error

Fernando Carreiro, 2022.07.22 18:22

Your EA must be coded to read the broker's contract specifications, such volume limitations, and prevent that from happening.

 SYMBOL_VOLUME_MIN Minimal volume for a deal double SYMBOL_VOLUME_MAX Maximal volume for a deal double SYMBOL_VOLUME_STEP Minimal volume change step for deal execution double SYMBOL_VOLUME_LIMIT Maximum allowed aggregate volume of an open position and pending orders in one direction (buy or sell) for the symbol. For example, with the limitation of 5 lots, you can have an open buy position with the volume of 5 lots and place a pending order Sell Limit with the volume of 5 lots. But in this case you cannot place a Buy Limit pending order (since the total volume in one direction will exceed the limitation) or place Sell Limit with the volume more than 5 lots. double

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.06.02 01:14

Here are two examples from AMP Global (Europe):

• Micro E-mini S&P 500 (Futures): point size = 0.01, tick size = 0.25, tick value = \$1.25
• EURO STOXX Banks (Stock Index): point size = 0.01, tick size = 0.05, tick value = €2.50

Forum on trading, automated trading systems and testing trading strategies

NormalizeDouble not "working"

Fernando Carreiro, 2017.09.23 00:41

Just as a side note.

Initially, when I started with MQL, I would manipulate prices as "doubles". Nowadays, especially in the more complex EA's, I manipulate prices as "ints". At the very first opportunity, I convert a price into ticks:

`int priceTicks = (int) round( price / tickSize );`

From then on, all my calculations and manipulations are done with "ints". Not only is it more memory compact and much faster, but comparisons are much easier to handle.  Doing a "priceA == priceB" for "doubles" is quite problematic, but not for "ints" because it gives exact matches. Not to mention, that in this way prices, stop sizes, etc. are ALWAYS aligned.

Then, just before I have to place or modify an order, I then convert it back:

`priceTicks * tickSize`
EDIT: I do the same for volume/lots by using the broker's Lot-Step.

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.05.18 21:05

```double
dbTickSize   = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_SIZE  ), // Tick size
dbTickValue  = SymbolInfoDouble( _symbol, SYMBOL_TRADE_TICK_VALUE ), // Tick value
dbPointSize  = SymbolInfoDouble( _symbol, SYMBOL_POINT ),            // Point size
dbPointValue = dbTickValue * dbPointSize / dbTickSize;               // Point value
```
Remember, it's best to use tick size and tick value in your calculations, instead of point size and its value.

Forum on trading, automated trading systems and testing trading strategies

Tick size vs Point(), can be a little tricky in Multicurrency EA

Fernando Carreiro, 2022.03.09 12:11

Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.

Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

```...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
return( round( price / tick_size ) * tick_size );
};```

The simplest way I think is:
Volume_to_close = (-30 - OrderCommision() - OrderSwap()) / ((Bid_Or_Ask - OrderOpenPrice())  * Tickvalue)
Reason: