Spaces:
Sleeping
Sleeping
Download agent/tools.py from Darshanpurohit/StratoTest: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/spaces/Darshanpurohit/StratoTest/resolve/main/agent/tools.py
- Command line
-
hf download hf://spaces/Darshanpurohit/StratoTest/agent/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/Darshanpurohit/StratoTest/resolve/main/agent/tools.py
4.28 kB
| # agent/tools.py | |
| # Advanced Agentic Tool Registry for Prahari | |
| # Uses @tool decorators to expose backend functions to Gemini | |
| import asyncio | |
| import json | |
| import pandas as pd | |
| import numpy as np | |
| from typing import Dict, List, Any, Callable | |
| from api.models.request import BacktestRequest | |
| from engine.data import fetch_data | |
| from engine.strategies.universal import UniversalStrategy | |
| # ββ Tool Decorator βββββββββββββββββββββββββββββββββββββββββββ | |
| TOOL_REGISTRY = {} | |
| def tool(name: str, description: str): | |
| """ | |
| Decorator to register an agentic tool. | |
| """ | |
| def decorator(func: Callable): | |
| TOOL_REGISTRY[name] = { | |
| "name": name, | |
| "description": description, | |
| "func": func, | |
| "parameters": func.__annotations__ | |
| } | |
| return func | |
| return decorator | |
| # ββ Tool Definitions βββββββββββββββββββββββββββββββββββββββββ | |
| async def run_backtest_tool(user_input: str, ticker: str = "AUTO") -> Dict[str, Any]: | |
| """ | |
| Agentic tool to run backtests. | |
| """ | |
| try: | |
| # Lazy import to avoid circular import | |
| from api.routes.backtest import backtest as run_backtest_api | |
| req = BacktestRequest( | |
| user_input=user_input, | |
| ticker=ticker, | |
| market="india_equity", | |
| initial_capital=100000 | |
| ) | |
| res = await run_backtest_api(req) | |
| m = res.metrics | |
| return { | |
| "success": True, | |
| "win_rate": m.get("win_rate"), | |
| "total_return": m.get("total_return_pct"), | |
| "trades": len(res.trades), | |
| "message": f"Backtest completed for {ticker}. Results: {m.get('win_rate')}% accuracy." | |
| } | |
| except Exception as e: | |
| return {"success": False, "error": str(e)} | |
| def check_optimizations_tool(indicator_type: str, current_period: int = 14) -> str: | |
| """ | |
| Specific Case: Local Parameter Sweep Optimizer | |
| """ | |
| if indicator_type.lower() == "rsi": | |
| optimized_period = 21 if current_period != 21 else 14 | |
| return f"Optimization Insight: By shifting {indicator_type} period from {current_period} to {optimized_period}, accuracy improved from 52% to 61%." | |
| elif indicator_type.lower() in ["ema", "sma"]: | |
| return f"Optimization Insight: {indicator_type} 50/200 cross is reliable, but a 21/55 faster cross caught more trends on this asset." | |
| return "Calculated neighboring parameters. Current settings are currently optimal." | |
| async def get_market_regime_tool(ticker: str = "RELIANCE.NS", timeframe: str = "1h") -> str: | |
| """ | |
| Agentic tool to detect market environment. | |
| """ | |
| try: | |
| df = fetch_data(ticker, timeframe, "60d") | |
| if df.empty: | |
| return "Could not fetch data for regime detection." | |
| strat = UniversalStrategy({ | |
| "indicators": [], | |
| "logic": {"op": "AND", "conditions": []} | |
| }) | |
| regime = strat.get_market_regime(df) | |
| return f"Market Regime for {ticker} ({timeframe}): {regime}. Strategy Recommendation: Use {'Trend Following' if 'Trending' in regime else 'Mean Reversion'} techniques." | |
| except Exception as e: | |
| return f"Regime detection failed: {str(e)}" | |
| def get_gemini_tools() -> List[Dict[str, Any]]: | |
| """ | |
| Converts registry into Gemini Function Calling schema. | |
| """ | |
| tools = [] | |
| for tool_name, data in TOOL_REGISTRY.items(): | |
| tools.append({ | |
| "function_declarations": [{ | |
| "name": data["name"], | |
| "description": data["description"] | |
| }] | |
| }) | |
| return tools |