delphi get stock shares

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, ExtCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    IdHTTP1: TIdHTTP;
    edt1: TEdit;
    edt2: TEdit;
    btn2: TButton;
    TMR1: † hours;
    procedure btn1Click(Sender: TObject);
    procedure edt1KeyPress(Sender: TObject; var Key: Char);
    procedure btn2Click(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    function GetResponseContentEx(const http: TIdHTTP; url: string;
        param: TStringStream=nil): string;
    function getResult(sl:TStrings):string;
  end;

where
  Form1: TForm1;

implementation

{$R *.dfm}


function TForm1.GetResponseContentEx(const http: TIdHTTP; url: string;
          param: TStringStream=nil): string;
var
  response: TStringStream;
begin
  result := '';

    response := TStringStream.Create('');

    if param=nil then
    begin
      http.Get(url,response);
    Over 
    presence  Begin
      http.Post(url,param,response);
    end;
    result := response.DataString;

  response.Free;
end;


// Get stock stock Qianshan drug machine 
// http://hq.sinajs.cn/list=sz300216 
Procedure TForm1.btn1Click (Sender: TObject);
 var 
  URL, S: String ;
  sl:TStrings;
  currentPrice,yesterdayPrice,increasePercent:Double;
begin
  url:= 'http://hq.sinajs.cn/list=sz300216';
  s:= GetResponseContentEx(IdHTTP1,url,nil);

  sl:= TStringList.Create;

  try
    sl.Delimiter:=',';
    sl.DelimitedText:= s;

    // the ShowMessage (SL [. 4]); // current value 
    // the ShowMessage (SL [. 3]); // Zuoshou 

    edt1.Text: = ' QS ' + the getResult (SL);

    url:= 'http://hq.sinajs.cn/list='+'sz399006';
    s:= GetResponseContentEx(IdHTTP1,url,nil);
    sl.DelimitedText:= s;
    edt2.Text:='c'+getResult(sl);

  finally
    sl.free;
  end;
end;

function TForm1.getResult(sl: TStrings): string;
var
  currentPrice,yesterdayPrice,increasePercent:Double;
begin
  result:= '';
  if sl=nil then exit;
  if (sl=nil)or(sl.Count<4) then Exit;

  currentPrice:= StrToFloat(sl[4]);
  yesterdayPrice:= StrToFloat(sl[3]);

  if currentPrice>=yesterdayPrice then
      result:='+++++'+FloatToStr( ((currentPrice-yesterdayPrice)/yesterdayPrice)*100)
    else
      result:='-----'+ FloatToStr( ((yesterdayPrice-currentPrice)/yesterdayPrice)*100);



//  if currentPrice>=yesterdayPrice then
//      increasePercent:= ((currentPrice-yesterdayPrice)/yesterdayPrice)*100
//    else
//      increasePercent:= -((yesterdayPrice-currentPrice)/yesterdayPrice)*100;
//
//    if increasePercent =0 then
//      Result:= '0'
//    else
//      Result:= Format('%.2f%s',[increasePercent,'%']);
end;

procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
    btn1Click (btn1);
end;



procedure TForm1.btn2Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.tmr1Timer(Sender: TObject);
begin
  btn1Click (btn1);
end;

end.

Guess you like

Origin www.cnblogs.com/tobetterlife/p/12169813.html