@yuants/app-trade-copier
v0.4.30
Published
A [trade copier](https://github.com/No-Trade-No-Life/Yuan/tree/main/apps/trade-copier) is an app that copies trades from some accounts to others.
Downloads
414
Readme
Trade Copier
A trade copier is an app that copies trades from some accounts to others.
graph LR;
SourceAccount1_Product1 -->|x2| TargetAccount1_Product1;
SourceAccount1_Product2 -->|x1| TargetAccount2_Product2;
SourceAccount3_Product1 -->|x-1| TargetAccount1_Product1;
SourceAccount1_Product1 -->|x3| TargetAccount2_Product1;
SourceAccount2_Product1 -->|x3| TargetAccount1_Product1;
SourceAccount2_Product2 -->|x2| TargetAccount2_Product2;
SourceAccount3_Product2.A -->|x0.5| TargetAccount2_Product2;
- If you want to follow the signals from some agent models in your accounts.
- If you want to follow some traders' trades in your accounts.
- If you want to make a account leading others.
- If you want to scale the positions.
Getting started
- Prepare a host.
- Prepare a MongoDB storage terminal.
- Prepare the accounts you want to copy trades from and to.
- Ensure the products configured in the storage. (You can use the GUI to finish this step.)
- Write data records of
trade_copy_relation
in the storage. (You can use the GUI to finish this step.) - If you want to use trading algorithm, you need to write data records of
trade_copier_trade_config
in the storage. (You can use the GUI to finish this step). - Deploy this app in the host.
- Restart this app if you update the
trade_copy_relation
or thetrade_copier_trade_config
records.
Technical Notes
When the trade copier starts, it will do the following things:
- Load relations of trade copier
trade_copy_relation
from the storage. See theITradeCopyRelation
interface. - Subscribe related account info as
Observable<IAccountInfo>
. - For each target account and each target product,
- Combine the latest related source account info list as
Observable<IAccountInfo[]>
; - Summary the target account's target position;
- Submit orders if the target position is not equal to the target account's current position;
- Wait until the order-submitting request done (no matter succeeds or fails);
- Wait until the next target account info feed back, to ensure the target account's current position is updated;
- Loop until the target account's target position is equal to the target account's current position.
- Combine the latest related source account info list as
graph TD;
StartAction -->|Immediately| AccountInfoAggregateAction;
AccountInfoAggregateAction -.->|wait for target and source accounts| CalcPositionDiffAction;
AccountInfoAggregateAction -.->|timeout in 30s and retry| AccountInfoAggregateAction;
CalcPositionDiffAction -->|position diff list| CyberTradeOrderDispatchAction;
CyberTradeOrderDispatchAction -->|if no order, immediately| CompleteAction;
CyberTradeOrderDispatchAction -->|if algorithm configured| SerialOrderPlaceAction;
CyberTradeOrderDispatchAction -->|if algorithm undefined| ConcurrentOrderPlaceAction;
SerialOrderPlaceAction -.->|Orders all settled| CompleteAction;
ConcurrentOrderPlaceAction -.->|Orders all settled| CompleteAction;
CompleteAction -->|Immediately| StartAction;
Model
- You can specify a certain product of a source account to a certain product of a target account.
- You can scale the position by specifying
multiple
. Trade copier will scale the position bymultiple
times. Trade copier will close the positions if the value ofmultiple
is equal to 0. If the value ofmultiple
is negative, the position's direction will be flip fromLONG
toSHORT
and vice versa. - You can ignore some positions by specifying
exclusive_comment_pattern
. The value ofexclusive_comment_pattern
should be a regular expression. If the comment of a position matches the regular expression, the position will be ignored.
interface ITradeCopyRelation {
source_account_id: string;
source_product_id: string;
target_account_id: string;
target_product_id: string;
multiple: number;
exclusive_comment_pattern?: string;
disabled?: boolean;
}
interface ITradeCopierTradeConfig {
account_id: string;
product_id: string;
max_volume_per_order: number;
}
Q&A
Q: What if multiple trade copiers copy trades work with same target account?
A: It's very dangerous. It may cause position oscillation. Orders may be over-submitted or under-submitted. Your account balance maybe rapidly decrease. You should avoid this problem.
- Ensure there's only one trade copier app instance in one host.
- Ensure every target account is under only one trade copier's control if you have multiple hosts.
Q: What if multiple source accounts work with same target account?
A: It's OK. The trade copier will sum up the positions from multiple source accounts.