Creating Strategies
This page explains the core strategy contract in TradeJS and where to place strategy logic.
TradeJS supports two strategy creation paths:
Typical Strategy Layout
Each strategy package (built-ins in packages/strategies, user plugins in src/strategies) usually has:
config.tscore.tsfigures.ts(if strategy draws custom lines/points/zones)strategy.tsmanifest.tsadapters/ai.ts(optional)adapters/ml.ts(optional)hooks.ts(optional)
Runtime Contract
core.ts returns one of three decisions:
skipentryexit
Shared runtime handles:
- signal enrichment (AI/ML)
- policy gates
- order execution
- lifecycle hooks
Files:
@tradejs/node/strategies@tradejs/core/strategies- Strategy Runtime Hooks (full lifecycle hooks catalog)
Import rule:
- import Node runtime wiring from
@tradejs/node/strategies - import pure strategy helpers from
@tradejs/core/strategies - import shared contracts from
@tradejs/types - avoid non-public deep imports
Minimal core.ts Example
export const createMyStrategyCore: CreateStrategyCore<
MyStrategyConfig
> = async ({ strategyApi }) => {
return async () => {
const positionExists = await strategyApi.isCurrentPositionExists();
if (positionExists) {
return strategyApi.skip('POSITION_EXISTS');
}
const { currentPrice } = await strategyApi.getMarketData();
const { stopLossPrice, takeProfitPrice } =
strategyApi.getDirectionalTpSlPrices({
price: currentPrice,
direction: 'LONG',
takeProfitDelta: 2,
stopLossDelta: 1,
unit: 'percent',
});
return strategyApi.entry({
direction: 'LONG',
orderPlan: {
qty: 1,
stopLossPrice,
takeProfits: [{ rate: 1, price: takeProfitPrice }],
},
});
};
};
Where Runtime Behavior Is Defined
-
manifest.ts:entryRuntimeDefaultshooksaiAdaptermlAdapter
-
tradejs.config.ts:hooksfor shared behavior across all strategies in the current project- useful for common risk rules, cross-strategy position handling, and shared order filters
-
adapters/*:- runtime policy mapping from config (
mapEntryRuntimeFromConfig) - AI/ML payload normalization
- runtime policy mapping from config (
Step-by-Step Guides
- TypeScript Strategy Step by Step — full TypeScript strategy walkthrough
- Pine Strategy Step by Step — full Pine strategy walkthrough
External Strategy as an npm Plugin
import { defineConfig } from '@tradejs/core/config';
import { basePreset } from '@tradejs/base';
export default defineConfig(basePreset, {
strategies: ['@scope/tradejs-strategy-pack'],
});
Plugin example:
- publish your strategy as an npm package and register it in
strategies