Repository navigation
Commit 2bbdd48
Floor an explicit close quantity to the quantity step (#839)
* Build an N-day chart on the year-restart N-day grid
A strategy whose chart timeframe is N days (2D, 3D, 5D ...) built from daily
bars ran on the days-since-1970 grid, and the chart's bar count, last_bar_index,
barstate.islast and open-trade row came from the wrong bars. TradingView builds
an N-day chart from N consecutive trading dates counted from the first trading
date of each calendar year (the last bucket of a year is cut at its end), and its
last_bar_index, barstate.islast and open-trade row follow that chart's own last
bar. The engine already had that partition for request.security; this change
installs it for the chart itself.
The scheduler creates the existing partition source for an aggregated chart with
a day count of two or more and hands it to the execution consumer, whose script
bucket cursor, interval lookup, chart close and chart-time offsets read it. Only
this chart path drops a leading bucket that opens before the input range and
seals the final historical bucket. The calendar gains an interval overload that
takes the partition's adjacent opens. The epoch-key paths, request.security, the
D, W and M charts, intraday charts and runs whose input and chart timeframes are
equal do not change, and a run without a partition adds nothing to the state
hash. A day count whose seconds do not fit the legacy int conversion is
calendar-only.
Limits: a live stream keeps the previous chart path, and a selected window on a
generated N-day chart partition is refused with the existing coded stop
window_mode_unsupported (capability selected_window_chart_timeframe), because the
window planner reconstructs the nominal calendar. Time offsets, exchange
calendars and intraday input are not measured against TradingView.
tests/test_n_day_chart_timeframe.cpp holds the rows on synthetic daily bars: the
3D grid, the last bar index, barstate.islast, the open row at the end, the 2D and
5D grids, the stream and refusal rows, the time offsets and the controls that
must not move. It is registered through tests/cr150.d/n-day-chart-timeframe.cmake.
scripts/check_native_cpp_versions.py lists the new reads of the retained input
bars in the scheduler.
TradingView tapes (sha256): 3D chart
1b2ef41a6f1ba4096e6659e12d5ef3f03c8bf2a51e4bb0d8331d814d9aade262, 2D chart
ea679d91cbc0cd078c80604e2c73b3c9bb3104ff8d1989695da6abecb8b9e174, 5D chart
5342be10bdda8185b12b2c6a95b9b26101334cd1f58663ceb1f28c42197cb5dc, last_bar_index
05617c83d33cbd8be7a0d64356dc8f40e5b8d2ab20baa1b74b08b8aa66a07d25, barstate.islast
4ae8e0a932b7da6ffb4e0bcd5de9ee4d514cd6b78dbbb3fa4207aba1a2c6420d.
Refs #811.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
* Re-anchor the line citations of the published pages (digits only)
The N-day chart commit moved code in src/native_execution_consumer.cpp,
src/native_execution_consumer.hpp, src/source/pine_strategy_host.cpp,
src/source/pine_scheduler_native.cpp and src/timeframe.cpp, so the file:line
citations of the published pages that point into them went stale, and
scripts/check_doc_anchors.py reported 212 bad anchors.
This commit changes digits only: the line numbers inside file:line citations
of nine pages (145 changed lines, 233 changed numbers) and nothing else. No
sentence, word, content pin or table is edited. The numbers were moved with the
repository's own tools: scripts/check_doc_anchors.py --fix (the cited symbol
sits on exactly one line) and scripts/reanchor_doc_citations.py --ref
360c4ab (a line map from the commit before the N-day chart change, where
the pages were correct). The last six citations, which claim no symbol, were
carried across the same line map by hand: the cited code line is the same text
at its new number. scripts/check_doc_anchors.py reports no bad anchor on the
result, and scripts/check_doc_reverts.py reports nothing, since a re-anchor is
not an edit.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
* Floor an explicit close quantity to the quantity step
strategy.close(id, qty = X) with an X that is not a multiple of the
declared quantity step (qty_step, or mincontract when no qty_step is
declared) placed no closing order: the quantity reached the native order
layer unchanged, and that layer refuses explicit units that are off the
grid. TradingView floors such a quantity to the step and closes that
much; a quantity that floors to zero, or is larger than the position,
closes the whole position.
An explicit close quantity now goes through the same floor as an explicit
entry quantity, including its handling of a quantity a hair under a step
multiple, before the order is made. A quantity that floors to zero closes
the whole exposure of the id when there is any. An explicit strategy.exit
quantity is floored the same way before its units are reserved; one below
a single step is ignored, so the exit covers the whole position (with the
default qty_percent), as TradingView does. Runs that declare no quantity
step are unchanged.
The new test runs the adapter on synthetic bars with a step of 0.00001
and pins the quantities TradingView closed on BTCUSDT 1-hour bars: a
half close of 0.03683 takes 0.01841 and leaves 0.01842, a close of
0.000004 or 0.123456 takes the whole position, a limit exit of 0.000004
or 0.123456 takes the whole position at its limit, and a limit exit of
0.018415, a short side and a reducing order floor the same way. One more
row checks that a close quantity a hair under a step multiple
(0.018409999999999) closes 0.0184, the same as an entry of that quantity
opens, and leaves 0.01843.
The lines added to src/source/pine_adapter.cpp move the lines the
documentation cites below them; the anchors that follow them are
re-pointed by the number of lines added (digits only, no sentence
changes).
Refs #716.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
* Re-anchor the line citations of the published pages (digits only)
Merging main moved code in src/native_execution_consumer.cpp,
src/source/pine_strategy_host.cpp, src/source/pine_scheduler_native.cpp and
the other files the N-day chart change touches, so the file:line citations of
the published pages that point into them went stale, and
scripts/check_doc_anchors.py reported 208 bad anchors on the merge.
This commit changes digits only: the line numbers inside file:line citations
of nine pages (140 changed lines) and nothing else. No sentence, word, content
pin or table is edited. The numbers were moved with the repository's own tools:
scripts/check_doc_anchors.py --fix (the cited symbol sits on exactly one line)
and scripts/reanchor_doc_citations.py --ref e163980 (a line map from main,
where the pages are correct). The last six citations, which claim no symbol,
were carried across by hand: the cited code line is the same text at its new
number (one of them, the cursor constructor, gained a parameter on its line).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Fail the run when a timezone lookup cannot be carried out
The check that accepts a timezone name written in a script looks the
name up in the timezone database with realpath, open and read, and it
treated every failure of those calls as "no such zone". When the process
had no file descriptor or memory left, or a read failed, a valid name
such as America/New_York was refused with pine_timezone_error (class
strategy, not retryable) and TradingView's text for an incorrect
timezone; a database directory that could not be resolved refused every
zone name the same way.
The lookup now tells an absent name from a lookup that could not be
carried out. ENOENT, ENOTDIR, ELOOP, ENAMETOOLONG and EISDIR still mean
that the name denotes no zone file and keep pine_timezone_error. ENOMEM
raises out_of_memory. Every other error raises engine_unclassified_error
with the errno kept and the failed call named in the message. When the
database directory itself cannot be resolved, a missing directory
included, the run fails the same way and the message names the
directory. In this check an interrupted call is tried up to three times,
and a header read that returns fewer than four bytes is continued. Only
the argument check of script timezones raises, through the internal
timezone_lookup_accepted; the installed native_calendar::timezone_accepted
keeps its false result, so its callers, the selected-run admission among
them, refuse as before. A name that is also a complete POSIX definition,
such as EST5EDT, stays accepted when its optional file lookup fails. A
failed lookup is not remembered as an accepted name.
tests/test_timezone_lookup_failure.cpp interposes realpath, open and
read to fail each call of the lookup with each class of error, and
checks the failure code, the errno and the call named in the message,
that the name is looked up again afterwards and a clean run then books
the same trades, that one interrupted call does not change the run, the
file header (wrong magic, end of file, a read split in two), the
database directory, the selected-run admission, and a TZDIR that is not
a directory, where UTC offsets and POSIX definitions still run.
Documentation sentences this change removes or rewrites:
- docs/coverage.md, the pine_str_format_time row: "The message is
TradingView's text with the engine's bar prefix, `Error on bar N: `,
or `Error before the first bar: ` when no bar is being executed, and
the arguments carry `bar_index`." It now begins "The error message for
a refused name is", after a new sentence on the lookup failures.
The allocation-bound manifest lists one new group, the message string
of the lookup refusal in the native calendar (ceiling 1418 -> 1419,
pinned count 1356 + 63 + 184), and its site inventory is re-emitted.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Test the void setter record under consecutive allocation failures
A failed void C setter records its failure in the handle's run
consumer. If a handle had no consumer, recording would first allocate
one, and a failure of that allocation would leave only the status,
without the code or the text. That state cannot be reached: the
engine's only constructor binds the consumer, and the native host can
be neither copied nor moved. These tests pin that, and show that the
record survives a second allocation failure, without a change to the
engine.
- Every host type of the test starts with its consumer: storing a
selected-window descriptor allocates nothing and does not return the
allocation refusal -4.
- When a setter's own string and then the rendering of the record's bar
prefix are both refused, the setter's out_of_memory record is read
without any allocation and its text falls back to "setter failed";
the next run start fails once with that record and publishes no
report, and the start after it produces the report of a clean run. A
setter refusal that carries its own code keeps that code when the
rendering fails.
- A sweep refuses two, and then three, consecutive allocations starting
at every allocation index of a configured run (the setters,
preparation, the bar callbacks and the report). Each injected run ends
in a typed setup refusal before the first bar, in out_of_memory after
it, or in a report byte-identical to the clean run, and the sweep
prints how many refusals each run received: a pair at 256 indexes
(Debug 258), a triple at 37 (Debug 39); the other 234 end at the first
refusal.
tests/fail_alloc_seam.hpp gains arm_span(first, count), which refuses
count consecutive allocation attempts, and refusals(), the number of
refusals delivered; arm(nth) is arm_span(nth, 1).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Keep a sub-step exit quantity when qty_percent is below 100
The explicit strategy.exit quantity floor sent every quantity that floors
to zero to percentage sizing. TradingView was measured only with the
default qty_percent = 100, where a quantity below one step leaves the
exit covering the whole position. With a qty_percent below 100 (not
measured) the fallback sized the exit from the percentage instead of the
script's quantity: on a one-lot position an explicit 0.5 with
qty_percent 50 armed the whole lot, against the existing control in
tests/test_integer_lot_percent_exit_min_step_l4b.cpp that keeps 0.5.
Now a qty_percent of 100 or more, or none, takes the fallback; below 100
a sub-step quantity is kept as the script gives it, as before the floor.
Quantities of one step or more still floor to the step. The changelog
entry and the SZ11 row of docs/pages/pine-to-native.md now say the same.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Sonnet 5.5 <[email protected]>1 parent 9f8309f commit 2bbdd48
9 files changed
Lines changed: 352 additions & 58 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
217 | | - | |
| 216 | + | |
| 217 | + | |
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
| 240 | + | |
241 | 241 | | |
242 | | - | |
| 242 | + | |
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
264 | | - | |
| 263 | + | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
| 275 | + | |
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| |||
719 | 719 | | |
720 | 720 | | |
721 | 721 | | |
722 | | - | |
723 | | - | |
| 722 | + | |
| 723 | + | |
724 | 724 | | |
725 | 725 | | |
726 | 726 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
347 | | - | |
| 347 | + | |
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| |||
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
384 | | - | |
| 384 | + | |
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
| |||
0 commit comments