-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdemo.py
More file actions
205 lines (166 loc) · 7.43 KB
/
demo.py
File metadata and controls
205 lines (166 loc) · 7.43 KB
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
"""
PyCharting Interactive Demo Suite.
This script provides multiple scenarios to demonstrate the flexibility of PyCharting:
1. Various Index Types (Numeric, Pandas Datetime, Unix Timestamps).
2. Chart Types (Candlestick vs Line).
3. Performance (Stress Test).
"""
import sys
import time
import numpy as np
import pandas as pd
from pycharting import plot, stop_server
def sma(values: np.ndarray, window: int) -> np.ndarray:
"""Calculate Simple Moving Average."""
kernel = np.ones(window, dtype=float) / float(window)
return np.convolve(values, kernel, mode="same")
def ema(values: np.ndarray, span: int) -> np.ndarray:
"""Calculate Exponential Moving Average."""
alpha = 2.0 / (span + 1.0)
out = np.empty_like(values, dtype=float)
out[0] = values[0]
for i in range(1, len(values)):
out[i] = alpha * values[i] + (1.0 - alpha) * out[i - 1]
return out
def rsi_like(values: np.ndarray, period: int = 14) -> np.ndarray:
"""Calculate RSI-like oscillator."""
delta = np.diff(values, prepend=values[0])
gain = np.where(delta > 0, delta, 0.0)
loss = np.where(delta < 0, -delta, 0.0)
avg_gain = sma(gain, period)
avg_loss = sma(loss, period)
rs = np.divide(avg_gain, avg_loss, out=np.zeros_like(avg_gain), where=avg_loss != 0)
rsi = 100.0 - (100.0 / (1.0 + rs))
return rsi
def generate_ohlc(n: int = 1000):
"""Generate synthetic OHLC data with indicators."""
base = 100.0
noise = np.random.randn(n)
close = np.cumsum(noise) + base
open_ = close + np.random.randn(n) * 0.5
high = np.maximum(open_, close) + np.abs(np.random.randn(n))
low = np.minimum(open_, close) - np.abs(np.random.randn(n))
# Calculate overlays
sma_50 = sma(close, 50)
ema_20 = ema(close, 20)
# Calculate subplots
rsi = rsi_like(close, 14)
volume = np.abs(np.random.randn(n)) * 10000 + 5000
overlays = {
"SMA 50": sma_50,
"EMA 20": ema_20,
}
subplots = {
"RSI": rsi,
"Volume": {"data": volume, "type": "bar"},
}
return open_, high, low, close, overlays, subplots
def run_demo(choice: str):
n = 5000 # Default size
open_, high, low, close, overlays, subplots = generate_ohlc(n)
numeric_index = np.arange(n)
if choice == "1":
print("\n--- Demo: Full OHLC (Numeric Index) with Indicators ---")
plot(numeric_index, open=open_, high=high, low=low, close=close,
overlays=overlays, subplots=subplots)
elif choice == "2":
print("\n--- Demo: Full OHLC (Pandas DatetimeIndex) with Indicators ---")
date_index = pd.date_range(start="2024-01-01", periods=n, freq="h")
plot(date_index, open=open_, high=high, low=low, close=close,
overlays=overlays, subplots=subplots)
elif choice == "3":
print("\n--- Demo: Full OHLC (Unix Timestamps) with Indicators ---")
# Milliseconds since epoch
start_ts = int(time.time() * 1000)
# 1 hour steps
ts_index = np.array([start_ts + i * 3600000 for i in range(n)], dtype=np.int64)
plot(ts_index, open=open_, high=high, low=low, close=close,
overlays=overlays, subplots=subplots)
elif choice == "4":
print("\n--- Demo: Line Chart (Close Only) - Numeric Index with Indicators ---")
# Only passing 'close' triggers line chart mode
plot(numeric_index, close=close, overlays=overlays, subplots=subplots)
elif choice == "5":
print("\n--- Demo: Line Chart (Close Only) - Datetime Index with Indicators ---")
date_index = pd.date_range(start="2024-01-01", periods=n, freq="h")
plot(date_index, close=close, overlays=overlays, subplots=subplots)
elif choice == "6":
print("\n--- Demo: Single Array (Open Only) as Line with Indicators ---")
# Should treat 'open' as the main line if it's the only one
plot(numeric_index, open=open_, overlays=overlays, subplots=subplots)
elif choice == "7":
print("\n--- Demo: Candlesticks with Trade Arrows + Volume Bars ---")
date_index = pd.date_range(start="2024-01-01", periods=n, freq="h")
trades = np.zeros(n, dtype=int)
signal_mask = np.random.rand(n) < 0.02
trades[signal_mask] = np.random.choice([-1, 1], size=signal_mask.sum())
vol_raw = np.abs(np.random.randn(n)) * 10000 + 5000
vol_sign = np.where(close >= open_, 1, -1)
signed_vol = vol_raw * vol_sign
vol_subplots = {
"RSI": subplots["RSI"],
"Volume": {"data": signed_vol, "type": "bar"},
}
plot(date_index, open=open_, high=high, low=low, close=close,
overlays=overlays, subplots=vol_subplots, trades=trades)
elif choice == "8":
print("\n--- Demo: Multi-Series Subplots (MACD + RSI/SMA + Scatter) ---")
date_index = pd.date_range(start="2024-01-01", periods=n, freq="h")
rsi = rsi_like(close, 14)
rsi_sma = sma(rsi, 20)
macd_line = ema(close, 12) - ema(close, 26)
signal_line = ema(macd_line, 9)
histogram = macd_line - signal_line
events = np.full(n, np.nan)
event_mask = np.random.rand(n) < 0.01
events[event_mask] = close[event_mask]
multi_subplots = {
"RSI": [
{"data": rsi, "type": "line", "color": "#FF9800", "label": "RSI"},
{"data": rsi_sma, "type": "line", "color": "#2196F3", "label": "RSI SMA(20)"},
],
"MACD": [
{"data": macd_line, "type": "line", "color": "#2196F3", "label": "MACD"},
{"data": signal_line, "type": "line", "color": "#FF9800", "label": "Signal"},
{"data": histogram, "type": "bar", "label": "Histogram"},
],
"Events": {"data": events, "type": "scatter", "color": "#9C27B0"},
}
plot(date_index, open=open_, high=high, low=low, close=close,
overlays=overlays, subplots=multi_subplots)
elif choice == "9":
print("\n--- Stress Test (1 Million Points) with Indicators ---")
n_stress = 1_000_000
o, h, l, c, ovr, sub = generate_ohlc(n_stress)
idx = np.arange(n_stress)
plot(idx, open=o, high=h, low=l, close=c, overlays=ovr, subplots=sub)
else:
print("Invalid choice.")
def main():
try:
while True:
print("\n" + "="*40)
print("PyCharting Feature Demos")
print("="*40)
print("1. Candlesticks - Numeric Index (0, 1, 2...)")
print("2. Candlesticks - Pandas DatetimeIndex")
print("3. Candlesticks - Unix Timestamps (ms)")
print("4. Line Chart - Numeric Index (Close only)")
print("5. Line Chart - Datetime Index (Close only)")
print("6. Line Chart - Open Price only (Flexible Input)")
print("7. Trade Arrows - Buy/Sell Signals on Chart")
print("8. Multi-Series - MACD, RSI/SMA, Scatter")
print("9. Stress Test - 1 Million Candles")
print("0. Exit")
print("="*40)
choice = input("Select a demo (0-9): ").strip()
if choice == "0":
break
run_demo(choice)
input("\nPress Enter to return to menu (Server keeps running)...")
except KeyboardInterrupt:
print("\nExiting...")
finally:
stop_server()
if __name__ == "__main__":
main()