Ejemplo n.º 1
0
        static bool TryProcessAsTradeRecord(TradeEntry entry, ICollection <TradeRecord> records)
        {
            var record = entry.Tag as TradeRecord;

            if (record == null)
            {
                return(false);
            }

            record.Profit = entry.Profit;
            record.Margin = entry.Margin;

            records.Add(record);

            return(true);
        }
Ejemplo n.º 2
0
        static bool TryProcessAsPosition(TradeEntry entry, ICollection <Position> positions)
        {
            var position = entry.Tag as Position;

            if (position == null)
            {
                return(false);
            }

            if (entry.Profit.HasValue)
            {
                position.Profit = position.Profit.GetValueOrDefault() + entry.Profit;
            }
            position.Margin = entry.Margin;

            // some magic; see Calculate method of state calcualtor
            if (entry.Side == TradeRecordSide.Buy)
            {
                positions.Add(position);
            }

            return(true);
        }
Ejemplo n.º 3
0
        void PrepareCalculator(AccountInfo accountUpdate, IEnumerable <CurrencyInfo> currencyUpdate, IEnumerable <SymbolInfo> symbolsUpdate, IDictionary <string, Quote> quotesUpdate)
        {
            if (accountUpdate != null)
            {
                this.account.Balance  = accountUpdate.Balance;
                this.account.Currency = accountUpdate.Currency;
                this.account.Type     = accountUpdate.Type;

                if (this.account.Type != AccountType.Cash)
                {
                    this.account.Leverage = accountUpdate.Leverage;

                    var provider = new SymbolInfoFeaturesProvider();
                    var features = provider.GetInfo(new FixProtocolVersion(this.feed.UsedProtocolVersion));

                    if (features.IsCurrencyPrecisionSupported && this.feed.Cache.Currencies.Select(o => o.Name).Contains(account.Currency))
                    {
                        var precisionProvider = new CurrencyPrecisionProvider(this.feed.Cache.Currencies);
                        this.account.RoundingService = new AccountRoundingService(FinancialRounding.Instance, precisionProvider, account.Currency);
                    }
                }
            }

            if (this.account.Type != AccountType.Cash)
            {
                this.account.Balance = this.trade.Cache.AccountInfo.Balance;
            }

            if (currencyUpdate != null)
            {
                var currencies = currencyUpdate.OrderBy(o => o.SortOrder).Select(c => new CurrencyEntry(this.calculator, c.Name, c.Precision, c.SortOrder));

                this.calculator.Currencies.Clear();

                foreach (var currency in currencies)
                {
                    this.calculator.Currencies.Add(currency);
                }
            }

            if (symbolsUpdate != null)
            {
                this.calculator.Symbols.Clear();

                var provider = new SymbolInfoFeaturesProvider();
                var features = provider.GetInfo(new FixProtocolVersion(this.feed.UsedProtocolVersion));

                foreach (var symbol in symbolsUpdate.OrderByDescending(o => o.Name))
                {
                    var entry = new SymbolEntry(this.calculator, symbol.Name, symbol.SettlementCurrency, symbol.Currency)
                    {
                        MarginFactor              = symbol.GetMarginFactor(),
                        MarginFactorOfPositions   = 1,
                        MarginFactorOfLimitOrders = 1,
                        MarginFactorOfStopOrders  = 1,
                        Hedging                         = symbol.MarginHedge,
                        MarginCalcMode                  = symbol.MarginCalcMode,
                        StopOrderMarginReduction        = symbol.StopOrderMarginReduction,
                        HiddenLimitOrderMarginReduction = symbol.HiddenLimitOrderMarginReduction
                    };

                    if (features.IsGroupSortOrderSupported)
                    {
                        entry.GroupSortOrder = symbol.GroupSortOrder;
                    }

                    if (features.IsSortOrderSupported)
                    {
                        entry.SortOrder = symbol.SortOrder;
                    }

                    this.calculator.Symbols.Add(entry);
                }

                if (features.IsCurrencyPrecisionSupported && this.feed.Cache.Currencies.Select(o => o.Name).Contains(account.Currency))
                {
                    var precisionProvider = new CurrencyPrecisionProvider(this.feed.Cache.Currencies);
                    this.account.RoundingService = new AccountRoundingService(FinancialRounding.Instance, precisionProvider, account.Currency);
                }
            }

            foreach (var quote in quotesUpdate.Values)
            {
                this.calculator.Prices.Update(quote.Symbol, quote.Bid, quote.Ask);
                this.calculatorQuotes[quote.Symbol] = quote;
            }

            this.account.Trades.Clear();

            var records = this.trade.Cache.TradeRecords;

            foreach (var record in records)
            {
                var entry = new TradeEntry(this.account, record.Type, record.Side, record.Symbol, record.Volume, record.MaxVisibleVolume, record.Price, record.StopPrice)
                {
                    Tag             = record,
                    Commission      = record.Commission,
                    AgentCommission = record.AgentCommission,
                    Swap            = record.Swap,
                };

                this.account.Trades.Add(entry);
            }

            var positions = this.trade.Cache.Positions;

            foreach (var position in positions)
            {
                var buy = new TradeEntry(this.account, TradeRecordType.Position, TradeRecordSide.Buy, position.Symbol, position.BuyAmount, null, position.BuyPrice.Value, null)
                {
                    Tag             = position,
                    Commission      = position.Commission,
                    AgentCommission = position.AgentCommission,
                    Swap            = position.Swap
                };
                var sell = new TradeEntry(this.account, TradeRecordType.Position, TradeRecordSide.Sell, position.Symbol, position.SellAmount, null, position.SellPrice.Value, null)
                {
                    Tag = position
                };

                // some magic; see TryProcessAsPosition of StateInfo class
                this.account.Trades.Add(buy);
                this.account.Trades.Add(sell);
            }
        }