-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Estratégia AlphaTrader Long Biased 2
- Loading branch information
1 parent
de21dba
commit a374059
Showing
24 changed files
with
886 additions
and
58 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
alpha_trader/src/__pycache__/abstractStrategy.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+3.83 KB
(120%)
alpha_trader/src/__pycache__/tradingPerformance.cpython-311.pyc
Binary file not shown.
Binary file modified
BIN
+15.5 KB
(390%)
alpha_trader/src/__pycache__/tradingStrategies.cpython-311.pyc
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from tradingStrategies import BuyNHold, AlphaTraderLongBiased, AlphaTraderLongBiased2, AFT01 " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"bnh = BuyNHold(1000, '2020-01-01', '2024-06-1')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"test = AlphaTraderLongBiased2(1000, '2020-01-01', '2024-12-17')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"test.data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"# Assume self.data with a DatetimeIndex and 'position' column exists\n", | ||
"data = test.data.copy()\n", | ||
"\n", | ||
"# Define masks for each type of position\n", | ||
"is_cash = (data['position'] == 0)\n", | ||
"is_long = (data['position'] > 0)\n", | ||
"is_short = (data['position'] < 0)\n", | ||
"\n", | ||
"def get_durations(mask, index):\n", | ||
" \"\"\"\n", | ||
" Given a boolean mask (e.g., is_cash) and a DatetimeIndex,\n", | ||
" find consecutive stretches of True and return their durations in days.\n", | ||
" \"\"\"\n", | ||
" int_mask = mask.astype(int)\n", | ||
" # Start points where we go from False to True\n", | ||
" starts = (int_mask.diff() == 1)\n", | ||
" # End points where we go from True to False\n", | ||
" ends = (int_mask.diff() == -1)\n", | ||
" \n", | ||
" starts_idx = index[starts]\n", | ||
" ends_idx = index[ends]\n", | ||
" \n", | ||
" # Handle edge cases:\n", | ||
" # If we start already in True condition at the beginning\n", | ||
" if mask.iloc[0]:\n", | ||
" starts_idx = starts_idx.insert(0, index[0])\n", | ||
" \n", | ||
" # If we end still in True condition at the end\n", | ||
" if mask.iloc[-1]:\n", | ||
" ends_idx = ends_idx.append(pd.Index([index[-1]]))\n", | ||
" \n", | ||
" min_len = min(len(starts_idx), len(ends_idx))\n", | ||
" starts_idx = starts_idx[:min_len]\n", | ||
" ends_idx = ends_idx[:min_len]\n", | ||
" \n", | ||
" durations = []\n", | ||
" for s, e in zip(starts_idx, ends_idx):\n", | ||
" if e > s:\n", | ||
" duration_days = (e - s).total_seconds() / (3600 * 24)\n", | ||
" durations.append(duration_days)\n", | ||
" return durations\n", | ||
"\n", | ||
"# Get durations for each position type\n", | ||
"cash_durations = get_durations(is_cash, data.index)\n", | ||
"long_durations = get_durations(is_long, data.index)\n", | ||
"short_durations = get_durations(is_short, data.index)\n", | ||
"\n", | ||
"# Plot histograms on the same figure with different colors\n", | ||
"plt.figure(figsize=(10, 6))\n", | ||
"plt.hist(cash_durations, bins=30, alpha=0.5, label='Cash Periods', color='blue', edgecolor='black')\n", | ||
"#plt.hist(long_durations, bins=30, alpha=0.5, label='Long Periods', color='green', edgecolor='black')\n", | ||
"#plt.hist(short_durations, bins=30, alpha=0.5, label='Short Periods', color='red', edgecolor='black')\n", | ||
"\n", | ||
"plt.title('Histogram of Durations by Position Type')\n", | ||
"plt.xlabel('Duration (Days)')\n", | ||
"plt.ylabel('Frequency')\n", | ||
"plt.legend()\n", | ||
"plt.grid(True)\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"fig, ax1 = plt.subplots(figsize=(10, 6))\n", | ||
"\n", | ||
"# Plot the first dataset on the primary y-axis\n", | ||
"ax1.plot(test.data['net_worth'], color='blue', label='Context')\n", | ||
"ax1.set_ylabel('Delta', color='blue')\n", | ||
"ax1.tick_params(axis='y', labelcolor='blue')\n", | ||
"\n", | ||
"# Create a second y-axis\n", | ||
"ax2 = ax1.twinx() \n", | ||
"# Plot the second dataset on the secondary y-axis\n", | ||
"ax2.plot(test.data['context'], color='red', label='Close')\n", | ||
"ax2.set_ylabel('Close', color='red')\n", | ||
"ax2.tick_params(axis='y', labelcolor='red')\n", | ||
"\n", | ||
"# Optionally, set titles and grid\n", | ||
"plt.title('Dual Axis Plot')\n", | ||
"ax1.grid()\n", | ||
"\n", | ||
"# Show the plot\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"fig, ax1 = plt.subplots(figsize=(10, 6))\n", | ||
"\n", | ||
"# Plot the first dataset on the primary y-axis\n", | ||
"ax1.plot(test.data['diff'], color='blue', label='Context')\n", | ||
"ax1.set_ylabel('Delta', color='blue')\n", | ||
"ax1.tick_params(axis='y', labelcolor='blue')\n", | ||
"\n", | ||
"# Create a second y-axis\n", | ||
"ax2 = ax1.twinx() \n", | ||
"# Plot the second on the secondary y-axis\n", | ||
"ax2.plot(test.data['close'], color='red', label='Close')\n", | ||
"ax2.set_ylabel('Close', color='red')\n", | ||
"ax2.tick_params(axis='y', labelcolor='red')\n", | ||
"\n", | ||
"# Optionally, set titles and grid\n", | ||
"plt.title('Dual Axis Plot')\n", | ||
"ax1.grid()\n", | ||
"\n", | ||
"# Show the plot\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "venv", | ||
"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.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.