Dzisiaj przyjrzymy się funkcji która umożliwi uzyskanie wyników notowań giełdowych za pomocą żądania GET z protokołem HTTPS i tradingviewapi.docs.apiary.io publicznego API.
Wwnie zgodnie z dokumentacją https://tradingviewapi.docs.apiary.io/#reference/0/history/0?console=1 w celu uzyskania notowań giełdowych trzeba użyć historii żądania GET
HISTORIA GET |
||
https://api.bcs.ru/udfdatafeed/v1/history?symbol=BRENT&resolution=60&from=1450772216&to=1450858616 |
||
PARAMETRY URI |
||
Nazwa |
Przykład |
Opis |
symbol |
BRENT |
para walutowa |
rezolucja |
D |
dyskretność świec, możliwe wartości: 1, 5, 15, 30, 45, 60, 120, 180, 240, D, W, M |
z |
1450772216 |
początek okresu |
do |
1450858616 |
koniec okresu |
Utwórz aplikację i dodaj składniki do formularza:
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; |
Dodawanie elementów do comboboxname i 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'; |
Dodawanie zmiennych globalnych
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; |
W przypadku kliknięcia buttonConnectToJSON , piszemy następujący kod:
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; |
Aby uzyskać dane podczas generowania raportu z tablic frxJSONArrayT, frxJSONArrayC, frxJSONArrayO, frxJSONArrayH, frxJSONArrayL, frxJSONArrayV za pośrednictwem składnika JSON_DS: TfrxUserDataSet, musimy użyć opcji OnGetVal:
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; |
Następnie utwórz szablon w projektancie raportów, nazwij go ChartJSON.fr3 i połącz JSON_DS z nim
Aby wyświetlić wykres, użyj serii Candle z pakietu TeeChart Pro VCL i połącz się z JSON_DS
1 2 3 4 5 6 7 8 9 10 |
procedure TFormJSON.ButtonDesignClick(Sender: TObject); begin if (Res = '') then ButtonConnectToJSON.Click; frxReport1.DesignReport(); end; |
Dodaj również obsługę zdarzeń Change dla ComboBoxName, DateTimePickerFrom i 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; |
Przy zamykaniu aplikacji nie zapomnij o zwolnieniu pamięci używanych obiektów.
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; |
Następnie uruchom aplikację
W tej aplikacji można wybrać żądane zapasy
Można również wybrać żądany zakres dat za pomocą kalendarza
Połączenie z JSON następuje po kliknięciu przycisków "Połącz z JSON", "Pokaż raport" lub "D", a także podczas zmiany dat lub nazw udziałów i wyświetla komunikat o stanie połączenia.
Po kliknięciu przycisku "Pokaż raport" zostanie utworzony raport i zostanie wyświetlony jego podgląd
Gratulacje, otrzymałeś notowania w formacie JSON przy użyciu żądania GET, podłączyłeś JSON do FastReport VCL 6 i zbudowano raport.
Pobierz link demo: DemoJSON.zip.