Reading A Datafile Example

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

The following code is an example of how to read a Datafile into Optuma

//
// Sample script for reading bar data from a file
// Sample data from the file
//
// Date,Time,Open,High,Low,Close,Volume,OI
// 2009-03-09,00:00:00,26.88,26.97,26.43,26.64,3896429,0
// 2009-03-10,00:00:00,26.85,27.78,26.75,27.78,5748436,0
// 2009-03-11,00:00:00,28.80,28.91,28.25,28.61,5789265,0
//

// This section is where variable are defined that need to be used in both the Init and Process procedures
var
    Plot1 : TPlot;

// 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 := 'ReadDataFile';
    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);
var
    i : Integer;
    dataFile, dataRow : TStringList;
begin
    Plot1 := Tool.AddPlot();
    Plot1.Color := clBlue;
    Plot1.PlotStyle := Shaded; // possible values are (Line, Dot, Histogram, Step, Shaded)
    Plot1.FillColor := clBlue;

    dataFile := TStringlist.Create;
    dataFile.Delimiter := #10;
    dataFile.LoadFromFile('c:\data\data.csv');
    dataRow := TStringList.Create;
    dataRow.Delimiter := ',';

    Plot1.Data.SetSize(dataFile.Count-1);
    // start from 1 because the file has a header
    for i := 1 to dataFile.Count-1 do
    begin
    dataRow.Clear;
    // break up each row into fields
    dataRow.CommaText := dataFile[i];
    if dataRow.Count > 5 then
    begin
        Plot1.Row[i-1].Date := VarToDateTime(dataRow[0]);
        Plot1.Row[i-1].Open := StrToFloatDef(dataRow[2], 0);
        Plot1.Row[i-1].High := StrToFloatDef(dataRow[3], 0);
        Plot1.Row[i-1].Low := StrToFloatDef(dataRow[4], 0);
        Plot1.Row[i-1].Close := StrToFloatDef(dataRow[5], 0);
        Plot1.Row[i-1].Hidden := False;
    end;
    end;
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);
begin

end;

Discussion