Spaces:
Runtime error
Runtime error
Upload 11 files
Browse files- analysis/__init__.py +0 -0
- analysis/chart_analyzer.py +29 -0
- analysis/chart_processor.py +29 -0
- analysis/indicator_generator.py +36 -0
- analysis/pattern_generator.py +32 -0
- app.py +59 -0
- data/__init__.py +0 -0
- data/market_data.py +30 -0
- model/__init__.py +0 -0
- model/vision_model.py +16 -0
- requirements.txt +23 -0
analysis/__init__.py
ADDED
File without changes
|
analysis/chart_analyzer.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
from src.model.vision_model import ChartVisionModel
|
3 |
+
|
4 |
+
class ChartAnalyzer:
|
5 |
+
def __init__(self):
|
6 |
+
self.vision_model = ChartVisionModel()
|
7 |
+
self.client = ollama.Client()
|
8 |
+
|
9 |
+
def analyze_full(self, chart_image, pattern_images, indicator_charts, historical_data):
|
10 |
+
vision_analysis = self.vision_model.analyze_chart(chart_image)
|
11 |
+
|
12 |
+
system_prompt = """Analyze this technical chart with all patterns and indicators:
|
13 |
+
1. Pattern Analysis (300+ patterns)
|
14 |
+
2. Indicator Analysis
|
15 |
+
3. Current Chart Analysis
|
16 |
+
4. Movement Predictions
|
17 |
+
Provide detailed analysis for each component."""
|
18 |
+
|
19 |
+
analysis = self.client.generate(
|
20 |
+
model="llama3.2-vision:latest",
|
21 |
+
prompt=system_prompt,
|
22 |
+
images=[chart_image] + pattern_images
|
23 |
+
)
|
24 |
+
|
25 |
+
return {
|
26 |
+
'patterns': self._analyze_patterns(pattern_images),
|
27 |
+
'indicators': self._analyze_indicators(indicator_charts),
|
28 |
+
'predictions': self._generate_predictions(analysis)
|
29 |
+
}
|
analysis/chart_processor.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.model.vision_model import ChartVisionModel
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
class ChartProcessor:
|
6 |
+
def __init__(self):
|
7 |
+
self.vision_model = ChartVisionModel()
|
8 |
+
|
9 |
+
def preprocess_image(self, image):
|
10 |
+
# Image preprocessing for analysis
|
11 |
+
return image
|
12 |
+
|
13 |
+
def detect_patterns(self, image):
|
14 |
+
# Pattern detection logic
|
15 |
+
return []
|
16 |
+
|
17 |
+
def analyze_indicators(self, image):
|
18 |
+
# Technical indicator analysis
|
19 |
+
return {}
|
20 |
+
|
21 |
+
def process_chart(self, image):
|
22 |
+
preprocessed = self.preprocess_image(image)
|
23 |
+
patterns = self.detect_patterns(preprocessed)
|
24 |
+
indicators = self.analyze_indicators(preprocessed)
|
25 |
+
|
26 |
+
return {
|
27 |
+
'patterns': patterns,
|
28 |
+
'indicators': indicators
|
29 |
+
}
|
analysis/indicator_generator.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
from lightweight_charts import Chart
|
3 |
+
|
4 |
+
class IndicatorGenerator:
|
5 |
+
def __init__(self):
|
6 |
+
self.client = ollama.Client()
|
7 |
+
self.code_model = "codellama:latest"
|
8 |
+
self.indicators = [
|
9 |
+
'rsi', 'macd', 'bollinger_bands', 'moving_averages',
|
10 |
+
'stochastic', 'atr', 'volume_profile'
|
11 |
+
]
|
12 |
+
|
13 |
+
def generate_all_indicators(self, historical_data):
|
14 |
+
indicator_charts = []
|
15 |
+
for indicator in self.indicators:
|
16 |
+
# Generate code for indicator using CodeLlama
|
17 |
+
code_prompt = f"Generate Lightweight Charts code for {indicator} indicator with proper styling and configuration"
|
18 |
+
code_response = self.client.generate(
|
19 |
+
model=self.code_model,
|
20 |
+
prompt=code_prompt
|
21 |
+
)
|
22 |
+
|
23 |
+
# Create chart using generated code
|
24 |
+
chart = Chart()
|
25 |
+
chart.set(code_response) # Applies the generated configuration
|
26 |
+
chart.add_data(historical_data)
|
27 |
+
|
28 |
+
indicator_charts.append(chart)
|
29 |
+
|
30 |
+
return indicator_charts
|
31 |
+
|
32 |
+
def _create_indicator_chart(self, data, indicator_type):
|
33 |
+
chart = Chart()
|
34 |
+
# Specific indicator configurations using Lightweight Charts
|
35 |
+
chart.add_indicator(indicator_type, data)
|
36 |
+
return chart
|
analysis/pattern_generator.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
from lightweight_charts import Chart
|
3 |
+
|
4 |
+
class PatternGenerator:
|
5 |
+
def __init__(self):
|
6 |
+
self.client = ollama.Client()
|
7 |
+
self.code_model = "codellama:latest"
|
8 |
+
self.patterns = [
|
9 |
+
'head_and_shoulders', 'double_top', 'double_bottom',
|
10 |
+
'triangle', 'channel', 'fibonacci', 'wedge', 'flag',
|
11 |
+
'pennant', 'cup_and_handle', 'rounding_bottom'
|
12 |
+
# Will expand to 300+ patterns
|
13 |
+
]
|
14 |
+
|
15 |
+
def generate_all_patterns(self, chart_image, historical_data):
|
16 |
+
pattern_charts = []
|
17 |
+
for pattern in self.patterns:
|
18 |
+
# Generate pattern drawing code using CodeLlama
|
19 |
+
code_prompt = f"Generate Lightweight Charts code to draw {pattern} pattern with proper styling and technical analysis markers"
|
20 |
+
code_response = self.client.generate(
|
21 |
+
model=self.code_model,
|
22 |
+
prompt=code_prompt
|
23 |
+
)
|
24 |
+
|
25 |
+
# Create chart with pattern overlay
|
26 |
+
chart = Chart()
|
27 |
+
chart.set(code_response)
|
28 |
+
chart.add_data(historical_data)
|
29 |
+
|
30 |
+
pattern_charts.append(chart)
|
31 |
+
|
32 |
+
return pattern_charts
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
4 |
+
project_root = os.path.dirname(current_dir)
|
5 |
+
sys.path.append(project_root)
|
6 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from src.analysis.chart_processor import ChartProcessor
|
10 |
+
from src.analysis.pattern_generator import PatternGenerator
|
11 |
+
from src.analysis.indicator_generator import IndicatorGenerator
|
12 |
+
from src.analysis.chart_analyzer import ChartAnalyzer
|
13 |
+
from src.data.market_data import MarketData
|
14 |
+
|
15 |
+
def analyze_full_chart(chart_image, symbol):
|
16 |
+
market_data = MarketData()
|
17 |
+
historical_data = market_data.fetch_ohlcv(symbol)
|
18 |
+
|
19 |
+
pattern_gen = PatternGenerator()
|
20 |
+
pattern_images = pattern_gen.generate_all_patterns(chart_image, historical_data)
|
21 |
+
|
22 |
+
indicator_gen = IndicatorGenerator()
|
23 |
+
indicator_charts = indicator_gen.generate_all_indicators(historical_data)
|
24 |
+
|
25 |
+
|
26 |
+
processor = ChartProcessor()
|
27 |
+
results = processor.process_chart(chart_image)
|
28 |
+
|
29 |
+
analyzer = ChartAnalyzer()
|
30 |
+
analysis_results = analyzer.analyze_full(
|
31 |
+
chart_image=chart_image,
|
32 |
+
pattern_images=pattern_images,
|
33 |
+
indicator_charts=indicator_charts,
|
34 |
+
historical_data=historical_data
|
35 |
+
)
|
36 |
+
|
37 |
+
return {
|
38 |
+
'pattern_analysis': analysis_results.patterns,
|
39 |
+
'indicator_analysis': analysis_results.indicators,
|
40 |
+
'predictions': analysis_results.predictions,
|
41 |
+
'interactive_charts': indicator_charts,
|
42 |
+
'chart_analysis':results,
|
43 |
+
}
|
44 |
+
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=analyze_full_chart,
|
47 |
+
inputs=[
|
48 |
+
gr.Image(type="pil", label="Chart Image"),
|
49 |
+
gr.Textbox(label="Symbol")
|
50 |
+
],
|
51 |
+
outputs=[
|
52 |
+
gr.Gallery(label="Pattern Overlays"),
|
53 |
+
gr.Gallery(label="Indicator Charts"),
|
54 |
+
gr.JSON(label="Analysis Results"),
|
55 |
+
gr.Plot(label="Interactive Indicators")
|
56 |
+
]
|
57 |
+
)
|
58 |
+
|
59 |
+
interface.launch()
|
data/__init__.py
ADDED
File without changes
|
data/market_data.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pandas as pd
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
|
5 |
+
class MarketData:
|
6 |
+
def __init__(self):
|
7 |
+
self.base_url = "YOUR_MARKET_DATA_API_ENDPOINT"
|
8 |
+
|
9 |
+
def fetch_ohlcv(self, symbol, timeframe='1d'):
|
10 |
+
"""
|
11 |
+
Fetch OHLCV data for given symbol
|
12 |
+
Returns: DataFrame with columns [timestamp, open, high, low, close, volume]
|
13 |
+
"""
|
14 |
+
endpoint = f"{self.base_url}/historical/{symbol}"
|
15 |
+
params = {
|
16 |
+
'timeframe': timeframe,
|
17 |
+
'limit': 365 # Last year of data
|
18 |
+
}
|
19 |
+
|
20 |
+
response = requests.get(endpoint, params=params)
|
21 |
+
data = response.json()
|
22 |
+
|
23 |
+
df = pd.DataFrame(data)
|
24 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
25 |
+
return df
|
26 |
+
|
27 |
+
def get_latest_price(self, symbol):
|
28 |
+
endpoint = f"{self.base_url}/price/{symbol}"
|
29 |
+
response = requests.get(endpoint)
|
30 |
+
return response.json()['price']
|
model/__init__.py
ADDED
File without changes
|
model/vision_model.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
class ChartVisionModel:
|
5 |
+
def __init__(self):
|
6 |
+
self.client = ollama.Client()
|
7 |
+
self.model = "llava:34b" # Using LLaVA for vision analysis
|
8 |
+
|
9 |
+
def analyze_chart(self, image):
|
10 |
+
response = self.client.generate(
|
11 |
+
model=self.model,
|
12 |
+
prompt="Analyze this technical chart in detail",
|
13 |
+
images=[image]
|
14 |
+
)
|
15 |
+
return response
|
16 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
torch>=2.0.0
|
3 |
+
transformers>=4.30.0
|
4 |
+
python-ollama>=0.1.0
|
5 |
+
Pillow>=10.0.0
|
6 |
+
huggingface-hub>=0.19.0
|
7 |
+
python-dotenv>=1.0.0
|
8 |
+
fastapi
|
9 |
+
uvicorn
|
10 |
+
gradio
|
11 |
+
huggingface-hub
|
12 |
+
transformers
|
13 |
+
torch
|
14 |
+
lightweight-charts
|
15 |
+
pandas
|
16 |
+
numpy
|
17 |
+
requests
|
18 |
+
python-multipart
|
19 |
+
ollama
|
20 |
+
python-dotenv
|
21 |
+
plotly
|
22 |
+
opencv-python
|
23 |
+
pillow
|