Skip to content

Commit 66be2c3

Browse files
authored
Merge pull request #5 from copyleftdev/feature/algo-trading-strategies
feat: add example trading strategies for momentum, pairs, arbitrage, …
2 parents 5fa2693 + bc45d93 commit 66be2c3

File tree

6 files changed

+627
-0
lines changed

6 files changed

+627
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
spec "ArbitrageStrategy" v1.0 {
2+
description: "Advanced multi-exchange arbitrage strategy with AI-powered opportunity detection and latency optimization for systematic price differential capture."
3+
4+
inputs:
5+
strategy_id: string
6+
trading_pair: string { default: "BTC/USD" }
7+
exchange_1: string { default: "binance" }
8+
exchange_2: string { default: "coinbase" }
9+
min_profit_threshold: float { default: 0.005 }
10+
11+
account_balance: float { default: 100000.0 }
12+
max_position_size: float { default: 0.1 }
13+
execution_speed_priority: bool { default: true }
14+
slippage_tolerance: float { default: 0.001 }
15+
16+
triangular_arbitrage: bool { default: false }
17+
cross_exchange_arbitrage: bool { default: true }
18+
statistical_arbitrage: bool { default: false }
19+
funding_rate_arbitrage: bool { default: false }
20+
21+
ai_models: string
22+
market_data_feeds: string
23+
execution_venues: string
24+
25+
latency_monitoring: bool { default: true }
26+
price_feed_redundancy: bool { default: true }
27+
execution_timeout: int { default: 10 }
28+
29+
risk_management_enabled: bool { default: true }
30+
max_exposure_per_exchange: float { default: 0.5 }
31+
balance_monitoring: bool { default: true }
32+
profit_tracking: bool { default: true }
33+
34+
computed:
35+
price_differential: -> calculate_price_diff()
36+
profit_opportunity: -> calculate_profit()
37+
execution_latency: -> measure_latency()
38+
39+
events:
40+
on_create(strategy): initialize_arbitrage
41+
on_opportunity(price_differential): execute_arbitrage
42+
on_fill(leg_1): execute_arbitrage_leg_2
43+
on_complete(arbitrage): record_profit
44+
on_error(execution_failure): handle_failed_arbitrage
45+
46+
actions:
47+
initialize_arbitrage {
48+
description: "Initialize arbitrage trading strategy"
49+
mcp_call: "trading/arbitrage/initialize"
50+
parameters: {
51+
strategy_id: strategy_id,
52+
trading_pair: trading_pair,
53+
exchange_1: exchange_1,
54+
exchange_2: exchange_2,
55+
account_balance: account_balance
56+
}
57+
}
58+
59+
execute_arbitrage {
60+
description: "Execute arbitrage opportunity"
61+
mcp_call: "trading/arbitrage/execute"
62+
parameters: {
63+
trading_pair: trading_pair,
64+
price_differential: price_differential,
65+
profit_opportunity: profit_opportunity,
66+
max_position_size: max_position_size,
67+
execution_speed_priority: execution_speed_priority
68+
}
69+
}
70+
71+
execute_arbitrage_leg_2 {
72+
description: "Execute second leg of arbitrage trade"
73+
mcp_call: "trading/arbitrage/execute_leg_2"
74+
parameters: {
75+
strategy_id: strategy_id,
76+
execution_timeout: execution_timeout,
77+
slippage_tolerance: slippage_tolerance
78+
}
79+
}
80+
81+
record_profit {
82+
description: "Record arbitrage profit and update metrics"
83+
mcp_call: "trading/arbitrage/record_profit"
84+
parameters: {
85+
strategy_id: strategy_id,
86+
profit_opportunity: profit_opportunity,
87+
execution_latency: execution_latency
88+
}
89+
}
90+
91+
handle_failed_arbitrage {
92+
description: "Handle failed arbitrage execution"
93+
mcp_call: "trading/arbitrage/handle_failure"
94+
parameters: {
95+
strategy_id: strategy_id,
96+
failure_reason: "execution_timeout",
97+
risk_management_enabled: risk_management_enabled
98+
}
99+
}
100+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
spec "GridTradingStrategy" v1.0 {
2+
description: "Advanced grid trading strategy with AI-powered volatility analysis and dynamic grid adjustment for systematic profit from market oscillations."
3+
4+
inputs:
5+
strategy_id: string
6+
trading_pair: string { default: "BTC/USD" }
7+
grid_levels: int { default: 10 }
8+
grid_spacing_pct: float { default: 0.02 }
9+
base_order_size: float { default: 1000.0 }
10+
11+
account_balance: float { default: 100000.0 }
12+
total_grid_allocation: float { default: 0.5 }
13+
upper_price_limit: float { default: 0.0 }
14+
lower_price_limit: float { default: 0.0 }
15+
16+
dynamic_grid_enabled: bool { default: true }
17+
volatility_adjustment: bool { default: true }
18+
trend_filter_enabled: bool { default: true }
19+
volume_weighted_orders: bool { default: true }
20+
21+
ai_models: string
22+
market_data_feed: string
23+
execution_venue: string
24+
25+
stop_loss_enabled: bool { default: false }
26+
take_profit_enabled: bool { default: false }
27+
grid_rebalance_frequency: int { default: 24 }
28+
29+
risk_management_enabled: bool { default: true }
30+
max_drawdown_pct: float { default: 0.15 }
31+
position_monitoring: bool { default: true }
32+
profit_taking_enabled: bool { default: true }
33+
34+
computed:
35+
current_price: -> get_current_price()
36+
grid_spacing: -> calculate_grid_spacing()
37+
volatility_factor: -> measure_volatility()
38+
39+
events:
40+
on_create(strategy): initialize_grid_trading
41+
on_price_hit(grid_level): execute_grid_order
42+
on_timer(grid_rebalance_frequency): rebalance_grid
43+
on_change(volatility_factor): adjust_grid_spacing
44+
on_error(max_drawdown): pause_grid_trading
45+
46+
actions:
47+
initialize_grid_trading {
48+
description: "Initialize grid trading strategy"
49+
mcp_call: "trading/grid/initialize"
50+
parameters: {
51+
strategy_id: strategy_id,
52+
trading_pair: trading_pair,
53+
grid_levels: grid_levels,
54+
grid_spacing_pct: grid_spacing_pct,
55+
base_order_size: base_order_size,
56+
account_balance: account_balance
57+
}
58+
}
59+
60+
execute_grid_order {
61+
description: "Execute buy or sell order when price hits grid level"
62+
mcp_call: "trading/grid/execute_order"
63+
parameters: {
64+
trading_pair: trading_pair,
65+
current_price: current_price,
66+
base_order_size: base_order_size,
67+
volume_weighted_orders: volume_weighted_orders
68+
}
69+
}
70+
71+
rebalance_grid {
72+
description: "Rebalance grid levels based on market conditions"
73+
mcp_call: "trading/grid/rebalance"
74+
parameters: {
75+
strategy_id: strategy_id,
76+
current_price: current_price,
77+
grid_spacing: grid_spacing,
78+
dynamic_grid_enabled: dynamic_grid_enabled
79+
}
80+
}
81+
82+
adjust_grid_spacing {
83+
description: "Dynamically adjust grid spacing based on volatility"
84+
mcp_call: "trading/grid/adjust_spacing"
85+
parameters: {
86+
volatility_factor: volatility_factor,
87+
volatility_adjustment: volatility_adjustment,
88+
grid_spacing_pct: grid_spacing_pct
89+
}
90+
}
91+
92+
pause_grid_trading {
93+
description: "Pause grid trading due to excessive drawdown"
94+
mcp_call: "trading/grid/pause"
95+
parameters: {
96+
strategy_id: strategy_id,
97+
max_drawdown_pct: max_drawdown_pct,
98+
reason: "drawdown_limit"
99+
}
100+
}
101+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
spec "MarketMakingStrategy" v1.0 {
2+
description: "Advanced market making strategy with AI-powered spread optimization and inventory management for systematic liquidity provision and bid-ask capture."
3+
4+
inputs:
5+
strategy_id: string
6+
trading_pair: string { default: "BTC/USD" }
7+
base_spread_bps: int { default: 10 }
8+
order_size: float { default: 1000.0 }
9+
max_inventory: float { default: 10.0 }
10+
11+
account_balance: float { default: 100000.0 }
12+
inventory_target: float { default: 0.0 }
13+
spread_multiplier: float { default: 1.0 }
14+
min_spread_bps: int { default: 5 }
15+
max_spread_bps: int { default: 50 }
16+
17+
dynamic_spread_enabled: bool { default: true }
18+
inventory_skew_enabled: bool { default: true }
19+
volatility_adjustment: bool { default: true }
20+
order_refresh_enabled: bool { default: true }
21+
22+
ai_models: string
23+
market_data_feed: string
24+
execution_venue: string
25+
26+
order_refresh_interval: int { default: 5 }
27+
inventory_rebalance_threshold: float { default: 0.8 }
28+
adverse_selection_protection: bool { default: true }
29+
30+
risk_management_enabled: bool { default: true }
31+
max_position_size: float { default: 0.1 }
32+
stop_loss_enabled: bool { default: true }
33+
profit_target_enabled: bool { default: false }
34+
35+
computed:
36+
current_inventory: -> calculate_inventory()
37+
optimal_spread: -> calculate_optimal_spread()
38+
market_volatility: -> measure_volatility()
39+
40+
events:
41+
on_create(strategy): initialize_market_making
42+
on_fill(order): manage_inventory
43+
on_timer(order_refresh_interval): refresh_quotes
44+
on_change(market_volatility): adjust_spread
45+
on_error(inventory_limit): rebalance_inventory
46+
47+
actions:
48+
initialize_market_making {
49+
description: "Initialize market making strategy"
50+
mcp_call: "trading/market_making/initialize"
51+
parameters: {
52+
strategy_id: strategy_id,
53+
trading_pair: trading_pair,
54+
base_spread_bps: base_spread_bps,
55+
order_size: order_size,
56+
account_balance: account_balance
57+
}
58+
}
59+
60+
manage_inventory {
61+
description: "Manage inventory after order fills"
62+
mcp_call: "trading/market_making/manage_inventory"
63+
parameters: {
64+
current_inventory: current_inventory,
65+
inventory_target: inventory_target,
66+
max_inventory: max_inventory,
67+
inventory_skew_enabled: inventory_skew_enabled
68+
}
69+
}
70+
71+
refresh_quotes {
72+
description: "Refresh bid and ask quotes"
73+
mcp_call: "trading/market_making/refresh_quotes"
74+
parameters: {
75+
trading_pair: trading_pair,
76+
optimal_spread: optimal_spread,
77+
order_size: order_size,
78+
current_inventory: current_inventory
79+
}
80+
}
81+
82+
adjust_spread {
83+
description: "Dynamically adjust spread based on market conditions"
84+
mcp_call: "trading/market_making/adjust_spread"
85+
parameters: {
86+
market_volatility: market_volatility,
87+
volatility_adjustment: volatility_adjustment,
88+
min_spread_bps: min_spread_bps,
89+
max_spread_bps: max_spread_bps
90+
}
91+
}
92+
93+
rebalance_inventory {
94+
description: "Rebalance inventory when limits are breached"
95+
mcp_call: "trading/market_making/rebalance"
96+
parameters: {
97+
strategy_id: strategy_id,
98+
current_inventory: current_inventory,
99+
inventory_rebalance_threshold: inventory_rebalance_threshold
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)