File size: 3,387 Bytes
32e3c06
5c43f7a
32e3c06
01e3cc0
805f7bd
 
 
32e3c06
 
 
805f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01e3cc0
32e3c06
 
 
 
 
 
 
 
 
 
 
805f7bd
32e3c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805f7bd
32e3c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
667bcfc
32e3c06
667bcfc
32e3c06
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
os.environ['HF_HOME'] = '/tmp/huggingface'
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import json
import pandas as pd
from pattern_logic import PatternLogic

class PatternAnalyzer:
    def __init__(self):
        model_kwargs = {
            "device_map": "auto",
            "torch_dtype": torch.float32,
            "low_cpu_mem_usage": True,
            "max_memory": {"cpu": "4GB"},
            "offload_folder": "/tmp/offload"
        }
        
        self.model = AutoModelForCausalLM.from_pretrained(
            "tmmdev/codellama-pattern-analysis",
            **model_kwargs
        )
        
        self.tokenizer = AutoTokenizer.from_pretrained(
            "tmmdev/codellama-pattern-analysis",
            use_fast=True
        )
        
        self.basic_patterns = {
            'channel': {'min_points': 4, 'confidence_threshold': 0.7},
            'triangle': {'min_points': 3, 'confidence_threshold': 0.75},
            'support': {'min_touches': 2, 'confidence_threshold': 0.8},
            'resistance': {'min_touches': 2, 'confidence_threshold': 0.8},
            'double_top': {'max_deviation': 0.02, 'confidence_threshold': 0.85},
            'double_bottom': {'max_deviation': 0.02, 'confidence_threshold': 0.85}
        }
        self.pattern_logic = PatternLogic()

    def analyze_data(self, ohlcv_data):
        data_prompt = f"""TASK: Identify high-confidence technical patterns only.
        Minimum confidence threshold: 0.8
        Required pattern criteria:
        1. Channel: Must have at least 3 touching points
        2. Triangle: Must have clear convergence point
        3. Support: Minimum 3 price bounces
        4. Resistance: Minimum 3 price rejections

        INPUT DATA:
        {ohlcv_data.to_json(orient='records')}

        Return ONLY high-confidence patterns (>0.8) in JSON format with exact price coordinates."""

        inputs = self.tokenizer(data_prompt, return_tensors="pt")
        outputs = self.model.generate(**inputs, max_length=1000)
        analysis = self.tokenizer.decode(outputs[0])

        return self.parse_analysis(analysis)

    def parse_analysis(self, analysis_text):
        try:
            json_start = analysis_text.find('{')
            json_end = analysis_text.rfind('}') + 1
            json_str = analysis_text[json_start:json_end]
            
            analysis_data = json.loads(json_str)
            patterns = []
            
            for pattern in analysis_data.get('patterns', []):
                pattern_type = pattern.get('type')
                if pattern_type in self.basic_patterns:
                    threshold = self.basic_patterns[pattern_type]['confidence_threshold']
                    if pattern.get('confidence', 0) >= threshold:
                        patterns.append({
                            'type': pattern_type,
                            'coordinates': pattern.get('coordinates', []),
                            'confidence': pattern.get('confidence'),
                            'metadata': {
                                'rules': self.basic_patterns[pattern_type],
                                'timestamp': pd.Timestamp.now().isoformat()
                            }
                        })
            
            return patterns
            
        except json.JSONDecodeError:
            return []