{"id":22546306,"url":"https://github.com/linsanity03/algorithmic_trading","last_synced_at":"2026-05-04T12:32:45.278Z","repository":{"id":264637250,"uuid":"893915346","full_name":"LINSANITY03/Algorithmic_trading","owner":"LINSANITY03","description":"Trading strategy based on the intersection of short-term and long-term moving averages.","archived":false,"fork":false,"pushed_at":"2024-12-03T23:08:30.000Z","size":56960,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T08:45:18.512Z","etag":null,"topics":["jupyter-notebook","matplotlib-pyplot","numpy","pandas"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LINSANITY03.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-25T12:36:37.000Z","updated_at":"2024-12-03T23:08:34.000Z","dependencies_parsed_at":"2025-02-02T09:26:49.564Z","dependency_job_id":"119115d3-d374-4c79-91da-396254bfffff","html_url":"https://github.com/LINSANITY03/Algorithmic_trading","commit_stats":null,"previous_names":["linsanity03/algorithmic_trading"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LINSANITY03%2FAlgorithmic_trading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LINSANITY03%2FAlgorithmic_trading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LINSANITY03%2FAlgorithmic_trading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LINSANITY03%2FAlgorithmic_trading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LINSANITY03","download_url":"https://codeload.github.com/LINSANITY03/Algorithmic_trading/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245999321,"owners_count":20707554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jupyter-notebook","matplotlib-pyplot","numpy","pandas"],"created_at":"2024-12-07T15:06:52.367Z","updated_at":"2026-05-04T12:32:45.218Z","avatar_url":"https://github.com/LINSANITY03.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moving Average Crossover Strategy\n\nCreate a trading strategy based on the intersection of short-term and long-term moving averages.\n\n## 1. Fetch Historical Data\n\nWe are using the price data from a popular stock [yfinance](https://github.com/ranaroussi/yfinance).\nThe dataset typically includes:\n\nTicker: Represents the stock symbol (e.g., \"AAPL\" for Apple Inc.).\nOpen: Price of the stock when the market opened for the day.\nHigh: Highest price of the stock traded during the day.\nLow: Lowest price of the stock traded during the day.\nClose: Price of the stock when the market closed for the day.\nAdj Close: A version of closing price accounting other factor affecting the stock value.\nVolume: The number of shares traded during the time period.\n\n## 2. Calculate short-long moving average.\n\nTest various short/long moving average window combinations for better output. For this instance, \nwe use 5 and 17 days respectively. (To change the value check **config.yaml** file)\n\n```\n    data[\"Short_MA\"] = data[\"Close\"].rolling(window=short_window).mean()\n    data[\"Long_MA\"] = data[\"Close\"].rolling(window=long_window).mean()\n```\n\n## 3. Implement trading strategy.\n\nImplement a moving average crossover strategy for a single stock. \n\n```\n    # Generate buy/sell signals\n    data['Signal'] = 0\n    data.loc[data['Short_MA'] \u003e data['Long_MA'], 'Signal'] = 1  # Buy signal\n    data.loc[data['Short_MA'] \u003c= data['Long_MA'], 'Signal'] = -1  # Sell signal\n\n    # Identify when trades occur\n    data['Trade'] = data['Signal'].diff().fillna(0).abs()\n```\n\n## 4. Simulate Trades\n\nBacktest the strategy by simulating trades based on signals.\n\n```\n\n    # Backtest the strategy\n    data['Daily_Return'] = data['Close'].pct_change()\n    data['Strategy_Return'] = data['Signal'].shift(1) * data['Daily_Return']  # Lag signal by 1 day\n\n    # Deduct transaction costs\n    data['Strategy_Return_with_Costs'] = data['Strategy_Return'] - (data['Trade'] * transaction_cost)\n\n    # Cumulative returns\n    data['Cumulative_Market_Return'] = (1 + data['Daily_Return']).cumprod()\n    data['Cumulative_Strategy_Return'] = (1 + data['Strategy_Return']).cumprod()\n    data['Cumulative_Strategy_Return_with_Costs'] = (1 + data['Strategy_Return_with_Costs']).cumprod()\n```\n\n## 5. Evaluate Performance\n\nCalculate key metrics to assess the strategy.\n\n```\n    sharpe_ratio_with_costs = data['Strategy_Return_with_Costs'].mean() / data['Strategy_Return_with_Costs'].std() * (252**0.5)\n\n    print(f\"Sharpe Ratio (With Costs): {sharpe_ratio_with_costs:.2f}\")\n```\n\n## 6. Visualize Results\n\nWe use matplotlib to visualize stock price with short and long moving average.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"results/stock_price.png\" alt=\"Algorithm_game\" height=\"360\" width=\"640\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"results/cumulative_returns.png\" alt=\"Algorithm_game\" height=\"360\" width=\"640\"\u003e\n\u003c/p\u003e\n\n## Collaboration\n\nFeel free to use this codebase for your projects. If you want to talk more about this project or have found bugs, create a pull request or contact me on **pujantamang92@gmail.com**.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinsanity03%2Falgorithmic_trading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinsanity03%2Falgorithmic_trading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinsanity03%2Falgorithmic_trading/lists"}