Example #1
0
        private void requestAndDrawSettlement(DateTime time)
        {
            DateTime previousEnd  = TradingHours.String2TradingHours(RTHTemplate).GetPreviousTradingDayEnd(time);
            DateTime previousEnd2 = TradingHours.String2TradingHours(RTHTemplate).GetPreviousTradingDayEnd(previousEnd.AddMinutes(-1));

            //Print("PREVIOUS END "+time+" = "+previousEnd+" "+previousEnd2);
            BarsRequest barsRequest = new BarsRequest(BarsArray[0].Instrument, previousEnd.AddDays(-2), previousEnd);

            barsRequest.BarsPeriod = new BarsPeriod {
                BarsPeriodType = BarsPeriodType.Day, Value = 1
            };

            DateTime time0 = Times[0][0];
            DateTime time1 = Times[0][1];

            barsRequest.Request(new Action <BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) =>
            {
                if (errorCode != ErrorCode.NoError)
                {
                    //Print(string.Format("Error on requesting bars: {0}, {1}", errorCode, errorMessage));
                    Log("JBMarketProfileLevels could not load settled bars", LogLevel.Error);
                    return;
                }

                double settlement2 = 0;
                if (bars.Bars.Count > 0)
                {
                    for (int i = bars.Bars.Count - 1; i >= 0; i--)
                    {
                        if (DateTime.Compare(bars.Bars.GetTime(i), previousEnd2) <= 0)
                        {
                            settlement2 = bars.Bars.GetClose(i);
                            break;
                        }
                        else if (DateTime.Compare(bars.Bars.GetTime(i), previousEnd) <= 0)
                        {
                            this.settlement = bars.Bars.GetClose(i);
                        }
                    }
                    if (DateTime.Compare(time0, previousEnd) <= 0)
                    {
                        DrawSettlementRay("SETTLEM " + previousEnd.ToString("MMM dd"), time1, settlement, time, SettlementColor);
                    }
                    else
                    {
                        DrawSettlementRay("SETTLEM " + previousEnd.ToString("MMM dd"), previousEnd, settlement, time, SettlementColor);
                    }
                    if (settlement2 > 0)
                    {
                        DrawSettlementRay("SETTLEM " + previousEnd2.ToString("MMM dd"), previousEnd2, settlement2, time, SettlementColor);
                    }
                }
            }));
        }
Example #2
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();
            }
        }