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

Current Monthly Open Price

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

    Current Monthly Open Price

    Hello,

    I want to be able to access the monthly open price in a strategy when the monthly bar has yet to close (current month). So for example, today in my strategy I want to access this month's open price from 3/1/19.

    How would I go about doing that?

    Thanks.

    #2
    Hello fiddich,

    Thank you for your post.

    This will depend on the calculate setting you want to use, and the modes you want to use the strategy in. If this is for realtime only and you are using OnEachTick or OnPriceChange, you can add a secondary Monthly series and just use the 0 BarsAgo open price from that series.

    If you are using OnBarClose or wanted to do this historically, that will be more difficult as you would only have access to the last Closed bar from the current Closed bar. Some options at accessing this value may include using logic to find the first day of the month and then get the values from that point in time however this may not exactly match the monthly bar open. Another approach could be using a secondary daily series to access the first days values. Again this may differ than the monthly bar however this would likely be closer as monthly bars are built using daily bars. For both of these items, these are not a concept that I have a specific sample of that I could provide, unfortunately.

    I was able to quickly create a sample that uses a daily series to find the open of the first day of the month, you may try using something like the following:
    Code:
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    AddPlot(Brushes.Red, "test");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Day, 1);
                }
            }
    
            double monthOpen = 0;
            protected override void OnBarUpdate()
            {
                if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
                if(BarsInProgress == 1)
                {
                    if(Time[0].Day == 1)
                    {
                        monthOpen = Open[0];    
                    }
                }
                if(BarsInProgress != 0) return;
                Value[0] = monthOpen;
            }
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello fiddich,

      Thank you for your post.

      This will depend on the calculate setting you want to use, and the modes you want to use the strategy in. If this is for realtime only and you are using OnEachTick or OnPriceChange, you can add a secondary Monthly series and just use the 0 BarsAgo open price from that series.

      If you are using OnBarClose or wanted to do this historically, that will be more difficult as you would only have access to the last Closed bar from the current Closed bar. Some options at accessing this value may include using logic to find the first day of the month and then get the values from that point in time however this may not exactly match the monthly bar open. Another approach could be using a secondary daily series to access the first days values. Again this may differ than the monthly bar however this would likely be closer as monthly bars are built using daily bars. For both of these items, these are not a concept that I have a specific sample of that I could provide, unfortunately.

      I was able to quickly create a sample that uses a daily series to find the open of the first day of the month, you may try using something like the following:
      Code:
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      AddPlot(Brushes.Red, "test");
      }
      else if (State == State.Configure)
      {
      AddDataSeries(BarsPeriodType.Day, 1);
      }
      }
      
      double monthOpen = 0;
      protected override void OnBarUpdate()
      {
      if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
      if(BarsInProgress == 1)
      {
      if(Time[0].Day == 1)
      {
      monthOpen = Open[0];
      }
      }
      if(BarsInProgress != 0) return;
      Value[0] = monthOpen;
      }
      I look forward to being of further assistance.
      Jesse, or whoever can answer as this is from 2014, but...

      if (Time[0].Day == 1)

      This is assuming the first of the month is a trading day. If the first trading day of the month is the 2nd, this will not work. Whats the workaround for that?

      Comment


        #4
        Hello ChrisR,

        If you are using an instrument which has trading days that fall off the first you would have to add additional conditions to make sure a value was set.

        Code:
        if(monthOpen <= 0 && Time[0].Day == 2)
        {
            monthOpen = Open[0];
        }
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        44 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        20 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        180 views
        0 likes
        Last Post jeronymite  
        Started by DanielSanMartin, Yesterday, 02:37 PM
        2 responses
        13 views
        0 likes
        Last Post DanielSanMartin  
        Working...
        X