FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pineforge-engine/src/engine_stream.cpp at main · pineforge-4pass/pineforge-engine · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pineforge-4pass
/
pineforge-engine
Public
Notifications
You must be signed in to change notification settings
Fork
42
Star
179
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pineforge-engine
/
src
/
engine_stream.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
384 lines (351 loc) · 14.7 KB
Breadcrumbs
pineforge-engine
/
src
/
engine_stream.cpp
Copy path
File metadata and controls
384 lines (351 loc) · 14.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* engine_stream.cpp — continuous historical warmup -> realtime trade stream
*/
#
include
"
engine_internal.hpp
"
#
include
<
algorithm
>
#
include
<
cmath
>
#
include
<
limits
>
#
include
<
stdexcept
>
#
include
<
string
>
namespace
pineforge
{
namespace
{
Bar
price_point
(
double
price,
double
volume,
int64_t
timestamp) {
return
Bar{price, price, price, price, volume, timestamp};
}
}
//
namespace
bool
BacktestEngine::stream_begin
(
const
Bar* warmup_bars,
int
n_warmup,
const
std::string& input_tf,
const
std::string& script_tf) {
last_error_.
clear
();
try
{
if
(!account_currency_fx_timestamps_.
empty
()) {
throw
std::runtime_error
(
"
timestamped account-currency FX is not supported by streaming
"
);
}
if
(stream_phase_ == StreamPhase::
REALTIME
) {
throw
std::runtime_error
(
"
stream is already realtime
"
);
}
if
(warmup_bars ==
nullptr
|| n_warmup <=
0
) {
throw
std::runtime_error
(
"
stream warmup requires at least one confirmed OHLCV bar
"
);
}
const
int
input_seconds =
tf_to_seconds
(input_tf);
if
(input_seconds <=
0
) {
throw
std::runtime_error
(
"
stream input timeframe must have a fixed positive duration:
"
+ input_tf);
}
for
(
int
i =
1
; i < n_warmup; ++i) {
if
(warmup_bars[i].
timestamp
<= warmup_bars[i -
1
].
timestamp
) {
throw
std::runtime_error
(
"
stream warmup timestamps must be strictly increasing
"
);
}
}
if
(!
std::isfinite
(warmup_bars[n_warmup -
1
].
close
)
|| warmup_bars[n_warmup -
1
].
close
<=
0.0
) {
throw
std::runtime_error
(
"
stream warmup final close must be finite and positive
"
);
}
//
A stream's warmup is historical context, not the rightmost realtime
//
bar. This keeps barstate.islast false until normalized trades take
//
over.
stream_warmup_mode_ =
true
;
run
(warmup_bars, n_warmup, input_tf, script_tf,
/*
bar_magnifier=
*/
false
,
4
, MagnifierDistribution::
ENDPOINTS
);
stream_warmup_mode_ =
false
;
if
(!last_error_.
empty
()) {
return
false
;
}
stream_input_tf_ms_ =
static_cast
<
int64_t
>(input_seconds) *
1000
;
const
int64_t
last_open = warmup_bars[n_warmup -
1
].
timestamp
;
if
(last_open > std::numeric_limits<
int64_t
>::
max
() - stream_input_tf_ms_) {
throw
std::runtime_error
(
"
stream warmup timestamp overflows next bar open
"
);
}
stream_next_input_open_ms_ = last_open + stream_input_tf_ms_;
stream_clock_ms_ = stream_next_input_open_ms_;
stream_last_tick_ms_ =
0
;
stream_last_sequence_ =
0
;
stream_seen_sequence_ =
false
;
stream_has_input_bar_ =
false
;
stream_input_bar_ = Bar{};
stream_last_price_ = warmup_bars[n_warmup -
1
].
close
;
stream_has_last_price_ =
true
;
stream_next_script_bar_index_ =
static_cast
<
int
>(diag_script_bars_processed_);
stream_script_bar_had_tick_ =
false
;
stream_script_tick_seen_ =
false
;
stream_phase_ = StreamPhase::
REALTIME
;
//
Exact normalized trades now drive the broker instead of inferred
//
OHLC paths. Strategy code remains close-only unless codegen opts in
//
to calc_on_every_tick; resting orders are nevertheless fillable on
//
each normalized trade, as on TradingView's realtime broker emulator.
bar_magnifier_enabled_ =
true
;
bar_index_ = stream_next_script_bar_index_;
last_bar_index_ = bar_index_;
last_bar_time_ = stream_next_input_open_ms_;
barstate_islast_ =
true
;
return
true
;
}
catch
(
const
std::exception& e) {
stream_warmup_mode_ =
false
;
stream_phase_ = StreamPhase::
IDLE
;
last_error_ = e.
what
();
return
false
;
}
catch
(...) {
stream_warmup_mode_ =
false
;
stream_phase_ = StreamPhase::
IDLE
;
last_error_ =
"
unknown error during BacktestEngine::stream_begin
"
;
return
false
;
}
}
bool
BacktestEngine::stream_push_tick
(
const
TradeTick& tick) {
last_error_.
clear
();
try
{
if
(stream_phase_ != StreamPhase::
REALTIME
) {
throw
std::runtime_error
(
"
stream_push_tick requires a realtime stream
"
);
}
if
(!
std::isfinite
(tick.
price
) || tick.
price
<=
0.0
) {
throw
std::runtime_error
(
"
stream tick price must be finite and positive
"
);
}
if
(!
std::isfinite
(tick.
quantity
) || tick.
quantity
<
0.0
) {
throw
std::runtime_error
(
"
stream tick quantity must be finite and non-negative
"
);
}
if
(tick.
timestamp
< stream_clock_ms_) {
throw
std::runtime_error
(
"
stream tick timestamp moved backwards
"
);
}
if
(tick.
sequence
!=
0
&& stream_seen_sequence_
&& tick.
sequence
<= stream_last_sequence_) {
throw
std::runtime_error
(
"
stream sequence must be strictly increasing
"
);
}
if
(!
stream_finalize_until
(tick.
timestamp
)) {
return
false
;
}
if
(!stream_has_input_bar_) {
stream_input_bar_ =
price_point
(
tick.
price
, tick.
quantity
, stream_next_input_open_ms_);
stream_has_input_bar_ =
true
;
}
else
{
stream_input_bar_.
high
=
std::max
(stream_input_bar_.
high
, tick.
price
);
stream_input_bar_.
low
=
std::min
(stream_input_bar_.
low
, tick.
price
);
stream_input_bar_.
close
= tick.
price
;
stream_input_bar_.
volume
+= tick.
quantity
;
}
stream_last_price_ = tick.
price
;
stream_has_last_price_ =
true
;
stream_last_tick_ms_ = tick.
timestamp
;
stream_clock_ms_ = tick.
timestamp
;
if
(tick.
sequence
!=
0
) {
stream_last_sequence_ = tick.
sequence
;
stream_seen_sequence_ =
true
;
}
//
Broker-only tick pass. Pine strategy code stays on its default
//
close-only cadence, but orders created on the preceding close fill
//
at the first actual source record and priced orders see the exact
//
trade path rather than an inferred OHLC traversal.
current_bar_ =
price_point
(tick.
price
, tick.
quantity
, tick.
timestamp
);
bar_index_ = stream_next_script_bar_index_;
last_bar_index_ = bar_index_;
last_bar_time_ = tick.
timestamp
;
barstate_islast_ =
true
;
is_first_tick_ = !stream_script_tick_seen_;
is_last_tick_ =
false
;
//
The overwhelming majority of source records arrive while many
//
strategies are flat and have no order in the broker. Such a print
//
still contributes to the forming OHLCV bar above, but there is no
//
broker, excursion, or margin state it can possibly mutate. Avoiding
//
the full order-sort/risk pass here is exact, not an approximation,
//
and makes long shared-feed corpus replays tractable.
if
(!pending_orders_.
empty
() || position_side_ != PositionSide::
FLAT
) {
if
(!pending_orders_.
empty
()) {
process_pending_orders
(current_bar_);
}
update_per_trade_extremes
();
const
std::
size_t
trades_before_mc = trades_.
size
();
process_margin_call
(current_bar_);
if
(trades_.
size
() != trades_before_mc) {
refresh_frozen_default_sizing_after_margin_call
();
}
}
stream_script_tick_seen_ =
true
;
return
true
;
}
catch
(
const
std::exception& e) {
last_error_ = e.
what
();
return
false
;
}
catch
(...) {
last_error_ =
"
unknown error during BacktestEngine::stream_push_tick
"
;
return
false
;
}
}
bool
BacktestEngine::stream_push_ticks
(
const
TradeTick* ticks,
int
n) {
last_error_.
clear
();
if
(n <
0
|| (n >
0
&& ticks ==
nullptr
)) {
last_error_ =
"
stream_push_ticks received an invalid tick array
"
;
return
false
;
}
for
(
int
i =
0
; i < n; ++i) {
if
(!
stream_push_tick
(ticks[i]))
return
false
;
}
return
true
;
}
bool
BacktestEngine::stream_advance_time
(
int64_t
timestamp_ms) {
last_error_.
clear
();
try
{
if
(stream_phase_ != StreamPhase::
REALTIME
) {
throw
std::runtime_error
(
"
stream_advance_time requires a realtime stream
"
);
}
if
(timestamp_ms < stream_clock_ms_) {
throw
std::runtime_error
(
"
stream clock moved backwards
"
);
}
if
(!
stream_finalize_until
(timestamp_ms))
return
false
;
stream_clock_ms_ = timestamp_ms;
return
true
;
}
catch
(
const
std::exception& e) {
last_error_ = e.
what
();
return
false
;
}
catch
(...) {
last_error_ =
"
unknown error during BacktestEngine::stream_advance_time
"
;
return
false
;
}
}
bool
BacktestEngine::stream_end
(
bool
finalize_partial_input_bar) {
last_error_.
clear
();
try
{
if
(stream_phase_ != StreamPhase::
REALTIME
) {
throw
std::runtime_error
(
"
stream_end requires a realtime stream
"
);
}
if
(finalize_partial_input_bar && stream_has_input_bar_) {
stream_feed_input_bar
(stream_input_bar_,
/*
had_tick=
*/
true
);
stream_has_input_bar_ =
false
;
stream_next_input_open_ms_ += stream_input_tf_ms_;
}
stream_phase_ = StreamPhase::
ENDED
;
return
true
;
}
catch
(
const
std::exception& e) {
last_error_ = e.
what
();
return
false
;
}
catch
(...) {
last_error_ =
"
unknown error during BacktestEngine::stream_end
"
;
return
false
;
}
}
bool
BacktestEngine::stream_finalize_until
(
int64_t
timestamp_ms) {
while
(timestamp_ms >= stream_next_input_open_ms_ + stream_input_tf_ms_) {
const
bool
had_tick = stream_has_input_bar_;
const
bool
in_session =
pine_session_ismarket
(
syminfo_.
session
, syminfo_.
timezone
,
stream_next_input_open_ms_);
//
A normalized provider may jump from one market session to the next.
//
Do not turn the closed interval into synthetic tradable bars. A real
//
source record is still honored even if the configured metadata is
//
imperfect, so provider data remains authoritative.
if
(!had_tick && !in_session) {
stream_input_bar_ = Bar{};
stream_next_input_open_ms_ += stream_input_tf_ms_;
continue
;
}
Bar completed;
if
(had_tick) {
completed = stream_input_bar_;
}
else
{
if
(!stream_has_last_price_) {
last_error_ =
"
stream cannot synthesize a gap before any price
"
;
return
false
;
}
completed =
price_point
(
stream_last_price_,
0.0
, stream_next_input_open_ms_);
}
stream_feed_input_bar
(completed, had_tick);
stream_has_input_bar_ =
false
;
stream_input_bar_ = Bar{};
stream_next_input_open_ms_ += stream_input_tf_ms_;
}
return
true
;
}
void
BacktestEngine::stream_feed_input_bar
(
const
Bar& bar,
bool
had_tick) {
++diag_input_bars_processed_;
last_bar_time_ = bar.
timestamp
;
for
(
auto
& state : security_eval_states_) {
feed_security_eval_state
(state, bar);
}
if
(!diag_needs_aggregation_) {
stream_dispatch_script_bar
(bar, had_tick);
return
;
}
AggregatedBar ab = script_tf_agg_.
feed
(bar);
const
bool
completed_on_boundary = ab.
is_complete
&&
tf_change
(ab.
bar
.
timestamp
, bar.
timestamp
, script_tf_,
syminfo_.
timezone
, syminfo_.
session
);
if
(completed_on_boundary) {
//
The current input bar opened the next bucket; the aggregator emitted
//
the preceding partial bucket before retaining this bar as its new
//
current state.
stream_dispatch_script_bar
(ab.
bar
, stream_script_bar_had_tick_);
stream_script_bar_had_tick_ = had_tick;
}
else
{
stream_script_bar_had_tick_ = stream_script_bar_had_tick_ || had_tick;
if
(ab.
is_complete
) {
stream_dispatch_script_bar
(ab.
bar
, stream_script_bar_had_tick_);
stream_script_bar_had_tick_ =
false
;
}
}
}
void
BacktestEngine::stream_dispatch_script_bar
(
const
Bar& bar,
bool
had_tick) {
const
int
this_bar_index = stream_next_script_bar_index_++;
bar_index_ = this_bar_index;
last_bar_index_ = this_bar_index;
last_bar_time_ = bar.
timestamp
;
barstate_islast_ =
true
;
is_first_tick_ =
true
;
is_last_tick_ =
true
;
++diag_script_bars_processed_;
pending_close_qty_in_bar_ =
0.0
;
//
A synthesized zero-volume interval has no raw broker pass. Give resting
//
market orders one carried-price point at its open so time advancement is
//
deterministic even through quiet in-session intervals.
if
(!had_tick) {
current_bar_ =
price_point
(bar.
open
,
0.0
, bar.
timestamp
);
process_pending_orders
(current_bar_);
update_per_trade_extremes
();
const
std::
size_t
trades_before_mc = trades_.
size
();
process_margin_call
(current_bar_);
if
(trades_.
size
() != trades_before_mc) {
refresh_frozen_default_sizing_after_margin_call
();
}
}
current_bar_ = bar;
session_ismarket_ =
pine_session_ismarket
(
syminfo_.
session
, syminfo_.
timezone
, current_bar_.
timestamp
);
session_isfirstbar_ = session_ismarket_ && !prev_in_session_;
if
(session_ismarket_ && script_tf_seconds_ >
0
) {
const
int64_t
next_ts = current_bar_.
timestamp
+
static_cast
<
int64_t
>(script_tf_seconds_) *
1000
;
session_islastbar_ = !
pine_session_ismarket
(
syminfo_.
session
, syminfo_.
timezone
, next_ts);
}
else
{
session_islastbar_ =
false
;
}
_push_source_series
();
invoke_chart_on_bar
(current_bar_);
if
(process_orders_on_close_) {
flush_same_bar_close
();
//
New close-time orders only get the closing price point. Re-walking
//
the full OHLC range would let a just-created order see prices that
//
occurred before it existed.
const
Bar completed_bar = current_bar_;
current_bar_ =
price_point
(
completed_bar.
close
,
0.0
, completed_bar.
timestamp
);
process_pending_orders
(current_bar_);
current_bar_ = completed_bar;
}
finalize_bar
();
prev_in_session_ = session_ismarket_;
update_equity_extremes
();
record_equity_point
(bar.
timestamp
);
prev_bar_timestamp_ = bar.
timestamp
;
//
Ticks belonging to the next script bar must compare pending-order
//
created_bar values against the next index before that bar closes.
bar_index_ = stream_next_script_bar_index_;
last_bar_index_ = bar_index_;
stream_script_tick_seen_ =
false
;
}
}
//
namespace pineforge
Back
|
FazBrowse Home
|
New Git URL