Help with Trade size for ordersend

 

Can anyone help?


I'm backtesting with 5 digit broker data. Have $100,000 starting balance.  


Does the equation for my tradesize seem right for using 1% of account balance as risk and 1.5x the ATR for a stop loss? Do I need to do anything to change the stop loss units because I'm using the ATR value ? I'm not concerned with the spread for my backtesting. I will add freemargin check later.  I read a bunch of posts here and I'm still not too sure. Some info on my first order:

AccountBalance: 100000.0, 1% of AB: 1000.0, DeltaValuePerLot: 99999.99999999999 1.5x ATR: 0.02680285714285714 lot step: 0.01 tradesize: 0.37

Free Margin: 98144.81 Account Free Margin Check: 96294.81 Margin Mode: 1 Account Lev: 20


double  DeltaValuePerLot(string pair="")
  {
   if(pair == "")
      pair = Symbol();
   return(MarketInfo(pair, MODE_TICKVALUE)
          / MarketInfo(pair, MODE_TICKSIZE));

  }

void CalcTradeSize()
{
   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   
tradesize = (AccountBalance()*0.01)/((1.5*(iATR(NULL,0,14,0)))*DeltaValuePerLot());

Print ("Tradesize before mathfloor: ", tradesize);

tradesize = MathFloor(MathMax(0,tradesize)/lotStep)*lotStep;

Print ("AccountBalance: ", AccountBalance()," 1% of AB: ", AccountBalance() * 0.01, " DeltaValuePerLot: ", DeltaValuePerLot(), " 1.5x ATR: ", 1.5*(iATR(NULL,0,14,0)), " lot step: ", lotStep, " tradesize: ", tradesize );

}
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
, then each symbol positions will be closed in the same order, in which they are opened, starting with the oldest one. In case of an attempt to close positions in a different order, the trader will receive an appropriate error. There are several types of accounts that can be opened on a trade server. The type of account on which an...
 
drinkyd:

Can anyone help?


I'm backtesting with 5 digit broker data. Have $100,000 starting balance.  


Does the equation for my tradesize seem right for using 1% of account balance as risk and 1.5x the ATR for a stop loss? Do I need to do anything to change the stop loss units because I'm using the ATR value ? I'm not concerned with the spread for my backtesting. I will add freemargin check later.  I read a bunch of posts here and I'm still not too sure. Some info on my first order:

AccountBalance: 100000.0, 1% of AB: 1000.0, DeltaValuePerLot: 99999.99999999999 1.5x ATR: 0.02680285714285714 lot step: 0.01 tradesize: 0.37

Free Margin: 98144.81 Account Free Margin Check: 96294.81 Margin Mode: 1 Account Lev: 20


Anyone?

 
drinkyd:

Anyone?

Hello,

the formula to calculate stop loss value per trade is:

Stop Loss value = 100 x 100,000 x Lot Size x (Stop Loss In Point * Tick Value) / Account Balance.


For example

Balance = 10,500

Pair = EURUSD

Type Order = Buy

Lot Size = 0.20

Open Price = 1.36150 

Stop Loss Price = 1.35600

Tick Value = 1.0


The risk is:

100 * 100,000 * 0.20 * ((1.36150 - 1.35600) * 1.0) / 10,500 = 200,000 * 0.0055  * 1.0 / 10,500 = 1.05%

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account. Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.
  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.

Reason: