Ejemplo n.º 1
0
        /// <summary>
        /// Gets the current cash balance for each currency held in the brokerage account
        /// </summary>
        /// <returns>The current cash balance for each currency available for trading</returns>
        public override List <CashAmount> GetCashBalance()
        {
            Log.Trace("FxcmBrokerage.GetCashBalance()");
            var cashBook = new List <CashAmount>();

            //Adds the account currency to the cashbook.
            cashBook.Add(new CashAmount(Convert.ToDecimal(_accounts[_accountId].getCashOutstanding()),
                                        _fxcmAccountCurrency));

            foreach (var trade in _openPositions.Values)
            {
                //settlement price for the trade
                var settlementPrice = Convert.ToDecimal(trade.getSettlPrice());
                //direction of trade
                var direction = trade.getPositionQty().getLongQty() > 0 ? 1 : -1;
                //quantity of the asset
                var quantity = Convert.ToDecimal(trade.getPositionQty().getQty());
                //quantity of base currency
                var baseQuantity = direction * quantity;
                //quantity of quote currency
                var quoteQuantity = -direction * quantity * settlementPrice;
                //base currency
                var baseCurrency = trade.getCurrency();
                //quote currency
                var quoteCurrency = FxcmSymbolMapper.ConvertFxcmSymbolToLeanSymbol(trade.getInstrument().getSymbol());
                quoteCurrency = quoteCurrency.Substring(quoteCurrency.Length - 3);

                var baseCurrencyAmount = cashBook.FirstOrDefault(x => x.Currency == baseCurrency);
                //update the value of the base currency
                if (baseCurrencyAmount != default(CashAmount))
                {
                    cashBook.Remove(baseCurrencyAmount);
                    cashBook.Add(new CashAmount(baseQuantity + baseCurrencyAmount.Amount, baseCurrency));
                }
                else
                {
                    //add the base currency if not present
                    cashBook.Add(new CashAmount(baseQuantity, baseCurrency));
                }

                var quoteCurrencyAmount = cashBook.Find(x => x.Currency == quoteCurrency);
                //update the value of the quote currency
                if (quoteCurrencyAmount != default(CashAmount))
                {
                    cashBook.Remove(quoteCurrencyAmount);
                    cashBook.Add(new CashAmount(quoteQuantity + quoteCurrencyAmount.Amount, quoteCurrency));
                }
                else
                {
                    //add the quote currency if not present
                    cashBook.Add(new CashAmount(quoteQuantity, quoteCurrency));
                }
            }
            return(cashBook);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the current cash balance for each currency held in the brokerage account
        /// </summary>
        /// <returns>The current cash balance for each currency available for trading</returns>
        public override List <Cash> GetCashBalance()
        {
            Log.Trace("FxcmBrokerage.GetCashBalance()");
            var cashBook = new List <Cash>();

            //Adds the account currency USD to the cashbook.
            cashBook.Add(new Cash(_fxcmAccountCurrency,
                                  Convert.ToDecimal(_accounts[_accountId].getCashOutstanding()),
                                  GetUsdConversion(_fxcmAccountCurrency)));

            foreach (var trade in _openPositions.Values)
            {
                //settlement price for the trade
                var settlementPrice = Convert.ToDecimal(trade.getSettlPrice());
                //direction of trade
                var direction = trade.getPositionQty().getLongQty() > 0 ? 1 : -1;
                //quantity of the asset
                var quantity = Convert.ToDecimal(trade.getPositionQty().getQty());
                //quantity of base currency
                var baseQuantity = direction * quantity;
                //quantity of quote currency
                var quoteQuantity = -direction * quantity * settlementPrice;
                //base currency
                var baseCurrency = trade.getCurrency();
                //quote currency
                var quoteCurrency = FxcmSymbolMapper.ConvertFxcmSymbolToLeanSymbol(trade.getInstrument().getSymbol());
                quoteCurrency = quoteCurrency.Substring(quoteCurrency.Length - 3);

                var baseCurrencyObject = (from cash in cashBook where cash.Symbol == baseCurrency select cash).FirstOrDefault();
                //update the value of the base currency
                if (baseCurrencyObject != null)
                {
                    baseCurrencyObject.AddAmount(baseQuantity);
                }
                else
                {
                    //add the base currency if not present
                    cashBook.Add(new Cash(baseCurrency, baseQuantity, GetUsdConversion(baseCurrency)));
                }

                var quoteCurrencyObject = (from cash in cashBook where cash.Symbol == quoteCurrency select cash).FirstOrDefault();
                //update the value of the quote currency
                if (quoteCurrencyObject != null)
                {
                    quoteCurrencyObject.AddAmount(quoteQuantity);
                }
                else
                {
                    //add the quote currency if not present
                    cashBook.Add(new Cash(quoteCurrency, quoteQuantity, GetUsdConversion(quoteCurrency)));
                }
            }
            return(cashBook);
        }