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

if () bug? 8.0.5.2 64-bit (Multi-Broker)

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

    if () bug? 8.0.5.2 64-bit (Multi-Broker)

    Hi,

    I'am trying to compare a bid and ask sorted list.

    i clear the list after each candle.

    My problem is i would like to find out a big imbalance and so i compare the list key value from 1 list with the second list -1*ticksize to get the right price.

    looks like this
    AskList:
    1,06120 -> look at sell list 1,0615 (1,06120-1Ticksize)
    i would like to use this but this is also not working

    if (sellList.TryGetValue(kvp.Key, out lookSellPriceVolTmp))
    {
    Print();
    }

    Here is a Print
    Every Ask i get through the complete Sell List and here you can see follow problem.

    Green = OK
    Red = Wrong

    Thats OK there is no 1,06225 in sell List
    10.04.2017 15:25:00 Buylist 1,0623 7 1,06225
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats Wrong 1,0623 is in sell List
    10.04.2017 15:25:00 Buylist 1,06235 37 1,0623
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats OK 1,06235 is in selllist and it show my match
    10.04.2017 15:25:00 Buylist 1,0624 62 1,06235
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 Match 1,0624 62 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats OK there is no 1,0624 in sell List
    10.04.2017 15:25:00 Buylist 1,06245 45 1,0624
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats OK 1,06245 is in selllist and it show my match
    10.04.2017 15:25:00 Buylist 1,0625 114 1,06245
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 Match 1,0625 114 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats OK 1,0625 is in selllist and it show my match
    10.04.2017 15:25:00 Buylist 1,06255 97 1,0625
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 Match 1,06255 97 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48

    Thats Wrong 1,06255 is in sell List
    10.04.2017 15:25:00 Buylist 1,0626 8 1,06255
    10.04.2017 15:25:00 SelllistinBuy 1,0623 47
    10.04.2017 15:25:00 SelllistinBuy 1,06235 23
    10.04.2017 15:25:00 SelllistinBuy 1,06245 20
    10.04.2017 15:25:00 SelllistinBuy 1,0625 63
    10.04.2017 15:25:00 SelllistinBuy 1,06255 48


    Here are the two for commands where i compare the values.

    foreach (KeyValuePair<double, long> kvpbuy in buyList) /// Go through all Buy Values
    {
    Print(Time[0] + " Buylist " + kvpbuy.Key + " " + kvpbuy.Value + " " + (kvpbuy.Key-1*TickSize));

    foreach (KeyValuePair<double, long> kvpsell in sellList) /// Go through all Sell Values at each Buy Value
    {
    Print(Time[0] + " SelllistinBuy " + kvpsell.Key + " " + kvpsell.Value);

    if (kvpbuy.Key == kvpsell.Key+1*TickSize) ///!!!Compare the Value!!!
    {
    Print(Time[0] + " Match " + kvpbuy.Key + " " + kvpbuy.Value + " " + kvpsell.Key + " " + kvpsell.Value);
    }
    }
    }

    I cannot find a wrong code so i need help.
    Please let me know if i made a mistake or if it is a bug.

    Thanks
    Stefan

    #2
    Hello Stefan_1982,

    Thank you for writing in.

    Are you using a lock statement to make sure the collection is not modified in the middle of collecting the values?

    This is demonstrating in the following reference example,


    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Thanks for the information.
      So you think the kvpbuy in buylist is changing before the it will trigger.

      ok here is what i have testes now

      i use historical tick date and only isfirsttickofbar to test it now, before it was realtime data
      (i check data 1 minute before because first tick will at the next minute and reset is after the test)

      protected override void OnMarketData(MarketDataEventArgs e)
      {
      lock (e.Instrument.SyncMarketDepth)
      {
      if (State==State.Historical)
      {
      try{
      if (IsFirstTickOfBar)
      {
      for...

      }
      }
      }
      but i get the same result

      0.04.2017 15:39:00 Buylist1,0622 26 1,06215
      10.04.2017 15:39:00 SelllistinBuy 1,0622 31
      10.04.2017 15:39:00 SelllistinBuy 1,06225 31
      10.04.2017 15:39:00 SelllistinBuy 1,0623 2

      10.04.2017 15:39:00 Buylist 1,06225 72 1,0622
      10.04.2017 15:39:00 SelllistinBuy 1,0622 31
      10.04.2017 15:39:00 SelllistinBuy 1,06225 31
      10.04.2017 15:39:00 SelllistinBuy 1,0623 2

      10.04.2017 15:39:00 Buylist 1,0623 27 1,06225
      10.04.2017 15:39:00 SelllistinBuy 1,0622 31
      10.04.2017 15:39:00 SelllistinBuy 1,06225 31
      10.04.2017 15:39:00 Match 1,0623 27 1,06225 31
      10.04.2017 15:39:00 SelllistinBuy 1,0623 2

      10.04.2017 15:39:00 Buylist 1,06235 18 1,0623
      10.04.2017 15:39:00 SelllistinBuy 1,0622 31
      10.04.2017 15:39:00 SelllistinBuy 1,06225 31
      10.04.2017 15:39:00 SelllistinBuy 1,0623 2

      Comment


        #4
        Update:

        i found that the problem is only if i i add the +1*TickSize or -1*TickSize to get the bid and the ask.
        if i compare the same price it is always working.
        i get all Matches

        if (kvpBuy.Key == kvpSell.Key)
        {
        Print(Time[0] + " Match " + kvpBuy + " " +kvpSell);
        }
        OK Match found
        11.04.2017 11:35:00 BuyList [1,0641, 38]
        11.04.2017 11:35:00 SellList [1,06405, 5]
        11.04.2017 11:35:00 SellList [1,0641, 10]
        11.04.2017 11:35:00 Match [1,0641, 38] [1,0641, 10]
        11.04.2017 11:35:00 SellList [1,06415, 1]

        OK Match found
        11.04.2017 11:35:00 BuyList [1,06415, 18]
        11.04.2017 11:35:00 SellList [1,06405, 5]
        11.04.2017 11:35:00 SellList [1,0641, 10]
        11.04.2017 11:35:00 SellList [1,06415, 1]
        11.04.2017 11:35:00 Match [1,06415, 18] [1,06415, 1]



        i do not get all Matches
        if (kvpBuy.Key == kvpSell.Key+1*TickSize)
        {
        Print(Time[0] + " Match2 " + kvpBuy + " " + kvpSell.Key+1*TickSize);
        }
        So that is the question why is this not working.

        11.04.2017 11:35:00 BuyList2 [1,0641, 38] 1,06405
        11.04.2017 11:35:00 SellList2 [1,06405, 5]
        11.04.2017 11:35:00 Match2 [1,0641, 38] [1,06405, 5]
        11.04.2017 11:35:00 SellList2 [1,0641, 10]
        11.04.2017 11:35:00 SellList2 [1,06415, 1]

        11.04.2017 11:35:00 BuyList2 [1,06415, 18] 1,0641
        11.04.2017 11:35:00 SellList2 [1,06405, 5]
        11.04.2017 11:35:00 SellList2 [1,0641, 10]
        11.04.2017 11:35:00 SellList2 [1,06415, 1]

        #########no Match2 found [1,06415, 18] [1,0641, 10]#########

        Comment


          #5
          Hello Stefan_1982,

          Have you considered using the following format to populate a list with the bids/offers?


          I look forward to your reply.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Hi Alan,

            no i use a simple sorted list, i will look at LadderRow, but a simple list if condition should also work i think.
            i send you a pm with the code
            if (e.Price >= e.Ask)
            {
            long buyVolTmp;
            if (buyList.TryGetValue(e.Price, out buyVolTmp))
            {
            buyList[e.Price] = (e.Volume + buyVolTmp);
            }
            else
            {
            buyList.Add(e.Price , e.Volume);
            }
            }
            if(e.Price <= e.Bid)
            {
            long sellVolTmp;
            if (sellList.TryGetValue(e.Price, out sellVolTmp))
            {
            sellList[e.Price] = (e.Volume + sellVolTmp);
            }
            else
            {
            sellList.Add(e.Price , e.Volume);
            }
            }

            Comment


              #7
              Will List<LadderRow> also work on OnMarketData(MarketDataEventArgs e)?
              i to not get 20 values at once like at OnMarketDepth(MarketDepthEventArgs e)!

              i'am counting every volume of the filled orders.
              Last edited by Stefan_1982; 04-11-2017, 08:43 AM.

              Comment


                #8
                Hi Alan,
                i see it is not a Ninjatrader problem it is a c# problem i need to check this at a few c# forums.
                i have the same problem @ visual studio.
                Thanks for your Help

                Comment


                  #9
                  Dear Alan,

                  i found my Problem.
                  datatyp double is very bad to compare.
                  Please have a look at this site.


                  is there a special reason why ninjatrader use double? decimal dose not have this problem!

                  my solution is to convert all values to decimal compare it and convert it back to double to use it with draw.text ...

                  Comment


                    #10
                    Trades don't always happen at TickSize.
                    Some are .99999999999 etc.

                    Look up RoundToTickSize in ninjatrader help.

                    Comment


                      #11
                      Hi sledge,

                      that is not the problem!
                      i tested this with fix numbers in Visual Studio and there i have the same results!
                      so it is a "double" problem if you make a google search "double compare" you will get 100 sites.
                      java c+ c++ c# vb ... all have this problem.

                      you need a few steps to get it working but i think convert to decimal is the best solution.

                      Comment


                        #12
                        Hi there would be a other solution as well but that is not an option for me.
                        the problem happens if you have a number like this 1,0623
                        if you multiply that value 1,0623 * 10 or 1,06235 * 100 everything is working to.

                        very easy test, try this on bar update,

                        if (1.06235 == 1.0623+0.00005)
                        {
                        Print("Double Working - 1.0623+0.00005 =" + (1.0623+0.00005));
                        }
                        else
                        {
                        Print("Double Not Working - 1.0623+0.00005 = " + (1.0623+0.00005));
                        }

                        if (Convert.ToDecimal(1.06235) == Convert.ToDecimal(1.0623)+Convert.ToDecimal(0.0000 5))
                        {
                        Print("Decimal Working - 1.0623+0.00005 =" + (Convert.ToDecimal(1.0623)+Convert.ToDecimal(0.000 05)));
                        }
                        else
                        {
                        Print("Decimal Not Working - 1.0623+0.00005 = " + (Convert.ToDecimal(1.0623)+Convert.ToDecimal(0.000 05)));
                        }

                        Here is the result!
                        Double Not Working - 1.0623+0.00005 = 1,06235
                        Decimal Working - 1.0623+0.00005 =1,06235
                        Double Not Working - 1.0623+0.00005 = 1,06235
                        Decimal Working - 1.0623+0.00005 =1,06235

                        Please have a look at: (it is the same with double)

                        Comparing for equality
                        Floating point math is not exact. Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. Different compilers and CPU architectures store temporary results at different precisions, so results will differ depending on the details of your environment. If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended.
                        Last edited by Stefan_1982; 04-12-2017, 12:46 AM.

                        Comment


                          #13
                          Double CompareTo method

                          Yep, known problem with doubles in general, does not matter the programming language. You might get more accurate results using the double.CompareTo method in C#.

                          https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

                          Comment


                            #14
                            So here is my question to Ninjatrader why are you using double?
                            is there a special reason?
                            Please let me know.
                            Thanks

                            Comment


                              #15
                              Hello Stefan_1982,

                              I would not be able to comment on why NT uses double however I would suggest seeing the following reference example, Floating-Point Arithmetic,


                              Please let us know if you need further assistance.
                              Alan P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by TraderBCL, Today, 04:38 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post TraderBCL  
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              105 views
                              0 likes
                              Last Post martin70  
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              471 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Working...
                              X