diff --git a/site/en/tutorials/quickstart/beginner.ipynb b/site/en/tutorials/quickstart/beginner.ipynb new file mode 100644 index 00000000000..441e988114d --- /dev/null +++ b/site/en/tutorials/quickstart/beginner.ipynb @@ -0,0 +1,758 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rX8mhOLljYeM" + }, + "source": [ + "##### Copyright 2019 The TensorFlow Authors." + ] + }, + { + "cell_type": "code", + "source": [ + "import tensorflow as tf\n", + "print(\"TensorFlow version:\", tf.__version__)\n", + "\n", + "import pandas as pd\n", + "import yfinance as yf\n", + "import numpy as np\n", + "from sklearn.ensemble import RandomForestRegressor\n", + "from sklearn.metrics import mean_squared_error\n", + "import matplotlib.pyplot as plt\n", + "\n", + "\n", + "# Define stock universe\n", + "stock_universe = ['AAPL', 'GOOG', 'MSFT', 'AMZN', 'FB']\n", + "\n", + "\n", + "def calculate_technical_indicators(stock_data):\n", + " # Iterate over each stock in the stock universe\n", + " for stock in stock_universe:\n", + " # Calculate technical indicators for each stock individually\n", + " stock_data[stock, 'ma_50'] = stock_data[stock]['Close'].rolling(window=50).mean()\n", + " stock_data[stock, 'ma_200'] = stock_data[stock]['Close'].rolling(window=200).mean()\n", + " stock_data[stock, 'rsi'] = stock_data[stock]['Close'].pct_change().rolling(window=14).apply(lambda x: x.ewm(com=13-1, adjust=False).std())\n", + " stock_data[stock, 'bb_20'] = stock_data[stock]['Close'].rolling(window=20).mean() + 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " stock_data[stock, 'bb_2'] = stock_data[stock]['Close'].rolling(window=20).mean() - 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " return stock_data\n", + "\n", + "\n", + "# Define machine learning algorithm\n", + "def predict_stock_prices(stock_data):\n", + " X = stock_data[['ma_50', 'ma_200', 'rsi', 'bb_20', 'bb_2']]\n", + " y = stock_data['Close']\n", + " model = RandomForestRegressor(n_estimators=100, random_state=42)\n", + " model.fit(X, y)\n", + " predictions = model.predict(X)\n", + " return predictions\n", + "\n", + "\n", + "# Define risk management system\n", + "def risk_management(stock_data, predictions):\n", + " position_size = 0.01\n", + " stop_loss = 0.05\n", + " take_profit = 0.10\n", + " for i in range(len(stock_data)):\n", + " if predictions[i] > stock_data['Close'].iloc[i]:\n", + " # Buy signal\n", + " stock_data.loc[i, 'position'] = position_size\n", + " stock_data.loc[i, 'stop_loss'] = stock_data['Close'].iloc[i] - stop_loss\n", + " stock_data.loc[i, 'take_profit'] = stock_data['Close'].iloc[i] + take_profit\n", + " elif predictions[i] < stock_data['Close'].iloc[i]:\n", + " # Sell signal\n", + " stock_data.loc[i, 'position'] = -position_size\n", + " stock_data.loc[i, 'stop_loss'] = stock_data['Close'].iloc[i] + stop_loss\n", + " stock_data.loc[i, 'take_profit'] = stock_data['Close'].iloc[i] - take_profit\n", + " return stock_data\n", + "\n", + "\n", + "# Define portfolio optimization algorithm\n", + "def portfolio_optimization(stock_data):\n", + " expected_returns = stock_data['Close'].pct_change().mean()\n", + " covariance_matrix = stock_data['Close'].pct_change().cov()\n", + " weights = np.array([0.2, 0.2, 0.2, 0.2, 0.2])\n", + " portfolio_return = np.sum(expected_returns * weights)\n", + " portfolio_volatility = np.sqrt(np.dot(weights.T, np.dot(covariance_matrix, weights)))\n", + " return portfolio_return, portfolio_volatility\n", + "\n", + "\n", + "# Main function\n", + "def main():\n", + " stock_data = yf.download(stock_universe, start='2020-01-01', end='2022-02-26')\n", + " stock_data = calculate_technical_indicators(stock_data)\n", + " predictions = predict_stock_prices(stock_data)\n", + " stock_data = risk_management(stock_data, predictions)\n", + " portfolio_return, portfolio_volatility = portfolio_optimization(stock_data)\n", + " print('Portfolio Return:', portfolio_return)\n", + " print('Portfolio Volatility:', portfolio_volatility)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n", + " #function calculate_technical_indicators(stock_data):\n", + " # Iterate over each stock in the stock universe\n", + " for stock in stock_universe:\n", + " # Calculate technical indicators for each stock individually\n", + " stock_data[stock, 'ma_50'] = stock_data[stock]['Close'].rolling(window=50).mean()\n", + " stock_data[stock, 'ma_200'] = stock_data[stock]['Close'].rolling(window=200).mean()\n", + " stock_data[stock, 'rsi'] = stock_data[stock]['Close'].pct_change().rolling(window=14).apply(lambda x: x.ewm(com=13-1, adjust=False).std())\n", + " stock_data[stock, 'bb_20'] = stock_data[stock]['Close'].rolling(window=20).mean() + 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " stock_data[stock, 'bb_2'] = stock_data[stock]['Close'].rolling(window=20).mean() - 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " return stock_data\n", + "\n", + "\n", + "\n" + ], + "metadata": { + "id": "lfJKxCb95srA", + "outputId": "9ab9f3f4-56d0-4620-f931-7fda7fdb3b7e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 668 + } + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "[*********************100%***********************] 5 of 5 completed\n", + "ERROR:yfinance:\n", + "1 Failed download:\n", + "ERROR:yfinance:['FB']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TensorFlow version: 2.17.1\n" + ] + }, + { + "output_type": "error", + "ename": "KeyError", + "evalue": "'AAPL'", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3804\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3805\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcasted_key\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3806\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mindex.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mindex.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyError\u001b[0m: 'AAPL'", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 80\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalculate_technical_indicators\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;31m# Iterate over each stock in the stock universe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0mstock_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0myf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdownload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_universe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'2020-01-01'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'2022-02-26'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m \u001b[0mstock_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcalculate_technical_indicators\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 71\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpredict_stock_prices\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[0mstock_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrisk_management\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpredictions\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mcalculate_technical_indicators\u001b[0;34m(stock_data)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mstock\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mstock_universe\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;31m# Calculate technical indicators for each stock individually\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ma_50'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Close'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m50\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ma_200'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Close'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rsi'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstock_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstock\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Close'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpct_change\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mewm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcom\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m13\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madjust\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 4099\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_single_key\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4100\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4101\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4102\u001b[0m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4103\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m_getitem_multilevel\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 4157\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4158\u001b[0m \u001b[0;31m# self.columns is a MultiIndex\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4159\u001b[0;31m \u001b[0mloc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4160\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mslice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4161\u001b[0m \u001b[0mnew_columns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/indexes/multi.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3038\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3039\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3040\u001b[0;31m \u001b[0mloc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_level_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3041\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_maybe_to_slice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3042\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/indexes/multi.py\u001b[0m in \u001b[0;36m_get_level_indexer\u001b[0;34m(self, key, level, indexer)\u001b[0m\n\u001b[1;32m 3389\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3390\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3391\u001b[0;31m \u001b[0midx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_loc_single_level_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlevel_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3392\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3393\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlevel\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_lexsort_depth\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/indexes/multi.py\u001b[0m in \u001b[0;36m_get_loc_single_level_index\u001b[0;34m(self, level_index, key)\u001b[0m\n\u001b[1;32m 2978\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2979\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2980\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mlevel_index\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2981\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2982\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3810\u001b[0m ):\n\u001b[1;32m 3811\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mInvalidIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3812\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3813\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3814\u001b[0m \u001b[0;31m# If we have a listlike key, _check_indexing_error will raise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyError\u001b[0m: 'AAPL'" + ] + } + ] + }, + { + "source": [ + "def calculate_technical_indicators(stock_data):\n", + " # Iterate over each stock in the stock universe\n", + " for stock in stock_universe:\n", + " # Calculate technical indicators for each stock individually\n", + " stock_data[stock, 'ma_50'] = stock_data[stock]['Close'].rolling(window=50).mean()\n", + " stock_data[stock, 'ma_200'] = stock_data[stock]['Close'].rolling(window=200).mean()\n", + " stock_data[stock, 'rsi'] = stock_data[stock]['Close'].pct_change().rolling(window=14).apply(lambda x: x.ewm(com=13-1, adjust=False).std())\n", + " stock_data[stock, 'bb_20'] = stock_data[stock]['Close'].rolling(window=20).mean() + 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " stock_data[stock, 'bb_2'] = stock_data[stock]['Close'].rolling(window=20).mean() - 2 * stock_data[stock]['Close'].rolling(window=20).std()\n", + " return stock_data" + ], + "cell_type": "code", + "metadata": { + "id": "7IHBPpHJ7RNV" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "BZSlp3DAjdYf" + }, + "outputs": [], + "source": [ + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3wF5wszaj97Y" + }, + "source": [ + "# TensorFlow 2 quickstart for beginners" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DUNzJc4jTj6G" + }, + "source": [ + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " View on TensorFlow.org\n", + " \n", + " Run in Google Colab\n", + " \n", + " View source on GitHub\n", + " \n", + " Download notebook\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "04QgGZc9bF5D" + }, + "source": [ + "This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:\n", + "\n", + "1. Load a prebuilt dataset.\n", + "1. Build a neural network machine learning model that classifies images.\n", + "2. Train this neural network.\n", + "3. Evaluate the accuracy of the model." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hiH7AC-NTniF" + }, + "source": [ + "This tutorial is a [Google Colaboratory](https://colab.research.google.com/notebooks/welcome.ipynb) notebook. Python programs are run directly in the browser—a great way to learn and use TensorFlow. To follow this tutorial, run the notebook in Google Colab by clicking the button at the top of this page.\n", + "\n", + "1. In Colab, connect to a Python runtime: At the top-right of the menu bar, select *CONNECT*.\n", + "2. To run all the code in the notebook, select **Runtime** > **Run all**. To run the code cells one at a time, hover over each cell and select the **Run cell** icon.\n", + "\n", + "![Run cell icon](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/quickstart/images/beginner/run_cell_icon.png?raw=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nnrWf3PCEzXL" + }, + "source": [ + "## Set up TensorFlow\n", + "\n", + "Import TensorFlow into your program to get started:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "0trJmd6DjqBZ", + "outputId": "02b38c02-35f0-4e52-f3a3-4888ec43a0b1", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TensorFlow version: 2.17.1\n" + ] + } + ], + "source": [ + "import tensorflow as tf\n", + "print(\"TensorFlow version:\", tf.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7NAbSZiaoJ4z" + }, + "source": [ + "If you are following along in your own development environment, rather than [Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/quickstart/beginner.ipynb), see the [install guide](https://www.tensorflow.org/install) for setting up TensorFlow for development.\n", + "\n", + "Note: Make sure you have upgraded to the latest `pip` to install the TensorFlow 2 package if you are using your own development environment. See the [install guide](https://www.tensorflow.org/install) for details.\n", + "\n", + "## Load a dataset\n", + "\n", + "Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. Scale these values to a range of 0 to 1 by dividing the values by `255.0`. This also converts the sample data from integers to floating-point numbers:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "7FP5258xjs-v" + }, + "outputs": [], + "source": [ + "mnist = tf.keras.datasets.mnist\n", + "\n", + "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", + "x_train, x_test = x_train / 255.0, x_test / 255.0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BPZ68wASog_I" + }, + "source": [ + "## Build a machine learning model\n", + "\n", + "Build a `tf.keras.Sequential` model:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "h3IKyzTCDNGo" + }, + "outputs": [], + "source": [ + "model = tf.keras.models.Sequential([\n", + " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", + " tf.keras.layers.Dense(128, activation='relu'),\n", + " tf.keras.layers.Dropout(0.2),\n", + " tf.keras.layers.Dense(10)\n", + "])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "l2hiez2eIUz8" + }, + "source": [ + "[`Sequential`](https://www.tensorflow.org/guide/keras/sequential_model) is useful for stacking layers where each layer has one input [tensor](https://www.tensorflow.org/guide/tensor) and one output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are composed of layers. This model uses the [`Flatten`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten), [`Dense`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense), and [`Dropout`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout) layers.\n", + "\n", + "For each example, the model returns a vector of [logits](https://developers.google.com/machine-learning/glossary#logits) or [log-odds](https://developers.google.com/machine-learning/glossary#log-odds) scores, one for each class." + ] + }, + { + "cell_type": "code", + "source": [ + "\"UnknownTimeZoneError\"\n" + ], + "metadata": { + "collapsed": true, + "id": "KQi20OlI_ZxL", + "outputId": "1465894a-0ba5-4e61-9708-38c8d941eb81", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + } + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'UnknownTimeZoneError'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "OeOrNdnkEEcR", + "outputId": "622b44ff-4558-4db8-ad45-85e4c890a3c4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 158 + } + }, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'model' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mpredictions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'model' is not defined" + ] + } + ], + "source": [ + "predictions = model(x_train[:1]).numpy()\n", + "predictions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tgjhDQGcIniO" + }, + "source": [ + "The `tf.nn.softmax` function converts these logits to *probabilities* for each class:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "zWSRnQ0WI5eq" + }, + "outputs": [], + "source": [ + "tf.nn.softmax(predictions).numpy()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "he5u_okAYS4a" + }, + "source": [ + "Note: It is possible to bake the `tf.nn.softmax` function into the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it's impossible to provide an exact and numerically stable loss calculation for all models when using a softmax output." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hQyugpgRIyrA" + }, + "source": [ + "Define a loss function for training using `losses.SparseCategoricalCrossentropy`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "RSkzdv8MD0tT" + }, + "outputs": [], + "source": [ + "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SfR4MsSDU880" + }, + "source": [ + "The loss function takes a vector of ground truth values and a vector of logits and returns a scalar loss for each example. This loss is equal to the negative log probability of the true class: The loss is zero if the model is sure of the correct class.\n", + "\n", + "This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to `-tf.math.log(1/10) ~= 2.3`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "NJWqEVrrJ7ZB" + }, + "outputs": [], + "source": [ + "loss_fn(y_train[:1], predictions).numpy()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ada44eb947d4" + }, + "source": [ + "Before you start training, configure and compile the model using Keras `Model.compile`. Set the [`optimizer`](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) class to `adam`, set the `loss` to the `loss_fn` function you defined earlier, and specify a metric to be evaluated for the model by setting the `metrics` parameter to `accuracy`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "9foNKHzTD2Vo" + }, + "outputs": [], + "source": [ + "model.compile(optimizer='adam',\n", + " loss=loss_fn,\n", + " metrics=['accuracy'])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ix4mEL65on-w" + }, + "source": [ + "## Train and evaluate your model\n", + "\n", + "Use the `Model.fit` method to adjust your model parameters and minimize the loss:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "y7suUbJXVLqP" + }, + "outputs": [], + "source": [ + "model.fit(x_train, y_train, epochs=5)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4mDAAPFqVVgn" + }, + "source": [ + "The `Model.evaluate` method checks the model's performance, usually on a [validation set](https://developers.google.com/machine-learning/glossary#validation-set) or [test set](https://developers.google.com/machine-learning/glossary#test-set)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "F7dTAzgHDUh7" + }, + "outputs": [], + "source": [ + "model.evaluate(x_test, y_test, verbose=2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T4JfEh7kvx6m" + }, + "source": [ + "The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the [TensorFlow tutorials](https://www.tensorflow.org/tutorials/)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Aj8NrlzlJqDG" + }, + "source": [ + "If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "rYb6DrEH0GMv" + }, + "outputs": [], + "source": [ + "probability_model = tf.keras.Sequential([\n", + " model,\n", + " tf.keras.layers.Softmax()\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "cnqOZtUp1YR_" + }, + "outputs": [], + "source": [ + "probability_model(x_test[:5])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-47O6_GLdRuT" + }, + "source": [ + "## Conclusion\n", + "\n", + "Congratulations! You have trained a machine learning model using a prebuilt dataset using the [Keras](https://www.tensorflow.org/guide/keras/overview) API.\n", + "\n", + "For more examples of using Keras, check out the [tutorials](https://www.tensorflow.org/tutorials/keras/). To learn more about building models with Keras, read the [guides](https://www.tensorflow.org/guide/keras). If you want learn more about loading and preparing data, see the tutorials on [image data loading](https://www.tensorflow.org/tutorials/load_data/images) or [CSV data loading](https://www.tensorflow.org/tutorials/load_data/csv).\n" + ] + } + ], + "metadata": { + "colab": { + "name": "beginner.ipynb", + "toc_visible": true, + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} +pip install pytz +pip install pandas +import datetime +import pytz +import pandas as pd # For pandas example + +# 1. Define the timezone of your data source. +exchange_timezone = pytz.timezone('America/New_York') # Example: New York Stock Exchange + +# 2. Suppose you have a datetime object (e.g., a time from your exchange data) +exchange_datetime = datetime.datetime(2023, 10, 27, 10, 30, 0) # 10:30 AM in the exchange's timezone + +# 3. Localize your timestamp using the exchange timezone +localized_datetime = exchange_timezone.localize(exchange_datetime) +print(f"Exchange Time (localized): {localized_datetime}") + +# 4. Convert to UTC: +utc_datetime = localized_datetime.astimezone(pytz.utc) +print(f"UTC Time: {utc_datetime}") + +# 5. Example: Convert to user's local time (system timezone): +local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo +local_datetime = utc_datetime.astimezone(local_timezone) +print(f"Local Time: {local_datetime}") + + +# Example using pandas + +df = pd.DataFrame({'time': [exchange_datetime] }) + +# Convert the column to timestamps and then localize +df['time'] = pd.to_datetime(df['time']) + +df['time'] = df['time'].dt.tz_localize(exchange_timezone) + +print("\nDataFrame after localizing column:\n", df['time']) + +df['utc_time'] = df['time'].dt.tz_convert(pytz.utc) +print("\nDataFrame after converting to UTC:\n", df['utc_time']) + +df['local_time'] = df['utc_time'].dt.tz_convert(local_timezone) +print("\nDataFrame after converting to local time:\n", df['local_time']) +Use code with caution. +Python +Explanation + +pytz.timezone('America/New_York'): Creates a timezone object for the New York Stock Exchange. Important: Always look up the correct timezone identifier for your specific stock exchange data. You can find lists of timezone identifiers on the pytz documentation and online. + +localize(exchange_datetime): Tells Python that the exchange_datetime was in the exchange_timezone. + +astimezone(pytz.utc): Converts the localized datetime to UTC, which is good for storage and internal processing. + +astimezone(datetime.timezone.utc).astimezone().tzinfo: Used to get the current user's timezone information. + +astimezone(local_timezone): Converts the UTC timestamp to the desired output timezone (in this case, user's local). + +Pandas time series: When using pandas, .dt.tz_localize() converts the timestamp object into one with the timezone information and .dt.tz_convert() converts the timestamp object into the target time zone (UTC or user's local). + +Integrating Timezone Handling into Your Program + +Data Acquisition: When loading or receiving time-related data, identify the source timezone and immediately apply the localize function to make sure the datetime object has timezone information. + +Core Logic: Once localized, convert timestamps to UTC for internal calculations, filtering, and aggregation. + +Reporting/Display: Before presenting any results to the user, convert the UTC timestamps to your target timezone. + +Configuration: Your program could allow users to specify their preferred timezones for display (e.g., through command line arguments or a configuration file). + +Be Explicit: Always be clear about timezones when working with time-related data. + +Important Tips + +Correct Timezone Identifiers: Double-check that you're using the right timezone identifiers. + +pytz vs datetime.timezone: pytz provides full timezone databases and handles daylight saving time (DST) more comprehensively. datetime.timezone can be good for simpler use cases but might not handle DST correctly. If your data involves time changes in specific places, pytz should be used instead of datetime.timezone. + +Pandas and Timezones: If you are working with time-series data, take full advantage of pandas built-in time zone functionality to simplify operations. + +Testing: Write unit tests to verify your timezone conversions are working correctly, especially around DST transitions. + +Example with Time Range Filtering + +import datetime +import pytz + +exchange_tz = pytz.timezone("America/New_York") + +# Mock historical data with some datetime objects +historical_data = [ + {"time": exchange_tz.localize(datetime.datetime(2023, 10, 27, 9, 0, 0)), "price": 150}, + {"time": exchange_tz.localize(datetime.datetime(2023, 10, 27, 10, 0, 0)), "price": 152}, + {"time": exchange_tz.localize(datetime.datetime(2023, 10, 27, 11, 0, 0)), "price": 154}, + {"time": exchange_tz.localize(datetime.datetime(2023, 10, 28, 9, 0, 0)), "price": 155}, + {"time": exchange_tz.localize(datetime.datetime(2023, 10, 28, 10, 0, 0)), "price": 157}, +] + +#Define the search window +start_time_local = datetime.datetime(2023, 10, 27, 10, 0, 0) +end_time_local = datetime.datetime(2023, 10, 28, 10, 0, 0) + +user_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo + +# Convert search window to UTC to compare with historical data +start_time_utc = user_timezone.localize(start_time_local).astimezone(pytz.utc) +end_time_utc = user_timezone.localize(end_time_local).astimezone(pytz.utc) + +#Filter data within range +filtered_data = [ + entry + for entry in historical_data + if start_time_utc <= entry["time"].astimezone(pytz.utc) <= end_time_utc +] + +for entry in filtered_data: + print(f"{entry['time'].astimezone(user_timezone)}: Price ${entry['price']}")