Ejemplo n.º 1
0
        // *****************************************************************
        // ****                     Public Methods                      ****
        // *****************************************************************
        //
        //
        //
        //
        // *********************************************************
        // ****                 Market_MarketChanged            ****
        // *********************************************************
        /// <summary>
        /// Called on every tick from the market.  Attempt was made to optimize this
        /// by filtering out times when we don't have a position to manage.  Additionally
        /// all calls that are not top of book are ignored.  Checks are created to watch for when market
        /// quantity crosses thresholds as this is what triggers the scratch event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void Market_MarketChanged(object sender, EventArgs eventArgs)
        {
            if (m_IPriceToPosition.Count == 0)
            {   // we are not actively managing a position, we dont care about market updates
                return;
            }

            if (m_Market.IsLastTickBestPriceChange)
            {                                  // this update was a best price change! we need to check everything to see if we need to scratch
                m_IPriceWorkSpaceList.Clear(); // use this list since TryScratchPosition removes keys from m_IPricePosition, we cannot iterate on it!

                foreach (KeyValuePair <int, int> kvPair in m_IPriceToPosition)
                {   // check each price we have a position at, if we need to scratch, then add it to our list of prices to scratch
                    if (IsScratchNeededAtIPrice(kvPair.Key))
                    {
                        m_IPriceWorkSpaceList.Add(kvPair.Key);
                    }
                }

                for (int i = 0; i < m_IPriceWorkSpaceList.Count; i++)
                {   // for every price we need to scratch
                    TryScratchPosition(m_IPriceWorkSpaceList[i], true);
                }

                // update our state variables with new market quantities!
                for (int side = 0; side < 2; ++side)
                {
                    m_WasInsideMarketQtyAboveThreshold[side] = m_Market.Qty[side][0] > m_ScratchThreshold;
                }
            }
            else
            {   // this could be a top of book qty change, lets check
                bool isMarketQtyOverThreshold;
                for (int side = 0; side < 2; ++side)
                {
                    if (m_Market.BestDepthUpdated[side] == 0)
                    {   // the last update on this side was top of market!
                        isMarketQtyOverThreshold = m_Market.Qty[side][0] > m_ScratchThreshold;
                        if ((m_IsActive & !isMarketQtyOverThreshold & m_WasInsideMarketQtyAboveThreshold[side]) |
                            (!m_IsActive & isMarketQtyOverThreshold & !m_WasInsideMarketQtyAboveThreshold[side]))
                        {   // we are actively scratching, the market is now under our threshold and it was previously above!
                            // OR we are passively scratching the market is now over our threshold and it was previously below!
                            int iPrice = QTMath.RoundToSafeIPrice(m_Market.Price[side][0], side, m_InstrumentDetails.TickSize);
                            if (IsScratchNeededAtIPrice(iPrice))
                            {   // this call will complete the checks and if returns true we actually need to scratch
                                TryScratchPosition(iPrice, true);
                            }
                        }
                        m_WasInsideMarketQtyAboveThreshold[side] = isMarketQtyOverThreshold;    // save new threshold state
                    }
                }
            }
        }