New Page 1
: TESBInstrument -
() .
.
.
, .
: TESBInstrument,
. , , ,
TESBAccount,
.
, :
//
TESBInstrument=class(TESBBaseClass)
protected
FPriceSource:TPASSPriceSource;
FCount:integer;
FName:string;
FCurrentCandle:LongInt;
FCanShort:boolean;
procedure SetPriceSource(APriceSource:TPASSPriceSource);
public
property PriceSource:TPASSPriceSource read
FPriceSource write SetPriceSource;
property Count:integer read FCount;
//
property Name:string read FName
write FName;
property CanShort:boolean read FCanShort
write FCanShort; //
constructor Create;
function CurrentPrice:double;
//
procedure Buy(ACount:integer);
//
procedure Sell(ACount:integer);
//
procedure Serialize(AStream:TStream);
override;
procedure Unserialize(AStream:TStream);
override;
function Next:boolean;
end; |
TPASSPriceSource,
PASSBaseObj,
, ,
.
Base
uses*:

:
// **************
TESBInstrument *****************
constructor TESBInstrument.Create;
begin
inherited Create;
FCanShort:=true;
FCount:=0;
FCurrentCandle:=0;
FPriceSource:=nil;
end;
function TESBInstrument.CurrentPrice:double;
begin
if FPriceSource<>nil then
begin
if FPriceSource.CurrentItemIndex<>FCurrentCandle
then FPriceSource.CurrentItemIndex:=FCurrentCandle;
Result:=FPriceSource.GetBarData.Open;
end else raise Exception.Create('TESBInstrument.CurrentPrice
- ');
end;
procedure TESBInstrument.Buy(ACount:integer);
begin
FCount:=FCount+ACount;
end;
procedure TESBInstrument.Sell(ACount:integer);
begin
if (ACount>FCount) and not(FCanShort)
then raise Exception.Create('TESBInstrument.Sell -
');
FCount:=FCount-ACount;
end;
procedure TESBInstrument.Serialize(AStream:TStream);
begin
inherited Serialize(AStream);
AStream.Write(FCanShort,sizeof(FCanShort));
AStream.Write(FCount,sizeof(FCount));
AStream.Write(FCurrentCandle,sizeof(FCurrentCandle));
SerializeString(FName,AStream);
end;
procedure TESBInstrument.Unserialize(AStream:TStream);
begin
inherited Unserialize(AStream);
AStream.Read(FCanShort,sizeof(FCanShort));
AStream.Read(FCount,sizeof(FCount));
AStream.Read(FCurrentCandle,sizeof(FCurrentCandle));
FName:=UnserializeString(AStream);
end;
procedure TESBInstrument.SetPriceSource(APriceSource:TPASSPriceSource);
begin
FPriceSource:=APriceSource;
FCurrentCandle:=APriceSource.CurrentItemIndex;
end;
function TESBInstrument.Next:boolean;
begin
Result:=FPriceSource.NextItem;
FCurrentCandle:=FCurrentCandle+1;
end; |
. ,
TESBAccount, Cash (),
, , .
Count,
Buy , Sell -.
CanShort
Count ,
. , , ,
, TPASSPriceSource.
(TPASSPriceSource) ,
,
.
,
*:

TfrmStockBot*:

:
procedure TfrmStockBot.FormCreate(Sender:
TObject);
begin
Instrument:=TESBInstrument.Create;
Instrument.CanShort:=false;
PriceSource:=nil;
end;
procedure TfrmStockBot.FormDestroy(Sender: TObject);
begin
FreeAndNil(Instrument);
if PriceSource<>nil then FreeAndNil(PriceSource);
end;
procedure TfrmStockBot.ShowCount;
begin
mmAccount.Lines.Add(FloatToStr(Instrument.Count));
end;
procedure TfrmStockBot.btnBuyClick(Sender: TObject);
begin
Instrument.Buy(StrToInt(edCount.Text));
ShowCount;
end;
procedure TfrmStockBot.btnSellClick(Sender: TObject);
begin
Instrument.Sell(StrToInt(edCount.Text));
ShowCount;
end;
procedure TfrmStockBot.itSaveClick(Sender: TObject);
var Stream:TFileStream;
begin
if SaveDialog.Execute then
begin
Stream:=TFileStream.Create(SaveDialog.FileName,fmCreate);
Instrument.Serialize(Stream);
FreeAndNil(Stream);
end;
end;
procedure TfrmStockBot.itOpenClick(Sender: TObject);
var Stream:TFileStream;
begin
if OpenDialog.Execute then
begin
Stream:=TFileStream.Create(OpenDialog.FileName,fmOpenRead);
FreeAndNil(Instrument);
Instrument:=TESBInstrument.Create;
Instrument.PriceSource:=PriceSource;
Instrument.Unserialize(Stream);
FreeAndNil(Stream);
mmAccount.Lines.Clear;
ShowCount;
lbPrice.Caption:=FloatToStr(Instrument.CurrentPrice);
end;
end;
procedure TfrmStockBot.itOpenPricesClick(Sender: TObject);
begin
if OpenDialogPrices.Execute then
begin
if PriceSource<>nil
then FreeAndNil(PriceSource);
PriceSource:=TPASSPriceSource.Create('',false);
PriceSource.LoadDataFromTextFile(OpenDialogPrices.FileName);
PriceSource.First;
Instrument.PriceSource:=PriceSource;
lbPrice.Caption:=FloatToStr(Instrument.CurrentPrice);
end;
end;
procedure TfrmStockBot.btnNextClick(Sender: TObject);
begin
Instrument.Next;
lbPrice.Caption:=FloatToStr(Instrument.CurrentPrice);
end; |
.
,
:

, * ,
"Delphi",
"Borland Software Corporation, (a
Micro Focus Company).
|