Beispiel #1
0
        static public void TestGlostenSimple()
        {
            PriceDiscovery glosten = new PriceDiscovery(1.0121M - 0.977M, 1.5M - 0.5M, 1, 0.5M);

            Random r = new Random();

            for (int i = 0; i < 21; i++)
            {
                bool    buy  = r.Next() % 2 == 0;
                decimal prop = (decimal)r.NextDouble();

                decimal bid, ask;

                glosten.GetBidAskForOrder(buy, prop, out bid, out ask);

                Console.WriteLine("i=" + i + " bid=" + bid + ",ask=" + ask);
                Console.WriteLine("trade was " + (buy ? "buy" : "sell") + ", informed = " + prop);
            }
        }
        private async void Start()
        {
            try
            {
                var allPairs = await _marketProfileService.GetAllPairsAsync();

                foreach (var asset in allPairs)
                {
                    var key = asset.AssetPair;
                    _lastReceivedAsks[key] = new Quote
                    {
                        AssetPair = key,
                        Price     = asset.AskPrice,
                        Timestamp = asset.AskPriceTimestamp
                    };

                    _lastReceivedBids[key] = new Quote
                    {
                        AssetPair = key,
                        Price     = asset.BidPrice,
                        Timestamp = asset.BidPriceTimestamp
                    };
                    if (!_priceDiscoveries.TryGetValue(key, out var prd))
                    {
                        prd = new PriceDiscovery(Threshold);
                        _priceDiscoveries[key] = prd;
                        var timestamp = asset.AskPriceTimestamp > asset.BidPriceTimestamp
                            ? asset.AskPriceTimestamp
                            : asset.BidPriceTimestamp;
                        var price = new Price(asset.AskPrice, asset.BidPrice, timestamp);
                        prd.Run(price);
                    }
                }
            }
            catch (Exception ex)
            {
                await _log.WriteFatalErrorAsync(nameof(FixQuotesManager), nameof(Start), "Loading market profile", ex);

                throw;
            }
        }
        /// <summary>	Calculates the market prices and limits. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <param name="market">				[in,out] The market. </param>
        /// <param name="bitsharesBalances">	The bitshares balances. </param>
        /// <param name="bitcoinBalance">       The bitcoin balance. </param>
        public override void ComputeMarketPricesAndLimits(ref MarketRow market,
                                                          Dictionary <int, ulong> bitsharesBalances,
                                                          decimal bitcoinBalance)
        {
            base.ComputeMarketPricesAndLimits(ref market, bitsharesBalances, bitcoinBalance);

            decimal baseBalance  = 0;
            decimal quoteBalance = bitcoinBalance;

            if (bitsharesBalances.ContainsKey(m_asset.id))
            {
                // only non zero balances return data, so this guard is necessary
                baseBalance = m_asset.GetAmountFromLarimers(bitsharesBalances[m_asset.id]);
            }

            decimal maxTransactionFactor;

            if (m_currency.uia)
            {
                // with UIA we got to handle the maximum buy size differently
                BitsharesAccount account = m_bitshares.WalletGetAccount(m_bitsharesAccount);
                if (m_asset.issuer_id == account.id)
                {
                    // we are the issuer!

                    // refresh the asset
                    m_asset = m_bitshares.BlockchainGetAsset(m_asset.symbol);

                    // this is how much we can issue, so lets stick that in there
                    baseBalance = m_asset.GetAmountFromLarimers(m_asset.max_supply - m_asset.current_supply);
                }
                else
                {
                    throw new UnexpectedCaseException();
                }

                maxTransactionFactor = 1;
                //maxTransactionFactor = kMaxTransactionFactor;
            }
            else
            {
                maxTransactionFactor = kMaxTransactionFactor;
            }

            decimal newAskMax, newBidMax;

            // askMax is in BITCOINS
            // bidMax is in BITASSETS

            if (m_flipped)
            {
                // BTC_bitUSD

                // baseBalance = 10 bitUSD
                // ask = 240
                // askMax = 10 / 240 = 0.04 BTC

                newAskMax = Numeric.TruncateDecimal((baseBalance / m_market.ask) * maxTransactionFactor, 8);
                newBidMax = Numeric.TruncateDecimal((quoteBalance * m_market.bid) * maxTransactionFactor, 8);
            }
            else
            {
                // BTS_BTC
                //
                // baseBalance = 1 BTS
                // ask = 0.00004
                // askMax = 1 * 0.0004 = 0.0004 BTC

                newAskMax = Numeric.TruncateDecimal((baseBalance * m_market.ask) * maxTransactionFactor, 8);
                newBidMax = Numeric.TruncateDecimal((quoteBalance / m_market.bid) * maxTransactionFactor, 8);
            }

            m_isDirty |= newAskMax != m_market.ask_max || newBidMax != m_market.bid_max;

            market.ask_max = newAskMax;
            market.bid_max = newBidMax;

            if (m_market.price_discovery)
            {
                //
                // update price discovery engine
                //

                decimal bitsharesBalance = m_asset.GetAmountFromLarimers(bitsharesBalances[m_asset.id]);

                if (m_asset.symbol == CurrencyHelpers.kBtcSymbol)
                {
                    m_lastFeedPrice = 1;
                }
                else
                {
                    m_lastFeedPrice = RecomputeFeedPriceInBtc();
                }

                decimal inventoryRatio = ComputeInventoryRatio(bitsharesBalances, bitcoinBalance);

                if (m_prices == null)
                {
                    //
                    // initialise the price discovery engine
                    //

                    m_prices = new PriceDiscovery(market.spread_percent, market.window_percent, m_lastFeedPrice, inventoryRatio);
                }

                decimal oldBid = m_market.bid;
                decimal oldAsk = m_market.ask;
                m_prices.UpdateParameters(m_lastFeedPrice, inventoryRatio, m_market.spread_percent, m_market.window_percent, out m_market.bid, out m_market.ask);

                m_isDirty |= oldBid != m_market.bid || oldAsk != m_market.ask;
            }
        }