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

Condition not evalulting as expected

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

    Condition not evalulting as expected

    I want some hints to correct the condition that does not evaluate properly.

    The condition is as simple as:
    Code:
                    SC11XXLCond = (Convert.ToInt32(TheStrat0123()[2]) == 1 ? true : false)
    The print formula is:
    Code:
    Print (Time[0] + " | SC11XXLCond: " + SC11XXLCond + " | " + Convert.ToInt32(TheStrat0123()[2]));
    In this case, I just tried to load both variables

    Sample of print result is:
    Code:
    2020-11-27 17:22:28 | SC11XXLCond: True | 0
    2020-11-27 17:24:29 | SC11XXLCond: True | 0
    2020-11-27 17:25:25 | SC11XXLCond: False | 2
    2020-11-27 17:26:34 | SC11XXLCond: True | 1
    2020-11-27 17:29:32 | SC11XXLCond: True | 1
    2020-11-27 17:31:42 | SC11XXLCond: True | 2
    2020-11-27 17:33:57 | SC11XXLCond: False | 2
    2020-11-27 17:36:12 | SC11XXLCond: True | 1
    2020-11-27 17:41:01 | SC11XXLCond: True | 1
    2020-11-27 17:44:59 | SC11XXLCond: True | 2
    2020-11-27 17:48:31 | SC11XXLCond: False | 0
    2020-11-27 17:53:46 | SC11XXLCond: True | 1
    2020-11-27 17:58:01 | SC11XXLCond: True | 1
    2020-11-27 18:01:32 | SC11XXLCond: True | 0
    2020-11-27 18:04:36 | SC11XXLCond: False | 2
    2020-11-27 18:08:15 | SC11XXLCond: True | 1
    2020-11-27 18:13:43 | SC11XXLCond: True | 3
    2020-11-27 18:16:41 | SC11XXLCond: True | 0
    2020-11-27 18:20:15 | SC11XXLCond: True | 2
    2020-11-27 18:23:39 | SC11XXLCond: True | 2
    2020-11-27 18:28:12 | SC11XXLCond: True | 2
    2020-11-27 18:31:53 | SC11XXLCond: True | 0
    2020-11-27 18:34:11 | SC11XXLCond: True | 0
    2020-11-27 18:36:15 | SC11XXLCond: True | 0
    2020-11-27 18:36:26 | SC11XXLCond: True | 3
    2020-11-27 18:38:04 | SC11XXLCond: True | 2
    2020-11-27 18:41:14 | SC11XXLCond: True | 2
    2020-11-27 18:44:34 | SC11XXLCond: False | 0
    2020-11-27 18:48:08 | SC11XXLCond: True | 1
    However, it should only evaluate as True, if the result is 1, but, as you can see, on random occassions it evaluates as True, even if the result is 0, 1, 2 or 3.

    How to correct that?

    P.s. TheStrat0123()[2] is a simple indicator, that returns values 0, 1, 2, 3. I tried to add code Convert.ToIn32 to solve issue, if it was treated as a string. But this also gave no improvement.
    I also tried changing formula to SC11XXLCond = (Convert.ToInt32(TheStrat0123()[2]) == "1", but that also was futile.

    Ideas?

    #2
    What is also interesting. If I put Convert.ToInt32(TheStrat0123()[2]) == 4 (4 is invalid number, it never occurs in TheStrat0123(), it evalutes correct, it returns False on every bar. But if I put any number between 0, 1, 2, 3, it doesn't evaluate correctly.

    Comment


      #3
      Hello UltraNIX,

      Thank you for the post.

      From the given example I am not certain what may be happening there. In your first post that seems somewhere in that statement its evaluating as a double. I tried this test on my end and used specific types instead and don't see the result you posted. Here is the result I see:


      1/25/2021 3:00:00 PM | test: 1 | val: 0 Cond: False
      1/26/2021 3:00:00 PM | test: 1 | val: 1 Cond: True
      1/27/2021 3:00:00 PM | test: 1 | val: 2 Cond: False
      1/28/2021 3:00:00 PM | test: 1 | val: 3 Cond: False
      1/29/2021 3:00:00 PM | test: 1 | val: 4 Cond: False
      I attached the test with this print.

      I look forward to being of further assistance.
      Attached Files
      JesseNinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello UltraNIX,

        Thank you for the post.

        From the given example I am not certain what may be happening there. In your first post that seems somewhere in that statement its evaluating as a double. I tried this test on my end and used specific types instead and don't see the result you posted. Here is the result I see:




        I attached the test with this print.

        I look forward to being of further assistance.
        You are basing it on indicator, while I have it on strategy, but that should not be an issue. If testing variables one by one, it evalues ok. But when I have a condition with boolean SC11XXLCond, it doesn't evaluate properly. What can I check and change, in order to fix this?

        Comment


          #5
          Also, if it works, I can get rid of Convert.ToInt32 code part. I only need it to work.

          Comment


            #6
            Hello UltraNIX,

            If you want to upload a zip that contains the setup needed to see this we could explore that further. I would otherwise suggest to approach it like I had in the indicator how explicit types are used for each variable. You could also use that approach to further explore what part of the syntax you used is not equating correctly. As mentioned it looks like part of that may be equating as a double which if it is then == should not be used because that may or may not be true in that type of comparison.

            I look forward to being of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              If I can send it to you privately, as I don't want to share indicator code as of now in pubic, as it's not finished yet, then yes. Private message or Email?

              Comment


                #8
                Hello UltraNIX,

                I would suggest to use the example I provided as a start point or to create your own sample. We cannot go through your existing code to debug it but we can review an extracted sample of a problem.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post
                  Hello UltraNIX,

                  If you want to upload a zip that contains the setup needed to see this we could explore that further. I would otherwise suggest to approach it like I had in the indicator how explicit types are used for each variable. You could also use that approach to further explore what part of the syntax you used is not equating correctly. As mentioned it looks like part of that may be equating as a double which if it is then == should not be used because that may or may not be true in that type of comparison.

                  I look forward to being of further assistance.
                  so what can be used instead of ==?

                  Comment


                    #10
                    Hello UltraNIX,

                    I am not seeing a problem with using equals on two int type variables. If you have a sample that shows otherwise I could test that so I have all the details surrounding what is being done.

                    I otherwise provided a working way to compare two int variables That should also work with a strategy, you could modify that as needed for your use case and re test it.

                    For double type variables and checking if a double equals another double/int you would need to use math to calculate how near you are to that value. https://ninjatrader.com/support/help...arithmetic.htm




                    I look forward to being of further assistance.



                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      I found interesting thing. If I put code of:
                      Code:
                      SC11XXLCond = (Convert.ToInt32(TheStrat0123()[2]) == 1 ? true : false)
                      at the beginning of OnBarUpdate, it evaluates correctly, however, if I nest it inside:
                      Code:
                      if (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short || ENSSBool)
                      Meaning, it would only go inside if there's Long or Short position or ENSSBool is enabled, then it evalutes incorrectly. As for this test ENSSBool is set to False, so it shouldn't produce any issues.

                      But why on earth does it alter the results of the calculation, which is in no way dependant on MarketPosition?

                      Comment


                        #12
                        Hello UltraNIX,

                        I would still suggest to use the sample that I provided in post #3 as a solution, if you are not sure why the code you are currently working with is not working right I would need you to attach a sample that I can test. I can't guess why that's not working but I would be able to test an example of the code you used so I can see what values were being used and how the code is overall being used.

                        Again the solution to comparing an int == int has been provided in post #3 so I would likely suggest to just use that instead of trying to pursue this further.

                        Please let me know if I may be of additional assistance.


                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Reason I cannot use your example from indicator - I don't know how to call it from the strategy, so it would reproduce results in Output window. Anyway, I copied adapted script into my strategy and that simple solution worked. But my code, however, improved, still needs work.

                          Update on my findings.

                          1) I adjusted a code to better reflect my actual solution to this:
                          Code:
                          SC11XXLCond = (((SC11XXLBool && (Str2 == Convert.ToInt32(1) && Str1 == Convert.ToInt32(1)))) ? true : false)
                          SC11XXLCond should evaluate as true, when 1) SC11XXLBool is set to True and 2) Str2 == Convert.ToInt32(1) && Str1 == Convert.ToInt32(1).
                          I also share codes to variables:
                          Str2 = Convert.ToInt32(TheStrat0123()[2]);
                          Str1 = Convert.ToInt32(TheStrat0123()[1]);

                          I attach the extract of Output Window:
                          Code:
                          SC11XXLCond: False | 03
                          SC11XXLCond: False | 32
                          SC11XXLCond: False | 21
                          SC11XXLCond: True | 11
                          SC11XXLCond: [B]False | 11[/B]
                          SC11XXLCond: False | 13
                          SC11XXLCond: False | 32
                          SC11XXLCond: False | 20
                          SC11XXLCond: False | 01
                          SC11XXLCond: False | 12
                          SC11XXLCond: False | 21
                          SC11XXLCond: False | 12
                          SC11XXLCond: False | 22
                          SC11XXLCond: False | 20
                          SC11XXLCond: False | 01
                          SC11XXLCond: True | 11
                          SC11XXLCond: [B]False | 11[/B]
                          SC11XXLCond: True | 11
                          SC11XXLCond: False | 12
                          SC11XXLCond: False | 22
                          What is good - No other scenario evalutes as true, but 11, which is expected. However, sometimes (I listed those times in Bold). I copied all output window data to Excel to compare, how many Times 11 would evaluate as True, and how many - as False. So it's 50/50. So still there's room for improvement.

                          2) What helped me achieve results in 1) point, was moving variable 'instantiation' from place before protected override void OnStateChange() (I used this line:
                          Code:
                          private bool        SC11XXLCond = false, SC11XXSCond = false;
                          )

                          and then I moved it inside protected override void OnBarUpdate():
                          Code:
                          bool SC11XXLCond = false;
                          bool SC11XXSCond = false;
                          Final questions:
                          1. Why instantiating variables before OnStateChange was not producing expected results?
                          2. How can I get to evaluate 11 (or any other code) as True all the time, and not just True at random times, and False at random times?

                          Comment


                            #14
                            Hello UltraNIX,

                            1. Why instantiating variables before OnStateChange was not producing expected results?
                            I couldn't say without more context. Without a sample to see what is being done its just a guess what may be wrong. The support forum works best if you make a simple test of what you are having trouble with and attach it. That gives us a chance to run the code and see what the values were etc. Without that detail I couldn't say why your script is doing X.

                            2. How can I get to evaluate 11 (or any other code) as True all the time, and not just True at random times, and False at random times?
                            If you are referring to the int equality, you need to make sure both values being compared are int values. A good way to do that is to use variables like shown in post 3.


                            Please let me know if I may be of additional assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              I tried adding variables
                              Code:
                              int Num0 = Convert.ToInt32(0);
                              int Num1 = Convert.ToInt32(1);
                              int Num2 = Convert.ToInt32(2);
                              int Num3 = Convert.ToInt32(3);
                              and then compare using: Str2 == Num1 && Str1 == Num1

                              But still same results.

                              Ok I will refactor my solution to only include necessary parts and upload here, because I am running out of options.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Barry Milan, Today, 10:35 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by WeyldFalcon, 12-10-2020, 06:48 PM
                              14 responses
                              1,427 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