Announcement

Collapse
No announcement yet.

Converting a number to a price

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

  • Converting a number to a price

    I'm using an EFS to do some backtesting. I buy at one moving average and sell at another.

    the problem is that eMA is a precise number and not a price e.g. the eMA on the ES could be 1180.443 but you can't sell at that price. I want to sell at the nearest actual ES price which, in this case, would be 1180.5 or 1180.25.

    Is there a function to convert a normal number into a price. I'm aware of formatPriceNumber but I think that formats a price as text for printing which isn't want I need. I need a valid price i.e 1180.25, 1180.50, 1180.75 etc.

    On a side note, what happens if you pass an invalid price to strategy.doSell e.g. you pass 1180.443 instead of 1180.25?

    Thank you.

  • #2
    Re: Converting a number to a price

    BerkoBob
    Use the rounding function Steve Hare posted here which will do what you are looking for
    As to your second question the strategy will use whatever price you pass to it
    Alex


    Originally posted by BerkoBob
    I'm using an EFS to do some backtesting. I buy at one moving average and sell at another.

    the problem is that eMA is a precise number and not a price e.g. the eMA on the ES could be 1180.443 but you can't sell at that price. I want to sell at the nearest actual ES price which, in this case, would be 1180.5 or 1180.25.

    Is there a function to convert a normal number into a price. I'm aware of formatPriceNumber but I think that formats a price as text for printing which isn't want I need. I need a valid price i.e 1180.25, 1180.50, 1180.75 etc.

    On a side note, what happens if you pass an invalid price to strategy.doSell e.g. you pass 1180.443 instead of 1180.25?

    Thank you.

    Comment


    • #3
      Perfect! Thank you.

      Comment


      • #4
        BerkoBob
        You are welcome
        Alex


        Originally posted by BerkoBob
        Perfect! Thank you.

        Comment


        • #5
          An alternative. Put this prototype in your EFS code:

          PHP Code:
          Number.prototype.round = function(tick) {
            return 
          Math.round(this tick) * tick;
          }; 
          Then you can write code like this:

          PHP Code:
          var num 1024.44;
          var 
          num2 num.round(0.25);  // num2 = 1024.50 
          Last edited by SteveH; 10-25-2010, 07:04 AM.

          Comment


          • #6
            Thank you Steve, that's very helpful.

            I now have a new problem (let me know if I should create a new post) which is getting a reliable min tick value.

            I've been using getMinTick() but am getting variable results on Forex (works fine with futures).

            e.g. I was using EURGBP A0-FX but everytime I ran my EFS I got a different result from getMinTick() and hence different trading results. The first time I ran it getMinTick() gave me 0.0, the second time 0.00011 and then 0.00001 - I don't think it ever gave me the correct result of 0.0001.

            Any ideas?

            Comment


            • #7
              I think the only problem you're going to have is with those Forex currency pairs. For stocks and futures from the regulated markets (e.g., Globex, NYBOT, NYMEX, etc), I think getMinTick() will be okay.

              There's a file in your eSignal program directory called "specs.tab" and, in it, you can see a field where they put the minimum tick value in the symbol specifications. Looks like they're missing for the Forex pairs and the eSignal charting program does some heuristic to figure it out.

              I recommend you save yourself a lot of time and hard-code the ones that are giving you problems with this kind of template code:

              PHP Code:
              var vTick null;


              function 
              preMain()
              {
                
              vTick getTick();
              }


              function 
              getTick()
              {
                var 
              vSymbol = ((getSymbol()).split(" "))[0];

                if (
              vSymbol == "EURGPB" || vSymbol == "EURCHF" || ...)
                {
                  return 
              0.0001;
                }
                else if (...)  
              // whatever currency pairs you know would give you 5 significan digits
                
              {
                  return 
              0.00001;
                }

                return 
              getMinTick();

              Here, you're just writing a function to intercept the ones you know aren't giving you the correct, consistent value and then passing off all the other ones to the getMinTick() function which will correctly give you the others.

              Then, later in your code when you need to get a rounded value, you can do this (with the code I posted before):

              PHP Code:
              // assuming price is declared with a var somewhere
              // and has a non-rounded value in it
              price price.round(vTick); 

              Comment

              Working...
              X