Announcement

Collapse
No announcement yet.

in need for a sleak EMA function (a real good one!)

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

  • in need for a sleak EMA function (a real good one!)

    hi fellow traders,
    im currently in the process of writing an efs trading system. im calculating a handfull (or two) of custom homegrown indicators. since a few of them are somewhat choppy i want to use EMAs to smooth them. now here's the problem:

    all the efs scripts i've come along are quite ugly - in other words it is not a real template that you can use for more than one EMA in the same script.

    the latest version - alexis ma-ema-wmaof_template.efs would solve the problem of calculating EMAs on my vars - but since i will have more than one ema in my tradings system script (short:TSS)
    i would have to add dozens of control variables outside of main (one set for each single EMA) and - what is much worse - you have to write multiple EMA formulas since the control variables are called from within the EMA functions (one function foe each individual EMA)!. this is true for all different EMA scripts ive found.

    is there a better solution already out somewhere ? There must be a better solution (programmingwise) . for instance linnsofts investor/rt based on esignal offers an EMA() template and you can feed a custom indicator to it too. so you only call
    EMA(indicator1,10);
    EMA(indicator2,20);
    EMA(indicator3,5);
    EMA(indicator4,8);
    this is neat code! one size fits all.no multiple EMA functions, not 100 control vars.

    I really wonder why nobody else has recognized the weakness of EMA functions so far.... or did I miss something?

    regards,
    EZ-T

  • #2
    Actually, I believe you are the first person ;>)

    Regardless, if you have seen any posts on EFS2, this is the next generation of technical indicators that will perform the studies in a simpler mannner. EFS2 functionality is currently being run through the beta testing process.

    Comment


    • #3
      Hello EZ-T,

      As Steve mentioned, this will be quick and easy process in EFS2. Until that arrives you can recycle a single EMA function to handle all your ema calcs. It's a little bit advanced, but you should be able to incorporate this into your study.

      For each indicator or data series you need an EMA for, you'll need to store it's values in a separate array. You might be doing this already. You'll also need to do a pop/unshift routine on each array to maintain the proper length for each. In the code snippet below you will see some global arrays, bPrimed, dPercent, vEMA and vEMA1. Set the length of these arrays to match the number of EMA calcs you will need.

      PHP Code:
      var bPrimed = new Array(7);
      bPrimed[0] = false;  // zm
      bPrimed[1] = false;  // e1
      bPrimed[2] = false;  // e2
      bPrimed[3] = false;  // e3
      bPrimed[4] = false;  // e4
      bPrimed[5] = false;  // e5
      bPrimed[6] = false;  // e6

      var dPercent = new Array(7);
      var 
      vEMA = new Array(7);
      var 
      vEMA1 = new Array(7);

      function 
      EMA(nLengthnArraynNumnIndicator) {
          var 
      nBarState getBarState();
          var 
      dSum 0.0;

          if(
      nBarState == BARSTATE_ALLBARS || bPrimed[nNum] == false) {
              
      dPercent[nNum] = (2.0 / (nLength 1.0));
              
      bPrimed[nNum] = false;
          }

          if (
      nBarState == BARSTATE_NEWBAR) {
              
      vEMA1[nNum] = vEMA[nNum];
          }

          if(
      bPrimed[nNum] == false) {
              for(
      0nLengthi++) {
                  
      dSum += nArray[i];
              }
              
      bPrimed[nNum] = true;
              return (
      dSum nLength);
          } else {
              return (((
      nIndicator vEMA1[nNum]) * dPercent[nNum]) + vEMA1[nNum]);
          }

      vEMA array stores the current bar's EMA value for each indicator.
      vEMA1 array stores the previous bar's EMA value for each indicator.

      When you call the EMA() function in main, you'll need to pass in the following parameters.

      nLength - Length for the number of EMA periods.
      nArray - The array of indicator values.
      nNum - The indicators array number that keeps track of it's bPrimed value and position for the EMA arrays. Each of your indicators will be assigned to a specific array position in the EMA arrays. So when ever you want to get the ema value for a specific indicator you need to tell the EMA() function which one you're requesting. Put some comments after the bPrimed[x] = false; lines to help you remember which is which. In main, this array number you pass will be hard coded.
      nIndicator - The current bar's indicator value.

      Give it a go and if you run into problems, post some code here and I'll help you through it.
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment


      • #4
        thanx for your replies guys!

        Great to hear my problem will turn to dust (hopefully) as soon as EFS2 arrives. Many thanx Jason for trying to write a better function in the meantime! It's good to have all you guys around here.

        BTW Steve, I didn't really think I was the first to register the weak EMAf's.
        :=))))))

        best regards,
        EZ-T

        Comment

        Working...
        X