pinets 0.9.16 → 0.9.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -11
- package/dist/pinets.min.browser.es.js +29 -29
- package/dist/pinets.min.browser.es.js.map +1 -1
- package/dist/pinets.min.browser.js +29 -29
- package/dist/pinets.min.browser.js.map +1 -1
- package/dist/pinets.min.cjs +29 -29
- package/dist/pinets.min.cjs.map +1 -1
- package/dist/pinets.min.es.js +29 -29
- package/dist/pinets.min.es.js.map +1 -1
- package/dist/types/Context.class.d.ts +2 -0
- package/dist/types/PineTS.class.d.ts +20 -7
- package/dist/types/namespaces/line/LineHelper.d.ts +9 -0
- package/dist/types/namespaces/linefill/LinefillHelper.d.ts +11 -0
- package/dist/types/namespaces/polyline/PolylineObject.d.ts +3 -3
- package/dist/types/namespaces/strategy/methods/account_currency.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/any.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/avg_losing_trade.d.ts +6 -0
- package/dist/types/namespaces/strategy/methods/avg_losing_trade_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/avg_trade.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/avg_trade_percent.d.ts +6 -0
- package/dist/types/namespaces/strategy/methods/avg_winning_trade.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/avg_winning_trade_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/cancel.d.ts +1 -0
- package/dist/types/namespaces/strategy/methods/cancel_all.d.ts +6 -0
- package/dist/types/namespaces/strategy/methods/cash.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/close.d.ts +1 -0
- package/dist/types/namespaces/strategy/methods/close_all.d.ts +1 -0
- package/dist/types/namespaces/strategy/methods/closedtrades.d.ts +15 -0
- package/dist/types/namespaces/strategy/methods/convert_to_account.d.ts +9 -0
- package/dist/types/namespaces/strategy/methods/convert_to_symbol.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/default_entry_qty.d.ts +8 -0
- package/dist/types/namespaces/strategy/methods/entry.d.ts +1 -0
- package/dist/types/namespaces/strategy/methods/equity.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/eventrades.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/exit.d.ts +1 -0
- package/dist/types/namespaces/strategy/methods/fixed.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/grossloss.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/grossloss_percent.d.ts +4 -0
- package/dist/types/namespaces/strategy/methods/grossprofit.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/grossprofit_percent.d.ts +4 -0
- package/dist/types/namespaces/strategy/methods/initial_capital.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/long.d.ts +4 -0
- package/dist/types/namespaces/strategy/methods/losstrades.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/margin_liquidation_price.d.ts +13 -0
- package/dist/types/namespaces/strategy/methods/max_contracts_held_all.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/max_contracts_held_long.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/max_contracts_held_short.d.ts +2 -0
- package/dist/types/namespaces/strategy/methods/max_drawdown.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/max_drawdown_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/max_runup.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/max_runup_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/netprofit.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/netprofit_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/openprofit.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/openprofit_percent.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/opentrades.d.ts +9 -0
- package/dist/types/namespaces/strategy/methods/order.d.ts +6 -0
- package/dist/types/namespaces/strategy/methods/param.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/percent_of_equity.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/position_avg_price.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/position_entry_name.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/position_size.d.ts +5 -0
- package/dist/types/namespaces/strategy/methods/risk.d.ts +22 -0
- package/dist/types/namespaces/strategy/methods/short.d.ts +4 -0
- package/dist/types/namespaces/strategy/methods/wintrades.d.ts +2 -0
- package/dist/types/namespaces/strategy/strategy.index.d.ts +7 -0
- package/dist/types/namespaces/strategy/types.d.ts +176 -0
- package/dist/types/namespaces/strategy/utils.d.ts +84 -0
- package/package.json +3 -3
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strategy configuration options.
|
|
3
|
+
*
|
|
4
|
+
* Field names mirror Pine's strategy() declaration parameters exactly
|
|
5
|
+
* (snake_case, single-word where Pine uses one word). See
|
|
6
|
+
* https://www.tradingview.com/pine-script-reference/v5/#fun_strategy
|
|
7
|
+
*/
|
|
8
|
+
export interface StrategyConfig {
|
|
9
|
+
title: string;
|
|
10
|
+
shorttitle?: string;
|
|
11
|
+
overlay: boolean;
|
|
12
|
+
format?: string;
|
|
13
|
+
precision?: number;
|
|
14
|
+
scale?: string;
|
|
15
|
+
pyramiding?: number;
|
|
16
|
+
calc_on_order_fills?: boolean;
|
|
17
|
+
calc_on_every_tick?: boolean;
|
|
18
|
+
max_bars_back?: number;
|
|
19
|
+
backtest_fill_limits_assumption?: number;
|
|
20
|
+
default_qty_type?: string;
|
|
21
|
+
default_qty_value?: number;
|
|
22
|
+
initial_capital?: number;
|
|
23
|
+
currency?: string;
|
|
24
|
+
slippage?: number;
|
|
25
|
+
commission_type?: string;
|
|
26
|
+
commission_value?: number;
|
|
27
|
+
process_orders_on_close?: boolean;
|
|
28
|
+
close_entries_rule?: string;
|
|
29
|
+
margin_long?: number;
|
|
30
|
+
margin_short?: number;
|
|
31
|
+
explicit_plot_zorder?: boolean;
|
|
32
|
+
max_lines_count?: number;
|
|
33
|
+
max_labels_count?: number;
|
|
34
|
+
max_boxes_count?: number;
|
|
35
|
+
max_polylines_count?: number;
|
|
36
|
+
calc_bars_count?: number;
|
|
37
|
+
risk_free_rate?: number;
|
|
38
|
+
use_bar_magnifier?: boolean;
|
|
39
|
+
fill_orders_on_standard_ohlc?: boolean;
|
|
40
|
+
dynamic_requests?: boolean;
|
|
41
|
+
behind_chart?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A single trade — either currently open or already closed.
|
|
45
|
+
*
|
|
46
|
+
* Field names mirror Pine's per-trade getters from
|
|
47
|
+
* strategy.closedtrades.*(idx) / strategy.opentrades.*(idx).
|
|
48
|
+
*
|
|
49
|
+
* `size` is SIGNED to match Pine: positive = long, negative = short.
|
|
50
|
+
* The historical direction/qty pair has been collapsed into this single
|
|
51
|
+
* field, matching what `strategy.closedtrades.size(idx)` returns.
|
|
52
|
+
*/
|
|
53
|
+
export interface Trade {
|
|
54
|
+
id: string;
|
|
55
|
+
entry_id: string;
|
|
56
|
+
entry_price: number;
|
|
57
|
+
entry_bar_index: number;
|
|
58
|
+
entry_time: number;
|
|
59
|
+
entry_comment?: string;
|
|
60
|
+
exit_id?: string;
|
|
61
|
+
exit_price?: number;
|
|
62
|
+
exit_bar_index?: number;
|
|
63
|
+
exit_time?: number;
|
|
64
|
+
exit_comment?: string;
|
|
65
|
+
size: number;
|
|
66
|
+
profit?: number;
|
|
67
|
+
commission?: number;
|
|
68
|
+
max_drawdown?: number;
|
|
69
|
+
max_runup?: number;
|
|
70
|
+
status: 'open' | 'closed';
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A pending or filled order tracked internally by the engine.
|
|
74
|
+
*
|
|
75
|
+
* No Pine API exposes pending orders directly. Field names follow Pine's
|
|
76
|
+
* `strategy.entry()` / `strategy.order()` parameter names where they map
|
|
77
|
+
* (`limit`, `stop`, `oca_name`, `oca_type`), and snake_case for the rest.
|
|
78
|
+
*/
|
|
79
|
+
export interface Order {
|
|
80
|
+
id: string;
|
|
81
|
+
direction: number;
|
|
82
|
+
qty: number;
|
|
83
|
+
type: 'market' | 'limit' | 'stop' | 'stop-limit';
|
|
84
|
+
limit?: number;
|
|
85
|
+
stop?: number;
|
|
86
|
+
bar: number;
|
|
87
|
+
time: number;
|
|
88
|
+
oca_name?: string;
|
|
89
|
+
oca_type?: 'cancel' | 'reduce' | 'none';
|
|
90
|
+
comment?: string;
|
|
91
|
+
fill_price?: number;
|
|
92
|
+
fill_bar?: number;
|
|
93
|
+
fill_time?: number;
|
|
94
|
+
status: 'pending' | 'filled' | 'cancelled';
|
|
95
|
+
category?: 'entry' | 'exit';
|
|
96
|
+
profit?: number;
|
|
97
|
+
loss?: number;
|
|
98
|
+
trail_price?: number;
|
|
99
|
+
trail_offset?: number;
|
|
100
|
+
trail_points?: number;
|
|
101
|
+
from_entry?: string;
|
|
102
|
+
qty_percent?: number;
|
|
103
|
+
comment_profit?: string;
|
|
104
|
+
comment_loss?: string;
|
|
105
|
+
comment_trailing?: string;
|
|
106
|
+
alert_message?: string;
|
|
107
|
+
alert_profit?: string;
|
|
108
|
+
alert_loss?: string;
|
|
109
|
+
alert_trailing?: string;
|
|
110
|
+
disable_alert?: boolean;
|
|
111
|
+
immediately?: boolean;
|
|
112
|
+
trail_peak?: number;
|
|
113
|
+
trail_armed?: boolean;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Strategy state stored on the Context after a backtest run.
|
|
117
|
+
*
|
|
118
|
+
* Top-level scalars mirror Pine's `strategy.*` properties 1:1 (snake_case,
|
|
119
|
+
* Pine's single-word concatenations like `netprofit` / `grossprofit` /
|
|
120
|
+
* `grossloss` / `openprofit` preserved). Position fields are FLATTENED
|
|
121
|
+
* — Pine exposes `strategy.position_size` / `position_avg_price` /
|
|
122
|
+
* `position_entry_name` as three separate scalars, not a nested object.
|
|
123
|
+
*
|
|
124
|
+
* The `opentrades` / `closedtrades` arrays use Pine's exact names with
|
|
125
|
+
* `.length` providing the count — same semantic as Pine's int count but
|
|
126
|
+
* also indexable for the per-trade getter equivalents.
|
|
127
|
+
*/
|
|
128
|
+
export interface StrategyState {
|
|
129
|
+
config: StrategyConfig;
|
|
130
|
+
opentrades: Trade[];
|
|
131
|
+
closedtrades: Trade[];
|
|
132
|
+
pending_orders: Order[];
|
|
133
|
+
position_size: number;
|
|
134
|
+
position_avg_price: number;
|
|
135
|
+
position_entry_name: string;
|
|
136
|
+
initial_capital: number;
|
|
137
|
+
account_currency: string;
|
|
138
|
+
equity: number;
|
|
139
|
+
netprofit: number;
|
|
140
|
+
grossprofit: number;
|
|
141
|
+
grossloss: number;
|
|
142
|
+
openprofit: number;
|
|
143
|
+
max_drawdown: number;
|
|
144
|
+
max_runup: number;
|
|
145
|
+
equity_peak: number;
|
|
146
|
+
equity_trough: number;
|
|
147
|
+
wintrades: number;
|
|
148
|
+
losstrades: number;
|
|
149
|
+
eventrades: number;
|
|
150
|
+
wintrades_total_profit: number;
|
|
151
|
+
losstrades_total_loss: number;
|
|
152
|
+
max_contracts_held_all: number;
|
|
153
|
+
max_contracts_held_long: number;
|
|
154
|
+
max_contracts_held_short: number;
|
|
155
|
+
risk_rules: {
|
|
156
|
+
allow_entry_in?: 'long' | 'short' | 'all';
|
|
157
|
+
max_cons_loss_days?: {
|
|
158
|
+
count: number;
|
|
159
|
+
alert_message?: string;
|
|
160
|
+
};
|
|
161
|
+
max_drawdown?: {
|
|
162
|
+
value: number;
|
|
163
|
+
type: 'cash' | 'percent_of_equity';
|
|
164
|
+
};
|
|
165
|
+
max_intraday_filled_orders?: {
|
|
166
|
+
count: number;
|
|
167
|
+
alert_message?: string;
|
|
168
|
+
};
|
|
169
|
+
max_intraday_loss?: {
|
|
170
|
+
value: number;
|
|
171
|
+
type: 'cash' | 'percent_of_equity';
|
|
172
|
+
};
|
|
173
|
+
max_position_size?: number;
|
|
174
|
+
};
|
|
175
|
+
risk_halted: boolean;
|
|
176
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Order, StrategyState } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Parse strategy() function arguments
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseStrategyOptions(args: any[]): any;
|
|
6
|
+
/**
|
|
7
|
+
* Calculate order quantity based on strategy configuration
|
|
8
|
+
*/
|
|
9
|
+
export declare function calculateOrderQty(context: any, specifiedQty: number | undefined, direction: number, fillPrice: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Process pending orders and execute them
|
|
12
|
+
*/
|
|
13
|
+
export declare function processStrategyOrders(context: any): void;
|
|
14
|
+
/**
|
|
15
|
+
* Parse direction string/number to numeric value
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseDirection(direction: number | string): number;
|
|
18
|
+
/**
|
|
19
|
+
* Returns true if adding a same-direction entry would exceed the strategy's
|
|
20
|
+
* pyramiding cap. Counts existing open trades in the requested direction.
|
|
21
|
+
*
|
|
22
|
+
* `strategy.entry()` (when implemented) consults this; `strategy.order()` does
|
|
23
|
+
* NOT — Pine treats strategy.order as a low-level primitive that ignores the
|
|
24
|
+
* pyramiding limit.
|
|
25
|
+
*/
|
|
26
|
+
export declare function wouldExceedPyramiding(strategy: StrategyState, direction: number): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Pre-fill risk-rule check. Returns true if the order should be BLOCKED.
|
|
29
|
+
*
|
|
30
|
+
* Consulted rules (independent; first violation wins):
|
|
31
|
+
* - risk_halted (latched by any catastrophic rule)
|
|
32
|
+
* - allow_entry_in: 'long' blocks short orders; 'short' blocks long
|
|
33
|
+
* - max_position_size: post-fill |position_size| would exceed N
|
|
34
|
+
*/
|
|
35
|
+
export declare function isOrderBlockedByRisk(strategy: StrategyState, order: Order): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Latches `risk_halted` when any catastrophic rule trips (max_drawdown,
|
|
38
|
+
* max_intraday_loss, max_cons_loss_days). Once halted, all entries are
|
|
39
|
+
* blocked for the rest of the run.
|
|
40
|
+
*
|
|
41
|
+
* Called after each close. The intraday rules use simple cumulative
|
|
42
|
+
* approximations — true day-rollover detection would require bar timestamp
|
|
43
|
+
* + timezone logic that's deferred.
|
|
44
|
+
*/
|
|
45
|
+
export declare function evaluateCatastrophicRiskHalt(strategy: StrategyState): void;
|
|
46
|
+
/**
|
|
47
|
+
* Open a new trade.
|
|
48
|
+
*
|
|
49
|
+
* @param direction +1 long, -1 short
|
|
50
|
+
* @param qty unsigned contract count
|
|
51
|
+
* @param price fill price
|
|
52
|
+
* @param time fill time (ms)
|
|
53
|
+
*/
|
|
54
|
+
export declare function openTrade(context: any, entryId: string, direction: number, qty: number, price: number, time: number): void;
|
|
55
|
+
/**
|
|
56
|
+
* Close partial or full position.
|
|
57
|
+
*
|
|
58
|
+
* FIFO accounting: closes oldest open trades first. Splits a trade if the
|
|
59
|
+
* close qty is smaller than the trade's remaining qty.
|
|
60
|
+
*/
|
|
61
|
+
export declare function closePartialPosition(context: any, qtyToClose: number, exitPrice: number, exitTime: number): void;
|
|
62
|
+
/**
|
|
63
|
+
* FIFO close of `qtyToClose` contracts from open trades, optionally filtered
|
|
64
|
+
* by `fromEntry` — when set, only trades whose `entry_id === fromEntry` are
|
|
65
|
+
* eligible. Falls back to closing across all open trades when empty/undefined.
|
|
66
|
+
*
|
|
67
|
+
* Wraps `closePartialPosition` by temporarily reorganizing `opentrades` so
|
|
68
|
+
* the matching trades sit at the head of the FIFO queue.
|
|
69
|
+
*/
|
|
70
|
+
export declare function closeMatching(context: any, fromEntry: string | undefined, qtyToClose: number, exitPrice: number, exitTime: number): void;
|
|
71
|
+
/**
|
|
72
|
+
* Process exit-category orders each bar (after entry-order fills, before the
|
|
73
|
+
* user script runs). Handles:
|
|
74
|
+
* - Market exits from strategy.close() / strategy.close_all() (fill at
|
|
75
|
+
* current bar's open if placed previously).
|
|
76
|
+
* - Conditional exits from strategy.exit() — TP / SL / trailing-stop
|
|
77
|
+
* triggers evaluated against current bar's high/low. Trailing-stop
|
|
78
|
+
* peak (trade.trail_peak) is updated each bar even when not triggered.
|
|
79
|
+
*/
|
|
80
|
+
export declare function processExitOrders(context: any): void;
|
|
81
|
+
/**
|
|
82
|
+
* Initialize strategy state
|
|
83
|
+
*/
|
|
84
|
+
export declare function initializeStrategy(context: any, config: any): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pinets",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.18",
|
|
4
4
|
"description": "Run Pine Script anywhere. PineTS is an open-source transpiler and runtime that brings Pine Script logic to Node.js and the browser with 1:1 syntax compatibility. Reliably write, port, and run indicators or strategies on your own infrastructure.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Pine Script",
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
},
|
|
106
106
|
"repository": {
|
|
107
107
|
"type": "git",
|
|
108
|
-
"url": "git+https://github.com/
|
|
108
|
+
"url": "git+https://github.com/LuxAlgo/PineTS.git"
|
|
109
109
|
},
|
|
110
|
-
"homepage": "https://
|
|
110
|
+
"homepage": "https://luxalgo.com/"
|
|
111
111
|
}
|