Get stock quotes using a GET request in JSON format and connect them to FastReport VCL 6

2020-05-07

logo FR      logo FR

Today we will look at the way to get stock quotes using a GET request with HTTPS protocol and public tradingviewapi.docs.apiary.io API

According to the documentation
https://tradingviewapi.docs.apiary.io/#reference/0/history/0?console=1 in order to get stock quotes you need to use the GET request history

GET history

https://api.bcs.ru/udfdatafeed/v1/history?symbol=BRENT&resolution=60&from=1450772216&to=1450858616

URI PARAMETERS

Name

Example

Description

symbol

BRENT

currency pair

resolution

D

discreteness of candles, possible values: 1, 5, 15, 30, 45, 60, 120, 180, 240, D, W, M

from

1450772216

beginning of period

to

1450858616

end of period

Create an application and add components to the form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 frxReport1: TfrxReport;
 JSON_DS: TfrxUserDataSet;
 ButtonConnectToJSON: TButton;
 Label1: TLabel;
 Label2: TLabel;
 Label3: TLabel;
 ComboBoxName: TComboBox;
 ComboBoxResolution: TComboBox;
 DateTimePickerFrom: TDateTimePicker;
 Label4: TLabel;
 DateTimePickerTo: TDateTimePicker;
 ButtonShowReport: TButton;
 Image1: TImage;
 Label5: TLabel;
 StatusBar1: TStatusBar;
 ButtonDesign: TButton;
 frxDesigner1: TfrxDesigner;
 frxChartObject1: TfrxChartObject;
 frxPDFExport1: TfrxPDFExport;

FastReport VCL

Add items to ComboBoxName and ComboBoxResolution

1
2
ComboBoxName.Items := 'GAZP SBER BRENT MOEX ROSN YNDX RUAL';
ComboBoxResolution.Items := '1 5 15 30 45 60 120 180 240 D W M';

Add global variables

1
2
3
4
5
6
7
8
var
 tHTTP: TfrxTransportHTTP;
 frxJSON: TfrxJSON;
 Res: String;
 Symbol,Resolution,FromCandlesHistory,ToCandlesHistory : String;
 frxJSONArrayT,frxJSONArrayC,frxJSONArrayO,
 frxJSONArrayH,frxJSONArrayL,frxJSONArrayV: TfrxJSONArray;
 S: TStringStream;

In Click event of ButtonConnectToJSON button, we write the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
procedure TFormJSON.ButtonConnectToJSONClick(Sender: TObject);
begin
 frxReport1.LoadFromFile('ChartJSON.fr3');
 JSON_DS.RangeEnd := reCount;
 Symbol := ComboBoxName.Items[ComboBoxName.ItemIndex];
 Resolution := ComboBoxResolution.Items[ComboBoxResolution.ItemIndex];
 FromCandlesHistory := DateTimeToUnix(DateTimePickerFrom.DateTime).ToString;
 ToCandlesHistory := DateTimeToUnix(DateTimePickerTo.DateTime).ToString;
 
//Creating a TfrxTransportHTTP Object for a GET Request over HTTPS
 
 tHTTP := TfrxTransportHTTP.Create(nil);
 try
 
//We form a GET request string and get a response in JSON format
 
 Res := tHTTP.Get('https://api.bcs.ru/udfdatafeed/v1/history?symbol='
 +Symbol+
 '&resolution='+Resolution +
 '&from='+ FromCandlesHistory+
 '&to='+ToCandlesHistory);
 
// if JSON is received incorrectly, then load it from the file and display a message in StatusBarr
 
 if (Res = '') or (pos('"s":"ok"',Res) = 0) then
 begin
 StatusBar1.SimpleText := 'Error loading JSON';
 S := TStringStream.Create('', TEncoding.UTF8);
 try
 S.LoadFromFile('JSON/'+Symbol+'.json');
 finally
 Res:= S.DataString;
 FreeAndNil(S);
 end;
 StatusBar1.SimpleText := 'Successful JSON loading from file '+Symbol+'.json';
 end
 else
 begin
 StatusBar1.SimpleText := 'Successful JSON('+Symbol+') loading';
 end;
 
// We load the received JSON from the Res line into the frxJSON object: TfrxJSON
 
 frxJSON := TfrxJSON.Create(Res);
 try
 if frxJSON.IsValid then
 begin
 StatusBar1.SimpleText :=StatusBar1.SimpleText +' /JSON is Valid';
 
// Read arrays
 
 if frxJSON.IsNameExists('t') then
 frxJSONArrayT := TfrxJSONArray.Create(frxJSON.ObjectByName('t'));
 frxJSONArrayC := TfrxJSONArray.Create(frxJSON.ObjectByName('c'));
 frxJSONArrayO := TfrxJSONArray.Create(frxJSON.ObjectByName('o'));
 frxJSONArrayH := TfrxJSONArray.Create(frxJSON.ObjectByName('h'));
 frxJSONArrayL := TfrxJSONArray.Create(frxJSON.ObjectByName('l'));
 frxJSONArrayV := TfrxJSONArray.Create(frxJSON.ObjectByName('v'));
// Prepare JSON_DS by clearing and adding fields
 JSON_DS.Fields.Clear;
 JSON_DS.Fields.Add('Ticker');
 JSON_DS.Fields.Add('Date');
 JSON_DS.Fields.Add('Time');
 JSON_DS.Fields.Add('Open');
 JSON_DS.Fields.Add('Close');
 JSON_DS.Fields.Add('High');
 JSON_DS.Fields.Add('Low');
 JSON_DS.Fields.Add('Vol');
 JSON_DS.RangeEndCount := frxJSONArrayT.Count;
 end
 else StatusBar1.SimpleText :=StatusBar1.SimpleText +' /JSON is Invalid';
 finally
 end;
 finally
 end;
end;

To get data when generating a report from the arrays frxJSONArrayT, frxJSONArrayC, frxJSONArrayO, frxJSONArrayH, frxJSONArrayL, frxJSONArrayV through the JSON_DS component: TfrxUserDataSet, we need to use the OnGetValue event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
procedure TFormJSON.JSON_DSGetValue(const VarName: string; var Value: Variant);
var
 Item: string;
 Time : string;
begin
 Item := frxJSONArrayT.GetString(JSON_DS.RecNo);
 DateTimeToString(Time, 't', UnixToDateTime(StrToInt64(Item)));
 
 if VarName = 'Ticker' then
 begin
 Value := Symbol;
 exit;
 end
 else if VarName = 'Date' then
 begin
 Value := DateToStr(UnixToDateTime(StrToInt64(Item)))+' '+Time;
 exit;
 end
 else if VarName = 'Time' then
 begin
 Value := Time;
 exit;
 end
 else if VarName = 'Open' then
 Item := frxJSONArrayO.GetString(JSON_DS.RecNo)
 else if VarName = 'Close' then
 Item := frxJSONArrayC.GetString(JSON_DS.RecNo)
 else if VarName = 'High' then
 Item := frxJSONArrayH.GetString(JSON_DS.RecNo)
 else if VarName = 'Low' then
 Item := frxJSONArrayL.GetString(JSON_DS.RecNo)
 else if VarName = 'Vol' then
 Item := frxJSONArrayV.GetString(JSON_DS.RecNo);
 
 Value := Item;
end;

Next, create a template in report designer, call it ChartJSON.fr3 and connect JSON_DS to it

Designer_FR

To display the chart, use the Candle Series from the TeeChart Pro VCL package and also connect to JSON_DS

Candle_Series_configuration

Next, add the Click event handlers for the remaining buttons:
 
1
2
3
4
5
6
7
8
9
10
procedure TFormJSON.ButtonDesignClick(Sender: TObject);
begin
 if (Res = '') then ButtonConnectToJSON.Click;
 frxReport1.DesignReport();
end;

procedure TFormJSON.ButtonShowReportClick(Sender: TObject); begin if (Res = '') then ButtonConnectToJSON.Click; frxReport1.ShowReport(); end;

We also add Change event handlers for ComboBoxName, DateTimePickerFrom and DateTimePickerTo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
procedure TFormJSON.ComboBoxNameChange(Sender: TObject);
begin
 ButtonConnectToJSON.Click;
end;
 
procedure TFormJSON.DateTimePickerFromChange(Sender: TObject);
begin
 ButtonConnectToJSON.Click;
end;
 
procedure TFormJSON.DateTimePickerToChange(Sender: TObject);
begin
 ButtonConnectToJSON.Click;
end;

Also when closing the application do not forget to free up the memory of used objects.

1
2
3
4
5
6
7
8
9
10
11
procedure TFormJSON.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 tHTTP.Free;
 frxJSON.Free;
 frxJSONArrayT.Free;
 frxJSONArrayC.Free;
 frxJSONArrayO.Free;
 frxJSONArrayH.Free;
 frxJSONArrayL.Free;
 frxJSONArrayV.Free;
end;

Next, run the application

FastReport VCL

In this application you can select desired stocks

FastReport VCL

And you can also select desired date range using the calendar

FastReport VCL

Connection to JSON occurs when you click on the "Connect to JSON", "Show Report" or "D" buttons, as well as when changing the dates or names of shares and displays a message about the connection status. 

FastReport VCL

When you click on the "Show Report" button, a report is built and its Preview is displayed

FastReport VCL

Congratulations, you received stock quotes in JSON format using a GET request, connected JSON to FastReport VCL 6 and built a report.

Download demo link: DemoJSON.zip
.

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
December 11, 2023

New S3 transport (Amazon) in FastReport VCL

In this article, we will look at the new transport in S3 (Amazon) for FastReport VCL, which is an object storage of files and buckets.
September 26, 2023

Installing FastReport and FastCube components in Lazarus

Instructions for installing FastReport in Lazarus for various operating systems with a comparison of Trial, Professional editions.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.