# Fixed amount of dollars

Hi, there! :)

I've generated a code with a programme but when I backtest the strategy, it calculates a fixed amount of lots. Instead, I'd like to backtest the strategy with a fixed amount of dollars. Would it be possible?

I've tried this to get the lots automatically, but doesn't work: "fixed amount of dollars"/PRICE_CLOSE.

I would really appreciate if you could help me solve the problem.

Thanks so much.

Testing trading strategies on real ticks
• www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.

metandres4: Hi, there! :) I've generated a code with a programme but when I backtest the strategy, it calculates a fixed amount of lots. Instead, I'd like to backtest the strategy with a fixed amount of dollars. Would it be possible? I've tried this to get the lots automatically, but doesn't work: "fixed amount of dollars"/PRICE_CLOSE. I would really appreciate if you could help me solve the problem.

Yes, it is possible, but if you don't know how to code and are using a generator to create your code, then we will not be able to help you much. Anything we explain or try to teach you, you will not be able to understand, much less implement.

But here goes anyway ,,,

how to calculate the maximum volume based on your balance

Fernando Carreiro, 2022.02.16 00:50

Risk can only be truely evaluated when you use some kind of stop-loss, be it real or virtual or just for risk evaluations. If you do not have any concept of a stop-loss in your strategy, even if not in some vague terms, then you can not truely calculate the maximum risk.

There are some traders that use a percentage of margin to calculate the volume, but that does not truly define a maximum risk, because you can still suffer great losses if your price change is too great (as there is no stop-loss).

So, get into the habit of calculating for some form of stop-loss or risk stop and combine it with margin calculations as well for extra protection.

How to calculate proper lot size ?

Fernando Carreiro, 2022.02.11 19:28

This is untested, uncompiled code, only to serve as a guide for you to further study and implement depending on your own requirements ...

```// Calculate Max Lot Size based on Maximum Risk
double dblLotsRisk( double dbStopLoss, double dbRiskRatio )
{
double
dbLotsMinimum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN       ),
dbLotsMaximum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX       ),
dbLotsStep     = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP      ),
dbTickSize     = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  ),
dbTickValue    = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE ),
dbValueAccount = fmin( fmin(
AccountInfoDouble( ACCOUNT_EQUITY      )  ,
AccountInfoDouble( ACCOUNT_BALANCE     ) ),
AccountInfoDouble( ACCOUNT_MARGIN_FREE ) ),
dbValueRisk    = dbValueAccount * dbRiskRatio,
dbLossOrder    = dbStopLoss * dbTickValue / dbTickSize,
dbCalcLot      = fmin(  dbLotsMaximum,                  // Prevent too greater volume
fmax(  dbLotsMinimum,                  // Prevent too smaller volume
round( dbValueRisk / dbLossOrder       // Calculate stop risk
/ dbLotsStep ) * dbLotsStep ) );       // Align to step value

return ( dbCalcLot );
};```

Code for Risk Management

William Roeder, 2021.02.20 17:58

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around \$10/pip but it takes account of the exchange rates of the pair vs. your account currency.)

3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

4. You must normalize lots properly and check against min and max.

5. You must also check FreeMargin to avoid stop out

Most pairs are worth about \$10 per PIP. A \$5 risk with a (very small) 5 PIP SL is \$5/\$10/5 or 0.1 Lots maximum.

Thanks so much for your help. I've been coding Pine Editor, so I understand it a bit. In fact, I've changed some parameters.

The fact is, it calculates lots according to max risk, but what I really need is a code that converts "X" amount of dollars into lots.

I used to use TSLab and the formula I used was "Math.round(20000/close)" and it gave me the amount of lots. Would it be possible to implement a math formula like this one in the "Lots" parameter?

return;

Thank you so much again.

metandres4 #: The fact is, it calculates lots according to max risk, but what I really need is a code that converts "X" amount of dollars into lots.

Then it seems that you did not understand or did not read the part about how the volume can only be calculated if you know your risk exposure.

You cannot convert "lots" into "money value", without also considering the "stop-loss" or something equivalent to it. The formula that you present is nonsensical.

Fernando Carreiro #:

Then it seems that you did not understand or did not read the part about how the volume can only be calculated if you know your risk exposure.

You cannot convert "lots" into "money value", without also considering the "stop-loss" or something equivalent to it. The formula that you present is nonsensical.

Yes! I got it! :)

If you divide the amount of USD you want to invest by the close price, it gives you the amount of lots.

So if you want to buy with \$30,000 and it's currently at \$15,000, it reads it has bought 2 lots.

I got it with iClose function (to get the close of the bar) and round(FixedAmountOfUSD/close).

I went to see the trades to check if the amount of lots were changing and yes, they do! The results were perfect. :D