Beispiel #1
0
        public virtual double GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming(string symbol, Direction direction
                                                                                   , out OrderSpreadSide oss, bool forceCrossMarket)
        {
            double priceLastQuote = this.LastQuoteGetPriceForMarketOrder(symbol);

            if (priceLastQuote == 0)
            {
                string msg = "QuickCheck ZERO priceLastQuote=" + priceLastQuote + " for Symbol=[" + symbol + "]"
                             + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot";
                Assembler.PopupException(msg);
                //throw new Exception(msg);
            }
            double currentBid = this.BestBidGetForMarketOrder(symbol);
            double currentAsk = this.BestAskGetForMarketOrder(symbol);

            if (currentBid == 0)
            {
                string msg = "ZERO currentBid=" + currentBid + " for Symbol=[" + symbol + "]"
                             + " while priceLastQuote=[" + priceLastQuote + "]"
                             + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot";
                ;
                Assembler.PopupException(msg);
                //throw new Exception(msg);
            }
            if (currentAsk == 0)
            {
                string msg = "ZERO currentAsk=" + currentAsk + " for Symbol=[" + symbol + "]"
                             + " while priceLastQuote=[" + priceLastQuote + "]"
                             + " from streamingProvider[" + this.streamingProvider.Name + "].StreamingDataSnapshot";
                Assembler.PopupException(msg);
                //throw new Exception(msg);
            }

            double price = 0;

            oss = OrderSpreadSide.ERROR;

            SymbolInfo    symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfo(symbol);
            MarketOrderAs spreadSide;

            if (forceCrossMarket)
            {
                spreadSide = MarketOrderAs.LimitCrossMarket;
            }
            else
            {
                spreadSide = (symbolInfo == null) ? MarketOrderAs.LimitCrossMarket : symbolInfo.MarketOrderAs;
            }
            if (spreadSide == MarketOrderAs.ERROR || spreadSide == MarketOrderAs.Unknown)
            {
                string msg = "Set Symbol[" + symbol + "].SymbolInfo.LimitCrossMarket; should not be spreadSide[" + spreadSide + "]";
                Assembler.PopupException(msg);
                throw new Exception(msg);
                //return;
            }

            switch (direction)
            {
            case Direction.Buy:
            case Direction.Cover:
                switch (spreadSide)
                {
                case MarketOrderAs.LimitTidal:
                    oss   = OrderSpreadSide.AskTidal;
                    price = currentAsk;
                    break;

                case MarketOrderAs.LimitCrossMarket:
                    oss   = OrderSpreadSide.BidCrossed;
                    price = currentBid;                                                 // Unknown (Order default) becomes CrossMarket
                    break;

                case MarketOrderAs.MarketMinMaxSentToBroker:
                    oss   = OrderSpreadSide.MaxPrice;
                    price = currentAsk;
                    break;

                case MarketOrderAs.MarketZeroSentToBroker:
                    oss   = OrderSpreadSide.MarketPrice;
                    price = currentAsk;                                                 // looks like default, must be crossmarket to fill it right now
                    break;

                default:
                    string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]";
                    throw new Exception(msg2);
                }
                break;

            case Direction.Short:
            case Direction.Sell:
                switch (spreadSide)
                {
                case MarketOrderAs.LimitTidal:
                    oss   = OrderSpreadSide.BidTidal;
                    price = currentBid;
                    break;

                case MarketOrderAs.LimitCrossMarket:
                    oss   = OrderSpreadSide.AskCrossed;
                    price = currentAsk;                                                 // Unknown (Order default) becomes CrossMarket
                    break;

                case MarketOrderAs.MarketMinMaxSentToBroker:
                    oss   = OrderSpreadSide.MinPrice;
                    price = currentBid;                                                 // Unknown (Order default) becomes CrossMarket
                    break;

                case MarketOrderAs.MarketZeroSentToBroker:
                    oss   = OrderSpreadSide.MarketPrice;
                    price = currentBid;                                                 // looks like default, must be crossmarket to fill it right now
                    break;

                default:
                    string msg2 = "no handler for spreadSide[" + spreadSide + "] direction[" + direction + "]";
                    throw new Exception(msg2);
                }
                break;

            default:
                string msg = "no handler for direction[" + direction + "]";
                throw new Exception(msg);
            }

            if (double.IsNaN(price))
            {
                Debugger.Break();
            }
            symbolInfo = Assembler.InstanceInitialized.RepositoryCustomSymbolInfo.FindSymbolInfoOrNew(symbol);
            price      = symbolInfo.AlignOrderPriceToPriceLevel(price, direction, MarketLimitStop.Market);
            return(price);
        }