-
Notifications
You must be signed in to change notification settings - Fork 0
/
uProcess.pas
276 lines (248 loc) · 11.4 KB
/
uProcess.pas
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
unit uProcess;
interface
uses
SysUtils, StrUtils, uConst, uOper, AbsMain, sComboBox;
const
template = '<b>{{Currency}}</b> - {{method}}' + CR +
'{{#NewInTop}}Íîâàÿ 🆕' + CR + '{{/NewInTop}}' +
'{{#LongInterval}}Ñîîáùåíèå ïîñëå ïåðåðûâà: {{BreakTime}}' + CR + '{{/LongInterval}}' +
'Amount: <code>{{amount}}</code>' + CR +
'{{#InTop}}Ìåñòî: <code>{{Place}}</code> â òîï-{{TopCnt}}, çàøëà â òîï {{EnteredToTop}}' + CR + '{{/InTop}}' +
'Çà ïåðèîä {{Period}} ìèí ñäåëîê / ñóììà: ' + CR +
' - added liq: <code>{{DealsBuy}}</code> / <code>{{VolumeBuy}}</code>' + CR +
' - removed liq: <code>{{DealsSell}}</code> / <code>{{VolumeSell}}</code>' + CR +
'<a href="{{TokenUrl}}">Transaction Details</a>' + CR +
'{{AppName}} - {{site}}';
template2 = '<b>{{Currency}} - {{Currency2}} </b> - {{method}}' + CR +
'{{#NewInTop}}Íîâàÿ {{Currency}} 🆕' + CR + '{{/NewInTop}}' +
'{{#NewInTop2}}Íîâàÿ {{Currency2}} 🆕' + CR + '{{/NewInTop2}}' +
'{{#LongInterval}}Ñîîáùåíèå ïîñëå ïåðåðûâà: {{BreakTime}}' + CR + '{{/LongInterval}}' +
'Amount {{Currency}}: <code>{{amount}}</code>' + CR +
'Amount {{Currency2}}: <code>{{amount2}}</code>' + CR +
'Ìåñòî {{Currency}}: <code>{{Place}}</code> â òîï-{{TopCnt}}, çàøëà â òîï {{EnteredToTop}}' + CR +
'Ìåñòî {{Currency2}}: <code>{{Place2}}</code> â òîï-{{TopCnt}}, çàøëà â òîï {{EnteredToTop2}}' + CR +
'Çà ïåðèîä {{Period}} ìèí ñäåëîê / ñóììà: ' + CR +
' - added liq {{Currency}}: <code>{{DealsBuy}}</code> / <code>{{VolumeBuy}}</code>, {{Currency2}}: <code>{{DealsBuy2}}</code> / <code>{{VolumeBuy2}}</code>'
+ CR +
' - removed liq {{Currency}}: <code>{{DealsSell}}</code> / <code>{{VolumeSell}}</code>, {{Currency2}}: <code>{{DealsSell2}}</code> / <code>{{VolumeSell2}}</code>'
+ CR +
'<a href="{{TokenUrl}}">Transaction Details</a>' + CR +
'{{AppName}} - {{site}}';
procedure SendSignal(const Opers: TOpArray);
procedure UpdateReturns(tbMemReturns: TAbsTable);
procedure SetupPlaces(CurrencyComboBox: TsComboBox; tbMemBigDeals: TAbsTable);
implementation
uses
dea.Tools, dea.debug, dea.cl, dea.TelegramHelper, System.Types, dea.Get,
uAppParam, uCurrency, // uDataModule,
SynCommons, SynMustache;
// http://www.delphikingdom.ru/asp/answer.asp?IDAnswer=68134
// èñïðàâëåíèå êîäèðîâêè íåêîòîðûõ ñòðàíèö
function FixStringCoding1251(const AData: string): string;
var
S: RawByteString;
X: Integer;
begin
SetLength(S, Length(AData)); // âûäåëèëè ïàìÿòü ïîä ANSI-áóôåð
for X := 1 to Length(AData) do // ïåðåíîñèì äàííûå èç âèäà 00A400B700E700D5 â A4B7E7D5
S[X] := AnsiChar(AData[X]);
SetCodePage(S, 1251, false); // ïîìåòèì, ÷òî äàííûå A4B7E7D5 èìåþò êîäèðîâêó Win1251.
// False óêàçûâàåò íà òî, ÷òî ñàìè äàííûå ìåíÿòü íå íàäî -
// ìû ïðîñòî óêàçûâàåì, â êàêîé îíè êîäèðîâêå
Result := string(S); // çäåñü êîìïèëÿòîð àâòîìàòè÷åñêè ïðåîáðàçóåò ñòðîêó èç ANSI/Win1251 â unicode
end;
// îòïðàâêà ñèãíàëà - óñò. - áîëüøèå ñóììû, çàøåäøèå â òîï
// UPD ïðîñòî áîëüøèå ñóììû
// âûçûâàåòñÿ èç ProcessOpers
procedure SendSignal(const Opers: TOpArray);
var
I: Integer;
P: TCurrencyCargo;
P2: TCurrencyCargo;
msg: string;
LongInterval: Boolean;
doc: variant;
mustache: TSynMustache;
oAdded: TOperType;
TwoZero: Boolean;
begin
for I := 0 to high(Opers) do begin
TwoZero := (Opers[I].amount = 0) and (Opers[I].amount2 = 0);
if not Opers[I].dupl and Opers[I].IsLiquid { and (Opers[I].amount <> 0) } then begin
if not TwoZero and (Opers[I].amount < AppParam.MinSum) and (Opers[I].amount2 < AppParam.MinSum) then
Continue;
if (AppParam.SignalKind = TSignalKind.added) and not Opers[I].inout then
Continue;
if (AppParam.SignalKind = TSignalKind.removed) and Opers[I].inout then
Continue;
if (Opers[I].Currency2 = '') and (AppParam.ExcludedCurrencies.IndexOf(Opers[I].Currency) <> -1) then
Continue; // â ñïèñêå èñêëþ÷åíèé
if AppParam.ExcludedCurrencies.IndexOf(Opers[I].Currency + ';' + Opers[I].Currency2) <> -1 then
Continue;
if AppParam.ExcludedCurrencies.IndexOf(Opers[I].Currency2 + ';' + Opers[I].Currency) <> -1 then
Continue;
if AppParam.ExcludedCurrencies.IndexOf(Opers[I].Currency + ' - ' + Opers[I].Currency2) <> -1 then
Continue;
if AppParam.ExcludedCurrencies.IndexOf(Opers[I].Currency2 + ' - ' + Opers[I].Currency) <> -1 then
Continue;
P := Currencies.Get(Opers[I].Currency); // íå ìîæåò áûòü nil
if Opers[I].Currency2 = '' then
P2 := nil
else
P2 := Currencies.Get(Opers[I].Currency2);
if true { P.NeedSignal } then begin // ñèãíàë åùå íå îòïðàâëÿëñÿ è áûë çàõîä â òîï - ñêàçàë îòïðàâëÿòü âñå ñóììû âûøå ïîðîãîâîé
if Opers[I].inout then
oAdded := _added
else
oAdded := _removed;
LongInterval := (P.Tops[oAdded].LastSignal <> 0) and (Now - P.Tops[oAdded].LastSignal >= AppParam.IntervalMins * OneMin);
TDocVariant.New(doc); // èíèöèàëèçàöèÿ ïåðåìåííîé doc
doc.method := Opers[I].method;
doc.Currency := Opers[I].Currency;
doc.amount := ff(Opers[I].amount);
doc.NewInTop := P.Tops[oAdded].NewInTop;
doc.LongInterval := LongInterval;
doc.BreakTime := TimeToStr(Now - P.Tops[oAdded].LastSignal);
doc.Place := P.Tops[oAdded].Place;
doc.TopCnt := AppParam.TopCnt;
doc.EnteredToTop := TimeToStr(P.Tops[oAdded].EnteredToTop);
doc.Period := AppParam.Period;
doc.DealsBuy := P.LastDealsBuy;
doc.DealsSell := P.LastDealsSell;
doc.VolumeBuy := P.LastVolumeBuy;
doc.VolumeSell := P.LastVolumeSell;
doc.TokenUrl := AppParam.GetTokenUrl(P.LastHash);
doc.InTop := P.Tops[oAdded].NeedSignal; // âûâîäèòü èíôîðìàöèþ ïî ìåñòó â òîïå
doc.AppName := AppParam.AppName;
doc.Site := AppParam.SiteCaption;
if P2 = nil then
mustache := TSynMustache.Parse(template)
else begin
doc.Currency2 := Opers[I].Currency2;
doc.amount2 := ff(Opers[I].amount2);
doc.Place2 := P2.Tops[oAdded].Place;
doc.NewInTop2 := P2.Tops[oAdded].NewInTop;
doc.EnteredToTop2 := TimeToStr(P2.Tops[oAdded].EnteredToTop);
doc.BreakTime2 := TimeToStr(Now - P2.Tops[oAdded].LastSignal);
doc.InTop2 := P2.Tops[oAdded].NeedSignal; // âûâîäèòü èíôîðìàöèþ ïî ìåñòó â òîïå
doc.DealsBuy2 := P2.LastDealsBuy;
doc.DealsSell2 := P2.LastDealsSell;
doc.VolumeBuy2 := P2.LastVolumeBuy;
doc.VolumeSell2 := P2.LastVolumeSell;
mustache := TSynMustache.Parse(template2);
end;
msg := UTF8ToString(mustache.Render(doc));
SendToTelegramHelper(msg, Iif(TwoZero, AppParam.Channel2, AppParam.Channel));
if AppParam.Channel <> AppParam.Channel2 then begin
if P.Tops[oAdded].NewInTop or LongInterval then // íîâûå ïîâòîðèòü âî âòîðîé êàíàë, èëè ìåæäó ñèãíàëàìè ïðîøëî áîëüøå ... ìèíóò
SendToTelegramHelper(msg, AppParam.Channel2);
end;
P.Tops[oAdded].NeedSignal := false;
P.Tops[oAdded].LastSignal := Now;
if P2 <> nil then begin
P2.Tops[oAdded].NeedSignal := false;
P2.Tops[oAdded].LastSignal := Now;
end;
end;
end;
end;
end;
// ïåðåðàñ÷åò îáîðîòîâ ïî êàæäîé âàëþòå â Currencies çà óêàçàííîå ÷èñëî ìèíóò - ïèøåò â tbMemReturns
// çàòåì ïðîñòàâëÿåò ïðèçíàêè â Currencies, ìåñòî íå ïðîñòàâëÿåò - åùå íåèçâåñòíû îáîðîòû ïî âñåì âàëþòàì
// âûçûâàåòñÿ èç DoWork
procedure UpdateReturns(tbMemReturns{, tbMemReturns2}: TAbsTable);
var
I, NewPlace: Integer;
cur: string;
P: TCurrencyCargo;
Return: TReturn;
begin
// ñ÷èòàåì îáîðîòû ïî êàæäîé âàëþòå è çàïîëíÿåì òàáëèöó â ïàìÿòè äëÿ îòîáðàæåíèÿ
// tbMemReturns.DisableControls;
// tbMemReturns2.DisableControls;
// try
tbMemReturns.Close;
tbMemReturns.EmptyTable;
tbMemReturns.Open;
for I := 0 to Currencies.Currencies.Count - 1 do begin
cur := Currencies.Currencies[I];
P := TCurrencyCargo(Currencies.Currencies.Objects[I]);
Currencies.GetReturns(cur, AppParam.Period, Return);
tbMemReturns.Append;
tbMemReturns.FieldByName('Currency').AsString := cur;
tbMemReturns.FieldByName('DealsBuy').AsInteger := Return.DealsBuy;
tbMemReturns.FieldByName('DealsSell').AsInteger := Return.DealsSell;
tbMemReturns.FieldByName('Deals').AsInteger := Return.Deals;
tbMemReturns.FieldByName('VolumeBuy').AsFloat := Return.VolumeBuy;
tbMemReturns.FieldByName('VolumeSell').AsFloat := Return.VolumeSell;
tbMemReturns.FieldByName('DexLink').AsString := P.DexLink;
tbMemReturns.FieldByName('LastAddedTime').AsDateTime := P.LastAddedTime;
tbMemReturns.FieldByName('LastRemovedTime').AsDateTime := P.LastRemovedTime;
tbMemReturns.Post;
end;
// îïðåäåëÿåì òîï - ïî êîëè÷åñòâó ñäåëîê äîáàâëåíèÿ ëèêâèäíîñòè
Currencies.ResetAllPlaces; // =0
tbMemReturns.IndexName := 'xDealsBuy';
tbMemReturns.First;
for NewPlace := 1 to { nTop } 200 do begin // ó÷èòûâàåì ïåðâûå ñîòíè âàëþò, äëÿ ñêîðîñòè îòñåêàåì ëóçåðîâ
if tbMemReturns.Eof or (tbMemReturns.FieldByName('DealsBuy').AsInteger = 0) then
Break;
Currencies.MarkAsTop( // âûñòàâëÿåò ïðèçíàêè InTop
_added,
tbMemReturns.FieldByName('Currency').AsString, NewPlace, AppParam.TopCnt,
tbMemReturns.FieldByName('DealsBuy').AsInteger,
tbMemReturns.FieldByName('VolumeBuy').AsFloat);
tbMemReturns.Next;
end;
tbMemReturns{2}.IndexName := 'xDealsSell';
tbMemReturns.First;
for NewPlace := 1 to { nTop } 200 do begin // ó÷èòûâàåì ïåðâûå ñîòíè âàëþò, äëÿ ñêîðîñòè îòñåêàåì ëóçåðîâ
if tbMemReturns.Eof or (tbMemReturns.FieldByName('DealsSell').AsInteger = 0) then
Break;
Currencies.MarkAsTop( // âûñòàâëÿåò ïðèçíàêè InTop
_removed,
tbMemReturns.FieldByName('Currency').AsString, NewPlace, AppParam.TopCnt,
tbMemReturns.FieldByName('DealsSell').AsInteger,
tbMemReturns.FieldByName('VolumeSell').AsFloat);
tbMemReturns.Next;
end;
// finally
// tbMemReturns.First;
// tbMemReturns.EnableControls;
// tbMemReturns2.Refresh;
// tbMemReturns2.First;
// tbMemReturns2.EnableControls;
// end;
end;
// ïðîïèñûâàåò âàëþòàì â ñïèñêå âàëþò ìåñòà â òîïå è ÷èñëî ñäåëîê, íà îñíîâàíèè tbMemReturn
// çàòåì ïðîïèñûâàåò ìåñòà â òàáëèöå áîëüøèõ ñäåëîê, óæå íà îñíîâàíèè ñïèñêà âàëþò äëÿ ñêîðîñòè
// çàîäíî çàïîëíÿåò êîìáîáîêñ ñ âàëþòàìè (äîïèñûâàåò íîâûå âàëþòû)
// âûçûâàåòñÿ èç ProcessOpers
procedure SetupPlaces(CurrencyComboBox: TsComboBox; tbMemBigDeals: TAbsTable);
var
I: Integer;
cur: string;
begin
// tbMemBigDeals.DisableControls; - â âûçûâàþùåé ïðîöåäóðå
CurrencyComboBox.Items.BeginUpdate;
try
tbMemBigDeals.IndexName := 'xDateRev';
tbMemBigDeals.First; // òîëüêî ïîñëåäíèå ïî âðåìåíè áîëüøèå ñäåëêè
for I := 1 to 500 do begin
if tbMemBigDeals.Eof then
Break;
cur := tbMemBigDeals.FieldByName('Currency').AsString;
tbMemBigDeals.Edit;
tbMemBigDeals.FieldByName('Place').AsInteger := Currencies.GetPlace(_added, cur);
tbMemBigDeals.FieldByName('Place2').AsInteger := Currencies.GetPlace(_removed, cur);
tbMemBigDeals.Post;
if CurrencyComboBox.Items.IndexOf(cur) = -1 then
CurrencyComboBox.Items.Add(cur);
tbMemBigDeals.Next;
end;
finally
CurrencyComboBox.Items.EndUpdate;
// tbMemBigDeals.EnableControls; - â âûçûâàþùåé ïðîöåäóðå
end;
end;
end.