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
|
@@ -4,6 +4,7 @@ import { Input } from './namespaces/input/input.index';
|
|
|
4
4
|
import PineMath from './namespaces/math/math.index';
|
|
5
5
|
import { PineRequest } from './namespaces/request/request.index';
|
|
6
6
|
import TechnicalAnalysis from './namespaces/ta/ta.index';
|
|
7
|
+
import { StrategyState } from './namespaces/strategy/strategy.index';
|
|
7
8
|
import { Series } from './Series';
|
|
8
9
|
import type { IndicatorOptions } from './types/PineTypes';
|
|
9
10
|
export declare class Context {
|
|
@@ -11,6 +12,7 @@ export declare class Context {
|
|
|
11
12
|
indicator: IndicatorOptions;
|
|
12
13
|
cache: any;
|
|
13
14
|
taState: any;
|
|
15
|
+
strategy?: StrategyState;
|
|
14
16
|
isSecondaryContext: boolean;
|
|
15
17
|
chartTimezone: string | null;
|
|
16
18
|
dataVersion: number;
|
|
@@ -144,10 +144,6 @@ export declare class PineTS {
|
|
|
144
144
|
on: (event: 'data' | 'error' | 'warning' | 'alert', callback: Function) => void;
|
|
145
145
|
stop: () => void;
|
|
146
146
|
};
|
|
147
|
-
/**
|
|
148
|
-
* Run the script completely and return the final context (backward compatible behavior)
|
|
149
|
-
* @private
|
|
150
|
-
*/
|
|
151
147
|
/**
|
|
152
148
|
* Run an already-transpiled PineTS function in this instance — no
|
|
153
149
|
* additional transpile/parse pass. Used by `request.security_lower_tf`'s
|
|
@@ -158,6 +154,16 @@ export declare class PineTS {
|
|
|
158
154
|
* arbitrary function is unsafe.
|
|
159
155
|
*/
|
|
160
156
|
runPretranspiled(transpiledFn: Function, inputs?: Record<string, any>, periods?: number): Promise<Context>;
|
|
157
|
+
/**
|
|
158
|
+
* Run the script completely and return the final context.
|
|
159
|
+
*
|
|
160
|
+
* Execution is split: all bars except the last are processed first, then a
|
|
161
|
+
* var-state snapshot is taken, then the last bar is processed. This gives
|
|
162
|
+
* updateTail() a reliable snapshot-based restore point, matching the
|
|
163
|
+
* pattern used by _runPaginated and eliminates the pop-based drift that
|
|
164
|
+
* occurred when var variables were modified in-place during re-execution.
|
|
165
|
+
* @private
|
|
166
|
+
*/
|
|
161
167
|
private _runComplete;
|
|
162
168
|
/**
|
|
163
169
|
* Run the script with pagination, yielding results page by page
|
|
@@ -196,9 +202,16 @@ export declare class PineTS {
|
|
|
196
202
|
private _appendCandle;
|
|
197
203
|
/**
|
|
198
204
|
* Update the secondary context's tail with fresh market data.
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
205
|
+
*
|
|
206
|
+
* Uses snapshot-restore for reliable var state rollback, matching the
|
|
207
|
+
* approach used by _runPaginated. The pop-based _removeLastResult is
|
|
208
|
+
* only used as a fallback for contexts that have no snapshot (e.g. those
|
|
209
|
+
* produced by runPretranspiled, which skips the split-execute pattern).
|
|
210
|
+
*
|
|
211
|
+
* After restoring state and re-executing, a fresh snapshot is taken before
|
|
212
|
+
* the last bar so that subsequent updateTail() calls also have a valid
|
|
213
|
+
* restore point.
|
|
214
|
+
*
|
|
202
215
|
* @param context - The cached secondary context to update
|
|
203
216
|
* @returns true if data was updated, false if no changes
|
|
204
217
|
*/
|
|
@@ -15,6 +15,15 @@ export declare class LineHelper {
|
|
|
15
15
|
* scalar value is used for line properties.
|
|
16
16
|
*/
|
|
17
17
|
private _resolve;
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a color value, PRESERVING na markers so renderers can
|
|
20
|
+
* detect "no color". Pine emits na as null (from `color(na)` —
|
|
21
|
+
* `color.any` returns null) or NaN (from raw `na` literal). Both
|
|
22
|
+
* must survive into the plot data — collapsing them to a default
|
|
23
|
+
* forces the renderer to paint a visible line where the script
|
|
24
|
+
* asked for none. Mirrors Box/Polyline `_resolveColor`.
|
|
25
|
+
*/
|
|
26
|
+
private _resolveColor;
|
|
18
27
|
private _createLine;
|
|
19
28
|
private _enforceMaxCount;
|
|
20
29
|
new(...args: any[]): LineObject;
|
|
@@ -11,6 +11,17 @@ export declare class LinefillHelper {
|
|
|
11
11
|
* Resolve a value that may be a Series, a bound function, or a plain scalar.
|
|
12
12
|
*/
|
|
13
13
|
private _resolve;
|
|
14
|
+
/**
|
|
15
|
+
* Resolve a color value, PRESERVING na markers so renderers can
|
|
16
|
+
* detect "no color" instead of being forced to paint a default.
|
|
17
|
+
* Pine emits na either as null (from `color(na)` — `color.any`
|
|
18
|
+
* returns null) or as NaN (from raw `na` literals). The previous
|
|
19
|
+
* implementation collapsed both to '' via `_resolve(color) || ''`,
|
|
20
|
+
* which destroyed the signal and made `linefill.new(line1, line2,
|
|
21
|
+
* color(na))` render as a default-coloured fill instead of being
|
|
22
|
+
* invisible. Mirror Box/Polyline `_resolveColor`.
|
|
23
|
+
*/
|
|
24
|
+
private _resolveColor;
|
|
14
25
|
new(line1: LineObject, line2: LineObject, color: any): LinefillObject;
|
|
15
26
|
any(...args: any[]): LinefillObject | null;
|
|
16
27
|
set_color(id: LinefillObject, color: any): void;
|
|
@@ -6,15 +6,15 @@ export declare class PolylineObject {
|
|
|
6
6
|
curved: boolean;
|
|
7
7
|
closed: boolean;
|
|
8
8
|
xloc: string;
|
|
9
|
-
line_color:
|
|
10
|
-
fill_color:
|
|
9
|
+
line_color: any;
|
|
10
|
+
fill_color: any;
|
|
11
11
|
line_style: string;
|
|
12
12
|
line_width: number;
|
|
13
13
|
force_overlay: boolean;
|
|
14
14
|
_deleted: boolean;
|
|
15
15
|
/** Bar index at which this object was created (for streaming rollback) */
|
|
16
16
|
_createdAtBar: number;
|
|
17
|
-
constructor(points: ChartPointObject[], curved?: boolean, closed?: boolean, xloc?: string, line_color?:
|
|
17
|
+
constructor(points: ChartPointObject[], curved?: boolean, closed?: boolean, xloc?: string, line_color?: any, fill_color?: any, line_style?: string, line_width?: number, force_overlay?: boolean);
|
|
18
18
|
delete(): void;
|
|
19
19
|
toPlotData(): any;
|
|
20
20
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Average loss across losing closed trades, returned as a POSITIVE number
|
|
3
|
+
* (Pine convention: average loss is unsigned). NaN when no losers yet.
|
|
4
|
+
* Matches Pine's strategy.avg_losing_trade.
|
|
5
|
+
*/
|
|
6
|
+
export declare function avg_losing_trade(context: any): () => number;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Average per-trade return as a percent. Computed as the mean of each
|
|
3
|
+
* closed trade's individual profit_percent = profit / (entry_price * |size|) * 100.
|
|
4
|
+
* NaN when no trades have closed. Matches Pine's strategy.avg_trade_percent.
|
|
5
|
+
*/
|
|
6
|
+
export declare function avg_trade_percent(context: any): () => number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cancel(context: any): (...args: any[]) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function close(context: any): (...args: any[]) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function close_all(context: any): (...args: any[]) => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pine's `strategy.closedtrades` serves a dual role:
|
|
3
|
+
* - As a scalar: a series int count of closed trades.
|
|
4
|
+
* - As a namespace: `strategy.closedtrades.profit(idx)` etc.
|
|
5
|
+
*
|
|
6
|
+
* Our transpiler renders the bare access as a CALL (`strategy.closedtrades()`),
|
|
7
|
+
* and the chained access as a CHAINED CALL (`strategy.closedtrades().profit(0)`).
|
|
8
|
+
* Both must work. The trick: the call returns a hybrid object that:
|
|
9
|
+
* - has `valueOf()` returning the count (so it behaves like an int in
|
|
10
|
+
* arithmetic/comparison contexts, e.g. `_ct > 0`),
|
|
11
|
+
* - has the per-trade methods (`profit`, `size`, ...) attached.
|
|
12
|
+
*
|
|
13
|
+
* This pattern keeps the transpiler unchanged while supporting both shapes.
|
|
14
|
+
*/
|
|
15
|
+
export declare function closedtrades(context: any): () => any;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a value from the symbol's currency to the account currency.
|
|
3
|
+
*
|
|
4
|
+
* For the common same-currency case (e.g. BTCUSDC's quote currency USDC ≈ USD),
|
|
5
|
+
* this is an identity. Genuine cross-currency conversion would require an FX
|
|
6
|
+
* rate source which we don't model; returns the input unchanged in that case
|
|
7
|
+
* with a console warning on first call.
|
|
8
|
+
*/
|
|
9
|
+
export declare function convert_to_account(context: any): (value: number) => number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the qty that would be used for an entry at the given fill price,
|
|
3
|
+
* given the strategy's `default_qty_type` and `default_qty_value`. Mirrors
|
|
4
|
+
* `calculateOrderQty()` without a specified qty (always uses the default).
|
|
5
|
+
*
|
|
6
|
+
* Pine signature: strategy.default_entry_qty(fill_price) → series float
|
|
7
|
+
*/
|
|
8
|
+
export declare function default_entry_qty(context: any): (fillPrice: number) => number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function entry(context: any): (...args: any[]) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function exit(context: any): (...args: any[]) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Price at which the current leveraged position would be force-liquidated.
|
|
3
|
+
* Returns NaN when flat or when the relevant margin% is 100 (no leverage).
|
|
4
|
+
*
|
|
5
|
+
* Formula (simplified):
|
|
6
|
+
* long → entry_avg_price * (1 - margin_long / 100)
|
|
7
|
+
* short → entry_avg_price * (1 + margin_short / 100)
|
|
8
|
+
*
|
|
9
|
+
* This is the simple peak-loss-tolerable price; real broker liquidation
|
|
10
|
+
* depends on maintenance margin schedules which we don't model.
|
|
11
|
+
* Matches Pine's strategy.margin_liquidation_price in the common case.
|
|
12
|
+
*/
|
|
13
|
+
export declare function margin_liquidation_price(context: any): () => number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pine's `strategy.opentrades` mirrors strategy.closedtrades' dual-role
|
|
3
|
+
* pattern — see that file's header for the rationale.
|
|
4
|
+
*
|
|
5
|
+
* strategy.opentrades → scalar count via valueOf
|
|
6
|
+
* strategy.opentrades.profit(0) → per-trade unrealized P&L
|
|
7
|
+
* strategy.opentrades.capital_held → property (sum of held capital)
|
|
8
|
+
*/
|
|
9
|
+
export declare function opentrades(context: any): () => any;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `strategy.risk` is a nested namespace with 6 setter functions that
|
|
3
|
+
* configure pre-trade risk rules. Each call mutates `context.strategy.risk_rules`;
|
|
4
|
+
* the actual blocking is performed by `checkRiskRules()` in utils.ts,
|
|
5
|
+
* invoked before each entry fills.
|
|
6
|
+
*
|
|
7
|
+
* Pine signatures:
|
|
8
|
+
* strategy.risk.allow_entry_in(value) → void
|
|
9
|
+
* strategy.risk.max_cons_loss_days(count, alert_message) → void
|
|
10
|
+
* strategy.risk.max_drawdown(value, type, alert_message) → void
|
|
11
|
+
* strategy.risk.max_intraday_filled_orders(count, alert_message) → void
|
|
12
|
+
* strategy.risk.max_intraday_loss(value, type, alert_message) → void
|
|
13
|
+
* strategy.risk.max_position_size(contracts) → void
|
|
14
|
+
*/
|
|
15
|
+
export declare function risk(context: any): {
|
|
16
|
+
allow_entry_in: (value: "long" | "short" | "all") => void;
|
|
17
|
+
max_cons_loss_days: (count: number, alert_message?: string) => void;
|
|
18
|
+
max_drawdown: (value: number, type: "cash" | "percent_of_equity", _alert_message?: string) => void;
|
|
19
|
+
max_intraday_filled_orders: (count: number, alert_message?: string) => void;
|
|
20
|
+
max_intraday_loss: (value: number, type: "cash" | "percent_of_equity", _alert_message?: string) => void;
|
|
21
|
+
max_position_size: (contracts: number) => void;
|
|
22
|
+
};
|