Skip to content

Commit

Permalink
feat(kernel): add stop loss unit back (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrimbda authored Oct 17, 2023
1 parent b47dd36 commit 908e4be
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 247 deletions.
10 changes: 10 additions & 0 deletions common/changes/@yuants/agent/2023-10-17-09-20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/agent",
"comment": "add stop loss",
"type": "patch"
}
],
"packageName": "@yuants/agent"
}
10 changes: 10 additions & 0 deletions common/changes/@yuants/kernel/2023-10-17-09-20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/kernel",
"comment": "add stop loss",
"type": "patch"
}
],
"packageName": "@yuants/kernel"
}
1 change: 1 addition & 0 deletions libraries/agent/etc/agent.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface IAgentConf {
publish_account?: boolean;
resume_on_source_margin_below?: number;
start_time?: string;
stop_loss_drawdown_quota?: number;
use_general_product?: boolean;
}

Expand Down
31 changes: 20 additions & 11 deletions libraries/agent/src/AgentScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface IAgentConf {
allow_fallback_specific_product?: boolean;
/** 止损后恢复开仓的保证金阈值(小于) */
resume_on_source_margin_below?: number;
/** 止损浮亏阈值 */
stop_loss_drawdown_quota?: number;
/** 仓位映射函数 */
coefficient_fn_str?: string;
/** 是否禁用打印日志 */
Expand Down Expand Up @@ -152,6 +154,10 @@ export const agentConfSchema: JSONSchema7 = {
type: 'number',
title: '恢复交易的使用保证金线',
},
stop_loss_drawdown_quota: {
type: 'number',
title: '止损浮亏阈值',
},
coefficient_fn_str: {
type: 'string',
title: '仓位映射函数',
Expand Down Expand Up @@ -385,7 +391,7 @@ export const AgentScene = async (terminal: Terminal, agentConf: IAgentConf) => {
let portfolioAccountPerformanceUnit: AccountPerformanceUnit | undefined;
let portfolioHistoryOrderUnit: HistoryOrderUnit | undefined;

if (agentConf.resume_on_source_margin_below) {
if (agentConf.resume_on_source_margin_below && agentConf.stop_loss_drawdown_quota) {
// TODO: 止损信号
const stopLossInitAccountInfo = createEmptyAccountInfo(
`${resolved_account_id}-SL`,
Expand All @@ -401,27 +407,30 @@ export const AgentScene = async (terminal: Terminal, agentConf: IAgentConf) => {
stopLossHistoryOrderUnit,
);

new StopLossOrderMapperUnit(
stopLossAccountInfoUnit = new AccountSimulatorUnit(
kernel,
stopLossInitAccountInfo.account_id,
agentConf.resume_on_source_margin_below,
productDataUnit,
quoteDataUnit,
originAccountInfoUnit,
originAccountPerformanceUnit,
originHistoryOrderUnit,
stopLossOrderMatchingUnit,
stopLossHistoryOrderUnit,
stopLossInitAccountInfo,
);
stopLossAccountPerformanceUnit = new AccountPerformanceUnit(kernel, stopLossAccountInfoUnit);

stopLossAccountInfoUnit = new AccountSimulatorUnit(
new StopLossOrderMapperUnit(
kernel,
stopLossInitAccountInfo.account_id,
agentConf.resume_on_source_margin_below,
agentConf.stop_loss_drawdown_quota,
productDataUnit,
quoteDataUnit,
positionLimitAccountInfoUnit || originAccountInfoUnit,
positionLimitAccountPerformanceUnit || originAccountPerformanceUnit,
positionLimitHistoryOrderUnit || originHistoryOrderUnit,
stopLossAccountInfoUnit,
stopLossOrderMatchingUnit,
stopLossHistoryOrderUnit,
stopLossInitAccountInfo,
);
stopLossAccountPerformanceUnit = new AccountPerformanceUnit(kernel, stopLossAccountInfoUnit);

if (agentConf.publish_account) {
const unit = new BasicUnit(kernel);
const accountInfo$ = new Subject<IAccountInfo>();
Expand Down
13 changes: 5 additions & 8 deletions libraries/kernel/etc/kernel.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,9 @@ export const ShellScene: (terminal: Terminal, shellConf: IShellConf, scriptResol
portfolioHistoryOrderUnit: HistoryOrderUnit | undefined;
}>;

// @public
export const StopLossAccountReplayScene: (terminal: Terminal, account_id: string, currency: string, leverage: number, start_timestamp: number, end_timestamp: number, period_in_sec: number, resumeOnSourceMarginBelow: number, datasource_id?: string) => {
kernel: Kernel;
accountInfoUnit: AccountSimulatorUnit;
accountPerformanceUnit: AccountPerformanceUnit;
};

// @public
export class StopLossOrderMapperUnit extends BasicUnit {
constructor(kernel: Kernel, account_id: string, resumeOnSourceMarginBelow: number, productDataUnit: ProductDataUnit, quoteDataUnit: QuoteDataUnit, sourceAccountSimulatorUnit: AccountSimulatorUnit, sourceAccountPerformanceUnit: AccountPerformanceUnit, sourceHistoryOrderUnit: HistoryOrderUnit, targetOrderMatchingUnit: OrderMatchingUnit, targetHistoryOrderUnit: HistoryOrderUnit);
constructor(kernel: Kernel, account_id: string, resumeOnSourceMarginBelow: number, stopLossDrawdownQuota: number, productDataUnit: ProductDataUnit, quoteDataUnit: QuoteDataUnit, sourceAccountSimulatorUnit: AccountSimulatorUnit, sourceAccountPerformanceUnit: AccountPerformanceUnit, sourceHistoryOrderUnit: HistoryOrderUnit, targetAccountSimulatorUnit: AccountSimulatorUnit, targetOrderMatchingUnit: OrderMatchingUnit, targetHistoryOrderUnit: HistoryOrderUnit);
// (undocumented)
account_id: string;
// (undocumented)
Expand All @@ -826,6 +819,10 @@ export class StopLossOrderMapperUnit extends BasicUnit {
// (undocumented)
sourceHistoryOrderUnit: HistoryOrderUnit;
// (undocumented)
stopLossDrawdownQuota: number;
// (undocumented)
targetAccountSimulatorUnit: AccountSimulatorUnit;
// (undocumented)
targetHistoryOrderUnit: HistoryOrderUnit;
// (undocumented)
targetOrderMatchingUnit: OrderMatchingUnit;
Expand Down
19 changes: 11 additions & 8 deletions libraries/kernel/src/scenes/ShellScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,27 +398,30 @@ export const ShellScene = async (
stopLossHistoryOrderUnit,
);

stopLossAccountInfoUnit = new AccountSimulatorUnit(
kernel,
productDataUnit,
quoteDataUnit,
stopLossHistoryOrderUnit,
stopLossInitAccountInfo,
);
stopLossAccountPerformanceUnit = new AccountPerformanceUnit(kernel, stopLossAccountInfoUnit);

new StopLossOrderMapperUnit(
kernel,
stopLossInitAccountInfo.account_id,
30000,
shellConf.resume_on_source_margin_below,
productDataUnit,
quoteDataUnit,
accountInfoUnit,
accountPerformanceUnit,
historyOrderUnit,
stopLossAccountInfoUnit,
stopLossOrderMatchingUnit,
stopLossHistoryOrderUnit,
);

stopLossAccountInfoUnit = new AccountSimulatorUnit(
kernel,
productDataUnit,
quoteDataUnit,
stopLossHistoryOrderUnit,
stopLossInitAccountInfo,
);
stopLossAccountPerformanceUnit = new AccountPerformanceUnit(kernel, stopLossAccountInfoUnit);
if (shellConf.publish_account) {
const unit = new BasicUnit(kernel);
const accountInfo$ = new Subject<IAccountInfo>();
Expand Down
170 changes: 0 additions & 170 deletions libraries/kernel/src/scenes/StopLossAccountReplayScene.ts

This file was deleted.

1 change: 0 additions & 1 deletion libraries/kernel/src/scenes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './AccountReplayScene';
export * from './OrderMergeReplayScene';
export * from './ShellScene';
export * from './StopLossAccountReplayScene';
export * from './ActivePortfolioManagementScene';
Loading

0 comments on commit 908e4be

Please sign in to comment.