Announcement

Collapse
No announcement yet.

Trendchange ... with LinRegSlope

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

  • Trendchange ... with LinRegSlope

    PHP Code:
    function preMain()
    {
        
    setStudyTitle("SEB LinRegSlope");
        
    setCursorLabelName("Slope"0);
        
    setCursorLabelName("ZeroLine"1);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarFgColor(Color.cyan1);
    }
    function 
    main(Length)
    {
        if (
    Length == nullLength 21;
        var 
    SL 0.0;
        var 
    Close getValue("Close"0, - Length);
        var 
    SumBars Length * (Length 1) * .5;
        var 
    SumSqrBars = (Length 1) * Length * (Length 1) / 6;
        var 
    Sum1 0.0;
        var 
    SumY 0.0;
        var 
    0;
        for (
    0Lengthi++)
        {
            
    Sum1 += Close[i];
            
    SumY += Close[i];
        }
        var 
    Sum2 SumBars SumY;
        var 
    Num1 Length Sum1 Sum2;
        var 
    Num2 SumBars SumBars Length SumSqrBars;
        
    SL Num1 Num2;
        return new Array(
    SL0);

    i want that the trend changes at the highest and at the deepest point...
    i was thinking about an oldSL and SL and if SL is bigger than the oldSL than the trend is upwards and when SL < oldSL than downwards...
    does anybody know how i have to code this...
    thanks and regards erilein

  • #2
    erilein
    The enclosed modification should do what you want. Comments are in the script
    Alex



    PHP Code:
    var SL=null//added
    var SL1=null;//added

    function preMain()
    {
        
    setStudyTitle("SEB LinRegSlope");
        
    setCursorLabelName("Slope"0);
        
    setCursorLabelName("ZeroLine"1);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarFgColor(Color.cyan1);
    }
    function 
    main(Length){
        
    //begin added section
        
    if(getBarState()==BARSTATE_NEWBAR && SL!=null){
        
    SL1=SL;
        }
        
    //end added section 
        
    if (Length == nullLength 21;
        
    //var SL = 0.0; //commented out
        
    var Close getValue("Close"0, - Length);
        var 
    SumBars Length * (Length 1) * .5;
        var 
    SumSqrBars = (Length 1) * Length * (Length 1) / 6;
        var 
    Sum1 0.0;
        var 
    SumY 0.0;
        var 
    0;
        for (
    0Lengthi++)
        {
            
    Sum1 += Close[i];
            
    SumY += Close[i];
        }
        var 
    Sum2 SumBars SumY;
        var 
    Num1 Length Sum1 Sum2;
        var 
    Num2 SumBars SumBars Length SumSqrBars;
        
    SL Num1 Num2;
        
    //begin added section
        
    if(SL>SL1)
        
    setBarFgColor(Color.lime);
        
    //end added section 
        
    return new Array(SL0);

    Comment


    • #3
      thank you... this is what i was looking for

      Comment


      • #4
        ok i tested it works fine ... but there is a little bug in it
        when SL == SL1 then the fg color is also set green.
        i tried
        PHP Code:
        if(SL>SL1 && SL!=SL1)setBarFgColor(Color.lime); 
        but it doesnt work like i want it to
        PHP Code:
        var SL=null//added
        var SL1=null;//added
        var =0;
        function 
        preMain()
        {
            
        setStudyTitle("SEB LinRegSlope");
            
        setCursorLabelName("Slope"0);
            
        setCursorLabelName("ZeroLine"1);
            
        setDefaultBarFgColor(Color.black0);
            
        setDefaultBarFgColor(Color.cyan1);
        }
        function 
        main(Length){
            
        //begin added section
            
        if(getBarState()==BARSTATE_NEWBAR && SL!=null){
            
        SL1=SL;
            }
            
        //end added section 
            
        if (Length == nullLength 21;
            
        //var SL = 0.0; //commented out
            
        var Close getValue("Close"0, - Length);
            var 
        SumBars Length * (Length 1) * .5;
            var 
        SumSqrBars = (Length 1) * Length * (Length 1) / 6;
            var 
        Sum1 0.0;
            var 
        SumY 0.0;
            var 
        0;
            for (
        0Lengthi++)
            {
                
        Sum1 += Close[i];
                
        SumY += Close[i];
            }
            var 
        Sum2 SumBars SumY;
            var 
        Num1 Length Sum1 Sum2;
            var 
        Num2 SumBars SumBars Length SumSqrBars;
            
        SL Num1 Num2;
            
        //begin added section
            
        if(SL>SL1 && SL!=SL1)setBarFgColor(Color.lime);
              
            
        //end added section 
            
        return new Array(SL0);

        thanks and regards erilein

        Comment


        • #5
          erilein
          I checked the formula and it does not always happen that if SL==SL1 the line is green. There are instances in which it is red.
          I am guessing it may have to do with decimals.
          Anyhow here is a possible fix that may work. Replace the relevant section with the following
          Alex

          PHP Code:
          //begin added section
              
          var x=(SL*1).toFixed(2)
              var 
          x1=(SL1*1).toFixed(2)
              if(
          x>x1)
              
          setBarFgColor(Color.lime);
              
          //end added section 

          Comment


          • #6
            hmm... doesnt work @ my pc...
            here is the result i get


            but i want it like that


            what do i have to change ?

            please help.... regards erilein

            ----EDIT----

            I mean the first formula u posted was very very good the only problem was that the color was changed @2 same vars
            ( e.g. 0.5 and 0.5 ) when it is a upward trend it should not change the color...

            regards erilein
            Last edited by erilein; 12-29-2003, 12:19 PM.

            Comment


            • #7
              ok my daddy has found an error.... there is 3 times the same slope ... and the color changes 2 times...

              here it is

              Comment


              • #8
                erilein
                After looking closer at the plot from the first formula I think it may be correct if you do not want it to be colored green when SL=SL1.
                The reason for the apparent error can be found in this message which is the same thing happening in this instance (see image below).



                If that is the case then the solution may be a lot simpler. Add the following in preMain()
                setPlotType(PLOTTYPE_INSTANTCOLORLINE, 0);
                which will return the plot in the image below.
                Alex

                Comment

                Working...
                X