Maximum Volume without Running Out of Money

Created at 16 Jun 2015, 21:45
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
deklin's avatar

deklin

Joined 31.12.2014

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?


@deklin
Replies

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

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

Spotware
01 Jul 2015, 12:27

Dear Trader,

The formula you use to define the MaxVolume is not correct.

In order to open a new position your equity and margin must meet the following criteria

equity/margin >1

(where margin = current margin + margin required for the new position)

 


@Spotware