dixitramjendra commited on
Commit
28861fc
·
1 Parent(s): 9287800

Add missing trade_screenshot.py module

Browse files
Files changed (1) hide show
  1. trade_screenshot.py +136 -0
trade_screenshot.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datetime import datetime
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ from typing import Dict, Optional
5
+
6
+ def capture_trade_screenshot(res: Dict, event_type: str, output_path: str):
7
+ """
8
+ Generates a high-quality image of a trade closure card using Pillow.
9
+ """
10
+ # Card Dimensions (Smaller to fit 6 per page)
11
+ width, height = 280, 450
12
+ background_color = (18, 24, 38) # Dark blue/black
13
+ card_color = (26, 34, 54) # Slightly lighter blue
14
+ text_color = (255, 255, 255)
15
+ accent_color = (59, 130, 246) # Blue accent
16
+
17
+ # Colors for status
18
+ buy_color = (34, 197, 94) # Green
19
+ sell_color = (239, 68, 68) # Red
20
+ neutral_color = (156, 163, 175) # Gray
21
+
22
+ # Create Image
23
+ img = Image.new('RGB', (width, height), background_color)
24
+ draw = ImageDraw.Draw(img)
25
+
26
+ # Draw Card Background
27
+ draw.rounded_rectangle([5, 5, width-5, height-5], radius=10, fill=card_color)
28
+
29
+ # Load Fonts
30
+ try:
31
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
32
+ if not os.path.exists(font_path):
33
+ font_path = "/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttf"
34
+
35
+ font_title = ImageFont.truetype(font_path, 20)
36
+ font_main = ImageFont.truetype(font_path, 14)
37
+ font_small = ImageFont.truetype(font_path, 11)
38
+ font_status = ImageFont.truetype(font_path, 12)
39
+ except:
40
+ font_title = ImageFont.load_default()
41
+ font_main = ImageFont.load_default()
42
+ font_small = ImageFont.load_default()
43
+ font_status = ImageFont.load_default()
44
+
45
+ # Header: Instrument
46
+ name = res.get('instrument', 'Unknown')
47
+ draw.text((20, 20), name, font=font_title, fill=text_color)
48
+
49
+ # LTP
50
+ ltp = res.get('ltp', 0.0)
51
+ draw.text((20, 50), f"LTP: {ltp:.5f}", font=font_main, fill=accent_color)
52
+
53
+ # Status Badges (Daily, 4H, 1H)
54
+ badge_y = 85
55
+ badges = [
56
+ ("1D TREND", res.get('daily', {}).get('bias', 'NEUTRAL')),
57
+ ("4H MOM", res.get('h4', {}).get('bias', 'NEUTRAL')),
58
+ ("1H ENTRY", res.get('h1', {}).get('status', 'NEUTRAL'))
59
+ ]
60
+
61
+ for i, (label, status) in enumerate(badges):
62
+ x = 20 + (i * 85)
63
+ color = buy_color if "BULLISH" in status or "BUY" in status else (sell_color if "BEARISH" in status or "SELL" in status else neutral_color)
64
+ draw.text((x, badge_y), label, font=font_small, fill=neutral_color)
65
+ draw.text((x, badge_y + 15), status.split('_')[0], font=font_status, fill=color)
66
+
67
+ # Active Signal Section
68
+ signal = res.get('signal')
69
+ if signal:
70
+ draw.line([20, 130, width-20, 130], fill=(50, 60, 80), width=1)
71
+
72
+ sig_type = signal.get('type', 'N/A')
73
+ sig_color = buy_color if sig_type == "BUY" else sell_color
74
+ draw.text((20, 145), "ACTIVE SIGNAL", font=font_main, fill=neutral_color)
75
+ draw.text((width-70, 145), sig_type, font=font_main, fill=sig_color)
76
+
77
+ # Signal Details
78
+ details_y = 175
79
+ details = [
80
+ ("Entry Price", f"{signal.get('entry_price', 0):.5f}"),
81
+ ("Initial SL", f"{signal.get('sl', 0):.5f}"),
82
+ ("TP 1 (1.5x)", f"{signal.get('tp1', 0):.5f}"),
83
+ ("TP 2 (3.0x)", f"{signal.get('tp2', 0):.5f}"),
84
+ ("TP 3 (5.0x)", f"{signal.get('tp3', 0):.5f}")
85
+ ]
86
+
87
+ for label, val in details:
88
+ draw.text((20, details_y), label, font=font_small, fill=neutral_color)
89
+ draw.text((width-90, details_y), val, font=font_small, fill=text_color)
90
+ details_y += 20
91
+
92
+ # Closure Event
93
+ draw.line([20, 300, width-20, 300], fill=(50, 60, 80), width=1)
94
+ draw.text((20, 315), "CLOSURE EVENT", font=font_main, fill=neutral_color)
95
+ draw.text((width-110, 315), event_type, font=font_main, fill=sig_color)
96
+
97
+ # Timestamps
98
+ active_time = signal.get('time', 'N/A')
99
+ if 'T' in active_time:
100
+ active_time = datetime.fromisoformat(active_time).strftime("%d/%b/%Y, %H:%M:%S")
101
+
102
+ close_time = datetime.now().strftime("%d/%b/%Y, %H:%M:%S")
103
+
104
+ draw.text((20, 350), "Detection Time:", font=font_small, fill=neutral_color)
105
+ draw.text((110, 350), active_time, font=font_small, fill=text_color)
106
+
107
+ draw.text((20, 375), "Closure Time:", font=font_small, fill=neutral_color)
108
+ draw.text((110, 375), close_time, font=font_small, fill=text_color)
109
+
110
+ # Save Image
111
+ output_dir = os.path.dirname(output_path)
112
+ if output_dir:
113
+ os.makedirs(output_dir, exist_ok=True)
114
+ img.save(output_path)
115
+ print(f"✅ Screenshot saved to {output_path}")
116
+
117
+ if __name__ == "__main__":
118
+ # Test generation
119
+ test_res = {
120
+ "instrument": "USD/CAD",
121
+ "flag": "🇺🇸🇨🇦",
122
+ "ltp": 1.37032,
123
+ "daily": {"bias": "BEARISH"},
124
+ "h4": {"bias": "BEARISH"},
125
+ "h1": {"status": "BEARISH_MOM"},
126
+ "signal": {
127
+ "type": "SELL",
128
+ "entry_price": 1.37063,
129
+ "sl": 1.37245,
130
+ "tp1": 1.36790,
131
+ "tp2": 1.36516,
132
+ "tp3": 1.36152,
133
+ "time": datetime.now().isoformat()
134
+ }
135
+ }
136
+ capture_trade_screenshot(test_res, "TP1 HIT", "test_trade.png")