@megatao/sdk 1.1.0
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/.env.example +37 -0
- package/CHANGELOG.md +19 -0
- package/README.md +199 -0
- package/bin/alf +4 -0
- package/cli/README.md +198 -0
- package/cli/TEST_MANUAL.md +577 -0
- package/cli/commands/account.ts +545 -0
- package/cli/commands/funding.ts +481 -0
- package/cli/commands/liquidation.ts +523 -0
- package/cli/commands/market.ts +590 -0
- package/cli/commands/orders.ts +395 -0
- package/cli/commands/position.ts +1085 -0
- package/cli/commands/shared/positionUtils.ts +239 -0
- package/cli/commands/trading.ts +483 -0
- package/cli/commands/utils.ts +281 -0
- package/cli/commands/vault.ts +522 -0
- package/cli/index.ts +169 -0
- package/cli/interactive.ts +530 -0
- package/cli/utils/client.ts +457 -0
- package/cli/utils/config.ts +226 -0
- package/cli/utils/display.ts +258 -0
- package/cli/utils/index.ts +10 -0
- package/cli/utils/prompts.ts +364 -0
- package/config.example.json +23 -0
- package/dist/AlphaFuturesClient.d.ts +36 -0
- package/dist/AlphaFuturesClient.d.ts.map +1 -0
- package/dist/AlphaFuturesClient.js +116 -0
- package/dist/AlphaFuturesClient.js.map +1 -0
- package/dist/abi/Alpha.json +5987 -0
- package/dist/abi/abis.d.ts +319 -0
- package/dist/abi/abis.d.ts.map +1 -0
- package/dist/abi/abis.js +128 -0
- package/dist/abi/abis.js.map +1 -0
- package/dist/abi/index.d.ts +11 -0
- package/dist/abi/index.d.ts.map +1 -0
- package/dist/abi/index.js +15 -0
- package/dist/abi/index.js.map +1 -0
- package/dist/config/contracts.config.d.ts +70 -0
- package/dist/config/contracts.config.d.ts.map +1 -0
- package/dist/config/contracts.config.js +137 -0
- package/dist/config/contracts.config.js.map +1 -0
- package/dist/config/environments/alpha.config.d.ts +17 -0
- package/dist/config/environments/alpha.config.d.ts.map +1 -0
- package/dist/config/environments/alpha.config.js +140 -0
- package/dist/config/environments/alpha.config.js.map +1 -0
- package/dist/config/environments/beta.config.d.ts +16 -0
- package/dist/config/environments/beta.config.d.ts.map +1 -0
- package/dist/config/environments/beta.config.js +131 -0
- package/dist/config/environments/beta.config.js.map +1 -0
- package/dist/config/environments/dev.config.d.ts +13 -0
- package/dist/config/environments/dev.config.d.ts.map +1 -0
- package/dist/config/environments/dev.config.js +123 -0
- package/dist/config/environments/dev.config.js.map +1 -0
- package/dist/config/environments/index.d.ts +48 -0
- package/dist/config/environments/index.d.ts.map +1 -0
- package/dist/config/environments/index.js +81 -0
- package/dist/config/environments/index.js.map +1 -0
- package/dist/config/environments/localhost.config.d.ts +16 -0
- package/dist/config/environments/localhost.config.d.ts.map +1 -0
- package/dist/config/environments/localhost.config.js +152 -0
- package/dist/config/environments/localhost.config.js.map +1 -0
- package/dist/config/environments/prod.config.d.ts +20 -0
- package/dist/config/environments/prod.config.d.ts.map +1 -0
- package/dist/config/environments/prod.config.js +143 -0
- package/dist/config/environments/prod.config.js.map +1 -0
- package/dist/config/index.d.ts +7 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +41 -0
- package/dist/config/index.js.map +1 -0
- package/dist/constants/assets.d.ts +76 -0
- package/dist/constants/assets.d.ts.map +1 -0
- package/dist/constants/assets.js +277 -0
- package/dist/constants/assets.js.map +1 -0
- package/dist/constants/contracts.d.ts +41 -0
- package/dist/constants/contracts.d.ts.map +1 -0
- package/dist/constants/contracts.js +57 -0
- package/dist/constants/contracts.js.map +1 -0
- package/dist/constants/index.d.ts +36 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +75 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/networks.d.ts +32 -0
- package/dist/constants/networks.d.ts.map +1 -0
- package/dist/constants/networks.js +174 -0
- package/dist/constants/networks.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +21 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/viem/AlphaViem.d.ts +518 -0
- package/dist/contracts/viem/AlphaViem.d.ts.map +1 -0
- package/dist/contracts/viem/AlphaViem.js +1287 -0
- package/dist/contracts/viem/AlphaViem.js.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts +71 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.js +212 -0
- package/dist/contracts/viem/PriceOracleViem.js.map +1 -0
- package/dist/contracts/viem/index.d.ts +9 -0
- package/dist/contracts/viem/index.d.ts.map +1 -0
- package/dist/contracts/viem/index.js +17 -0
- package/dist/contracts/viem/index.js.map +1 -0
- package/dist/errors/index.d.ts +44 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +83 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/types/alpha.d.ts +299 -0
- package/dist/types/alpha.d.ts.map +1 -0
- package/dist/types/alpha.js +6 -0
- package/dist/types/alpha.js.map +1 -0
- package/dist/types/client.d.ts +24 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +13 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/contracts.d.ts +48 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/contracts.js +6 -0
- package/dist/types/contracts.js.map +1 -0
- package/dist/types/funding.d.ts +27 -0
- package/dist/types/funding.d.ts.map +1 -0
- package/dist/types/funding.js +6 -0
- package/dist/types/funding.js.map +1 -0
- package/dist/types/index.d.ts +92 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +47 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/liquidation.d.ts +20 -0
- package/dist/types/liquidation.d.ts.map +1 -0
- package/dist/types/liquidation.js +6 -0
- package/dist/types/liquidation.js.map +1 -0
- package/dist/types/margin.d.ts +29 -0
- package/dist/types/margin.d.ts.map +1 -0
- package/dist/types/margin.js +6 -0
- package/dist/types/margin.js.map +1 -0
- package/dist/types/oracle.d.ts +21 -0
- package/dist/types/oracle.d.ts.map +1 -0
- package/dist/types/oracle.js +6 -0
- package/dist/types/oracle.js.map +1 -0
- package/dist/types/positions.d.ts +43 -0
- package/dist/types/positions.d.ts.map +1 -0
- package/dist/types/positions.js +13 -0
- package/dist/types/positions.js.map +1 -0
- package/dist/utils/calculations.d.ts +84 -0
- package/dist/utils/calculations.d.ts.map +1 -0
- package/dist/utils/calculations.js +155 -0
- package/dist/utils/calculations.js.map +1 -0
- package/dist/utils/errors.d.ts +24 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +129 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/events.d.ts +40 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +73 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/format.d.ts +40 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +86 -0
- package/dist/utils/format.js.map +1 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/network.d.ts +52 -0
- package/dist/utils/network.d.ts.map +1 -0
- package/dist/utils/network.js +192 -0
- package/dist/utils/network.js.map +1 -0
- package/dist/utils/positionCalculations.d.ts +145 -0
- package/dist/utils/positionCalculations.d.ts.map +1 -0
- package/dist/utils/positionCalculations.js +278 -0
- package/dist/utils/positionCalculations.js.map +1 -0
- package/dist/utils/validation.d.ts +28 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +68 -0
- package/dist/utils/validation.js.map +1 -0
- package/docs/README.md +40 -0
- package/docs/api/API.md +831 -0
- package/docs/guides/GETTING_STARTED.md +316 -0
- package/docs/guides/TRADING_GUIDE.md +677 -0
- package/docs/integration/INTEGRATION_GUIDE.md +1679 -0
- package/docs/integration/VIEM_INTEGRATION.md +294 -0
- package/docs/reference/CLI_QUICK_REFERENCE.md +197 -0
- package/docs/reference/TROUBLESHOOTING.md +922 -0
- package/package.json +113 -0
- package/src/AlphaFuturesClient.ts +158 -0
- package/src/abi/.gitkeep +1 -0
- package/src/abi/Alpha.json +5987 -0
- package/src/abi/README.md +99 -0
- package/src/abi/abis.ts +131 -0
- package/src/abi/index.ts +13 -0
- package/src/config/contracts.config.ts +186 -0
- package/src/config/environments/alpha.config.ts +139 -0
- package/src/config/environments/beta.config.ts +130 -0
- package/src/config/environments/dev.config.ts +122 -0
- package/src/config/environments/index.ts +87 -0
- package/src/config/environments/localhost.config.ts +153 -0
- package/src/config/environments/prod.config.ts +142 -0
- package/src/config/index.ts +29 -0
- package/src/constants/assets.ts +299 -0
- package/src/constants/contracts.ts +64 -0
- package/src/constants/index.ts +69 -0
- package/src/constants/networks.ts +182 -0
- package/src/contracts/index.ts +5 -0
- package/src/contracts/viem/AlphaViem.ts +1615 -0
- package/src/contracts/viem/PriceOracleViem.ts +272 -0
- package/src/contracts/viem/index.ts +11 -0
- package/src/errors/index.ts +87 -0
- package/src/index.ts +59 -0
- package/src/types/VIEM_TYPES_README.md +70 -0
- package/src/types/alpha.ts +358 -0
- package/src/types/client.ts +27 -0
- package/src/types/contracts.ts +74 -0
- package/src/types/funding.ts +31 -0
- package/src/types/index.ts +108 -0
- package/src/types/liquidation.ts +23 -0
- package/src/types/margin.ts +34 -0
- package/src/types/oracle.ts +24 -0
- package/src/types/positions.ts +48 -0
- package/src/utils/calculations.ts +175 -0
- package/src/utils/errors.ts +147 -0
- package/src/utils/events.ts +98 -0
- package/src/utils/format.ts +84 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/network.ts +212 -0
- package/src/utils/positionCalculations.ts +317 -0
- package/src/utils/validation.ts +76 -0
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
# Alpha Futures Trading Guide
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
|
|
5
|
+
- [Introduction](#introduction)
|
|
6
|
+
- [Understanding Perpetual Futures](#understanding-perpetual-futures)
|
|
7
|
+
- [Getting Started](#getting-started)
|
|
8
|
+
- [Trading Strategies](#trading-strategies)
|
|
9
|
+
- [Risk Management](#risk-management)
|
|
10
|
+
- [Funding Rates](#funding-rates)
|
|
11
|
+
- [Liquidations](#liquidations)
|
|
12
|
+
- [Advanced Strategies](#advanced-strategies)
|
|
13
|
+
- [Common Pitfalls](#common-pitfalls)
|
|
14
|
+
- [FAQ](#faq)
|
|
15
|
+
|
|
16
|
+
## Introduction
|
|
17
|
+
|
|
18
|
+
Alpha Futures is a decentralized perpetual futures protocol on Bittensor EVM that enables leveraged trading of Alpha tokens using TAO as collateral. This guide will help you understand how to trade effectively on the platform.
|
|
19
|
+
|
|
20
|
+
### Key Features
|
|
21
|
+
|
|
22
|
+
- **Synthetic Exposure**: Trade Alpha token price movements without holding actual tokens
|
|
23
|
+
- **TAO Settlement**: All profits/losses are settled in TAO
|
|
24
|
+
- **Up to 10x Leverage**: Amplify your trading positions
|
|
25
|
+
- **No Expiry**: Perpetual contracts that never expire
|
|
26
|
+
- **Funding Rates**: Mechanism to keep futures prices aligned with spot
|
|
27
|
+
|
|
28
|
+
## Understanding Perpetual Futures
|
|
29
|
+
|
|
30
|
+
### What are Perpetual Futures?
|
|
31
|
+
|
|
32
|
+
Perpetual futures are derivative contracts that:
|
|
33
|
+
- Track the price of an underlying asset (Alpha tokens)
|
|
34
|
+
- Have no expiration date
|
|
35
|
+
- Allow both long and short positions
|
|
36
|
+
- Use funding rates to maintain price parity with spot markets
|
|
37
|
+
|
|
38
|
+
### Key Concepts
|
|
39
|
+
|
|
40
|
+
**Leverage**: Multiply your exposure to price movements
|
|
41
|
+
- 2x leverage = $2,000 position with $1,000 margin
|
|
42
|
+
- 10x leverage = $10,000 position with $1,000 margin
|
|
43
|
+
|
|
44
|
+
**Margin**: Collateral required to open and maintain positions
|
|
45
|
+
- Initial Margin: Required to open a position (33.33% for 3x leverage)
|
|
46
|
+
- Maintenance Margin: Minimum required to keep position open (20%)
|
|
47
|
+
|
|
48
|
+
**Funding Rate**: Periodic payments between longs and shorts
|
|
49
|
+
- Positive rate: Longs pay shorts
|
|
50
|
+
- Negative rate: Shorts pay longs
|
|
51
|
+
|
|
52
|
+
## Getting Started
|
|
53
|
+
|
|
54
|
+
### Step 1: Setup
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { AlphaFuturesClient } from '@alpha-futures/sdk';
|
|
58
|
+
import { ethers } from 'ethers';
|
|
59
|
+
|
|
60
|
+
const client = new AlphaFuturesClient({
|
|
61
|
+
rpcUrl: 'https://your-rpc-url',
|
|
62
|
+
privateKey: 'your-private-key',
|
|
63
|
+
network: 'mainnet'
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Step 2: Deposit Margin
|
|
68
|
+
|
|
69
|
+
Before trading, deposit TAO as margin:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Deposit 1,000 TAO
|
|
73
|
+
const depositAmount = ethers.parseEther('1000');
|
|
74
|
+
const tx = await client.marginAccount.deposit(depositAmount);
|
|
75
|
+
await tx.wait();
|
|
76
|
+
|
|
77
|
+
// Check your account
|
|
78
|
+
const account = await client.marginAccount.getMarginAccount(yourAddress);
|
|
79
|
+
console.log('Available margin:', ethers.formatEther(account.availableMargin));
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Step 3: Check Market Conditions
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// Get current price
|
|
86
|
+
const price = await client.priceOracle.getPrice('ALPHA');
|
|
87
|
+
console.log('ALPHA price:', ethers.formatEther(price));
|
|
88
|
+
|
|
89
|
+
// Check funding rate
|
|
90
|
+
const funding = await client.fundingRate.getCurrentFundingRate('ALPHA');
|
|
91
|
+
console.log('Funding rate:', funding.rate / 100n, '%');
|
|
92
|
+
console.log('Longs pay shorts:', funding.isPositive);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Step 4: Open Your First Position
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Open a 3x leveraged long position
|
|
99
|
+
const position = await client.positionManager.openPosition({
|
|
100
|
+
asset: 'ALPHA',
|
|
101
|
+
size: ethers.parseEther('3000'), // $3,000 position
|
|
102
|
+
leverage: 3,
|
|
103
|
+
isLong: true,
|
|
104
|
+
slippage: 50 // 0.5% max slippage
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
await position.wait();
|
|
108
|
+
console.log('Position opened!');
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Trading Strategies
|
|
112
|
+
|
|
113
|
+
### 1. Trend Following
|
|
114
|
+
|
|
115
|
+
**Strategy**: Trade in the direction of the prevailing trend
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
async function trendFollowingStrategy() {
|
|
119
|
+
// Get price history (example using external data)
|
|
120
|
+
const currentPrice = await client.priceOracle.getPrice('ALPHA');
|
|
121
|
+
|
|
122
|
+
// Simple moving average calculation (pseudo-code)
|
|
123
|
+
const sma20 = calculateSMA(priceHistory, 20);
|
|
124
|
+
const sma50 = calculateSMA(priceHistory, 50);
|
|
125
|
+
|
|
126
|
+
// Bullish signal: Price above both SMAs
|
|
127
|
+
if (currentPrice > sma20 && sma20 > sma50) {
|
|
128
|
+
await client.positionManager.openPosition({
|
|
129
|
+
asset: 'ALPHA',
|
|
130
|
+
size: ethers.parseEther('1000'),
|
|
131
|
+
leverage: 2,
|
|
132
|
+
isLong: true
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Bearish signal: Price below both SMAs
|
|
137
|
+
if (currentPrice < sma20 && sma20 < sma50) {
|
|
138
|
+
await client.positionManager.openPosition({
|
|
139
|
+
asset: 'ALPHA',
|
|
140
|
+
size: ethers.parseEther('1000'),
|
|
141
|
+
leverage: 2,
|
|
142
|
+
isLong: false
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 2. Range Trading
|
|
149
|
+
|
|
150
|
+
**Strategy**: Buy at support, sell at resistance in ranging markets
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
async function rangeTrading(support: bigint, resistance: bigint) {
|
|
154
|
+
const currentPrice = await client.priceOracle.getPrice('ALPHA');
|
|
155
|
+
const positions = await client.positionManager.getAllPositions(yourAddress);
|
|
156
|
+
|
|
157
|
+
// Near support - open long
|
|
158
|
+
if (currentPrice <= support * 102n / 100n) { // Within 2% of support
|
|
159
|
+
if (!positions.some(p => p.isLong)) {
|
|
160
|
+
await client.positionManager.openPosition({
|
|
161
|
+
asset: 'ALPHA',
|
|
162
|
+
size: ethers.parseEther('2000'),
|
|
163
|
+
leverage: 3,
|
|
164
|
+
isLong: true
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Near resistance - open short
|
|
170
|
+
if (currentPrice >= resistance * 98n / 100n) { // Within 2% of resistance
|
|
171
|
+
if (!positions.some(p => !p.isLong)) {
|
|
172
|
+
await client.positionManager.openPosition({
|
|
173
|
+
asset: 'ALPHA',
|
|
174
|
+
size: ethers.parseEther('2000'),
|
|
175
|
+
leverage: 3,
|
|
176
|
+
isLong: false
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 3. Funding Rate Arbitrage
|
|
184
|
+
|
|
185
|
+
**Strategy**: Capture funding payments when rates are extreme
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
async function fundingArbitrage() {
|
|
189
|
+
const funding = await client.fundingRate.getCurrentFundingRate('ALPHA');
|
|
190
|
+
|
|
191
|
+
// High positive funding rate - shorts earn
|
|
192
|
+
if (funding.isPositive && funding.rate > 30n) { // > 0.3%
|
|
193
|
+
await client.positionManager.openPosition({
|
|
194
|
+
asset: 'ALPHA',
|
|
195
|
+
size: ethers.parseEther('5000'),
|
|
196
|
+
leverage: 2,
|
|
197
|
+
isLong: false // Short to earn funding
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// High negative funding rate - longs earn
|
|
202
|
+
if (!funding.isPositive && funding.rate > 30n) { // < -0.3%
|
|
203
|
+
await client.positionManager.openPosition({
|
|
204
|
+
asset: 'ALPHA',
|
|
205
|
+
size: ethers.parseEther('5000'),
|
|
206
|
+
leverage: 2,
|
|
207
|
+
isLong: true // Long to earn funding
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### 4. Scalping
|
|
214
|
+
|
|
215
|
+
**Strategy**: Quick trades on small price movements
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
async function scalpingStrategy() {
|
|
219
|
+
const entryPrice = await client.priceOracle.getPrice('ALPHA');
|
|
220
|
+
|
|
221
|
+
// Open position
|
|
222
|
+
const tx = await client.positionManager.openPosition({
|
|
223
|
+
asset: 'ALPHA',
|
|
224
|
+
size: ethers.parseEther('10000'),
|
|
225
|
+
leverage: 5,
|
|
226
|
+
isLong: true
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const receipt = await tx.wait();
|
|
230
|
+
const positionId = extractPositionId(receipt);
|
|
231
|
+
|
|
232
|
+
// Monitor price
|
|
233
|
+
const checkPrice = setInterval(async () => {
|
|
234
|
+
const currentPrice = await client.priceOracle.getPrice('ALPHA');
|
|
235
|
+
const priceChange = (currentPrice - entryPrice) * 100n / entryPrice;
|
|
236
|
+
|
|
237
|
+
// Take profit at 0.5% gain or stop loss at 0.3% loss
|
|
238
|
+
if (priceChange >= 50n || priceChange <= -30n) {
|
|
239
|
+
await client.positionManager.closePosition(positionId);
|
|
240
|
+
clearInterval(checkPrice);
|
|
241
|
+
}
|
|
242
|
+
}, 5000); // Check every 5 seconds
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Risk Management
|
|
247
|
+
|
|
248
|
+
### Position Sizing
|
|
249
|
+
|
|
250
|
+
Never risk more than you can afford to lose. Use proper position sizing:
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
function calculatePositionSize(
|
|
254
|
+
accountBalance: bigint,
|
|
255
|
+
riskPercentage: number,
|
|
256
|
+
stopLossPercentage: number
|
|
257
|
+
): bigint {
|
|
258
|
+
// Risk 2% of account per trade with 5% stop loss
|
|
259
|
+
const riskAmount = accountBalance * BigInt(riskPercentage) / 100n;
|
|
260
|
+
const positionSize = riskAmount * 100n / BigInt(stopLossPercentage);
|
|
261
|
+
return positionSize;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Example: $10,000 account, 2% risk, 5% stop loss
|
|
265
|
+
const accountBalance = ethers.parseEther('10000');
|
|
266
|
+
const positionSize = calculatePositionSize(accountBalance, 2, 5);
|
|
267
|
+
// Result: $4,000 position size
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Stop Losses
|
|
271
|
+
|
|
272
|
+
Always use stop losses to limit downside:
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
async function monitorStopLoss(
|
|
276
|
+
positionId: bigint,
|
|
277
|
+
stopLossPrice: bigint,
|
|
278
|
+
isLong: boolean
|
|
279
|
+
) {
|
|
280
|
+
const checkPrice = setInterval(async () => {
|
|
281
|
+
const currentPrice = await client.priceOracle.getPrice('ALPHA');
|
|
282
|
+
|
|
283
|
+
const shouldStop = isLong
|
|
284
|
+
? currentPrice <= stopLossPrice
|
|
285
|
+
: currentPrice >= stopLossPrice;
|
|
286
|
+
|
|
287
|
+
if (shouldStop) {
|
|
288
|
+
await client.positionManager.closePosition(positionId);
|
|
289
|
+
clearInterval(checkPrice);
|
|
290
|
+
console.log('Stop loss triggered');
|
|
291
|
+
}
|
|
292
|
+
}, 10000); // Check every 10 seconds
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Leverage Guidelines
|
|
297
|
+
|
|
298
|
+
- **Beginners**: Start with 1-2x leverage
|
|
299
|
+
- **Intermediate**: 2-5x leverage with strict risk management
|
|
300
|
+
- **Advanced**: Up to 10x only for short-term trades with tight stops
|
|
301
|
+
|
|
302
|
+
### Diversification
|
|
303
|
+
|
|
304
|
+
Don't put all margin into one position:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
async function diversifiedPortfolio() {
|
|
308
|
+
const account = await client.marginAccount.getMarginAccount(yourAddress);
|
|
309
|
+
const availableMargin = account.availableMargin;
|
|
310
|
+
|
|
311
|
+
// Allocate margin across multiple positions
|
|
312
|
+
const positions = [
|
|
313
|
+
{ asset: 'ALPHA', allocation: 40, leverage: 2, isLong: true },
|
|
314
|
+
{ asset: 'BETA', allocation: 30, leverage: 3, isLong: false },
|
|
315
|
+
{ asset: 'GAMMA', allocation: 30, leverage: 2, isLong: true }
|
|
316
|
+
];
|
|
317
|
+
|
|
318
|
+
for (const pos of positions) {
|
|
319
|
+
const margin = availableMargin * BigInt(pos.allocation) / 100n;
|
|
320
|
+
const size = margin * BigInt(pos.leverage);
|
|
321
|
+
|
|
322
|
+
await client.positionManager.openPosition({
|
|
323
|
+
asset: pos.asset,
|
|
324
|
+
size,
|
|
325
|
+
leverage: pos.leverage,
|
|
326
|
+
isLong: pos.isLong
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## Funding Rates
|
|
333
|
+
|
|
334
|
+
### Understanding Funding
|
|
335
|
+
|
|
336
|
+
Funding rates balance long and short demand:
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
async function analyzeFunding() {
|
|
340
|
+
const funding = await client.fundingRate.getCurrentFundingRate('ALPHA');
|
|
341
|
+
|
|
342
|
+
console.log('Current funding rate:', funding.rate / 100n, '%');
|
|
343
|
+
console.log('Annualized:', funding.rate * 3n * 365n / 100n, '%'); // 3 times daily
|
|
344
|
+
|
|
345
|
+
// Calculate daily funding cost/income
|
|
346
|
+
const positionSize = ethers.parseEther('10000');
|
|
347
|
+
const dailyFunding = positionSize * funding.rate * 3n / 10000n;
|
|
348
|
+
|
|
349
|
+
if (funding.isPositive) {
|
|
350
|
+
console.log('Longs pay:', ethers.formatEther(dailyFunding), 'TAO daily');
|
|
351
|
+
console.log('Shorts earn:', ethers.formatEther(dailyFunding), 'TAO daily');
|
|
352
|
+
} else {
|
|
353
|
+
console.log('Shorts pay:', ethers.formatEther(dailyFunding), 'TAO daily');
|
|
354
|
+
console.log('Longs earn:', ethers.formatEther(dailyFunding), 'TAO daily');
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### Funding Strategy
|
|
360
|
+
|
|
361
|
+
Use funding rates to enhance returns:
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
async function fundingStrategy() {
|
|
365
|
+
const funding = await client.fundingRate.getCurrentFundingRate('ALPHA');
|
|
366
|
+
const price = await client.priceOracle.getPrice('ALPHA');
|
|
367
|
+
|
|
368
|
+
// Only take positions when funding is favorable
|
|
369
|
+
if (bullishOnAlpha && (!funding.isPositive || funding.rate < 10n)) {
|
|
370
|
+
// Open long when funding is negative or low positive
|
|
371
|
+
await openLongPosition();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (bearishOnAlpha && (funding.isPositive && funding.rate > 20n)) {
|
|
375
|
+
// Open short when funding is high positive
|
|
376
|
+
await openShortPosition();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Liquidations
|
|
382
|
+
|
|
383
|
+
### Avoiding Liquidation
|
|
384
|
+
|
|
385
|
+
Monitor your margin ratio:
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
async function monitorLiquidationRisk() {
|
|
389
|
+
const positions = await client.positionManager.getAllPositions(yourAddress);
|
|
390
|
+
|
|
391
|
+
for (const position of positions) {
|
|
392
|
+
const status = await client.liquidationEngine.checkLiquidationStatus(position.id);
|
|
393
|
+
|
|
394
|
+
console.log(`Position ${position.id}:`);
|
|
395
|
+
console.log(`Margin ratio: ${status.marginRatio / 100n}%`);
|
|
396
|
+
console.log(`Liquidation price: ${ethers.formatEther(status.liquidationPrice)}`);
|
|
397
|
+
|
|
398
|
+
// Warning if close to liquidation
|
|
399
|
+
if (status.marginRatio < 2500n) { // Below 25%
|
|
400
|
+
console.warn('⚠️ Position at risk! Consider adding margin or closing.');
|
|
401
|
+
|
|
402
|
+
// Add margin to avoid liquidation
|
|
403
|
+
if (status.marginRatio < 2200n) { // Below 22%
|
|
404
|
+
const additionalMargin = ethers.parseEther('500');
|
|
405
|
+
await client.positionManager.modifyPosition(
|
|
406
|
+
position.id,
|
|
407
|
+
additionalMargin,
|
|
408
|
+
true // add margin
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Liquidation Hunting
|
|
417
|
+
|
|
418
|
+
Advanced traders can profit from liquidating underwater positions:
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
async function liquidationHunter() {
|
|
422
|
+
// Get at-risk positions
|
|
423
|
+
const atRisk = await client.liquidationEngine.getAtRiskPositions(2100n); // 21% margin
|
|
424
|
+
|
|
425
|
+
for (const position of atRisk) {
|
|
426
|
+
console.log(`Found at-risk position: ${position.positionId}`);
|
|
427
|
+
console.log(`Margin ratio: ${position.marginRatio / 100n}%`);
|
|
428
|
+
|
|
429
|
+
// Check if profitable to liquidate
|
|
430
|
+
const gasEstimate = await client.liquidationEngine.estimateGas
|
|
431
|
+
.liquidatePosition(position.positionId);
|
|
432
|
+
|
|
433
|
+
const gasCost = gasEstimate * (await provider.getFeeData()).gasPrice;
|
|
434
|
+
const expectedBonus = position.remainingMargin * 5n / 100n; // 5% bonus
|
|
435
|
+
|
|
436
|
+
if (expectedBonus > gasCost * 2n) { // 2x gas cost for profit
|
|
437
|
+
const tx = await client.liquidationEngine.liquidatePosition(position.positionId);
|
|
438
|
+
await tx.wait();
|
|
439
|
+
console.log('Position liquidated! Bonus earned:', ethers.formatEther(expectedBonus));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
## Advanced Strategies
|
|
446
|
+
|
|
447
|
+
### Market Making
|
|
448
|
+
|
|
449
|
+
Provide liquidity by maintaining balanced positions:
|
|
450
|
+
|
|
451
|
+
```typescript
|
|
452
|
+
class MarketMaker {
|
|
453
|
+
private client: AlphaFuturesClient;
|
|
454
|
+
private spread: bigint = 50n; // 0.5% spread
|
|
455
|
+
|
|
456
|
+
async maintainQuotes() {
|
|
457
|
+
const midPrice = await this.client.priceOracle.getPrice('ALPHA');
|
|
458
|
+
const positions = await this.client.positionManager.getAllPositions(this.address);
|
|
459
|
+
|
|
460
|
+
// Calculate current exposure
|
|
461
|
+
let netExposure = 0n;
|
|
462
|
+
for (const pos of positions) {
|
|
463
|
+
netExposure += pos.isLong ? pos.size : -pos.size;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// Rebalance if needed
|
|
467
|
+
if (netExposure > ethers.parseEther('1000')) {
|
|
468
|
+
// Too long - open short to balance
|
|
469
|
+
await this.client.positionManager.openPosition({
|
|
470
|
+
asset: 'ALPHA',
|
|
471
|
+
size: netExposure,
|
|
472
|
+
leverage: 2,
|
|
473
|
+
isLong: false
|
|
474
|
+
});
|
|
475
|
+
} else if (netExposure < ethers.parseEther('-1000')) {
|
|
476
|
+
// Too short - open long to balance
|
|
477
|
+
await this.client.positionManager.openPosition({
|
|
478
|
+
asset: 'ALPHA',
|
|
479
|
+
size: -netExposure,
|
|
480
|
+
leverage: 2,
|
|
481
|
+
isLong: true
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
### Delta Neutral Strategies
|
|
489
|
+
|
|
490
|
+
Profit from funding while minimizing directional risk:
|
|
491
|
+
|
|
492
|
+
```typescript
|
|
493
|
+
async function deltaNeutralFunding() {
|
|
494
|
+
const funding = await client.fundingRate.getCurrentFundingRate('ALPHA');
|
|
495
|
+
|
|
496
|
+
// Only enter if funding is significant
|
|
497
|
+
if (funding.rate < 20n) return; // Less than 0.2%
|
|
498
|
+
|
|
499
|
+
const size = ethers.parseEther('10000');
|
|
500
|
+
|
|
501
|
+
if (funding.isPositive) {
|
|
502
|
+
// Open short on futures (earn funding)
|
|
503
|
+
await client.positionManager.openPosition({
|
|
504
|
+
asset: 'ALPHA',
|
|
505
|
+
size,
|
|
506
|
+
leverage: 1,
|
|
507
|
+
isLong: false
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
// Buy spot ALPHA to hedge (example - would need spot exchange integration)
|
|
511
|
+
// await spotExchange.buy('ALPHA', size);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Monitor and close when funding normalizes
|
|
515
|
+
}
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
### Statistical Arbitrage
|
|
519
|
+
|
|
520
|
+
Trade mean reversion between correlated assets:
|
|
521
|
+
|
|
522
|
+
```typescript
|
|
523
|
+
async function statArb(asset1: string, asset2: string) {
|
|
524
|
+
const price1 = await client.priceOracle.getPrice(asset1);
|
|
525
|
+
const price2 = await client.priceOracle.getPrice(asset2);
|
|
526
|
+
|
|
527
|
+
// Calculate price ratio
|
|
528
|
+
const ratio = price1 * PRECISION / price2;
|
|
529
|
+
const historicalMean = calculateHistoricalMean(asset1, asset2);
|
|
530
|
+
const stdDev = calculateStdDev(asset1, asset2);
|
|
531
|
+
|
|
532
|
+
// Trade when ratio deviates significantly
|
|
533
|
+
const zScore = (ratio - historicalMean) / stdDev;
|
|
534
|
+
|
|
535
|
+
if (zScore > 2n) {
|
|
536
|
+
// Ratio too high - short asset1, long asset2
|
|
537
|
+
await client.positionManager.openPosition({
|
|
538
|
+
asset: asset1,
|
|
539
|
+
size: ethers.parseEther('5000'),
|
|
540
|
+
leverage: 3,
|
|
541
|
+
isLong: false
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
await client.positionManager.openPosition({
|
|
545
|
+
asset: asset2,
|
|
546
|
+
size: ethers.parseEther('5000'),
|
|
547
|
+
leverage: 3,
|
|
548
|
+
isLong: true
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
## Common Pitfalls
|
|
555
|
+
|
|
556
|
+
### 1. Over-leveraging
|
|
557
|
+
|
|
558
|
+
**Problem**: Using maximum leverage on every trade
|
|
559
|
+
**Solution**: Start small and increase leverage gradually
|
|
560
|
+
|
|
561
|
+
### 2. Ignoring Funding Rates
|
|
562
|
+
|
|
563
|
+
**Problem**: Not accounting for funding costs
|
|
564
|
+
**Solution**: Factor funding into your P&L calculations
|
|
565
|
+
|
|
566
|
+
### 3. No Stop Losses
|
|
567
|
+
|
|
568
|
+
**Problem**: Letting losses run hoping for reversal
|
|
569
|
+
**Solution**: Always set stop losses before entering
|
|
570
|
+
|
|
571
|
+
### 4. Revenge Trading
|
|
572
|
+
|
|
573
|
+
**Problem**: Increasing position size after losses
|
|
574
|
+
**Solution**: Stick to your position sizing rules
|
|
575
|
+
|
|
576
|
+
### 5. FOMO Trading
|
|
577
|
+
|
|
578
|
+
**Problem**: Entering positions due to fear of missing out
|
|
579
|
+
**Solution**: Wait for your setup and trade your plan
|
|
580
|
+
|
|
581
|
+
## FAQ
|
|
582
|
+
|
|
583
|
+
### Q: What happens if I get liquidated?
|
|
584
|
+
|
|
585
|
+
When liquidated:
|
|
586
|
+
- Your position is closed at market price
|
|
587
|
+
- Remaining margin (if any) is returned minus liquidation penalty
|
|
588
|
+
- A 5% liquidation bonus goes to the liquidator
|
|
589
|
+
- The rest goes to the insurance fund
|
|
590
|
+
|
|
591
|
+
### Q: How often are funding rates charged?
|
|
592
|
+
|
|
593
|
+
Funding rates are typically charged every 8 hours at:
|
|
594
|
+
- 00:00 UTC
|
|
595
|
+
- 08:00 UTC
|
|
596
|
+
- 16:00 UTC
|
|
597
|
+
|
|
598
|
+
### Q: Can I trade with less than the minimum position size?
|
|
599
|
+
|
|
600
|
+
No, positions must meet the minimum size requirement (usually 100 TAO worth).
|
|
601
|
+
|
|
602
|
+
### Q: What's the maximum position size?
|
|
603
|
+
|
|
604
|
+
Position size is limited by:
|
|
605
|
+
- Your available margin
|
|
606
|
+
- Protocol's risk limits
|
|
607
|
+
- Available liquidity in the vault
|
|
608
|
+
|
|
609
|
+
### Q: How do I calculate my break-even price?
|
|
610
|
+
|
|
611
|
+
```typescript
|
|
612
|
+
function calculateBreakeven(
|
|
613
|
+
entryPrice: bigint,
|
|
614
|
+
isLong: boolean,
|
|
615
|
+
tradingFee: bigint,
|
|
616
|
+
expectedFunding: bigint,
|
|
617
|
+
positionSize: bigint
|
|
618
|
+
): bigint {
|
|
619
|
+
const totalCosts = tradingFee + expectedFunding;
|
|
620
|
+
const costPercentage = totalCosts * 10000n / positionSize;
|
|
621
|
+
|
|
622
|
+
if (isLong) {
|
|
623
|
+
return entryPrice * (10000n + costPercentage) / 10000n;
|
|
624
|
+
} else {
|
|
625
|
+
return entryPrice * (10000n - costPercentage) / 10000n;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
### Q: What's the best time to trade?
|
|
631
|
+
|
|
632
|
+
- **High volatility**: More opportunities but higher risk
|
|
633
|
+
- **Low funding rates**: Reduced carrying costs
|
|
634
|
+
- **Good liquidity**: Tighter spreads and less slippage
|
|
635
|
+
|
|
636
|
+
### Q: How do I track my performance?
|
|
637
|
+
|
|
638
|
+
```typescript
|
|
639
|
+
async function trackPerformance(startDate: number, endDate: number) {
|
|
640
|
+
const account = await client.marginAccount.getMarginAccount(yourAddress);
|
|
641
|
+
|
|
642
|
+
// Get all closed positions in date range
|
|
643
|
+
const events = await client.positionManager.queryFilter(
|
|
644
|
+
client.positionManager.filters.PositionClosed(null, yourAddress),
|
|
645
|
+
startDate,
|
|
646
|
+
endDate
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
let totalPnL = 0n;
|
|
650
|
+
let winCount = 0;
|
|
651
|
+
let lossCount = 0;
|
|
652
|
+
|
|
653
|
+
for (const event of events) {
|
|
654
|
+
const pnl = event.args.realizedPnL;
|
|
655
|
+
totalPnL += pnl;
|
|
656
|
+
|
|
657
|
+
if (pnl > 0n) winCount++;
|
|
658
|
+
else lossCount++;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
console.log('Total P&L:', ethers.formatEther(totalPnL));
|
|
662
|
+
console.log('Win rate:', (winCount / (winCount + lossCount) * 100).toFixed(2), '%');
|
|
663
|
+
console.log('Total trades:', winCount + lossCount);
|
|
664
|
+
}
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
## Conclusion
|
|
668
|
+
|
|
669
|
+
Successful trading on Alpha Futures requires:
|
|
670
|
+
- Understanding of perpetual futures mechanics
|
|
671
|
+
- Solid risk management
|
|
672
|
+
- Disciplined execution
|
|
673
|
+
- Continuous learning and adaptation
|
|
674
|
+
|
|
675
|
+
Start small, manage risk carefully, and always trade within your means. The protocol provides powerful tools for both speculation and hedging - use them wisely.
|
|
676
|
+
|
|
677
|
+
Remember: Past performance doesn't guarantee future results. Always do your own research and never trade with funds you cannot afford to lose.
|