File size: 11,119 Bytes
d3932f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Dataset Generator for Candlestick Charts
Run this to create training data before training the model.

Usage:
    python generate_data.py --num_samples 10000 --output_dir ./dataset
"""

import os
import json
import random
import argparse
import numpy as np
from PIL import Image
from tqdm import tqdm
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import io


class CandlestickGenerator:
    def __init__(self, image_size=(128, 128), num_candles=20):
        self.image_size = image_size
        self.num_candles = num_candles
        self.bg_color = "#1a1a2e"
        self.bullish_color = "#00ff88"
        self.bearish_color = "#ff4466"
        
        self.patterns = {
            "bullish_trend": self._bullish_trend,
            "bearish_trend": self._bearish_trend,
            "sideways": self._sideways,
            "volatile": self._volatile,
            "bullish_reversal": self._bullish_reversal,
            "bearish_reversal": self._bearish_reversal,
            "double_top": self._double_top,
            "double_bottom": self._double_bottom,
        }
    
    def _bullish_trend(self, n, vol):
        candles = []
        price = 100
        for i in range(n):
            trend = random.uniform(0.5, 2.0)
            noise = random.gauss(0, vol)
            o = price + noise
            c = o + random.uniform(0, vol * 2) + trend
            if random.random() < 0.7:
                c = max(c, o + 0.5)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
            price = c
        return candles
    
    def _bearish_trend(self, n, vol):
        candles = []
        price = 150
        for i in range(n):
            trend = random.uniform(0.5, 2.0)
            noise = random.gauss(0, vol)
            o = price + noise
            c = o - random.uniform(0, vol * 2) - trend
            if random.random() < 0.7:
                c = min(c, o - 0.5)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
            price = c
        return candles
    
    def _sideways(self, n, vol):
        candles = []
        base = 100
        for i in range(n):
            center = base + random.gauss(0, vol * 2)
            o = center + random.gauss(0, vol)
            c = center + random.gauss(0, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        return candles
    
    def _volatile(self, n, vol):
        candles = []
        price = 100
        high_vol = vol * 3
        for i in range(n):
            direction = 1 if random.random() > 0.5 else -1
            move = random.uniform(high_vol, high_vol * 2) * direction
            o = price + random.gauss(0, high_vol)
            c = o + move
            h = max(o, c) + random.uniform(high_vol * 0.5, high_vol)
            l = min(o, c) - random.uniform(high_vol * 0.5, high_vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
            price = c
        return candles
    
    def _bullish_reversal(self, n, vol):
        mid = n // 2
        part1 = self._bearish_trend(mid, vol)
        last = part1[-1]["c"]
        part2 = []
        price = last
        for i in range(n - mid):
            trend = random.uniform(0.5, 1.5)
            o = price + random.gauss(0, vol)
            c = o + random.uniform(0, vol * 2) + trend
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            part2.append({"o": o, "h": h, "l": l, "c": c})
            price = c
        return part1 + part2
    
    def _bearish_reversal(self, n, vol):
        mid = n // 2
        part1 = self._bullish_trend(mid, vol)
        last = part1[-1]["c"]
        part2 = []
        price = last
        for i in range(n - mid):
            trend = random.uniform(0.5, 1.5)
            o = price + random.gauss(0, vol)
            c = o - random.uniform(0, vol * 2) - trend
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            part2.append({"o": o, "h": h, "l": l, "c": c})
            price = c
        return part1 + part2
    
    def _double_top(self, n, vol):
        third = n // 3
        candles = []
        base, peak = 100, 120
        
        for i in range(third):
            p = base + (peak - base) * (i / third) + random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        for i in range(third):
            p = peak - (peak - base) * 0.5 * (i / third) + random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        for i in range(n - 2 * third):
            prog = i / (n - 2 * third)
            if prog < 0.5:
                p = (base + peak) / 2 + (peak - (base + peak) / 2) * (prog * 2)
            else:
                p = peak - (peak - base) * ((prog - 0.5) * 2)
            p += random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        return candles
    
    def _double_bottom(self, n, vol):
        third = n // 3
        candles = []
        base, bottom = 120, 100
        
        for i in range(third):
            p = base - (base - bottom) * (i / third) + random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        for i in range(third):
            p = bottom + (base - bottom) * 0.5 * (i / third) + random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        for i in range(n - 2 * third):
            prog = i / (n - 2 * third)
            if prog < 0.5:
                p = (base + bottom) / 2 - ((base + bottom) / 2 - bottom) * (prog * 2)
            else:
                p = bottom + (base - bottom) * ((prog - 0.5) * 2)
            p += random.gauss(0, vol)
            o, c = p, p + random.uniform(-vol, vol)
            h = max(o, c) + random.uniform(0, vol)
            l = min(o, c) - random.uniform(0, vol)
            candles.append({"o": o, "h": h, "l": l, "c": c})
        
        return candles
    
    def render(self, candles):
        fig, ax = plt.subplots(figsize=(self.image_size[0]/100, self.image_size[1]/100), dpi=100)
        fig.patch.set_facecolor(self.bg_color)
        ax.set_facecolor(self.bg_color)
        
        highs = [c["h"] for c in candles]
        lows = [c["l"] for c in candles]
        price_min = min(lows) * 0.98
        price_max = max(highs) * 1.02
        
        width = 0.6
        for i, c in enumerate(candles):
            color = self.bullish_color if c["c"] >= c["o"] else self.bearish_color
            ax.plot([i, i], [c["l"], c["h"]], color=color, linewidth=1)
            body_bottom = min(c["o"], c["c"])
            body_height = abs(c["c"] - c["o"]) or 0.1
            rect = Rectangle((i - width/2, body_bottom), width, body_height, 
                            facecolor=color, edgecolor=color)
            ax.add_patch(rect)
        
        ax.set_xlim(-1, len(candles))
        ax.set_ylim(price_min, price_max)
        ax.axis("off")
        
        buf = io.BytesIO()
        plt.savefig(buf, format="png", facecolor=self.bg_color, 
                   bbox_inches="tight", pad_inches=0.1)
        plt.close(fig)
        buf.seek(0)
        
        img = Image.open(buf).convert("RGB")
        img = img.resize(self.image_size, Image.Resampling.LANCZOS)
        return img
    
    def generate_sample(self):
        pattern = random.choice(list(self.patterns.keys()))
        vol_name = random.choice(["low", "medium", "high"])
        vol_map = {"low": 1.0, "medium": 3.0, "high": 6.0}
        
        candles = self.patterns[pattern](self.num_candles, vol_map[vol_name])
        image = self.render(candles)
        
        descriptions = {
            "bullish_trend": [f"bullish trend {vol_name} volatility", f"upward trending market {vol_name} movement", "strong buying pressure"],
            "bearish_trend": [f"bearish trend {vol_name} volatility", f"downward trending market {vol_name} movement", "strong selling pressure"],
            "sideways": [f"sideways market {vol_name} volatility", "range-bound trading", "consolidation pattern"],
            "volatile": ["highly volatile market", "erratic price movement", "choppy market conditions"],
            "bullish_reversal": [f"bullish reversal {vol_name} volatility", "v-shaped recovery", "trend change bearish to bullish"],
            "bearish_reversal": [f"bearish reversal {vol_name} volatility", "inverted v pattern", "trend change bullish to bearish"],
            "double_top": [f"double top pattern {vol_name} volatility", "m-shaped reversal", "bearish double top"],
            "double_bottom": [f"double bottom pattern {vol_name} volatility", "w-shaped reversal", "bullish double bottom"],
        }
        
        description = random.choice(descriptions[pattern])
        return image, description


def generate_dataset(output_dir, num_samples=10000, image_size=128):
    os.makedirs(output_dir, exist_ok=True)
    os.makedirs(os.path.join(output_dir, "images"), exist_ok=True)
    
    generator = CandlestickGenerator(image_size=(image_size, image_size))
    labels = {}
    
    print(f"Generating {num_samples} samples...")
    for i in tqdm(range(num_samples)):
        image, description = generator.generate_sample()
        filename = f"chart_{i:06d}.png"
        image.save(os.path.join(output_dir, "images", filename))
        labels[filename] = description
    
    with open(os.path.join(output_dir, "labels.json"), "w") as f:
        json.dump(labels, f, indent=2)
    
    print(f"✅ Dataset saved to {output_dir}")
    print(f"   - {num_samples} images")
    print(f"   - Labels in labels.json")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--num_samples", type=int, default=10000)
    parser.add_argument("--output_dir", type=str, default="./dataset")
    parser.add_argument("--image_size", type=int, default=128)
    args = parser.parse_args()
    
    generate_dataset(args.output_dir, args.num_samples, args.image_size)