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

OnMarketData question

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

    OnMarketData question

    Hello

    This is an indicator, which in real time separate volume on every Last tick to bulls or bears (if last price = bid - volume is bearish; if last price = ask - volume is bullish)

    Here is a code:

    Code:
            int Previous_time;
            double Previous_Ask, Previous_Bid; 
            double Bulls_Volume, Bears_Volume;
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Overlay                = true;
                CalculateOnBarClose = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
    
            protected override void OnMarketData(MarketDataEventArgs e)
            {
                
              if (e.MarketDataType == MarketDataType.Ask) {
                Previous_Ask = e.Price;
                Print("Ask = " + e.Price + " " + e.Time);    
              }    
            
              if (e.MarketDataType == MarketDataType.Bid) {
                Previous_Bid = e.Price; 
                Print("Bid = " + e.Price + " " + e.Time);    
              }
                
              if (e.MarketDataType == MarketDataType.Last) {
                if(e.Price == Previous_Ask) {
                  Bulls_Volume += e.Volume;
                } else {
                  Bears_Volume += e.Volume; 
                }
                
                
                /*
                if(e.Price == Previous_Bid) {
                  Bears_Volume += e.Volume; 
                }*/
                
                //Print("Last = " + e.Price + " " + e.Time + " Volume: " +e.Volume);
              }
            
            }        
    
            }
    Result:

    Look at this picture:
    SaveImg &#1087;&#1086;&#1079;&#1074;&#1086;&#1083;&#1080;&#1090; &#1074;&#1072;&#1084; &#1079;&#1072;&#1075;&#1088;&#1091;&#1079;&#1080;&#1090;&#1100; &#1092;&#1086;&#1090;&#1086; &#1080;&#1083;&#1080; &#1082;&#1072;&#1088;&#1090;&#1080;&#1085;&#1082;&#1091; &#1073;&#1077;&#1089;&#1087;&#1083;&#1072;&#1090;&#1085;&#1086; &#1080; &#1073;&#1077;&#1079; &#1088;&#1077;&#1075;&#1080;&#1089;&#1090;&#1088;&#1072;&#1094;&#1080;&#1080;


    On some bars a sum of bearish (red color) and bullish (green color) is equals total volume on bar (black color) - and it's exactly correct, but on some bars this sum of bearish and bullish volumes doesn't equal total volume on bar - why it's so?
    Last edited by agafon2; 02-06-2013, 09:39 AM.

    #2
    Hello Agafon2,

    Thank you for your post.

    Once this is seen on your chart from your indicator and you then reload all historical data does the correct value display?

    Does the same behavior occur when using CalculateOnBarClose = True?

    I look forward to your response.

    Comment


      #3
      Originally posted by NinjaTrader_PatrickH View Post

      Once this is seen on your chart from your indicator and you then reload all historical data does the correct value display?

      I look forward to your response.
      It's a real-time indicator only - if i reload chart data all drawings will be removed and i can compare it

      Originally posted by NinjaTrader_PatrickH View Post
      Does the same behavior occur when using CalculateOnBarClose = True?
      Yes

      Comment


        #4
        Hello Agafon2,

        Thank you for your response.

        Who do you connect to for data?

        Does this occur on all charts or just the 6E?

        I look forward to your response.

        Comment


          #5
          Do you look throuth my code? Is it correct?

          Comment


            #6
            This issue is common for every instrument
            Last edited by agafon2; 02-05-2013, 01:02 PM.

            Comment


              #7
              Originally posted by agafon2 View Post
              Hello

              This is an indicator, which in real time separate volume on every Last tick to bulls or bears (if last price = bid - volume is bearish; if last price = ask - volume is bullish)

              Here is a code:

              Code:
                      int Previous_time;
                      double Previous_Ask, Previous_Bid; 
                      double Bulls_Volume, Bears_Volume;
               
                      /// <summary>
                      /// This method is used to configure the indicator and is called once before any bar data is loaded.
                      /// </summary>
                      protected override void Initialize()
                      {
                          Overlay                = true;
                          CalculateOnBarClose = false;
                      }
               
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
               
                      protected override void OnMarketData(MarketDataEventArgs e)
                      {
               
                        if (e.MarketDataType == MarketDataType.Ask) {
                          Previous_Ask = e.Price;
                          Print("Ask = " + e.Price + " " + e.Time);    
                        }    
               
                        if (e.MarketDataType == MarketDataType.Bid) {
                          Previous_Bid = e.Price; 
                          Print("Bid = " + e.Price + " " + e.Time);    
                        }
               
                        if (e.MarketDataType == MarketDataType.Last) {
                          if(e.Price == Previous_Ask) {
                            Bulls_Volume += e.Volume;
                          } else {
                            Bears_Volume += e.Volume; 
                          }
               
               
                          /*
                          if(e.Price == Previous_Bid) {
                            Bears_Volume += e.Volume; 
                          }*/
               
                          //Print("Last = " + e.Price + " " + e.Time + " Volume: " +e.Volume);
                        }
               
                      }        
               
               
                      protected override void OnBarUpdate()
                      {
                        if(!Historical) {
                          if(Convert.ToInt32(Time[0].Subtract(new DateTime(1970,1,1)).TotalSeconds) != Previous_time) {
                            Previous_time = Convert.ToInt32(Time[0].Subtract(new DateTime(1970,1,1)).TotalSeconds);
                            DrawText("1"+CurrentBar+Convert.ToString(Time[1]), Bulls_Volume.ToString(), 1, High[1]+0.0005, Color.Green);
                            DrawText("2"+CurrentBar+Convert.ToString(Time[1]), Bears_Volume.ToString(), 1, Low[1]-0.0005, Color.Red);
                            DrawText("3"+CurrentBar+Convert.ToString(Time[1]), Volume[1].ToString(), 1, High[1]+0.0010, Color.Black);                
                            Bulls_Volume = 0;
                            Bears_Volume = 0;
                          }
                        }  
                      }
              Result:

              Look at this picture:
              SaveImg &#1087;&#1086;&#1079;&#1074;&#1086;&#1083;&#1080;&#1090; &#1074;&#1072;&#1084; &#1079;&#1072;&#1075;&#1088;&#1091;&#1079;&#1080;&#1090;&#1100; &#1092;&#1086;&#1090;&#1086; &#1080;&#1083;&#1080; &#1082;&#1072;&#1088;&#1090;&#1080;&#1085;&#1082;&#1091; &#1073;&#1077;&#1089;&#1087;&#1083;&#1072;&#1090;&#1085;&#1086; &#1080; &#1073;&#1077;&#1079; &#1088;&#1077;&#1075;&#1080;&#1089;&#1090;&#1088;&#1072;&#1094;&#1080;&#1080;


              On some bars a sum of bearish (red color) and bullish (green color) is equals total volume on bar (black color) - and it's exactly correct, but on some bars this sum of bearish and bullish volumes doesn't equal total volume on bar - why it's so?
              It is so because you are summing volume on traded prices, while you are setting the prices Bid or Asked, whether or not they are traded.

              Comment


                #8
                Originally posted by koganam View Post
                It is so because you are summing volume on traded prices, while you are setting the prices Bid or Asked, whether or not they are traded.
                How to modify code to avoid this bug?

                Comment


                  #9
                  Originally posted by agafon2 View Post
                  How to modify code to avoid this bug?
                  I do not understand your question. It is not a bug: the code did as you asked.

                  If there is a discrepancy, it is because what you intend is not what you coded. What are you intending to code?

                  Comment


                    #10
                    In real time i want to separate every tick to bulls or bears: if last price = ask - to bulls; if last price = bid - to bears. And after it i want to calculate a sum of bullish and bearish volume for bar

                    Comment


                      #11
                      Originally posted by agafon2 View Post
                      In real time i want to separate every tick to bulls or bears: if last price = ask - to bulls; if last price = bid - to bears. And after it i want to calculate a sum of bullish and bearish volume for bar
                      I notice that you are using this on the 6E. What is the bid/ask spread on the instrument?

                      Comment


                        #12
                        From 1 point

                        Comment


                          #13
                          Originally posted by agafon2 View Post
                          From 1 point
                          Sorry. I do not understand your answer, and as I do not trade the instrument, I do not wish to load it to find out. How many ticks is the bid/ask spread?

                          Comment


                            #14
                            One or more ticks is spread. Spread is dynamical

                            Comment


                              #15
                              Originally posted by agafon2 View Post
                              One or more ticks is spread. Spread is dynamical
                              If your bid/ask spread can be larger than one tick, then that volume represents trades that took place at neither the Bid nor the Ask, but somewhere in the middle of the spread. This can be caused by a very fast market, or just by the need for the Market Maker to rapidly liquidate a position, where they will transact at a non-posted Bid or Ask price, for a small quantity that will allow them to go flat.

                              That having been said, 1/381 is such a small percentage, it will be well within the bounds of error of measurement, in what is essentially a statistical data-gathering process. It seems to me that you are so focused on having 100% precision that you are losing the bigger picture. I find it a bit hard to see how your analysis could be thrown off by a difference of 0.262%, so why are you sweating it? That is just slightly more than one quarter of one percent!

                              Just my opinion, but millisecond tick timing precision and 100% volume precision are hardly necessary to a retail trader's ability to make decisions. Then again, I do not really know your situation, so maybe I am talking out of turn.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Barry Milan, Today, 10:35 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by WeyldFalcon, 12-10-2020, 06:48 PM
                              14 responses
                              1,428 views
                              0 likes
                              Last Post Handclap0241  
                              Started by DJ888, Yesterday, 06:09 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              40 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Today, 08:51 AM
                              2 responses
                              16 views
                              0 likes
                              Last Post bill2023  
                              Working...
                              X