feat: implement strategy-aware analysis intervals and remove manual leverage controls

- Remove manual leverage field from automation v2 page since AI now handles leverage automatically
- Fix scalping strategy analysis intervals from 60 minutes to 2 minutes for proper high-frequency trading
- Implement intelligent interval detection based on selected timeframes:
  * Scalping: 2 minutes (5m, 3m, or multiple short timeframes)
  * Day trading: 5 minutes (1h, 2h timeframes)
  * Swing trading: 15 minutes (4h, daily timeframes)
- Fix Drift SDK API calls: replace getTotalPerpPositionValue() with getFreeCollateral()
- Clean up UI by removing manual controls since AI systems handle optimization
- Fix syntax errors in automation service and balance API
- Ensure proper margin calculations using correct Drift Protocol methods

Tested: Scalping strategy now correctly analyzes every 2 minutes instead of 60 minutes
This commit is contained in:
mindesbunister
2025-07-24 13:25:41 +02:00
parent 29d0516a07
commit 451a8248d8
4 changed files with 62 additions and 31 deletions

View File

@@ -87,16 +87,19 @@ export async function GET() {
unrealizedPnl = 0 // Default to 0 if we can't calculate
}
let freeCollateralFromDrift = 0
try {
// Calculate margin requirement using Drift's method
marginRequirement = await driftClient.getUser().getTotalPerpPositionValue() / 1e6 * 0.1 // 10% margin
// Calculate margin requirement using proper Drift SDK methods
freeCollateralFromDrift = await driftClient.getUser().getFreeCollateral() / 1e6 // Convert to USDC
marginRequirement = Math.max(0, totalCollateral - freeCollateralFromDrift) // Used collateral
} catch (marginError) {
console.warn('⚠️ Could not get margin requirement, calculating manually:', marginError.message)
marginRequirement = 0 // Default to 0 if we can't calculate
}
// Calculate free collateral and other derived values
const freeCollateral = totalCollateral - marginRequirement + unrealizedPnl
// Use Drift's free collateral if available, otherwise calculate manually
const freeCollateral = freeCollateralFromDrift > 0 ? freeCollateralFromDrift : Math.max(0, totalCollateral - marginRequirement + unrealizedPnl)
const accountValue = totalCollateral + unrealizedPnl
const leverage = marginRequirement > 0 ? (marginRequirement / accountValue) : 0
const availableBalance = Math.max(0, freeCollateral)