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

BuySellVolume Indicator

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

    BuySellVolume Indicator

    Hello - I am trying to use this indicator in a simple strategy but am having difficulty with reading it in live mode.

    It runs in tick mode and I am good with working with IsFirstTickOfBar. The issue I run into is that I think the indicator only returns the last tick of the [1] candle, even though it looks fine in the indicator and in the data box.

    2 questions:
    1 - Can you specify that an indictor needs to run in Tick mode when calling AddChartIndicator? I could not find an example that does this.
    2 - Do you have any sample code set that will just print the data from the indicator, when I write one that just displays the plots I get an odd set of results.
    Thanks

    #2
    Hello cpabiz20k,

    To make sure the indicator is running OnEachTick you need to set the strategy to OnEachTick. The strategy will pass the calculate setting to any indicators being used so they match it.

    If you are trying to get a value from this indicator during IsFirstTickOfBar the [0] BarsAgo value will be invalid due to how it resets. If you take a look at the code below from the indicator you can see why that would happen. The CurrentBar != activeBar is simulating IsFirstTickOfBar, the final accumulation is plotted on the previous bar so the previous bar is complete and then after that the buys/sells are reset. Following that the current bar has 0's plotted so if your strategy accesses [0] BarsAgo during IsFirstTickOfBar it will be an reset value.

    Code:
    if (CurrentBar != activeBar)
    {
    Sells[1] = sells;
    Buys[1] = buys + sells;
    buys = 0;
    sells = 0;
    activeBar = CurrentBar;
    }
    
    Sells[0] = sells;
    Buys[0] = buys + sells;
    In your strategy you would have to get the previous bars value during IsFirstTickOfBar if you wanted to retrieve a value at that time, you can otherwise access the [0] BarsAgo value after the first tick of the bar.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response. When I write a simple script to display the data it works great on past bars but does not display anything for bars that are processed while the strategy is active. Not sure how to send my code so I will just copy/paste it for you to look at. I have also attached a screenshot of the output (high, low, open close, buyvolume, sellvolume, long/short).
      /****************/

      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.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class BuySellDisplayV1 : Strategy
      {

      private BuySellVolume BuySellVolume1;
      private double buys;
      private double sells;
      private int activeBar = 0;



      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "BuySellDisplayV1";
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;

      }
      else if (State == State.Configure)
      {
      BuySellVolume1 = BuySellVolume(Close);
      }
      else if (State == State.DataLoaded)
      {

      BuySellVolume1 = BuySellVolume(Close);

      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 20)
      return;

      BuySellVolume1 = BuySellVolume(Close);


      // Set 1
      if (Position.MarketPosition == MarketPosition.Flat
      && IsFirstTickOfBar
      && Close[1] > Open[1]

      )
      {
      Print(ToTime(Time[1]) + " " + High[1] + " " + Low[1] + " " + Open[1] + " " + Close[1] + " " + BuySellVolume1.Buys[1] + " " + BuySellVolume1.Sells[1] + " Long");

      }



      // Set 4
      if (Position.MarketPosition == MarketPosition.Flat


      && IsFirstTickOfBar

      && Close[1] < Open[1]

      )
      {
      Print(ToTime(Time[1]) + " " + High[1] + " " + Low[1] + " " + Open[1] + " " + Close[1] + " " + BuySellVolume1.Buys[1] + " " + BuySellVolume1.Sells[1] + " Short");


      }



      }

      region Properties

      #endregion
      }
      }
      Attached Files

      Comment


        #4
        Hello cpabiz20k,

        Please try using the 0 bars ago value without IsFirstTickOfBar. That would work going forward in realtime. You can store the value of the indicator to a custom series if you wanted to use it on each first tick of bar but you would need to do that in a set before the others and without any conditions so the value is always updated.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks for the reply - I tried the 0 bar and it gives me no data at all when I run the strategy in onbarclose - the indicator needs to be run on each tick. When I run the strategy in oneachtick mode I get a display every tick and the data accumulates in a continuous stream. How can I store the value of the indicator in a custom data series? Do you have an example you can share? This seems like a simple problem so pardon me if I appear a little behind the curve.

          Comment


            #6
            Can I ask a favor? I have spent a few hours on this and it is likely just one thing I am missing. Can you load my program into an editor and run it on a 15 second ES chart. Have the output window and the data box open and see how the historical data matches in the 2 and then the current/active data displays 0's in the BuySellVolume1.Buys[1] and Sells[1]. What could be causing this? I am completely lost. Thanks for anything you can do to help.

            Comment


              #7
              Hello cpabiz20k,

              Right you need to use OnEachTick with this indicator, it wont work OnBarClose. I did try what you had posted which is why I suggested using 0 bars ago instead.

              You can see the following post which shows how to store data in a custom series: https://ninjatrader.com/support/foru...86#post1099186

              JesseNinjaTrader Customer Service

              Comment


                #8
                Just to clarify - I am not sure what value I am setting to the custom series. Am I looking to put all of the indicator code into the strategy and let it recalculate on each bar, then write it to the custom series?

                Does it seem odd that you can see the right data available in the data box but that I cant get the right value from BuySellVolume1.Buys[1]? If I cant get it why do we think the custom series will be able to? Just trying to not go on a fishing expedition for the rest of the day with this idea. Thanks for your help.

                Comment


                  #9
                  Hello cpabiz20k,

                  You can store the indicator value to a custom series if you needed to use IsFirstTickOfBar for your condition. In that use case you would have stored the value of the indicator to the series and can access the [1] bars ago of that series during IsFirstTickOfBar to get a non zero value. If you don't need to use IsFirstTickOfBar then you dont need a custom series, you can just use the indicator directly but would need to use the [0] bars ago value.

                  The databox and plot work based on how the indicator plots, due to this being a realtime indicator and the way it stores data strategy use is different than what you would see in the chart.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    I think that is my whole reason for opening this chat - when I use Bar[0] it does not return any values for BuySellVolume1.Buys[0] or BuySellVolume1.Sells[0] - any thoughts? Sorry for being a pest...

                    Comment


                      #11
                      Hello cpabiz20k,

                      Are you using OnEachTick and not using IsFirstTickOfBar? The indicator will be zeroed out on the first tick of the bar so that cant be used in a strategy by using [0] BarsAgo at that specific time. You can use the [0] bars ago value any time past that point. If you need to use IsFirstTickOfBar as part of your condition you will have to use a custom series to save the indicator values for the bar before IsFirstTickOfBar, when IsFirstTickOfBar happens the indicator is zeroed out and you would use the previous bars values that you stored in the custom series in the strategy.

                      Due to the way this saves data and resets on the first tick you will have to account for that if you are using the first tick in your strategy as part of the condition. If you don't use the first tick the [0] bars ago value will increment for every tick and the strategy can observe that value.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Good data there - thanks. If you look at the code it is using IsFirstTickOfBar and I am looking back 1 back - that is what I don't get at this point. I understand the [0] bar issue with IsFirstTickOfBar. Why does the history work and not the real-time? Any idea why the indicator still plots the correct values on the chart (I just can't access them by using BuySellVolume1.Buys[1]?

                        Comment


                          #13
                          Do you know of any indicators that just display the buy and sell volume at the close of a candle that I might substitute?

                          Comment


                            #14
                            Hello cpabiz20k,

                            As mentioned you would need to use a custom series if you wanted to use IsFirstTickOfBar to store the indicator data. That would allow you to reference the [1] BarsAgo on that custom series which would hold the indicator data. This indicator plots in a custom way which is not easily used with strategies due to its logic. The way its logic is made is so that it can be visualized on a chart correctly.

                            The buy and sell volume are accumulated in realtime based on the incoming market data which is why it works OnEachTick, I am not aware of any indicator that could work that way using OnBarClose. If you are using OnBarClose then you would just be getting the volume. You can add additional data series for the Ask and Bid data if your data provider offers that as historical data.



                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              I think the issue is this...mapping a custom series like myBuys[1] = BuySellVolume1.Buys[1] doesnt do anything because BuySellVolume1.Buys[1] doesn't have any value in it, even after IsFirstTickOfBar - if it did I could access it. Is there a good programmer you know of who can help me without having to develop a major spec and having it cost hundreds of dollars - I just need a quick fix to this.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X