{"id":25452827,"url":"https://github.com/yllvar/orderbook-backtesting","last_synced_at":"2025-09-08T13:48:26.815Z","repository":{"id":182662709,"uuid":"668880881","full_name":"yllvar/orderbook-backtesting","owner":"yllvar","description":"This is a Kucoin Futures Order Book with SMA20 Backtesting ","archived":false,"fork":false,"pushed_at":"2023-07-20T20:22:27.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-04T00:00:50.488Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/yllvar.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}},"created_at":"2023-07-20T20:16:26.000Z","updated_at":"2024-01-30T04:13:33.000Z","dependencies_parsed_at":"2023-07-20T22:07:42.011Z","dependency_job_id":null,"html_url":"https://github.com/yllvar/orderbook-backtesting","commit_stats":null,"previous_names":["yllvar/orderbook-backtesting"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yllvar/orderbook-backtesting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yllvar%2Forderbook-backtesting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yllvar%2Forderbook-backtesting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yllvar%2Forderbook-backtesting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yllvar%2Forderbook-backtesting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yllvar","download_url":"https://codeload.github.com/yllvar/orderbook-backtesting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yllvar%2Forderbook-backtesting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274195679,"owners_count":25239147,"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","status":"online","status_checked_at":"2025-09-08T02:00:09.813Z","response_time":121,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-02-17T23:41:44.645Z","updated_at":"2025-09-08T13:48:26.773Z","avatar_url":"https://github.com/yllvar.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"Sure, here is a detailed manual on how to use and understand the backtesting code:\n\n## Backtesting Code Manual\n\n### Overview\nThe provided backtesting code is designed to simulate trading strategies on cryptocurrency futures data using order book imbalance as the entry signal. The code uses the Kucoin Futures API to fetch historical market data (OHLCV) and Level 3 order book data. It then analyzes the data, simulates trades, and evaluates the performance of the trading strategy.\n\n### Prerequisites\nTo use the backtesting code, you need to have the following prerequisites:\n1. Python 3.x installed on your system.\n2. The required Python packages: ccxt, pandas, numpy, matplotlib, seaborn, dotenv, itertools, tabulate, logging.\n\n### Setup\n1. Install the required Python packages by running the following command in your terminal:\n   ```\n   pip install ccxt pandas numpy matplotlib seaborn python-dotenv tabulate\n   ```\n\n2. Create a `.env` file in the same directory as the backtesting code. This file will store your Kucoin Futures API credentials. Add the following lines to the `.env` file, replacing `YOUR_API_KEY`, `YOUR_SECRET_KEY`, and `YOUR_PASSPHRASE` with your actual API credentials:\n   ```\n   API_KEY=YOUR_API_KEY\n   SECRET_KEY=YOUR_SECRET_KEY\n   PASSPHRASE=YOUR_PASSPHRASE\n   ```\n\n### Usage\nTo use the backtesting code, follow these steps:\n\n1. Import the required libraries and classes:\n   ```python\n   import os\n   import time\n   import ccxt\n   import pandas as pd\n   import numpy as np\n   import matplotlib.pyplot as plt\n   import seaborn as sns\n   from dotenv import load_dotenv\n   from itertools import product\n   from tabulate import tabulate\n   import logging\n   ```\n\n2. Load the API credentials from the `.env` file:\n   ```python\n   load_dotenv()\n   ```\n\n3. Define the `DataFetcher` class, which is responsible for fetching OHLCV and order book data:\n   ```python\n   class DataFetcher:\n       # ... (DataFetcher class implementation)\n   ```\n\n4. Define the `OrderBookAnalyzer` class, which contains the backtesting logic and performance metrics calculations:\n   ```python\n   class OrderBookAnalyzer:\n       # ... (OrderBookAnalyzer class implementation)\n   ```\n\n5. Define the `optimize_parameters` function, which optimizes the trading strategy parameters using a brute-force approach:\n   ```python\n   def optimize_parameters(symbol, exchange, limit_entry_params, initial_capital, threshold_positive_range,\n                           threshold_negative_range, time_criteria_range):\n       # ... (optimize_parameters function implementation)\n   ```\n\n6. Define the `setup_logger` function to set up logging for the backtesting process:\n   ```python\n   def setup_logger():\n       # ... (setup_logger function implementation)\n   ```\n\n7. Define the `main` function to run the backtesting process:\n   ```python\n   def main():\n       # ... (main function implementation)\n   ```\n\n8. Call the `main` function inside an if condition to ensure the script runs when executed directly:\n   ```python\n   if __name__ == '__main__':\n       main()\n   ```\n\n9. Initialize the required variables for backtesting, such as API credentials, symbol, timeframe, limit_entry_parameters, initial_capital, threshold ranges, and the logger:\n   ```python\n   # Define the time duration in seconds for the backtesting loop (e.g., 1 hour)\n   backtesting_duration = 3600\n\n   # Initialize the API credentials\n   api_key = os.getenv('API_KEY')\n   secret_key = os.getenv('SECRET_KEY')\n   passphrase = os.getenv('PASSPHRASE')\n\n   # Create an instance of the Kucoin Futures exchange\n   exchange = ccxt.kucoinfutures({\n       'apiKey': api_key,\n       'secret': secret_key,\n       'password': passphrase,\n       'enableRateLimit': True  # Adjust as needed\n   })\n\n   # Initialize the symbol, timeframe, and limit_entry_parameters\n   symbol = 'ETH/USDT:USDT'\n   timeframe = '5m'\n   limit_entry_parameters = {'ETH/USDT:USDT': {'limit': 1000}}\n\n   # Define the initial capital for the backtesting\n   initial_capital = 100\n\n   # Define the threshold ranges for the trading strategy\n   threshold_positive_range = [40, 50, 60]\n   threshold_negative_range = [-80, -75, -70]\n   time_criteria_range = [4, 5, 6]\n\n   # Initialize the logger\n   logger = setup_logger()\n\n   # Initialize the 'data_fetcher' variable\n   data_fetcher = DataFetcher(exchange, symbol)\n   ```\n\n10. Perform the backtesting logic inside a loop for the specified duration:\n   ```python\n   # Get the end time for the backtesting loop\n   start_time = time.time()\n   end_time = start_time + backtesting_duration\n\n   # Initialize the 'symbol' variable outside the loop\n   symbol = 'ETH/USDT:USDT'\n\n   while time.time() \u003c end_time:\n       # ... (backtesting logic inside the loop)\n   ```\n\n11. Use the `OrderBookAnalyzer` class to run the\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyllvar%2Forderbook-backtesting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyllvar%2Forderbook-backtesting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyllvar%2Forderbook-backtesting/lists"}