Spaces:
Sleeping
Sleeping
File size: 25,478 Bytes
a213ac9 e8fc579 a213ac9 614a841 a213ac9 0e5b201 a213ac9 e8fc579 a213ac9 0e5b201 b35faa8 0e5b201 a213ac9 0e5b201 a213ac9 e8fc579 a213ac9 0e5b201 a213ac9 0e5b201 a213ac9 0e5b201 a213ac9 0e5b201 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 e8fc579 a213ac9 614a841 a213ac9 e8fc579 a213ac9 b35faa8 a213ac9 b35faa8 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 0e5b201 a213ac9 0e5b201 a213ac9 6e08397 a213ac9 0e5b201 6e08397 0e5b201 6e08397 0e5b201 e8fc579 0e5b201 e8fc579 0e5b201 e8fc579 0e5b201 a213ac9 0e5b201 614a841 0e5b201 614a841 0e5b201 614a841 6e08397 614a841 b35faa8 6e08397 0e5b201 6e08397 0e5b201 6e08397 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 614a841 a213ac9 |
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from model_intra import get_data, walk_forward_validation\n",
"import lightgbm as lgb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"getting econ tickers: 100%|ββββββββββ| 3/3 [00:00<00:00, 3.10it/s]\n",
"Getting release dates: 100%|ββββββββββ| 8/8 [00:01<00:00, 4.87it/s]\n",
"Making indicators: 100%|ββββββββββ| 8/8 [00:00<00:00, 3997.91it/s]\n",
"Found cached dataset text (C:/Users/WINSTON-ITX/.cache/huggingface/datasets/boomsss___text/boomsss--spx_intra-e0e5e7af8fd43022/0.0.0/cb1e9bd71a82ad27976be3b12b407850fe2837d80c22c5e03a28949843a8ace2)\n",
"Merging econ data: 100%|ββββββββββ| 8/8 [00:00<00:00, 799.22it/s]\n"
]
}
],
"source": [
"data, df_final, final_row = get_data(5)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data['ClosePct'] = (data['Close'] / data['PrevClose']) - 1\n",
"data['HighPct'] = (data['High'] / data['PrevClose']) - 1\n",
"data['LowPct'] = (data['Low'] / data['PrevClose']) - 1\n",
"data['ClosePct'] = data['ClosePct'].shift(-1)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Calculate the rolling likelihood\n",
"rolling_likelihood = (data['H1Break'] & data['H1Touch']==True).rolling(window=100).mean()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"data['H1BreakTouchPct'] = data['H1Break'].expanding().sum() / data['H1Touch'].expanding().sum()\n",
"data['H2BreakTouchPct'] = data['H2Break'].expanding().sum() / data['H2Touch'].expanding().sum()\n",
"data['H1BreakTouchPct'] = data['L1Break'].expanding().sum() / data['L1Touch'].expanding().sum()\n",
"data['H2BreakTouchPct'] = data['L2Break'].expanding().sum() / data['L2Touch'].expanding().sum()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>H2Touch</th>\n",
" <th>H2Break</th>\n",
" <th>H2BreakTouch</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2018-07-02</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-07-03</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-07-05</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-07-06</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-07-09</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-10</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>0.588235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-11</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>0.588235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-12</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>0.588235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-13</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>0.588235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-16</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>0.571429</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1332 rows Γ 3 columns</p>\n",
"</div>"
],
"text/plain": [
" H2Touch H2Break H2BreakTouch\n",
"2018-07-02 False False NaN\n",
"2018-07-03 False False NaN\n",
"2018-07-05 False False NaN\n",
"2018-07-06 False False NaN\n",
"2018-07-09 False False NaN\n",
"... ... ... ...\n",
"2023-10-10 True False 0.588235\n",
"2023-10-11 False False 0.588235\n",
"2023-10-12 False False 0.588235\n",
"2023-10-13 False False 0.588235\n",
"2023-10-16 True False 0.571429\n",
"\n",
"[1332 rows x 3 columns]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[['H2Touch','H2Break','H2BreakTouch']]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2018-07-02 NaN\n",
"2018-07-03 NaN\n",
"2018-07-05 NaN\n",
"2018-07-06 NaN\n",
"2018-07-09 NaN\n",
" ... \n",
"2023-10-10 0.22\n",
"2023-10-11 0.21\n",
"2023-10-12 0.21\n",
"2023-10-13 0.21\n",
"2023-10-16 0.22\n",
"Name: H1BreakPct, Length: 1332, dtype: float64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['H1BreakPct']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res1, model1 = walk_forward_validation(df_final.dropna(axis=0), 'Target_clf', 100, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# Plot feature importances\n",
"plt.figure(figsize=(10, 12))\n",
"lgb.plot_importance(model1) # Adjust max_num_features as needed\n",
"plt.title(\"Feature Importances\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yfinance as yf\n",
"\n",
"vix = yf.Ticker('^TNX')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vix.history(interval='30m')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_final.groupby(pd.qcut(df_final['CurrentGap'], 10))['Target_clf'].mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import roc_auc_score, precision_score, recall_score\n",
"# st.subheader('New Prediction')\n",
"nq = 7\n",
"\n",
"df_probas = res1.groupby(pd.qcut(res1['Predicted'],nq)).agg({'True':[np.mean,len,np.sum]})\n",
"# df_probas = res1.groupby(pd.cut(res1['Predicted'],[-np.inf, 0.27, 0.375, 0.625, 0.7, np.inf])).agg({'True':[np.mean,len,np.sum]})\n",
"df_probas.columns = ['PctGreen','NumObs','NumGreen']\n",
"\n",
"# Calculate quantiles\n",
"quantiles = pd.qcut(res1['Predicted'], nq, labels=False, duplicates='drop')\n",
"\n",
"# Determine the number of quantiles\n",
"num_quantiles = len(quantiles.unique())\n",
"\n",
"# Calculate the middle quantile(s)\n",
"if num_quantiles % 2 == 0: # Even number of quantiles\n",
" middle_quantiles = quantiles.isin([num_quantiles // 2 - 1, num_quantiles // 2])\n",
"else: # Odd number of quantiles\n",
" middle_quantiles = quantiles == num_quantiles // 2\n",
"\n",
"# Extract the lower and upper thresholds\n",
"lo_thres = 0.4 # res1.loc[middle_quantiles, 'Predicted'].min()\n",
"hi_thres = 0.6 # res1.loc[middle_quantiles, 'Predicted'].max()\n",
"\n",
"roc_auc_score_all = roc_auc_score(res1['True'].astype(int), res1['Predicted'].values)\n",
"roc_auc_score_calib = roc_auc_score(res1.dropna(subset='CalibPredicted')['True'].astype(int), res1.dropna(subset='CalibPredicted')['CalibPredicted'].values)\n",
"precision_score_all = precision_score(res1['True'].astype(int), res1['Predicted'] > 0.5)\n",
"recall_score_all = recall_score(res1['True'].astype(int), res1['Predicted'] > 0.5)\n",
"len_all = len(res1)\n",
"\n",
"res2_filtered = res1.loc[(res1['Predicted'] > hi_thres) | (res1['Predicted'] <= lo_thres)]\n",
"\n",
"roc_auc_score_hi = roc_auc_score(res2_filtered['True'].astype(int), res2_filtered['Predicted'].values)\n",
"roc_auc_score_hi_calib = roc_auc_score(res2_filtered.dropna(subset='CalibPredicted')['True'].astype(int), res2_filtered.dropna(subset='CalibPredicted')['CalibPredicted'].values)\n",
"precision_score_hi = precision_score(res2_filtered['True'].astype(int), res2_filtered['Predicted'] > 0.5)\n",
"recall_score_hi = recall_score(res2_filtered['True'].astype(int), res2_filtered['Predicted'] > 0.5)\n",
"len_hi = len(res2_filtered)\n",
"\n",
"df_performance = pd.DataFrame(\n",
" index=[\n",
" 'N',\n",
" 'ROC AUC',\n",
" 'Calib. AUC',\n",
" 'Precision',\n",
" 'Recall'\n",
" ],\n",
" columns = [\n",
" 'All',\n",
" 'High Confidence'\n",
" ],\n",
" data = [\n",
" [len_all, len_hi],\n",
" [roc_auc_score_all, roc_auc_score_hi],\n",
" [roc_auc_score_calib, roc_auc_score_hi_calib],\n",
" [precision_score_all, precision_score_hi],\n",
" [recall_score_all, recall_score_hi]\n",
" ]\n",
").round(2)\n",
"\n",
"def get_acc(t, p):\n",
" if t == False and p <= lo_thres:\n",
" return 'β
' # ✅</p>\n",
" elif t == True and p > hi_thres:\n",
" return 'β
' # \n",
" elif t == False and p > hi_thres:\n",
" return 'β' # ❌</p>\n",
" elif t == True and p <= lo_thres:\n",
" return 'β'\n",
" else:\n",
" return 'π¨' # ⬜</p>\n",
" \n",
"def get_acc_html(t, p):\n",
" if t == False and p <= lo_thres:\n",
" return '✅'\n",
" elif t == True and p > hi_thres:\n",
" return '✅'\n",
" elif t == False and p > hi_thres:\n",
" return '❌'\n",
" elif t == True and p <= lo_thres:\n",
" return '❌'\n",
" else:\n",
" return '⬜'\n",
"\n",
"\n",
"perf_daily = res1.copy()\n",
"perf_daily['Accuracy'] = [get_acc(t, p) for t, p in zip(perf_daily['True'], perf_daily['Predicted'])]\n",
"perf_daily['HTML'] = [get_acc_html(t, p) for t, p in zip(perf_daily['True'], perf_daily['Predicted'])]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"perf_daily.tail(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_probas.loc[df_probas.index[0], 'NumObs']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_levels = pd.DataFrame(\n",
" index=['H2','H1','L1','L2'],\n",
" columns=['Level','BreakPct(100)','TouchPct(100)'],\n",
" data=[\n",
" [f\"{data['H2'].iloc[-1]:.2f}\",f\"{data['H2BreakPct'].iloc[-2]:.1%}\",f\"{data['H2TouchPct'].iloc[-2]:.1%}\"],\n",
" [f\"{data['H1'].iloc[-1]:.2f}\",f\"{data['H1BreakPct'].iloc[-2]:.1%}\",f\"{data['H1TouchPct'].iloc[-2]:.1%}\"],\n",
" [f\"{data['L1'].iloc[-1]:.2f}\",f\"{data['L1BreakPct'].iloc[-2]:.1%}\",f\"{data['L1TouchPct'].iloc[-2]:.1%}\"],\n",
" [f\"{data['L2'].iloc[-1]:.2f}\",f\"{data['L2BreakPct'].iloc[-2]:.1%}\",f\"{data['L2TouchPct'].iloc[-2]:.1%}\"]\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_levels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_performance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import plotly.graph_objs as go\n",
"\n",
"plot_data = perf_daily.merge(data[['Open','High','Low','Close']], left_index=True, right_index=True)\n",
"\n",
"df = plot_data.copy()\n",
"\n",
"y_min = df['Low'].tail(50).min() - 50\n",
"y_max = df['High'].tail(50).max()\n",
"\n",
"increasing_color = '#3399ff' # Blue\n",
"decreasing_color = '#ff5f5f' # Red \n",
"\n",
"# Create a candlestick trace\n",
"candlestick_trace = go.Candlestick(\n",
" x=df.index,\n",
" open=df['Open'],\n",
" high=df['High'],\n",
" low=df['Low'],\n",
" close=df['Close'],\n",
" increasing_fillcolor=increasing_color, # Color for increasing candles\n",
" increasing_line_color=increasing_color, # Color for increasing candles\n",
" decreasing_fillcolor=decreasing_color, # Color for decreasing candles\n",
" decreasing_line_color=decreasing_color, # Color for decreasing candles\n",
" name='OHLC Chart'\n",
")\n",
"\n",
"# Create a scatter trace for symbols (correct and incorrect)\n",
"scatter_trace = go.Scatter(\n",
" x=df.index,\n",
" y=df['Low'] * 0.995,\n",
" text=df['HTML'],\n",
" mode='text',\n",
" marker=dict(size=10),\n",
" textposition='bottom center',\n",
" name='Predictions'\n",
")\n",
"\n",
"# Create a layout with initial x-axis range for the last 30 candles\n",
"layout = go.Layout(\n",
" title='OHLC Chart with Predictions (Right/Wrong)',\n",
" xaxis=dict(title='Date', range=[df.index[-50], df.index[-1]]), # Set initial range to last 30 data points\n",
" yaxis=dict(title='Price', range=[y_min, y_max]),\n",
" xaxis_rangeslider_visible=False,\n",
" template='plotly_dark'\n",
")\n",
"\n",
"# Create a figure\n",
"fig = go.Figure(data=[candlestick_trace, scatter_trace], layout=layout)\n",
"\n",
"fig.update_xaxes(\n",
" rangebreaks=[\n",
" # NOTE: Below values are bound (not single values), ie. hide x to y\n",
" dict(bounds=[\"sat\", \"mon\"]), # hide weekends, eg. hide sat to before mon\n",
" # dict(bounds=[16, 9.5], pattern=\"hour\"), # hide hours outside of 9.30am-4pm\n",
" # dict(values=[\"2019-12-25\", \"2020-12-24\"]) # hide holidays (Christmas and New Year's, etc)\n",
" ]\n",
" )\n",
"\n",
"\n",
"# Show the figure (you can also save it as an HTML file)\n",
"fig.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"XXX"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import mplfinance as mpf\n",
"\n",
"# Sample data: assuming you already have df with OHLC data\n",
"# df['Date'] = df.index\n",
"# df.set_index('Date', inplace=True)\n",
"\n",
"# Determine the data range for the y-axis\n",
"# y_min = df['Low'].min()\n",
"# y_max = df['High'].max()\n",
"\n",
"df1 = df.tail(50)\n",
"\n",
"# Create a custom color map for increasing and decreasing candles\n",
"mc = mpf.make_marketcolors(\n",
" up='#3399ff', # Blue for increasing candles\n",
" down='#ff5f5f', # Red for decreasing candles\n",
" inherit=True\n",
")\n",
"\n",
"s = mpf.make_mpf_style(marketcolors=mc,\n",
" gridcolor='#333333', # Set grid color to match dark background\n",
" facecolor='#222222', # Set background color to match dark background\n",
" edgecolor='#222222' # Set edge color to match dark background)\n",
")\n",
"\n",
"correct = np.where(df1['HTML']=='✅',1, np.nan) * df1['Low'] * 0.997\n",
"mids = np.where(df1['HTML']=='⬜',1, np.nan) * df1['Low'] * 0.997\n",
"wrongs = np.where(df1['HTML']=='❌',1, np.nan) * df1['Low'] * 0.997\n",
"\n",
"\n",
"# Create an addplot for annotations\n",
"add_plot = mpf.make_addplot(correct, color='green', sc='green')\n",
"add_mids = mpf.make_addplot(mids, color='yellow', marker='green')\n",
"add_wrongs = mpf.make_addplot(wrongs, color='red', marker='green')\n",
"\n",
"# Create an OHLC chart with mplfinance\n",
"mpf.plot(df1, type='candle', style='binance', title='OHLC Chart with Predictions (Right/Wrong)', ylabel='Price',\n",
" # hlines=dict(hlines=[y_min, y_max], colors=['gray', 'gray']), # Add horizontal lines for y-axis range\n",
" xrotation=0, # Rotate x-axis labels if needed\n",
" figratio=(10, 6), # Adjust the figure size as needed\n",
" volume=False,\n",
" addplot = [add_plot, add_mids, add_wrongs]) # Disable volume bars\n",
"\n",
"\n",
"# Show the chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mpf.available_fonts()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"perf_daily1 = perf_daily.merge(data['ClosePct'], left_index=True, right_index=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res2 = res1.merge(data[['ClosePct','HighPct','LowPct']], left_index=True, right_index=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int_labels = ['(-β, .20]', '(.20, .40]', '(.40, .60]', '(.60, .80]', '(.80, β]']\n",
"df_probas = res2.groupby(pd.qcut(res2['Predicted'],7)).agg({'True':[np.mean,len,np.sum],'ClosePct':[np.mean, np.std], 'HighPct':[np.mean], 'LowPct':[np.mean]})\n",
"# df_probas = res2.groupby(pd.cut(res2['Predicted'], bins = [-np.inf, 0.2, 0.4, 0.6, 0.8, np.inf], labels = int_labels)).agg({'True':[np.mean,len,np.sum],'ClosePct':[np.mean], 'HighPct':[np.mean], 'LowPct':[np.mean]})\n",
"df_probas.columns = ['PctGreen','NumObs','NumGreen','AvgPerf','PerfStD','AvgHigh','AvgLow']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_probas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mean_predicted_value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res2['Quantile'] = pd.cut(res2['Predicted'], bins = [-np.inf, 0.2, 0.4, 0.6, 0.8, np.inf], labels = int_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# Assuming you have a DataFrame 'res2' with the columns 'Quantile' and 'ClosePct'\n",
"# Assuming you have a list 'int_labels' containing the unique values for 'Quantile'\n",
"\n",
"# Create a 2x3 grid of subplots\n",
"fig, axs = plt.subplots(2, 3, figsize=(15, 8))\n",
"\n",
"# Loop through the 'int_labels' and plot the histograms in each subplot\n",
"for i, lbl in enumerate(int_labels):\n",
" # Get the subplot position based on the index i\n",
" row = i // 3\n",
" col = i % 3\n",
" \n",
" # Filter the DataFrame based on the specified value\n",
" data_subset = res2.loc[res2['Quantile'] == lbl, 'LowPct']\n",
" \n",
" # Plot the histogram in the corresponding subplot\n",
" axs[row, col].hist(data_subset)\n",
" axs[row, col].set_title(lbl)\n",
"\n",
"# Add some space between the subplots\n",
"plt.tight_layout()\n",
"\n",
"# Show the plot\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Investigate EM\n",
"data['VIX_EM'] = data['Close'] * (data['Close_VIX']/100) * (np.sqrt( 1 ) / np.sqrt(252))\n",
"data['VIX_EM_High'] = data['Close'] + data['VIX_EM']\n",
"data['VIX_EM_Low'] = data['Close'] - data['VIX_EM']\n",
"\n",
"data['VIX_EM_125'] = data['VIX_EM'] * 1.25\n",
"data['VIX_EM_125_High'] = data['Close'] + data['VIX_EM_125']\n",
"data['VIX_EM_125_Low'] = data['Close'] - data['VIX_EM_125']\n",
"\n",
"data['VIX_EM_15'] = data['VIX_EM'] * 1.5\n",
"data['VIX_EM_15_High'] = data['Close'] + data['VIX_EM_15']\n",
"data['VIX_EM_15_Low'] = data['Close'] - data['VIX_EM_15']\n",
"\n",
"data['VIX_EM'] = data['VIX_EM'].shift(1)\n",
"data['VIX_EM_High'] = data['VIX_EM_High'].shift(1)\n",
"data['VIX_EM_Low'] = data['VIX_EM_Low'].shift(1)\n",
"\n",
"data['VIX_EM_15'] = data['VIX_EM_15'].shift(1)\n",
"data['VIX_EM_15_High'] = data['VIX_EM_15_High'].shift(1)\n",
"data['VIX_EM_15_Low'] = data['VIX_EM_15_Low'].shift(1)\n",
"\n",
"data['VIX_EM_125'] = data['VIX_EM_125'].shift(1)\n",
"data['VIX_EM_125_High'] = data['VIX_EM_125_High'].shift(1)\n",
"data['VIX_EM_125_Low'] = data['VIX_EM_125_Low'].shift(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data[['VIX_EM','VIX_EM_15','VIX_EM_15_High','Close']]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often did price close within EM?\n",
"len(data.query('Close <= VIX_EM_High & Close >= VIX_EM_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often was EM tested?\n",
"len(data.query('High > VIX_EM_High | Low < VIX_EM_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often did price close within EM?\n",
"len(data.query('Close <= VIX_EM_125_High & Close >= VIX_EM_125_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often was EM tested?\n",
"len(data.query('High > VIX_EM_125_High | Low < VIX_EM_125_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often did price close within EM?\n",
"len(data.query('Close <= VIX_EM_15_High & Close >= VIX_EM_15_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# How often was EM tested?\n",
"len(data.query('High > VIX_EM_15_High | Low < VIX_EM_15_Low')) / len(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py39",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|