Time Events Example

Author: Optuma Team Last updated: Jan 23, 2024 09:00

The following code is an example of how to create a tool based on Time Events.

//
// Sample script for Optuma tool scripting
// Creates three vertical lines yesterday, 30 days ago and 60 days ago
// It also creates a vertical line for every bar that has the Close greater than the Open
// It also adds a region between 45 days ago and 10 days ago
//

// This section is where variable are defined that need to be used in both the Init and Process procedures
var
    Events : TEvents;
    Event : TEvent;
    bCreateEvents : Boolean;

// DefineTool is where the settings for the Tool are defined
// This procedure is called once when the tool is loaded
// Normally this procedure does not need to be changed
procedure DefineTool(Tool : TTool);
begin
    Tool.Name := 'Time Events';
    Tool.MouseClicks := 1;
    Tool.Hint := '';
    Tool.ToolType := ttDataList;
end;

// Init is called to initialise the tool
// This procedure is called once when the tool is added to a chart
procedure Init(Tool : TTool);
begin
   Events := Tool.AddEvents();
   Events.Extend := true;
   Events.AddRegion(Now()-45, Now()-10, clBlue);    // This creates a region between 45 days ago and 10 days ago
   Events.FillTransparency := 40;

   Event := Events.AddEvent(Now()-1, clRed);
   Event := Events.AddEvent(Now()-30, clRed);
   Event := Events.AddEvent(Now()-60, clBlue);

   bCreateEvents := True;
end;

// Process is called to calculate and drawn the tool on screen
// This procedure is called when new data is received or loaded and
// when a selection point is moved by the user
procedure Process(Tool : TTool; ProcessStart : Integer; ProcessEnd : Integer; DataIn : TDataList);
var
  i : Integer;
begin
   if bCreateEvents then  // this is so the lines are drawn only the first time
   begin
      for i := ProcessStart to ProcessEnd do
      begin
         if DataIn.Row[i].Close > DataIn.Row[i].Open then
         begin
            Events.AddEvent(DataIn.Row[i].Date, clRed);
         end;
      end;
   end;
   bCreateEvents := False;
end;

Discussion