Moving a Trendline Around A Chart

 

This script may or may not be of interest to anybody directly, but it does also show by example how to read the keyboard, which may increase its usefulness.

Be warned that reading keys like this will take key presses that "belong to anyone". In other words if the key is pressed for any reason the script will react. It doesn't matter if the window focus is on a Word document on another screen, this script will read the key and act on it. I do not recommend this method for live trading activities such as one key buy/sell for the reason just mentioned. I am just using it to test some ideas on charts.

#import "user32.dll"
   int GetKeyState(int nVirtKey);
#import

// This script puts a trend line on a chart between two Close prices
// You can move this line left or right and change its length (in time)

/* The keys used are :

   TURBO move LEFT = G
   Move LEFT       = H
   TURBO move RIGHT= K
   Move Right      = J
   
   TURBO length increase = I
   Length Increase       = U
   TURBO length decrease = M
   Length Decrease       = N
*/

int initialBar = 1;
int finalBar   = 100;

#define AUTO_REPEAT 200
#define TURBO 10
//-----------------------------------------------------------------------------------+
int init(){
   Comment("");
   DrawLine(finalBar,initialBar);
   return( 0 );
}
//-----------------------------------------------------------------------------------+
int deinit(){
   Comment("");
   return( 0 );
}
//-----------------------------------------------------------------------------------+
int start(){
   int n=0;
  
   while( !IsStopped() ){
      Sleep(100);
      
      while( KeyDown('G') ){ // TURBO MOVE LEFT
         if( finalBar < Bars-1 ){
            for( n=0; n<TURBO; n++ ){
               initialBar++;
               finalBar++;
               if( finalBar >= Bars - 1 )
                  break;
            }
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }
      
      while( KeyDown('H') ){ // MOVE LEFT
         if( finalBar < Bars-1 ){
            initialBar++;
            finalBar++;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }
      
      while( KeyDown('K') ){ // TURBO MOVE RIGHT
         if( initialBar >= 1 ){
            for( n=0; n<TURBO; n++ ){
               initialBar--;
               finalBar--;
               if( initialBar <= 0 )
                  break;
            }
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }

      while( KeyDown('J') ){ // MOVE RIGHT
         if( initialBar > 1 ){
            initialBar--;
            finalBar--;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }
      
      while( KeyDown('M') ){ // TURBO REDUCE LENGTH (DOWN)
         if( finalBar> initialBar+1 ){
            finalBar-= TURBO;
            if( finalBar < initialBar + 1 )
                finalBar = initialBar + 1;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }

      while( KeyDown('N') ){ // REDUCE LENGTH (DOWN)
         if( finalBar> initialBar+1 ){
            finalBar--;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }
      
      while( KeyDown('I') ){   // TURBO INCREASE LENGTH (UP)
         if( finalBar < Bars-1 ){
            finalBar+= TURBO;
            if( finalBar > Bars - 1 )
                finalBar = Bars - 1;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }

      while( KeyDown('U') ){   // INCREASE LENGTH (UP)
         if( finalBar < Bars-1 ){
            finalBar++;
            DrawLine(finalBar,initialBar); // only when something has changed
         }
         Sleep(AUTO_REPEAT);
      }
   }    
   
   return(0);
}
//-----------------------------------------------------------------------------------+
bool KeyDown( int chr ){   // Accepts upper case letters only
   if( chr < 'A' || chr > 'Z' ){
      Print("Invalid Character" + CharToStr(chr) );
      return( false);
   }
   
   int nVirtKey = chr - 'A' + 0x41;       // The Virtual KeyCode for "A" is 0x41
   if( GetKeyState(nVirtKey) < -100 )
      return( true );

   return( false );
}
//------------------------------------------------------------------------------------+
bool DrawLine( int last, int first ){
   Comment("First bar is " + first + "\nLast bar is " + last + "\nLength is " + (last-first));
   ObjectDelete("trendy");
   ObjectCreate( "trendy",OBJ_TREND,0, Time[last],Close[last],Time[first],Close[first]);
   ObjectSet( "trendy", OBJPROP_COLOR, Aqua);
   ObjectSet( "trendy", OBJPROP_RAY, false );
   ObjectSet("trendy", OBJPROP_WIDTH, 3);
   return( true );
}
 

Cool. like paying a game.

How about ObjectDeleteAll in deinit() ?.

D'you have a mouse clicks ?

 
onewithzachy:

Cool. like paying a game.

How about ObjectDeleteAll in deinit() ?.

I really dislike it when people do that . . . . IMO an Indicator or Script should delete it's own Objects . . . but not all.
 
RaptorUK:
I really dislike it when people do that . . . . IMO an Indicator or Script should delete it's own Objects . . . but not all.

(: You're right :)

dabbler, I forgot to thank. Thank.

 
RaptorUK:
I really dislike it when people do that . . . . IMO an Indicator or Script should delete it's own Objects . . . but not all.

Just as well I don't have an ObjectsDeleteAll() in the init function ... anymore :-)
Reason: