 
    
            Maximum Volume without Running Out of Money
            
                 16 Jun 2015, 21:45
            
                    
How can I calculate what the maximum volume could be for a trade without without getting a "Not Enough Money" error?
Replies
                     deklin
                     01 Jul 2015, 01:44
                                    
What is wrong with this? It does not place the order. It returns an error: No Money!
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSmartBotRobot : Robot
    {
        protected override void OnStart()
        {
            long MaxVolume = Symbol.NormalizeVolume((Account.FreeMargin / Symbol.Ask * Account.Leverage), RoundingMode.Down);
            var result = ExecuteMarketOrder(TradeType.Buy, Symbol, MaxVolume);
            if (!result.IsSuccessful) Print("Error: " + Symbol.Code + " " + (long)MaxVolume + " Failed - " + result.Error);
            Stop();
        }
    }
}
@deklin

galafrin
16 Jun 2015, 23:55
Last time I checked out, Calgo would not provide leverage, lot size, nor margin required of a symbol, so we have to do it ourselves.
We need a function that returns the leverage of a symbol, then a function that returns the conversion rate from one currency to another, then a function that returns the lot size of a symbol. Those are used to compute the margin required by one lot of a symbol. The max volume will be free margin divided by margin required in lot units.
Margin required = Lotsize * symbol bid * rate (quote currency to account currency) / leverage
max volume = free margin / margin required
@galafrin