Cost of Trade/Margin/SpreadBetting Help?

 

Hi Guys,


Firstly, thank you for taking the time to read this post and hopefully answer, I'm fairly new to building Mql4 and am struggling with some understanding of how much of my balance gets used to place a trade.

So I've a balance of 4,108.95, I'm trading the US30 and looking to risk 205.45 of my total balance (5%). Ignore the risk management strategies etc here i'm just looking to get the system to risk 205.45 of the account balance but cannot get a formula to work it out exactly.

After each trade my program spits out an excel file so I can see what its doing properly. To calculate the lots used I use the following code:

   MyAccountBal = AccountBalance();
   double ContractSize = MarketInfo(SymbolIn, MODE_LOTSIZE);
   ValuetoRisk=MyAccountBal*(Risk/100);  

TradePrice =MarketInfo(TradeSym,MODE_ASK);


   double lots;
   lots=ValuetoRisk/((TradePrice-StopLossIn)*ContractSize);

The output from my excel File is: 


TradeSym=|US30
TradeCmd=|0
TradeVol=|1.187557803468008
TradePrice=|24150.2
TradeSlippage=|1000
TradeStopLoss=|24132.80000000001
TradeTakeProfit=|0
TradeComment=| 
TradeMagic=|24031350
TradeMarginLot=|121
TradeSpread=|24
TradeStop=|150
Trade Value to Risk=|205.4475
Cost of this Trade=|171.1000000000004
(TotalBal-FreeMargin)=|171.1000000000004
AccStopoutMode=|0
AccStopoutLevel=|50
SwapsTripleDay=|Wednesday
ProfitCurrency=|USD
account_currency=|USD
ProfitCalcMode=|1
AccountBalance=|4108.95
AccountPreviousFreeMargin=|4108.95
AccountFreeMargin=|3937.849999999999
AccountLeverage=|200
LotSizeBaseCurr=|10
TickSize=|0.1
TickPrice=|1
MinLot=|0.01
MaxLot=|30
LotStep=|0.01
LotStep_digits=|0
MarginHedging=|1
LotSize=|10


I have no idea why it's risking 171 and not 205.44? The 171 is calculated by simply taking the AccountFreeMargin prior to the trade and then comparing it to the AccountFreeMargin directly after the trade has been place.

If one of you genius coders could point me in the right direction I'd be eternally grateful! I'm looking to trade Forex, Equities and Indicies so I believe the formula is different for each... I just cant get it to work on Indicies at all.

P.s. I will purely be spreadbetting using Ig Index and not buying full shares or options etc.


Thank you in advance.

 
 lots=ValuetoRisk/((TradePrice-StopLossIn)*ContractSize);
Not Contract size but the value of a lot.
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    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. Account Balance * 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.
    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=0.1 Lots maximum.
  2. Use a GUI EA like mine (for MT4): Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 6
 
whroeder1:
Not Contract size but the value of a lot.
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    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. Account Balance * 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.
    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=0.1 Lots maximum.
  2. Use a GUI EA like mine (for MT4): Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 6

Thank you for this so much, this is very useful indeed. However I'm still very confused as to how to calculate it with what numbers? I think it's mostly because there is very different terminology used and most of which I don't actually understand what values people are actually talking about?

if we take the US30 and use the trade I have made above as a example so we can use real numbers and I can see exactly where it's coming from, can you point me in the direction of what DeltaPerLot means? I would usually pay the commission via the spread on a spreadbet wouldn't I? So would I have to take into account the actual price of the entity minus the bid price? (if buying)

Account balance Risk % RISK OrderLots OrderOpenPrice OrderStopLoss DeltaPerLot CommissionPerLot ExchangeRates
4108.95 0.05 205.4475 1.187557 24150.2 24132.8 ? ? ?
17.4
Need to get back to this value
171.1


So Account Balance * percent/100 = RISK is simple.. I get that bit completely. But I cannot get numbers to come back to 171.10...

I do believe it has something to do with exchange rates as I'm in UK and the US30 is traded in its base currency of USD.... but I'm still not working back to 171.10. GBP/USD is about1.4 but the calc between 205.4475 and 171.10 is 1.2 approx.

Again I do realise to an experienced person some of these questions sound very stupid but I'm lost in a lost of terminology I cant seem to get my head around here.... (my background is i'm an accountant and a computer programmer - I've studied the markets for over 9 years but now programming in MQL4 some of the terms have thrown me completely)

 
NashCo:

Hi Guys,


Firstly, thank you for taking the time to read this post and hopefully answer, I'm fairly new to building Mql4 and am struggling with some understanding of how much of my balance gets used to place a trade.

So I've a balance of 4,108.95, I'm trading the US30 and looking to risk 205.45 of my total balance (5%). Ignore the risk management strategies etc here i'm just looking to get the system to risk 205.45 of the account balance but cannot get a formula to work it out exactly.

After each trade my program spits out an excel file so I can see what its doing properly. To calculate the lots used I use the following code:

   MyAccountBal = AccountBalance();
   double ContractSize = MarketInfo(SymbolIn, MODE_LOTSIZE);
   ValuetoRisk=MyAccountBal*(Risk/100);  

TradePrice =MarketInfo(TradeSym,MODE_ASK);


   double lots;
   lots=ValuetoRisk/((TradePrice-StopLossIn)*ContractSize);

The output from my excel File is: 


TradeSym=|US30
TradeCmd=|0
TradeVol=|1.187557803468008
TradePrice=|24150.2
TradeSlippage=|1000
TradeStopLoss=|24132.80000000001
TradeTakeProfit=|0
TradeComment=| 
TradeMagic=|24031350
TradeMarginLot=|121
TradeSpread=|24
TradeStop=|150
Trade Value to Risk=|205.4475
Cost of this Trade=|171.1000000000004
(TotalBal-FreeMargin)=|171.1000000000004
AccStopoutMode=|0
AccStopoutLevel=|50
SwapsTripleDay=|Wednesday
ProfitCurrency=|USD
account_currency=|USD
ProfitCalcMode=|1
AccountBalance=|4108.95
AccountPreviousFreeMargin=|4108.95
AccountFreeMargin=|3937.849999999999
AccountLeverage=|200
LotSizeBaseCurr=|10
TickSize=|0.1
TickPrice=|1
MinLot=|0.01
MaxLot=|30
LotStep=|0.01
LotStep_digits=|0
MarginHedging=|1
LotSize=|10


I have no idea why it's risking 171 and not 205.44? The 171 is calculated by simply taking the AccountFreeMargin prior to the trade and then comparing it to the AccountFreeMargin directly after the trade has been place.

If one of you genius coders could point me in the right direction I'd be eternally grateful! I'm looking to trade Forex, Equities and Indicies so I believe the formula is different for each... I just cant get it to work on Indicies at all.

P.s. I will purely be spreadbetting using Ig Index and not buying full shares or options etc.


Thank you in advance.

NashCo, have you trade on the Demo platform for long enough?

If not! do not trade on the live account with that large balance. It is vital to learn and understand the market.

If you have traded on the demo account before, you were probably going to figure out a risk calculation to be used on your balance.

Well if that account balance is on demo keep practicing, till you are satisfyed with your learning result.

 
Chris Mukengeshayi:

NashCo, have you trade on the Demo platform for long enough?

If not! do not trade on the live account with that large balance. It is vital to learn and understand the market.

If you have traded on the demo account before, you were probably going to figure out a risk calculation to be used on your balance.

Well if that account balance is on demo keep practicing, till you are satisfyed with your learning result.

Hi Chris,


Yes don't worry I have no intention of trading at all live until my system is built and tested over a 3-6 month period of time. Until then i'll be running purely on demo account... thank god for demo accounts!!

 
NashCo:

Hi Chris,


Yes don't worry I have no intention of trading at all live until my system is built and tested over a 3-6 month period of time. Until then i'll be running purely on demo account... thank god for demo accounts!!

No problem, Good luck and all the best.

 
whroeder1:
Not Contract size but the value of a lot.
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    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. Account Balance * 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.
    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=0.1 Lots maximum.
  2. Use a GUI EA like mine (for MT4): Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 6

Thank you Whroeder1, after re-reading this several times it's starting to sink in. I've been chasing my tail a little and trying to calculate a stupid value!!

The risk I was asking is exactly what I was doing but I hadn't really understood it in my head properly.


Thank you all again

Reason: