Skip to main content
Glama
strategy.ts6.03 kB
import { AuraAdapter } from '../core/aura-adapter.js' import { GuardEngine } from '../core/guard-engine.js' import { BacktestRequest, BacktestResponse, StrategyProposeRequest, StrategyProposeResponse, McpResponse } from '../types/index.js' export class StrategyTools { constructor( private auraAdapter: AuraAdapter, private guardEngine: GuardEngine ) {} async handleTool(name: string, args: any): Promise<McpResponse> { switch (name) { case 'strategy.backtest': return await this.backtest(args as BacktestRequest) case 'strategy.propose': return await this.propose(args as StrategyProposeRequest) default: throw new Error(`Unknown strategy tool: ${name}`) } } private async backtest(request: BacktestRequest): Promise<McpResponse<BacktestResponse>> { try { const result = await this.performBacktest(request.name, request.params, request.lookbackDays) return { success: true, data: result, metadata: { timestamp: new Date().toISOString(), requestId: `backtest_${request.name}_${Date.now()}` } } } catch (error) { return { success: false, error: { code: 'BACKTEST_ERROR', message: error instanceof Error ? error.message : String(error) }, metadata: { timestamp: new Date().toISOString() } } } } private async propose(request: StrategyProposeRequest): Promise<McpResponse<StrategyProposeResponse>> { try { // For propose, we need an address - this should come from the context // For now, we'll use a placeholder const address = '0x69bfD720Dd188B8BB04C4b4D24442D3c15576D10' // Default from AURA const result = await this.auraAdapter.proposeStrategy(request.intent, request.params, address) return { success: true, data: result, metadata: { timestamp: new Date().toISOString(), requestId: `propose_${request.intent}_${Date.now()}` } } } catch (error) { return { success: false, error: { code: 'PROPOSE_ERROR', message: error instanceof Error ? error.message : String(error) }, metadata: { timestamp: new Date().toISOString() } } } } private async performBacktest( strategyName: string, params: any, lookbackDays: number ): Promise<BacktestResponse> { // This would perform actual backtesting with historical data // For now, return mock data based on strategy type switch (strategyName) { case 'dca_event_aware': return this.backtestDCAEventAware(params, lookbackDays) case 'basket_rotation': return this.backtestBasketRotation(params, lookbackDays) case 'hedge_guard': return this.backtestHedgeGuard(params, lookbackDays) default: throw new Error(`Unknown strategy for backtest: ${strategyName}`) } } private backtestDCAEventAware(params: any, lookbackDays: number): BacktestResponse { // Mock backtest data for DCA Event-Aware strategy const equityCurve = [] const startValue = 10000 let currentValue = startValue // Generate mock equity curve for (let i = 0; i < lookbackDays; i++) { // Add some volatility const dailyReturn = (Math.random() - 0.5) * 0.02 // ±1% daily volatility currentValue *= (1 + dailyReturn) equityCurve.push({ t: Date.now() - (lookbackDays - i) * 24 * 60 * 60 * 1000, v: currentValue }) } const totalReturn = (currentValue - startValue) / startValue const cagr = Math.pow(1 + totalReturn, 365 / lookbackDays) - 1 return { metrics: { cagr: cagr, maxDD: 0.15, // 15% max drawdown sharpe: 1.2, winrate: 0.65 // 65% win rate }, equityCurve, notes: 'DCA Event-Aware strategy shows good risk-adjusted returns with controlled drawdowns' } } private backtestBasketRotation(params: any, lookbackDays: number): BacktestResponse { // Mock backtest data for Basket Rotation strategy const equityCurve = [] const startValue = 10000 let currentValue = startValue for (let i = 0; i < lookbackDays; i++) { const dailyReturn = (Math.random() - 0.4) * 0.015 // Slightly negative bias currentValue *= (1 + dailyReturn) equityCurve.push({ t: Date.now() - (lookbackDays - i) * 24 * 60 * 60 * 1000, v: currentValue }) } const totalReturn = (currentValue - startValue) / startValue const cagr = Math.pow(1 + totalReturn, 365 / lookbackDays) - 1 return { metrics: { cagr: cagr, maxDD: 0.25, // 25% max drawdown sharpe: 0.8, winrate: 0.55 // 55% win rate }, equityCurve, notes: 'Basket Rotation strategy shows moderate performance with higher volatility' } } private backtestHedgeGuard(params: any, lookbackDays: number): BacktestResponse { // Mock backtest data for Hedge Guard strategy const equityCurve = [] const startValue = 10000 let currentValue = startValue for (let i = 0; i < lookbackDays; i++) { const dailyReturn = (Math.random() - 0.5) * 0.01 // Lower volatility due to hedging currentValue *= (1 + dailyReturn) equityCurve.push({ t: Date.now() - (lookbackDays - i) * 24 * 60 * 60 * 1000, v: currentValue }) } const totalReturn = (currentValue - startValue) / startValue const cagr = Math.pow(1 + totalReturn, 365 / lookbackDays) - 1 return { metrics: { cagr: cagr, maxDD: 0.08, // 8% max drawdown (hedged) sharpe: 1.5, winrate: 0.72 // 72% win rate }, equityCurve, notes: 'Hedge Guard strategy shows excellent risk-adjusted returns with low drawdowns' } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/antidump/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server