Announcement

Collapse
No announcement yet.

Real-Time study values VS. Historical study values

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

  • Real-Time study values VS. Historical study values

    I suppose I wanted to delay this as long as possible, but I need to get a deeper understanding of why this happens.

    I am using an EFS developed by TSS as an input into one of my systems. I have successfully written all the code to get the variables from the TSS EFS into my own EFS, so that is not a problem.

    When my own EFS is loaded, it processes bars in the past, and I print out the value for my input from TSS. I get value X. When the EFS catches up to the runs in current bar, I request the same value from the EFS study only on the first tick of the new bar, and I get a TOTALLY different value.

    This was driving me crazy, so I really thought about why this was happening, and I think the answer is clear, but anyone who reads this I would appreciate a stamp of approval.

    Take the following EFS by TSS for instance:

    /************************************************** *****************
    Description : This Indicator plots Ergotic_TSI indicator
    Provided By : Developed by TS Support, LLC for eSignal. (c) Copyright 2002
    ************************************************** ******************/

    function preMain()
    {
    setStudyTitle("Ergotic_TSI");
    setCursorLabelName("ErgTSI", 0);
    setCursorLabelName("SigLin", 1);
    setCursorLabelName("ZeroLine", 2);
    setDefaultBarFgColor(Color.fushcia, 0);
    setDefaultBarFgColor(Color.grey, 1);
    setDefaultBarFgColor(Color.cyan, 2);
    }
    var XA1_1 = 0.0;
    var XA2_1 = 0.0;
    var XA3_1 = 0.0;

    var aXA1_1 = 0.0;
    var aXA2_1 = 0.0;
    var aXA3_1 = 0.0;

    var XA_1 = 0.0;

    function main(sPrice, r, s, u, ZeroLine, SmthLen)
    {
    if (sPrice == null) sPrice = "Close";
    if (r == null) r = 4;
    if (s == null) s = 8;
    if (u == null) u = 6;
    if (ZeroLine == null) ZeroLine = 0;
    if (SmthLen == null) SmthLen = 3;
    var Price = getValue(sPrice, 0, -2);
    var vPrice = Price[0] - Price[1];
    var FactorR = 2 / (r + 1);
    var FactorS = 2 / (s + 1);
    var FactorU = 2 / (u + 1);
    var XA1 = FactorR * vPrice + (1 - FactorR) * XA1_1;
    var XA2 = FactorS * XA1 + (1 - FactorS) * XA2_1;
    var XA3 = FactorU * XA2 + (1 - FactorU) * XA3_1;
    vPrice = Math.abs(Price[0] - Price[1]);
    var aXA1 = FactorR * vPrice + (1 - FactorR) * aXA1_1;
    var aXA2 = FactorS * aXA1 + (1 - FactorS) * aXA2_1;
    var aXA3 = FactorU * aXA2 + (1 - FactorU) * aXA3_1;
    var Val1 = 100 * XA3;
    var Val2 = aXA3;
    var TSI = 0.0;
    if (Val2 != 0) TSI = Val1 / Val2;
    else TSI = 0.0;
    var Factor = 2 / (SmthLen + 1);
    var XA = Factor * TSI + (1 - Factor) * XA_1;
    if (getBarState() == BARSTATE_NEWBAR)
    {
    XA1_1 = XA1;
    XA2_1 = XA2;
    XA3_1 = XA3;
    aXA1_1 = aXA1;
    aXA2_1 = aXA2;
    aXA3_1 = aXA3;
    XA_1 = XA;
    }
    return new Array(TSI, XA, ZeroLine);
    }

    Some of the code in this I just cross off to computation, and I am not going into extreme depth on it, but the essentials for price in terms of high, low, open and close are computed on the full bar once it is formed. Meaning, that on the last possible tick of a bar. In this manner, the EFS has all the data to compute a return value for that bar based on all possible data points that made that bar.

    When I write an EFS, I can only react to that signal, on the very first tick of the NEXT bar, so I coded my EFS to reflect this, and only process signals on the next bar, and as an error, I was calling the value from my TSS EFS on the very FIRST tick of the NEW bar. The first tick of the new bar has identical values for open, close, high and low. It is no wonder why my values were so off compared to backtesting and real-time results.

    Am I correct in this assumption?

    I have since corrected my EFS and will be able to fully test on Monday's market. I will post my findings then.

    Regards,

    Thomas.

  • #2
    Hi,

    When I write an EFS, I can only react to that signal, on the very first tick of the NEXT bar, so I coded my EFS to reflect this, and only process signals on the next bar, and as an error, I was calling the value from my TSS EFS on the very FIRST tick of the NEW bar. The first tick of the new bar has identical values for open, close, high and low. It is no wonder why my values were so off compared to backtesting and real-time results.
    Jason and I were just going over this on another thread for someone else. You are basically correct.

    If you wait for NEWBAR then it is really run on the first tick on the start of a new bar. For historical data this means you are getting the high/low/close for the current bar. In essence BARSTATE_CURRENTBAR and BARSTATE_NEWBAR will give the same results with historical data.

    However, in realtime mode if you wait for a NEWBAR, all the data you really want is one bar back, but if your calculations are based on just close(), the odd's are you can use close(0) and get the same (or very, very close to the same) value as the closing value of the last bar when running. However if you also use high() and low() or volume() then you will want to make sure you are looking
    one bar back.

    Hope this helps.

    Garth
    Garth

    Comment


    • #3
      Exactly . . .

      I changed my EFS and everything should be fine now, but you know how it is when you develop something and you zip right through something that is innocent in nature and then find out it is SUCH an important element.

      Thanks for your help, I really appreciate knowing you guys are always around. If there is anything I can ever do for the e-sig community, let me know. I try to post my greatest blunders whenever possible.

      Regards,

      Thomas.

      Comment

      Working...
      X