Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

download historical and realtime data with addon

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    download historical and realtime data with addon

    is there a sample for downloading historical and realtime data with a addon?
    here is a example where its obviously not working as i am not seeing any data on my output window
    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.Gui.Tools;
    #endregion
    
    //This namespace holds Add ons in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.AddOns
    {
    
    
    
    
    /* Example of subscribing/unsubscribing to bars data events from an Add On as well as making bars requests.
    The concept can be carried over to any NinjaScript object you may be working on. */
    public class AddonAlertsTab : NinjaTrader.NinjaScript.AddOnBase
    {
    private int daysBack = 5;
    private bool barsRequestSubscribed = false;
    private BarsRequest barsRequest;
    
    protected override void OnStateChange()
    {
    try
    {
    if (State == State.SetDefaults)
    {
    PrintTo = PrintTo.OutputTab2;
    Description = @"Enter the description for your new custom Add on here.";
    Name = "AddonAlertsTab";
    barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("EUR/USD"), 8);
    
    
    
    
    
    }
    
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    // Parametrize your request.
    barsRequest.BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 30 };
    barsRequest.TradingHours = TradingHours.Get("Default 24 x 7");
    
    // Attach event handler for real-time events if you want to process real-time data
    barsRequest.Update += OnBarUpdate;
    }
    else if (State == State.Terminated)
    {
    // Unsubscribe to events
    try
    {
    barsRequest.Update -= OnBarUpdate;
    barsRequest.Dispose();
    barsRequest = null;
    
    
    
    }
    catch (Exception e)
    {
    Print(e.StackTrace);
    
    }
    }
    }
    
    catch (Exception e)
    {
    Print(e.StackTrace);
    
    }
    
    }
    
    
    // This method is fired on real-time bar events
    private void OnBarUpdate(object sender, BarsUpdateEventArgs e)
    {
    /* Depending on the BarsPeriod type of your barsRequest you can have situations where more than one bar is
    updated by a single tick. Be sure to process the full range of updated bars to ensure you did not miss a bar. */
    
    // Output bar information on each tick
    for (int i = e.MinIndex; i <= e.MaxIndex; i++)
    {
    // Processing every single tick
    NinjaTrader.Code.Output.Process(string.Format("Tim e: {0} Open: {1} High: {2} Low: {3} Close: {4}",
    e.BarsSeries.GetTime(i),
    e.BarsSeries.GetOpen(i),
    e.BarsSeries.GetHigh(i),
    e.BarsSeries.GetLow(i),
    e.BarsSeries.GetClose(i)), PrintTo.OutputTab1);
    }
    }
    
    
    
    
    }
    
    }

    #2
    Hello junkone,

    Below is a link to a working Addon example that uses a BarsRequest.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3


      how can i detect isFirstTickOftheBar from the Bars object?
      how do i know if the bars is realtime bar or historical bar?

      protected void BarUpdate(object sender, BarsUpdateEventArgs e)
      {
      Bars theBars = sender as Bars;
      string message = string.Format("{0} | {1}", theBars.GetTime(0), theBars.Instrument.FullName );
      NinjaTrader.Code.Output.Process(message, PrintTo.OutputTab1);
      }
      Last edited by junkone; 01-18-2021, 06:03 PM.

      Comment


        #4
        I copied the code made from junkone and inserted into a new AddOn.
        The code compiles, but i can't find a way to run it from a chart.
        how can we link it into something where it runs??
        Thank you!!

        Comment


          #5
          Originally posted by efeuvejota01 View Post
          I copied the code made from junkone and inserted into a new AddOn.
          The code compiles, but i can't find a way to run it from a chart.
          how can we link it into something where it runs??
          Thank you!!
          my code is bad code. dont use it. use the example shared earlier at post #2 above.

          In addition to Ninjatrader team,
          how can i detect isFirstTickOftheBar from the Bars object?
          how do i know if the bars is realtime bar or historical bar?

          protected void BarUpdate(object sender, BarsUpdateEventArgs e)
          {
          Bars theBars = sender as Bars;
          string message = string.Format("{0} | {1}", theBars.GetTime(0), theBars.Instrument.FullName );
          NinjaTrader.Code.Output.Process(message, PrintTo.OutputTab1);
          }

          Comment


            #6
            Hello junkone,

            You can save the bar index to a variable and then compare this with e.MaxIndex to see when a new bar is opened.

            The AddOn_Framework_NinjaScript_Basic has example code on lines 230, 1504 to 1526.


            If the bar is being looped over from the .Request callback, it is historical data. If the .Update event is running it is real-time data.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by alifarahani, Today, 09:40 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by RookieTrader, Today, 09:37 AM
            4 responses
            17 views
            0 likes
            Last Post RookieTrader  
            Started by PaulMohn, Today, 12:36 PM
            0 responses
            5 views
            0 likes
            Last Post PaulMohn  
            Started by love2code2trade, 04-17-2024, 01:45 PM
            4 responses
            40 views
            0 likes
            Last Post love2code2trade  
            Started by junkone, Today, 11:37 AM
            3 responses
            25 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X