Announcement

Collapse
No announcement yet.

Drawing Trendline between two values

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

  • Drawing Trendline between two values

    Hello

    I’m trying (trying is the key word) to write an EFS file and here is what it’s suppose to do.

    Using the 10 moving average, getting the value of the current bar of the 10 moving average and getting the value of one bar ago of the 10 moving average and drawing a line between the two values and extend it to the right, preserving the angle of the line or the trendline.

    Or in another word drawing a trendline between the current and the last value of the 10 moving average, yet persevering the angle of the trendline.

    Using drawlinerelative you lose the angle of the line since you have to define the end of the line or maybe I’m wrong and there is another way of doing it.


    Or Extending the Moving average to the right base on the current and last value of the moving average.


    Thank you

    Ketoma
    Last edited by ketoma21; 10-16-2003, 12:57 PM.

  • #2
    it can be done...

    This can be done...

    But you have to put into place all of the features necessary to accomplish this task...

    So, here goes with some basic help...

    First, you have to include a bar counter feature. This will allow you to record a bar number and to automatically calculate the START and END time (bar number) of the lines you want to draw.

    Here is some code for a bar counter..

    PHP Code:
    var lastrawtime 0;
    var 
    BarCount 0;

    function 
    main() {

    if (
    lastrawtime != getValue("rawtime"0)) {
      
    BarCount += 1;
      
    lastrawtime getValue("rawtime"0);
    }


    // end of main... 
    Now, with this in place, what you want to do is record the starting position of your line, then calculate the angle and then draw it to the right of the current chart bar...


    PHP Code:
    var study = new MAStudy(100"Close"MAStudy.SIMPLE);

    var 
    lastMABar 0;  //  record the last MA bar count
    var lastMABarlevel 0//  record the last bars MA level.
    var lastrawtime 0;
    var 
    BarCount 0;
    var 
    rightbarcount 15// how many bars to plot to the right of current price.

    function main() {

    if (
    lastrawtime != getValue("rawtime"0)) {
      
    BarCount += 1;
      
    lastrawtime getValue("rawtime"0);

       
    lastMABar BarCount-1;  //  record the last MA bar count
       
    lastMABarlevel study.getValue(MAStudy.MA,-1);//  record the last bars MA level.

    }

    //  Now, we need to calculate the angle of the current line and plot the line x bars to the right.
    var LineAngle study.getValue(MAStudy.MA,0)-study.getValue(MAStudy.MA,-1);
    // calculate the RIGHT edge price
    var rightprice study.getValue(MAStudy.MA,0)+(LineAngle*rightbarcount);

    // draw the line..
    drawLineRelative( (lastMABar-BarCount),lastMABarlevel rightbarcount rightprice PS_SOLID1Color.blue"Line");

    // end of main... 
    Now, using the variables I set up, you could modify the start and end points of the line and everything should be automatic - regarding the line drawing feature.

    Hope this helps..
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Hello Doji3333

      Thank you very much

      The code works great

      After I created this post I tried on my own to work on the problem just in case no one answered me and I came up with this solution which also works great. What do you think of it? Your feed back is greatly appreciated.

      var vMA10 = new MAStudy(10, 0, "Close", MAStudy.SIMPLE);

      function preMain() {
      setPriceStudy(true);
      setStudyTitle("Angle");
      }


      function main() {
      if(getCurrentBarIndex() == 0) {

      clearLineTool(LineTool.RAY);
      addLineTool(LineTool.RAY, -1, vMA10.getValue(MAStudy.MA,-1), 0, vMA10.getValue(MAStudy.MA,0), 2, Color.fushcia, "ray");

      }

      }


      Is there a way you can calculate the actual angle of that line whether it’s 45 degrees or 35 degrees, etc… and placing it on the chart?

      Thank you

      Ketoma

      Comment

      Working...
      X