Esempio n. 1
0
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name                 = "Num Touches";
                Buys                 = new ConcurrentDictionary <double, long>();
                BackColor            = Brushes.Transparent;
                BarColor             = Application.Current.TryFindResource("brushVolumeColumnBackground") as Brush;
                BuyColor             = Brushes.Green;
                DefaultWidth         = 160;
                PreviousWidth        = -1;
                DisplayText          = true;
                ForeColor            = Application.Current.TryFindResource("brushVolumeColumnForeground") as Brush;
                ImmutableBarColor    = Application.Current.TryFindResource("immutableBrushVolumeColumnBackground") as Brush;
                ImmutableForeColor   = Application.Current.TryFindResource("immutableBrushVolumeColumnForeground") as Brush;
                IsDataSeriesRequired = true;
                LastVolumes          = new ConcurrentDictionary <double, long>();
                SellColor            = Brushes.Red;
                Sells                = new ConcurrentDictionary <double, long>();
                Bidask               = VolumeSide.bid;
            }
            else if (State == State.Configure)
            {
                if (UiWrapper != null && PresentationSource.FromVisual(UiWrapper) != null)
                {
                    Matrix m         = PresentationSource.FromVisual(UiWrapper).CompositionTarget.TransformToDevice;
                    double dpiFactor = 1 / m.M11;
                    gridPen      = new Pen(Application.Current.TryFindResource("BorderThinBrush") as Brush, 1 * dpiFactor);
                    halfPenWidth = gridPen.Thickness * 0.5;
                }

                if (SuperDom.Instrument != null && SuperDom.IsConnected)
                {
                    BarsPeriod bp = new BarsPeriod
                    {
                        MarketDataType = MarketDataType.Last,
                        BarsPeriodType = BarsPeriodType.Tick,
                        Value          = 1
                    };

                    SuperDom.Dispatcher.InvokeAsync(() => SuperDom.SetLoadingString());
                    clearLoadingSent = false;

                    if (BarsRequest != null)
                    {
                        BarsRequest.Update -= OnBarsUpdate;
                        BarsRequest         = null;
                    }

                    BarsRequest = new BarsRequest(SuperDom.Instrument,
                                                  Cbi.Connection.PlaybackConnection != null ? Cbi.Connection.PlaybackConnection.Now : Core.Globals.Now,
                                                  Cbi.Connection.PlaybackConnection != null ? Cbi.Connection.PlaybackConnection.Now : Core.Globals.Now);

                    BarsRequest.BarsPeriod   = bp;
                    BarsRequest.TradingHours = (TradingHoursData == TradingHours.UseInstrumentSettings || TradingHours.Get(TradingHoursData) == null) ? SuperDom.Instrument.MasterInstrument.TradingHours : TradingHours.Get(TradingHoursData);
                    BarsRequest.Update      += OnBarsUpdate;

                    BarsRequest.Request((request, errorCode, errorMessage) =>
                    {
                        // Make sure this isn't a bars callback from another column instance
                        if (request != BarsRequest)
                        {
                            return;
                        }

                        lastMaxIndex    = 0;
                        maxVolume       = 0;
                        totalBuyVolume  = 0;
                        totalLastVolume = 0;
                        totalSellVolume = 0;
                        Sells.Clear();
                        Buys.Clear();
                        LastVolumes.Clear();

                        if (State >= NinjaTrader.NinjaScript.State.Terminated)
                        {
                            return;
                        }

                        if (errorCode == Cbi.ErrorCode.UserAbort)
                        {
                            if (State <= NinjaTrader.NinjaScript.State.Terminated)
                            {
                                if (SuperDom != null && !clearLoadingSent)
                                {
                                    SuperDom.Dispatcher.InvokeAsync(() => SuperDom.ClearLoadingString());
                                    clearLoadingSent = true;
                                }
                            }

                            request.Update -= OnBarsUpdate;
                            request.Dispose();
                            request = null;
                            return;
                        }

                        if (errorCode != Cbi.ErrorCode.NoError)
                        {
                            request.Update -= OnBarsUpdate;
                            request.Dispose();
                            request = null;
                            if (SuperDom != null && !clearLoadingSent)
                            {
                                SuperDom.Dispatcher.InvokeAsync(() => SuperDom.ClearLoadingString());
                                clearLoadingSent = true;
                            }
                        }
                        else if (errorCode == Cbi.ErrorCode.NoError)
                        {
                            SessionIterator superDomSessionIt = new SessionIterator(request.Bars);
                            bool isInclude60 = request.Bars.BarsType.IncludesEndTimeStamp(false);
                            if (superDomSessionIt.IsInSession(Core.Globals.Now, isInclude60, request.Bars.BarsType.IsIntraday))
                            {
                                for (int i = 0; i < request.Bars.Count; i++)
                                {
                                    DateTime time = request.Bars.BarsSeries.GetTime(i);
                                    if ((isInclude60 && time <= superDomSessionIt.ActualSessionBegin) || (!isInclude60 && time < superDomSessionIt.ActualSessionBegin))
                                    {
                                        continue;
                                    }

                                    double ask   = request.Bars.BarsSeries.GetAsk(i);
                                    double bid   = request.Bars.BarsSeries.GetBid(i);
                                    double close = request.Bars.BarsSeries.GetClose(i);
                                    long volume  = request.Bars.BarsSeries.GetVolume(i);

                                    if (ask != double.MinValue && close >= ask)
                                    {
                                        Buys.AddOrUpdate(close, volume, (price, oldVolume) => oldVolume + volume);
                                        totalBuyVolume += volume;
                                    }
                                    else if (bid != double.MinValue && close <= bid)
                                    {
                                        Sells.AddOrUpdate(close, volume, (price, oldVolume) => oldVolume + volume);
                                        totalSellVolume += volume;
                                    }

                                    long newVolume;
                                    LastVolumes.AddOrUpdate(close, newVolume = volume, (price, oldVolume) => newVolume = 0);
                                    totalLastVolume += volume;

                                    if (newVolume > maxVolume)
                                    {
                                        maxVolume = newVolume;
                                    }
                                }

                                lastMaxIndex = request.Bars.Count - 1;

                                // Repaint the column on the SuperDOM
                                OnPropertyChanged();
                            }

                            if (SuperDom != null && !clearLoadingSent)
                            {
                                SuperDom.Dispatcher.InvokeAsync(() => SuperDom.ClearLoadingString());
                                clearLoadingSent = true;
                            }
                        }
                    });
                }
            }
            else if (State == State.Active)
            {
                if (!DisplayText)
                {
                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .AddHandler(UiWrapper, "MouseMove", OnMouseMove);

                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .AddHandler(UiWrapper, "MouseEnter", OnMouseEnter);

                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .AddHandler(UiWrapper, "MouseLeave", OnMouseLeave);

                    mouseEventsSubscribed = true;
                }
            }
            else if (State == State.Terminated)
            {
                if (BarsRequest != null)
                {
                    BarsRequest.Update -= OnBarsUpdate;
                    BarsRequest.Dispose();
                }

                BarsRequest = null;

                if (SuperDom != null && !clearLoadingSent)
                {
                    SuperDom.Dispatcher.InvokeAsync(() => SuperDom.ClearLoadingString());
                    clearLoadingSent = true;
                }

                if (!DisplayText && mouseEventsSubscribed)
                {
                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .RemoveHandler(UiWrapper, "MouseMove", OnMouseMove);

                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .RemoveHandler(UiWrapper, "MouseEnter", OnMouseEnter);

                    WeakEventManager <System.Windows.Controls.Panel, MouseEventArgs> .RemoveHandler(UiWrapper, "MouseLeave", OnMouseLeave);

                    mouseEventsSubscribed = false;
                }

                lastMaxIndex    = 0;
                maxVolume       = 0;
                totalBuyVolume  = 0;
                totalLastVolume = 0;
                totalSellVolume = 0;
                Sells.Clear();
                Buys.Clear();
                LastVolumes.Clear();
            }
        }
Esempio n. 2
0
        private void OnBarsUpdate(object sender, BarsUpdateEventArgs e)
        {
            if (State == State.Active && SuperDom != null && SuperDom.IsConnected)
            {
                if (SuperDom.IsReloading)
                {
                    OnPropertyChanged();
                    return;
                }

                BarsUpdateEventArgs barsUpdate = e;
                lock (barsSync)
                {
                    int currentMaxIndex = barsUpdate.MaxIndex;

                    for (int i = lastMaxIndex + 1; i <= currentMaxIndex; i++)
                    {
                        if (barsUpdate.BarsSeries.GetIsFirstBarOfSession(i))
                        {
                            // If a new session starts, clear out the old values and start fresh
                            maxVolume       = 0;
                            totalBuyVolume  = 0;
                            totalLastVolume = 0;
                            totalSellVolume = 0;
                            Sells.Clear();
                            Buys.Clear();
                            LastVolumes.Clear();
                        }

                        double ask    = barsUpdate.BarsSeries.GetAsk(i);
                        double bid    = barsUpdate.BarsSeries.GetBid(i);
                        double close  = barsUpdate.BarsSeries.GetClose(i);
                        long   volume = barsUpdate.BarsSeries.GetVolume(i);

                        if (ask != double.MinValue && close >= ask)
                        {
                            Buys.AddOrUpdate(close, volume, (price, oldVolume) => oldVolume + volume);
                            totalBuyVolume += volume;
                        }
                        else if (bid != double.MinValue && close <= bid)
                        {
                            Sells.AddOrUpdate(close, volume, (price, oldVolume) => oldVolume + volume);
                            totalSellVolume += volume;
                        }

                        long newVolume;
                        LastVolumes.AddOrUpdate(close, newVolume = volume, (price, oldVolume) => newVolume = oldVolume + volume);
                        totalLastVolume += volume;

                        if (newVolume > maxVolume)
                        {
                            maxVolume = newVolume;
                        }
                    }

                    lastMaxIndex = barsUpdate.MaxIndex;
                    if (!clearLoadingSent)
                    {
                        SuperDom.Dispatcher.InvokeAsync(() => SuperDom.ClearLoadingString());
                        clearLoadingSent = true;
                    }
                }
            }
        }