Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing Strings or Formatting Ints

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

    Comparing Strings or Formatting Ints

    *ignore the title, they are not ints I'm looking to format.

    I'm working on a strategy and I have two questions.

    First, I'm trying to figure out how to compare two different strings.

    As of now, I have:

    string1 == string2

    which results as a bool and works properly, but I would like to know if string1 is greater/less than string2. The values are converted into strings because I am looking to retrieve a number with 0.00 formatting and that was the only way I know how to. So maybe my problems could be solved with some kind of decimal formatting that I'm missing.

    Secondly, I would like for string2 (if possible) to be a range of numbers. For instance, 0.00 - 0.20.

    So ultimately, I would like to know if something like this:

    string1 < (0.00 through 0.20)

    is possible or even something like:

    string1 > 0.00
    && string1 < 0.20


    Here is the specific line, if it makes a difference:

    ((((((((Close[0] - Low[1])*4)+8)*RiskReward))/35).ToString("F2")) == 0.20.ToString("F2"))


    I'm very much a beginner if you couldn't tell.
    Thanks in advance.
    .


    Last edited by setardiff; 07-15-2024, 04:42 PM.

    #2
    Hello setardiff,

    To round a number to a certain decimal place you can use Math.Round(yourNumber, 2)

    It is not possible to compare strings as numbers without converting them into numbers, if the value started as a number its better to just round it as a number and then compare it as a number.

    To make a condition that is a range of numbers you would need to use 2 conditions checking if the value is greater or equal to the lower value and less or equal to the greater value.

    Rounds a value to the nearest integer or to the specified number of fractional digits.

    Comment


      #3
      Thanks Jesse.

      When I try:

      ((((((((Close[0] - Low[1])*4)+8)*RiskReward))/StopLoss))) > .2;

      I get another cs0019 error, saying '>' cannot be applied to type 'void' and 'double'.

      Is there a way to convert 'void' to 'double' or is there another way to achieve a comparison of these two values?
      I'm assuming I can use Math.Round once I figure out the appropriate values.

      Thanks again.

      Comment


        #4
        Hello setardiff,

        From this code I don't see that you are using a void. Are you certain that is the line having the error? Void is a method that does not return a value.

        Its likely best if you simplify the code into individual values and then do a comparison as the last step, for example:

        Code:
        double unrounded = ((((((((Close[0] - Low[1])*4)+8)*RiskReward))/StopLoss)));
        double rounded = Math.Round(unrounded, 2);
        bool isTrue = rounded > .2;

        Comment


          #5
          Yes, I am certain that line was the problem, as it was highlighted when double clicking the error code.

          As stated, I'm very much a beginner. so forgive me for these seemingly simple questions.

          To clarify, I put:

          double unrounded = ((((((((Close[0] - Low[1])*4)+8)*RiskReward))/StopLoss)));
          double rounded = Math.Round(unrounded, 2);

          in the DataLoaded section and use:

          bool isTrue = rounded > .2;

          as the comparison within the OnBarUpdate/conditions section?

          I tried that and am attempting to Print (bool isTrue = rounded > .2); within the actions and it will not compile.

          I assume I am just misplacing these lines.

          I appreciate your help.




          Comment


            #6
            Hello setardiff,

            That code cannot go in State.DataLoaded, bars have not processed in DataLoaded. To use bar data you need to put that code inside OnBarUpdate. All 3 lines would go in OnBarUpdate.

            Comment


              #7
              All set, thanks again, much appreciated.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              48 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              126 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              66 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X