Пример #1
0
        public override void OnBarOpen(Bar bar)
        {
            if (!bar.IsWithinRegularTradingHours(Instrument.Type))
            {
                return;
            }

            if (bar.Size == PeriodConstants.PERIOD_DAILY)
            {
                return;
            }

            PublishOpenBar(bar);

            base.OnBarOpen(bar);
        }
        protected bool IsItOkToHandleBar(Bar bar)
        {
            if (!bar.IsWithinRegularTradingHours(Instrument.Type))
            {
                LoggingUtility.WriteTraceFormat(this, "Bar is not within regular trading hours: {0}", bar);
                return false;
            }

            return true;
        }
Пример #3
0
        public override void OnBarOpen(Bar bar)
        {
            if (!bar.IsWithinRegularTradingHours(Instrument.Type))
            {
                return;
            }

            if (bar.Size == PeriodConstants.PERIOD_DAILY)
            {
                // Since we will like to capture any gaps in the current day
                // open and corresponding effects of ATR calculations
                OnBar(bar);
            }
            else if (bar.BeginTime.Date >= CurrentValidityDateTime.Date)
            {
                OnBar(bar);
            }

            base.OnBarOpen(bar);
        }
Пример #4
0
        public override void OnBar(Bar bar)
        {
            if (!bar.IsWithinRegularTradingHours(Instrument.Type))
            {
                return;
            }
            else
            {
                DataManager.Add(Instrument, bar);
            }

            try
            {
                CurrentBarRef = bar;

                if (bar.Size == PeriodConstants.PERIOD_DAILY)
                {
                    if (CurrentDailyBarSeries.Count <= AtrPeriod)
                    {
                        return;
                    }
                }

                if (bar.Size == CurrentExecutionTimePeriodInSeconds)
                {
                    int[] periods = new[]
                    {
                        SlowMaPeriod,
                        FastMaPeriod,
                        StochasticsDPeriod,
                        StochasticsKPeriod,
                        StochasticsSmoothPeriod
                    };

                    int maxPeriod = periods.Max();

                    if (CurrentExecutionBarSeries.Count <= maxPeriod)
                    {
                        return;
                    }
                }

                // If everything is filled - then exit:
                if (IsStrategyOrderFilled)
                    return;

                CurrentClosePrice = bar.Close;

                if (bar.Size == PeriodConstants.PERIOD_DAILY)
                {
                    CurrentAtrPrice = DailyAtrIndicator.Last;
                    if (CurrentClosePrice > 0 && IsBarCloseEnoughForLogging(bar))
                    {
                        LoggingUtility.WriteInfoFormat(this,
                            "Setting ATR to {0:c} which is {1:p} of the last close price {2:c}",
                            CurrentAtrPrice,
                            CurrentAtrPrice / CurrentClosePrice,
                            CurrentClosePrice);
                    }
                }

                if (bar.Size == PeriodConstants.PERIOD_DAILY)
                {
                    // We do not need to worry about any other type of bar
                    return;
                }

                if (bar.Size == CurrentExecutionTimePeriodInSeconds)
                {
                    // Ensure that intraday indicators are evaluated
                    GetIsStochInBullishMode(bar);
                    GetIsStochInBearishMode(bar);
                    GetIsEmaInBearishMode(bar);
                    GetIsEmaInBullishMode(bar);
                }

                if (CurrentAtrPrice <= 0)
                {
                    // need the ATR to process further
                    return;
                }

                if (CurrentAskPrice <= 0)
                {
                    // need the Ask to process further
                    return;
                }

                if (CurrentBidPrice <= 0)
                {
                    // need the Bid to process further
                    return;
                }

                if (GetCurrentDateTime() < CurrentValidityDateTime)
                {
                    return;
                }

                // 1. If the order is not triggered yet - then check when is the right time to trigger the order
                if (null == CurrentOrder)
                {
                    if (GetHasOrderBeenTriggered())
                    {
                        QueueOrder();
                    }
                }
                else
                {
                    if (IsStrategyOrderInFailedState)
                    {
                        throw new InvalidOperationException("Order is in an invalid state");
                    }
                    else
                    {
                        // 4. Partially filled? then queue up the remaining order
                        // We need to retry by adjusting the prices
                        if (GetHasOrderBeenReTriggered())
                        {
                            QueueOrder();
                        }
                    }
                }
            }
            finally
            {
                CurrentBarRef = null;
            }
        }