- Collateral Symbols - For Advanced Users - Trading Operations
- Margin Calculation: Retail Forex, Futures - For Advanced Users - Trading Operations
- Quotes - MetaTrader 5 for Android
make the highest probability trades be a higher/bigger lotsize.
I most often use a fibonacci multiplier, however, it might be wiser if I used a some multiplier of margin required for that symbol to open a minimum trade.
dont make it more complicated than that or you get bogged down in the planning stage and then in redevelopment and refreshing; etc etc etc.I am currently looking for a method to determine the size of the instrument position in the portfolio based on the available margin and the required margin.
Problems begin when it comes to instruments other than Forex or Metals, because SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL) does not return the margin.
So I have to introduce logic based on what the broker announces, e.g. for Indices 5% of the contract value, for the Stock Exchange 20% of the contract, etc.
However, I'm not sure if this is the best strategy, and I think there's something wrong with my code.
struct SymbolParams { string symbol; double weight; double margin_contract_percent; }; double CalculatePositionSizeByMargin(SymbolParams &symbol_params, double margin_allocated) { double margin_per_contract = -1; if(symbol_params.margin_contract_percent <= 0) { margin_per_contract = SymbolInfoDouble(symbol_params.symbol, SYMBOL_MARGIN_INITIAL); margin_per_contract /= (double)AccountInfoInteger(ACCOUNT_LEVERAGE); if (margin_per_contract == 0) { Print("Error: No margin for symbol ", symbol_params.symbol); return 0.0; } } else if(symbol_params.margin_contract_percent > 0) { margin_per_contract = CalculateMarginByContractPercent(symbol_params.symbol, symbol_params.margin_contract_percent); } string currency_margin = SymbolInfoString(symbol_params.symbol, SYMBOL_CURRENCY_MARGIN); string account_currency = AccountInfoString(ACCOUNT_CURRENCY); if (currency_margin != account_currency) { string conversion_symbol = currency_margin + account_currency; if (!SymbolSelect(conversion_symbol, true)) conversion_symbol = account_currency + currency_margin; double conversion_rate = SymbolInfoDouble(conversion_symbol, SYMBOL_BID); if (conversion_rate == 0) { Print("Error: No exchange rate ", conversion_symbol); return 0.0; } if (conversion_symbol == currency_margin + account_currency) margin_per_contract *= conversion_rate; else margin_per_contract /= conversion_rate; } double position_size = margin_allocated / margin_per_contract; double min_volume = SymbolInfoDouble(symbol_params.symbol, SYMBOL_VOLUME_MIN); double max_volume = SymbolInfoDouble(symbol_params.symbol, SYMBOL_VOLUME_MAX); double volume_step = SymbolInfoDouble(symbol_params.symbol, SYMBOL_VOLUME_STEP); position_size = fmin(max_volume, fmax(min_volume, round(position_size / volume_step) * volume_step)); return position_size; } double CalculateMarginByContractPercent(string symbol, double margin_percent) { double contract_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE); double price = SymbolInfoDouble(symbol, SYMBOL_BID); double leverage = (double)AccountInfoInteger(ACCOUNT_LEVERAGE); if (contract_size == 0 || price == 0 || leverage == 0) { Print("Error: No data for symbol ", symbol); return 0.0; } double contract_value = contract_size * price; double required_margin = (contract_value * margin_percent) / 100.0; double actual_margin = required_margin / leverage; return actual_margin; } void OnStart() { double total_margin = AccountInfoDouble(ACCOUNT_MARGIN_FREE); double trading_margin_percentage = 1.0; double trading_margin = (trading_margin_percentage / 100.0) * total_margin; Print("total_margin: ", total_margin, " trading_margin: ", trading_margin); SymbolParams symbols[] = { {"USTEC", 0.3, 5}, {"XAGUSD", 0.2, -1.0}, {"AAPL.NAS", 0.5, 20.0}, }; for (int i = 0; i < ArraySize(symbols); i++) { double margin_allocated = trading_margin * symbols[i].weight; double position_size = CalculatePositionSizeByMargin(symbols[i], margin_allocated); Print("Symbol: ", symbols[i].symbol, ", Margin allocated: ", margin_allocated, ", Volume size: ", position_size); } }
well... your desciption in that 2nd msg is much more descriptive and your coding attempt will surely ensure responses from the pro's now; whereas with the initial msg, you would not have got any response, i am sure.
But i recommend that you look thru the experts codes in the includes folder. I am sure that there is margin lotsize code there.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use