예제 #1
0
        public void OnTick(params Tick[] ticks)
        {
            //Update the current market.

            foreach (Tick t in ticks)
            {
                if (!currentOffers.ContainsKey(t.Symbol))
                {
                    currentOffers.Add(t.Symbol, t);
                }
                else
                {
                    currentOffers[t.Symbol] = t;
                }
            }


            //-----------------------------------------------------------
            // On each tick, we must check for stop and limit order hits.
            //-----------------------------------------------------------

            foreach (Tick t in ticks)
            {
                //------------------------------------------------------
                // Stop Order Check
                //------------------------------------------------------

                if (_outStandingStopOrders.ContainsKey(t.Symbol))
                {
                    StopOrder stopOrder = _outStandingStopOrders[t.Symbol];

                    //If trailing, need to relcalculate trigger price.
                    if (stopOrder.Trailing)
                    {
                        stopOrder.RecalculateTrail(t);
                    }

                    //Trigger stop if price drops -below- Buy Stop Trigger
                    if (stopOrder.IsBuyStop())
                    {
                        //net stop to close all
                        if (t.BidLow <= stopOrder.TriggerPrice)
                        {
                            CloseStopOrder(t, stopOrder);
                        }
                    }

                    //Trigger stop if price rises -above- Sell Stop Trigger
                    else if (stopOrder.IsSellStop())
                    {
                        if (t.AskHigh >= stopOrder.TriggerPrice)
                        {
                            CloseStopOrder(t, stopOrder);
                        }
                    }
                }


                //------------------------------------------------------
                // Limit Order Check
                //------------------------------------------------------

                if (_outStandingLimitOrders.ContainsKey(t.Symbol))
                {
                    LimitOrder limitOrder = _outStandingLimitOrders[t.Symbol];

                    //If trailing, need to relcalculate trigger price.
                    if (limitOrder.Trailing)
                    {
                        limitOrder.RecalculateTrail(t);
                    }

                    //Trigger limit if price goes -above- Buy Limit Trigger
                    if (limitOrder.IsBuyLimit())
                    {
                        //net limit to close all
                        if (t.BidClose >= limitOrder.TriggerPrice)
                        {
                            CloseLimitOrder(t, limitOrder);
                        }
                    }

                    //Trigger limit if price drops -below- Limit Stop Trigger
                    else if (limitOrder.IsSellLimit())
                    {
                        if (t.AskClose <= limitOrder.TriggerPrice)
                        {
                            CloseLimitOrder(t, limitOrder);
                        }
                    }
                }
            }


            //-----------------------------------------------------------
            // Adjust portfolio equity/margin
            //-----------------------------------------------------------

            _portfolio.AdjustPortfolioEquityAndMargin(ticks);
        }