Skip to content

Commit

Permalink
feat: Shannon Strategy (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Dec 3, 2023
1 parent ce5cb17 commit 04cd644
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
51 changes: 51 additions & 0 deletions @models/shannon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useCounterParty, useSeriesMap, useSimplePositionManager } from "@libs";

// Shannon's re-balance strategy
export default () => {
// Define parameters of the agent
const datasource_id = useParamString('DataSource', 'Y');
const product_id = useParamString('Product');
const period = useParamString('Period', 'PT1H');
// Get the product information and price data
const product = useProduct(datasource_id, product_id);
const { close } = useOHLC(datasource_id, product_id, period);
// More parameters
const initial_balance = useParamNumber('Initial Balance', 100_000);
const threshold = useParamNumber('Threshold', 1);
// Get the account information
const accountInfo = useAccountInfo();
// Use a simple position manager
const [actualVolume, setVolume] = useSimplePositionManager(accountInfo.account_id, product_id);
// Re-balance the position
useEffect(() => {
if (close.length < 2) return;
const price = close[close.length - 1];
const totalValue = accountInfo.money.equity + initial_balance;
const totalValueToHold = totalValue * 0.5;
// infer the volume to hold
const valuePerVolume =
price * (product.value_speed ?? 1) * (product.is_underlying_base_currency ? -1 / price : 1);
const expectedVolume = totalValueToHold / valuePerVolume;
// calculate the error rate
const volume_step = product.volume_step ?? 1;
const errorRate = Math.abs((actualVolume - expectedVolume) / volume_step);
if (errorRate > threshold) {
setVolume(roundToStep(expectedVolume, volume_step));
}
}, [close.length]);
// Advanced: Visualize the equity and margin
useSeriesMap(
"Equity",
close,
{ display: "line", chart: "new" },
() => accountInfo.money.equity
);
useSeriesMap(
"Margin",
close,
{ display: "line", chart: "new" },
() => accountInfo.money.used
);
// Advanced: use counter-party to show if we take the opposite position
useCounterParty(accountInfo.account_id);
};
19 changes: 19 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,25 @@ declare const useRecordTable: <T extends Record<string, any>>(
declare const formatTime: (timestamp: number) => string;
/** Generate a UUID (Universal-Unique-ID) */
declare const UUID: () => string;
declare const roundToStep: (
value: number,
step: number,
roundFn?: ((x: number) => number) | undefined
) => number;
declare const getProfit: (
product: IProduct,
openPrice: number,
closePrice: number,
volume: number,
variant: string,
currency: string,
quotes: (product_id: string) =>
| {
ask: number;
bid: number;
}
| undefined
) => number;

// Deployment script context
/**
Expand Down

0 comments on commit 04cd644

Please sign in to comment.