PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
pine_strategy_commands.cpp
Go to the documentation of this file.
2
3#include <limits>
4
5namespace pineforge::source {
6
7namespace {
8
9// The validator deliberately warms Pine state before the first reportable
10// trade. The legacy source command path ignored commands during that span,
11// while still evaluating the script, and retained one script interval for a
12// stop/limit placed on the preceding source bar. Keeping this at the source
13// command boundary preserves both the warmup semantics and the legacy
14// PendingOrder-incarnation provenance exported with closed trades.
15bool trading_window_active(std::int64_t current_ms, std::int64_t start_ms,
16 int script_tf_seconds) noexcept {
17 if (start_ms == std::numeric_limits<std::int64_t>::min()) return true;
18 const std::int64_t buffer_ms = script_tf_seconds > 0
19 ? static_cast<std::int64_t>(script_tf_seconds) * 1000 : 0;
20 return current_ms >= start_ms - buffer_ms;
21}
22
23} // namespace
24
25void PineStrategyHost::strategy_entry(const std::string& id, bool is_long,
26 double limit_price, double stop_price, double qty,
27 const std::string& comment,
28 const std::string& oca_name, int oca_type,
29 int qty_type) {
30 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
31 return;
32 adapter_.set_configuration(config_);
33 adapter_.entry(id, is_long, limit_price, stop_price, qty, comment, oca_name,
34 oca_type, qty_type);
35}
36
37void PineStrategyHost::strategy_close(const std::string& id, const std::string& comment,
38 double qty, double qty_percent, bool immediately) {
39 adapter_.set_configuration(config_);
40 adapter_.close(id, comment, qty, qty_percent, immediately);
41}
42
43void PineStrategyHost::strategy_close(const std::string& id, const std::string& comment,
44 double qty, double qty_percent, bool immediately,
45 std::uint64_t callsite_token) {
46 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
47 return;
48 adapter_.set_configuration(config_);
49 adapter_.close(id, comment, qty, qty_percent, immediately, callsite_token);
50}
51
53 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
54 return;
55 adapter_.set_configuration(config_);
56 adapter_.close_all();
57}
58
59void PineStrategyHost::strategy_exit(const std::string& id, const std::string& from_entry,
60 double limit_price, double stop_price,
61 double trail_points, double trail_offset,
62 double trail_price, double qty_percent,
63 const std::string& comment, double qty,
64 const std::string& oca_name,
65 double profit_ticks, double loss_ticks) {
66 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
67 return;
68 adapter_.set_configuration(config_);
69 adapter_.exit(id, from_entry, limit_price, stop_price, trail_points, trail_offset,
70 trail_price, qty_percent, comment, qty, oca_name, profit_ticks,
71 loss_ticks);
72}
73
74void PineStrategyHost::strategy_exit_cancel_bracket(const std::string& exit_id,
75 const std::string& from_entry,
76 const std::string& comment) {
77 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
78 return;
79 adapter_.set_configuration(config_);
80 adapter_.exit_cancel_bracket(exit_id, from_entry, comment);
81}
82
83void PineStrategyHost::strategy_cancel(const std::string& id) {
84 adapter_.set_configuration(config_);
85 adapter_.cancel(id);
86}
87
89 adapter_.set_configuration(config_);
90 adapter_.cancel_all();
91}
92
93void PineStrategyHost::strategy_order(const std::string& id, bool is_long, double qty,
94 double limit_price, double stop_price,
95 const std::string& oca_name, int oca_type) {
96 if (!trading_window_active(current_bar_.timestamp, trade_start_time_, script_tf_seconds_))
97 return;
98 adapter_.set_configuration(config_);
99 adapter_.order(id, is_long, qty, limit_price, stop_price, oca_name, oca_type);
100}
101
102} // namespace pineforge::source
void strategy_entry(const std::string &id, bool is_long, double limit_price=std::numeric_limits< double >::quiet_NaN(), double stop_price=std::numeric_limits< double >::quiet_NaN(), double qty=std::numeric_limits< double >::quiet_NaN(), const std::string &comment={}, const std::string &oca_name={}, int oca_type=0, int qty_type=-1)
void strategy_order(const std::string &id, bool is_long, double qty, double limit_price=std::numeric_limits< double >::quiet_NaN(), double stop_price=std::numeric_limits< double >::quiet_NaN(), const std::string &oca_name={}, int oca_type=0)
void strategy_exit_cancel_bracket(const std::string &exit_id, const std::string &from_entry, const std::string &comment={})
void strategy_cancel(const std::string &id)
void strategy_exit(const std::string &id, const std::string &from_entry, double limit_price, double stop_price, double trail_points=std::numeric_limits< double >::quiet_NaN(), double trail_offset=std::numeric_limits< double >::quiet_NaN(), double trail_price=std::numeric_limits< double >::quiet_NaN(), double qty_percent=100.0, const std::string &comment={}, double qty=std::numeric_limits< double >::quiet_NaN(), const std::string &oca_name={}, double profit_ticks=std::numeric_limits< double >::quiet_NaN(), double loss_ticks=std::numeric_limits< double >::quiet_NaN())
void strategy_close(const std::string &id, const std::string &comment={}, double qty=std::numeric_limits< double >::quiet_NaN(), double qty_percent=std::numeric_limits< double >::quiet_NaN(), bool immediately=false)