Not enough money - help me understand the basic math

 

Before any solution code, pls help me understand the basic math. I'm back testing on a $1000 CAD Account trading EURUSD. My broker is AvaTrade and for CAD account trading EURUSD, they list 4.5% as margin: 

https://www.avatrade.ca/trading-info/financial-instruments-index/forex

I want to trade 0.06 lots. So it is 0.06*100,000 = 6,000 units, which should cost around 7,320 USD (6,000 * 1.22 EURUSD). Converted to CAD around 1.3 USDCAD, around 9,516 CAD. Then 4.5% of that is 428 CAD. 

This is the error:

2023.07.07 19:31:02.047 2021.01.04 07:10:00   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1000.00, Margin: 1681.28, FreeMargin: -681.28

2023.07.07 19:31:02.047 2021.01.04 07:10:00   CTrade::OrderSend: market buy 0.06 EURUSD sl: 1.22484 tp: 1.22780 [not enough money]

Having a hard time understanding where these numbers come from above ?  Margin: 1681.28, FreeMargin: -681.28

Thanks. 
Forex - Trading Conditions & Charges {+Live Graphs} | AvaTrade
  • www.avatrade.ca
The FX Fixed Trading Conditions display the Standard Bid-Ask Spread (Pips) for FX Instruments unless otherwise stated. Standard Spreads are as stated under Normal Market Conditions. Spreads can widen depending on market conditions up to a maximum of Standard Spread x3 (Triple). Spread Cost Formula: Spread x Trade Size = Spread Charge in...
 
Algo T:

Before any solution code, pls help me understand the basic math. I'm back testing on a $1000 CAD Account trading EURUSD. My broker is AvaTrade and for CAD account trading EURUSD, they list 4.5% as margin: 

https://www.avatrade.ca/trading-info/financial-instruments-index/forex

I want to trade 0.06 lots. So it is 0.06*100,000 = 6,000 units, which should cost around 7,320 USD (6,000 * 1.22 EURUSD). Converted to CAD around 1.3 USDCAD, around 9,516 CAD. Then 4.5% of that is 428 CAD. 

This is the error:

2023.07.07 19:31:02.047 2021.01.04 07:10:00   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1000.00, Margin: 1681.28, FreeMargin: -681.28

2023.07.07 19:31:02.047 2021.01.04 07:10:00   CTrade::OrderSend: market buy 0.06 EURUSD sl: 1.22484 tp: 1.22780 [not enough money]

Having a hard time understanding where these numbers come from above ?  Margin: 1681.28, FreeMargin: -681.28

Thanks. 
Margin is calculated according to the formulas listed here:

https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants#enum_symbol_calc_mode

These 4.5% you are using is the margin rate. What you are missing is the leverage. According to your calculations, I guess your leverage is 1:30. But not sure about that.

Anyways, your calculations are missing the leverage.