Spaces:
Runtime error
Runtime error
import ollama | |
import numpy as np | |
import pandas as pd | |
from lightweight_charts import Chart | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
class IndicatorAnalyzer: | |
def __init__(self): | |
self.model = AutoModelForCausalLM.from_pretrained("tmm-dev/codellama-pattern-analysis") | |
self.tokenizer = AutoTokenizer.from_pretrained("tmm-dev/codellama-pattern-analysis") | |
def analyze_indicators(self, ohlcv_data): | |
indicator_prompt = f""" | |
Analyze this OHLCV data and calculate optimal indicators: | |
{ohlcv_data.to_json(orient='records')} | |
Calculate and return: | |
- Moving Averages (EMA, SMA with optimal periods) | |
- Oscillators (RSI, Stochastic, MACD) | |
- Volatility (Bollinger Bands, ATR) | |
- Volume indicators | |
- Custom combinations of indicators | |
Return the analysis in JSON format with exact values and coordinates. | |
""" | |
response = self.client.chat( | |
model='codellama:latest', | |
messages=[ | |
{ | |
'role': 'system', | |
'content': 'You are a technical analysis indicator calculation model.' | |
}, | |
{ | |
'role': 'user', | |
'content': indicator_prompt | |
} | |
] | |
) | |
return self.parse_indicator_analysis(response['message']['content']) | |
def parse_indicator_analysis(self, analysis): | |
try: | |
# Convert string response to structured data | |
if isinstance(analysis, str): | |
# Extract JSON if embedded in text | |
json_start = analysis.find('{') | |
json_end = analysis.rfind('}') + 1 | |
if json_start >= 0 and json_end > 0: | |
analysis = analysis[json_start:json_end] | |
indicators = { | |
'moving_averages': {}, | |
'oscillators': {}, | |
'volatility': {}, | |
'volume': {}, | |
'custom': {} | |
} | |
# Add any custom parsing logic here | |
return indicators | |
except Exception as e: | |
print(f"Error parsing indicator analysis: {str(e)}") | |
return {} | |