Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading on level 2 values

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

    Trading on level 2 values

    Hi im creating a Thread for myself and who ever needs help to create strategies with level 2 data, as well as trading on level 2 changes.

    I know I need to incorporate:

    (1) onMarketDepth() ! to get me level 2 data values.

    USE these links:







    JP

    #2
    on market depth update

    Hi,

    I have a problem,

    I took the SampleMarketDepth indicator code from the previous post (the one given by ninja trader), and made some changes. The ideal was that I wanted to print the complete book every time a value in my book changed.

    so I took the :
    PHP Code:
    // Prints the L2 Ask Book we created. Cycles through the whole List and prints the contained objects.
    Print("Ask Book");
    for (int idx = 0; idx < askRows.Count; idx++)
    Print("Ask Price=" + askRows[idx].Price + " Volume=" + askRows[idx].Volume + " Position=" + idx);
                    
    // Prints the L2 Bid Book we created. Cycles through the whole List and prints the contained objects.
    Print("Bid Book");
    for (int idx = 0; idx < bidRows.Count; idx++)
    Print("Bid Price=" + bidRows[idx].Price + " Volume=" + bidRows[idx].Volume + " Position=" + idx); 
    
    and placed it in the :

    PHP Code:
    protected override void OnMarketDepth(MarketDepthEventArgs e)
            {
    } 
    
    with the rest of the code on top. (I included a picture of my problem)

    NOTE : I know that the comment says that it cycles through the book, but I need this to stop - I want to update the whole book. (I want to see the full 10 bid and ask every time the book updates)

    Where do I need to make the change?

    Thanks

    JP
    Attached Files

    Comment


      #3
      another fine thread:

      Comment


        #4
        cummulative dept - onmarket order

        Found the answer, although I think my code could be simplified... if anyone knows how I would be very happy.

        Anyway, to transform the SampleMarketDepth.cs indicator code that was given by ninjatrader into a strategy and to give you cummulative depth you do this:

        1) add variables :
        === From ===
        [PHP]

        PHP Code:
        private    long sum1 = 0;
        private    long sum2 = 0;      
        private int bidbookfilled=0;
        private int askbookfilled=0; 
        
        2) then in OnMarketDepth(MarketDepthEventArgs e)
        === From ===
        PHP Code:
        protected override void OnMarketDepth(MarketDepthEventArgs e)
                {
        
        
                    
                    // Prints the L2 Ask Book we created. Cycles through the whole List and prints the contained objects.
                        Print("Ask Book");
                        for (int idx = 0; idx < askRows.Count; idx++)
                            Print("Ask Price=" + askRows[idx].Price + " Volume=" + askRows[idx].Volume + " Position=" + idx);
                        
                        // Prints the L2 Bid Book we created. Cycles through the whole List and prints the contained objects.
                        Print("Bid Book");
                        for (int idx = 0; idx < bidRows.Count; idx++)
                            Print("Bid Price=" + bidRows[idx].Price + " Volume=" + bidRows[idx].Volume + " Position=" + idx); 
        
        === To ===

        PHP Code:
        protected override void OnMarketDepth(MarketDepthEventArgs e)
                {
                    
                    // Prints the L2 Ask Book we created. Cycles through the whole List and prints the contained objects.
                    ///Print("Ask Book");
                    for (int idx = 0; idx < askRows1.Count; idx++)
                    {
                        ///Print("Ask Price=" + askRows1[idx].Price + " Volume=" + askRows1[idx].Volume + " Position=" + idx);    
                        if (idx == 9)
                        {
                            askbookfilled=1;
                            sum1 = askRows1[0].Volume + askRows1[1].Volume + askRows1[2].Volume + askRows1[3].Volume + askRows1[4].Volume + askRows1[5].Volume + askRows1[6].Volume + askRows1[7].Volume + askRows1[8].Volume + askRows1[9].Volume  ;
                        }
                    }
                    // Prints the L2 Bid Book we created. Cycles through the whole List and prints the contained objects.
                    ///Print("Bid Book");
                    for (int idx = 0; idx < bidRows1.Count; idx++)
                    {
                        ///Print("Bid Price=" + bidRows1[idx].Price + " Volume=" + bidRows1[idx].Volume + " Position=" + idx);
                        if (idx == 9)
                        {
                            bidbookfilled=1;
                            sum2 = bidRows1[0].Volume + bidRows1[1].Volume + bidRows1[2].Volume + bidRows1[3].Volume + bidRows1[4].Volume + bidRows1[5].Volume + bidRows1[6].Volume + bidRows1[7].Volume + bidRows1[8].Volume + bidRows1[9].Volume  ;
                        }
                    }
                    
                    if (bidbookfilled==1 && askbookfilled==1)
                    {
                            Print("OnMarketDepth()");
                        Print("Time : " + DateTime.Now.ToLongTimeString());
                        Print("Cummulative Dept ASK is  " + sum1 );
                        Print("Cummulative Dept BID is  " + sum2 );
                        bidbookfilled=0;
                        askbookfilled=0;
                        Print(Environment.NewLine); 
                    } 
        
        3) and the rest is the same.

        4) ***if you know how to simplify this please reply and give the code.

        ChanceHero
        Attached Files

        Comment


          #5
          saving output

          Hi,

          I have a SERIOUS problem

          I am trying to save some values from a strategy to a text file, the problem is that I call upon

          PHP Code:
          using System.IO;    
          using System.Text; 
          
          and in variables:

          PHP Code:
          TextWriter tw = new StreamWriter("c:\\strat_data.txt"); 
          
          and in the code:

          PHP Code:
          tw.WriteLine(DateTime.Now.ToLongTimeString() + "," + ask ); 
          


          the problem is that the system IO is interfering with my actually being able to load the strategy (it simply disappears from my options)

          HELP,

          ChanceHero
          Last edited by chancehero; 02-05-2012, 11:17 AM.

          Comment


            #6
            see:

            Comment


              #7
              chancehero, could you please clarify what exact issue you would see? The strategy is disappearing then and could not be started up at all? Are there any errors in the log tab as this happens for you?

              Comment


                #8
                YUp, just disappeared, regardless, I used the link below and just did that and it worked.




                ChanceHero

                Comment


                  #9
                  random numbers and saving their values

                  Hi,

                  new problem, I am generating a log of a random number and I need to use the previous generated values (lets say the last 10 values) in the OnMarketDepth.

                  This is what I have up to now. Any leads on how I can do this?

                  PHP Code:
                  #region Variables    
                  private double x;        //random number
                  private double logx;    // log of random number    
                  #endregion 
                  
                  PHP Code:
                  protected override void OnMarketDepth(MarketDepthEventArgs e)
                  {
                        {
                        Random RandomClass = new Random();
                        x = RandomClass.Next(0, 10); 
                        logx=Math.Log(x);
                        }
                  } 
                  

                  thanks,

                  ChanceHero

                  Comment


                    #10
                    ChanceHero, so you mean accessing the previous generated random #'s? Just store them then in a queue or list for example.

                    Comment


                      #11
                      list

                      hi,

                      exactly, felling pretty stupic right now . Thanks , the list is perfect.


                      Next Question
                      I am noticing something with the

                      PHP Code:
                      random RandomClass = new Random();
                      x = RandomClass.Next(0, 10); 
                      logx=Math.Log(x); 
                      
                      I am calling this in onMarketDepth

                      and the random number has a clear time component to it. it clearly uses hours, minutes and seconds and 1/2 seconds. since it spits out the same value within 1/2 a second. I included my output file.
                      time , log (random) , and some other number which is update every time onmarketdepth is called.

                      how can I get a random number which will actually be updated with onmarketdepth?

                      Thanks
                      Attached Files

                      Comment


                        #12
                        chancehero, I would look into calling the random # generation only if you see your event calling the OnMarketDepth, so for example an Ask Update...

                        Comment


                          #13
                          random number time problem

                          sorry, to clarify,

                          the random number is called , when ever the lvl2 book is updated and fully rebuilt. (I have 10 ask and 10 bid)

                          i guess, what I am asking is it their other pseudo random number generator which don't have a time component ? because I need this at that precise time - and can't have the random number lagging.

                          ChanceHero

                          Comment


                            #14
                            I would closely review the comments made here as the challenges you run into are mentioned as well - http://www.dotnetperls.com/random

                            So far I've not personally worked in this area, but the above should give you hopefully some hints to tackle it.

                            Comment


                              #15
                              thanks, it worked ok.

                              in onmarketdepth, how do I create a condition to make sure I have enough data. (the same ideal as if I have a MA of 5 periods, to make sure I have 5 data points.)

                              ChanceHero

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              596 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              343 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              556 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              554 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X