Announcement

Collapse
No announcement yet.

problem with writing MA of own formula

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

  • problem with writing MA of own formula

    I am new to writing on efs. I have written a simple formula as below. It seems to work on my esignal chart. I am trying to create a formula which reads as a 21 simple moving average of the formula below.

    /
    function preMain() {

    setPriceStudy(false);
    setStudyTitle("My Strength");
    setCursorLabelName("Strength");
    setDefaultBarFgColor(Color.blue);
    setPlotType(PLOTTYPE_LINE);
    setDefaultBarThickness(1);
    }

    function main() {

    return (2*close()-upperDonchian(21)-lowerDonchian(21));
    }


    I went to formula wizard and realised that i could use the function editor to do it. I do not know what to fill in for the property studyVarName and dataSeriesID columns.
    Could someone please guide me how to write this formula.

    Thanks for any help,

    fortune 88

  • #2
    fortune88
    You will not be able to do what you are trying to accomplish using the Formula Wizard. This is because the Formula Wizard uses efs1 functions which do not accept custom variables (such as your equation) as the source. Those functions can only use a price series (ie "High", "Low", "Close", etc) and in some cases another built-in study.
    To do what you want you will need to use the efs2 functions which can use a price series, another built-in study or a custom series as a source.
    To create a custom series you need to first calculate your equation in a separate function or external efs and then retrieve the results of that calculation using the efsInternal() or efsExternal() functions This series can then be used as a source for the built-in studies.
    In your specific case the first thing you need to do is to rename the main() function and call it [for example] Strength() as shown below.
    PHP Code:
    function Strength() {

        return (
    2*close()-upperDonchian(21)-lowerDonchian(21));

    At this point in the same script create a new main() function in which you will use the efsInternal() function to call your Strength() function
    PHP Code:
    function main(){
        
        var 
    myVar efsInternal("Strength");//myVar is now a series and can be used as a source of a builtin study
        
    var myVarAvg sma(21myVar);//myVar is used as the source of the sma() function

        
    return new Array (myVarmyVarAvg);

    Note that the above example is written in the simplest possible form [which is not necessarily the most efficient] so as to make the logic more apparent.
    For the complete description of the efsInternal() and efsExternal() functions and the required syntax see the links above which will take you to the corresponding articles in the EFS KnowledgeBase.
    You can also find several detailed examples on how to use efsInternal() and efsExternal() in this and this thread.
    Alex

    Comment


    • #3
      Dear Alex,
      Many thanks for your reply.
      I am now able to plot var myVarAvg.
      I am now trying to set the condition below

      if (myVarAvg(0)>myVarAvg(-1)){
      setBarBgColor(Color.blue);}
      if (myVarAvg(0)<myVarAvg(-1)){
      setBarBgColor(Color.red);}
      return new Array (myVar, myVarAvg);

      What i want to see is that my indicator var myVarAvg is blue when it is more that the previous time frame( eg day) and red when reverse condition.

      I would also like to see the same color rule applied to the price bars.

      I applied the following , but it results in a syntax error.


      function preMain() {

      setPriceStudy(false);
      setStudyTitle("My Strength");
      setCursorLabelName("myVar",0);
      setCursorLabelName("myVarAvg",1);
      setColorPriceBars(true);
      setDefaultBarFgColor(Color.blue,0);
      setDefaultBarFgColor(Color.gray,1);
      setPlotType(PLOTTYPE_LINE,0);
      setPlotType(PLOTTYPE_LINE,1);

      setDefaultBarThickness(1,0);
      setDefaultBarThickness(1,1)
      }

      function Strength() {

      return (2*close()-upperDonchian(21)-lowerDonchian(21));
      }

      function main(){

      var myVar = efsInternal("Strength");//myVar is now a series and can be used as a source of a builtin study
      var myVarAvg = sma(21, myVar);//myVar is used as the source of the sma() function


      if (myVarAvg(0)>myVarAvg(-1)){
      setBarBgColor(Color.blue);}
      if (myVarAvg(0)<myVarAvg(-1)){
      setBarBgColor(Color.red);}
      return new Array (myVar, myVarAvg);

      Thanks again for your time and assistance.
      Warm regards,
      fortune 88

      Comment


      • #4
        fortune88
        You are most welcome.
        Once you have a series you need to use the .getValue() method to retrieve any of its values.
        If you then wish to color the average you need to use the setBarFgColor() command and assign it to the specific element of the return array using the series index parameter (0 is the first element of the array, 1 is the second, etc)
        With regards to painting the price bars you need to add the setPriceBarColor() command to your existing conditions. Note that as indicated in the linked article setPriceBarColor() needs to be used in conjunction with setColorPriceBars() and setDefaultPriceBarColor()
        Your example would be written as shown below
        Alex

        PHP Code:
        if (myVarAvg.getValue(0)>myVarAvg.getValue(-1)){
            
        setBarBgColor(Color.blue,1);//the ,1 assigns the command to the second element of the return array
            
        setPriceBarColor(Color.blue);
        }
        if (
        myVarAvg.getValue(0)<myVarAvg.getValue(-1)){
            
        setBarBgColor(Color.red,1);
            
        setPriceBarColor(Color.red);

        Comment


        • #5
          Dear Alexis,

          Thanks again.

          This time when I introduced the additional instruction PHP as indicated in your last answer, it resulted in the myVarAvg indicator line alone being seen with the whole background segment of the window being colored blue or red. Additionally, the myVar indicator is no longer visible.

          What I wanted to achieve was for both the myVar and myVarAvg lines to be visible, and for the myVarAvg line only to be colored blue or red as the case may be. I did not want the background to be colored blue or red but to just maintain the usual (in my case) white background color.

          I wonder if you could help me again to sort this out. Sorry for the inconvenience.

          Warm regards,

          Fortune 88

          Comment


          • #6
            fortune88
            If you want to color the average you need to replace the setBarBgColor() function with the setBarFgColor() function suggested in my prior reply. The example I provided in that reply was simply using your code to illustrate how the conditional statement needed to be written
            Alex

            Comment

            Working...
            X