From 413d17af77854f47e3c3773b6aab895cb6af01ad Mon Sep 17 00:00:00 2001 From: EiJackGH Date: Mon, 16 Feb 2026 19:30:27 +0800 Subject: [PATCH] Revert "Develop" --- .Jules/palette.md | 10 +- .github/workflows/codeql.yml | 101 ------------------ .gitignore | 1 + README.md | 31 +----- ...bitcoin_trading_simulation.cpython-312.pyc | Bin 8406 -> 0 bytes .../test_bitcoin_trading.cpython-312.pyc | Bin 5175 -> 0 bytes bitcoin_trading_simulation.py | 64 ++++------- test.py | 3 + test_bitcoin.py | 35 ------ test_bitcoin_trading.py | 74 ------------- test_simulation.py | 33 +++--- 11 files changed, 54 insertions(+), 298 deletions(-) delete mode 100644 .github/workflows/codeql.yml delete mode 100644 __pycache__/bitcoin_trading_simulation.cpython-312.pyc delete mode 100644 __pycache__/test_bitcoin_trading.cpython-312.pyc create mode 100644 test.py delete mode 100644 test_bitcoin.py delete mode 100644 test_bitcoin_trading.py diff --git a/.Jules/palette.md b/.Jules/palette.md index 018831f..231399f 100644 --- a/.Jules/palette.md +++ b/.Jules/palette.md @@ -1,3 +1,7 @@ -## 2024-05-23 - CLI UX Enhancement -**Learning:** Even in CLI apps, visual distinction (colors, emojis) significantly reduces cognitive load when scanning logs. -**Action:** Use ANSI colors and consistent emojis for key events (success/failure) in future CLI tools. +## 2024-05-22 - Visual Hierarchy in CLI Output +**Learning:** Adding color-coded indicators (Green/Red) and emojis (💰, 📉) in CLI tools significantly reduces cognitive load when parsing financial data streams. It transforms a wall of text into a scannable narrative. +**Action:** For data-heavy CLI applications, always implement a semantic color system and visual anchors (icons/emojis) for key events. + +## 2024-05-23 - Accessibility and Control in CLI Tools +**Learning:** While color and emojis improve scannability, they can be distracting or inaccessible (e.g., for color-blind users or automated parsing). Providing `--no-color` and `--quiet` flags is essential for accessibility and flexibility. +**Action:** Always include flags to disable visual enhancements and suppress verbose output in CLI tools to respect user preferences and support automation. diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index 36232fc..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,101 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL Advanced" - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - schedule: - - cron: '42 16 * * 6' - -jobs: - analyze: - name: Analyze (${{ matrix.language }}) - # Runner size impacts CodeQL analysis time. To learn more, please see: - # - https://gh.io/recommended-hardware-resources-for-running-codeql - # - https://gh.io/supported-runners-and-hardware-resources - # - https://gh.io/using-larger-runners (GitHub.com only) - # Consider using larger runners or machines with greater resources for possible analysis time improvements. - runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} - permissions: - # required for all workflows - security-events: write - - # required to fetch internal or private CodeQL packs - packages: read - - # only required for workflows in private repositories - actions: read - contents: read - - strategy: - fail-fast: false - matrix: - include: - - language: actions - build-mode: none - - language: python - build-mode: none - # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' - # Use `c-cpp` to analyze code written in C, C++ or both - # Use 'java-kotlin' to analyze code written in Java, Kotlin or both - # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both - # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, - # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. - # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how - # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Add any setup steps before running the `github/codeql-action/init` action. - # This includes steps like installing compilers or runtimes (`actions/setup-node` - # or others). This is typically only required for manual builds. - # - name: Setup runtime (example) - # uses: actions/setup-example@v1 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v4 - with: - languages: ${{ matrix.language }} - build-mode: ${{ matrix.build-mode }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - # If the analyze step fails for one of the languages you are analyzing with - # "We were unable to automatically build your code", modify the matrix above - # to set the build mode to "manual" for that language. Then modify this step - # to build your code. - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - name: Run manual build steps - if: matrix.build-mode == 'manual' - shell: bash - run: | - echo 'If you are using a "manual" build mode for one or more of the' \ - 'languages you are analyzing, replace this with the commands to build' \ - 'your code, for example:' - echo ' make bootstrap' - echo ' make release' - exit 1 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v4 - with: - category: "/language:${{matrix.language}}" diff --git a/.gitignore b/.gitignore index 907591b..2e049dd 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ # Python __pycache__/ *.pyc +.pytest_cache/ diff --git a/README.md b/README.md index 4cd8db6..be561ef 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,10 @@ A Python-based CLI tool that simulates Bitcoin trading using a 'Golden Cross' mo ## Features -- **Price Simulation:** Uses Geometric Brownian Motion to simulate Bitcoin prices. +- **Price Simulation:** Uses Geometric Brownian Motion to simulate 60 days of Bitcoin prices. - **Trading Strategy:** Implements a Golden Cross strategy (Short MA > Long MA = Buy, Short MA < Long MA = Sell). -- **Rich CLI Output:** Features color-coded logs (Green for Buy/Profit, Red for Sell/Loss) and emojis for better readability. +- **Rich CLI Output:** features color-coded logs (Green for Buy/Profit, Red for Sell/Loss) and emojis for better readability. - **Performance metrics:** Compares the strategy's performance against a "Buy and Hold" approach. -- **Customizable:** Configure simulation parameters via command-line arguments. ## Installation @@ -21,36 +20,16 @@ pip install -r requirements.txt ## Usage -Run the simulation script with default settings (60 days, $10k initial cash): +Run the simulation script: ```bash python bitcoin_trading_simulation.py ``` -### Options - -Customize the simulation with the following arguments: - -```bash -python bitcoin_trading_simulation.py --days 100 --initial-cash 5000 --initial-price 60000 --volatility 0.03 -``` - -- `--days`: Number of days to simulate (default: 60) -- `--initial-cash`: Initial cash amount (default: 10000) -- `--initial-price`: Initial Bitcoin price (default: 50000) -- `--volatility`: Price volatility (default: 0.02) -- `--quiet`: Suppress daily portfolio log (only show final result) -- `--no-color`: Disable colored output (for accessibility or logging) - -Example: -```bash -python bitcoin_trading_simulation.py --days 30 --quiet --no-color -``` - ## Tests -Run the test suite using `pytest`: +Run the test suite: ```bash -pytest +python test.py ``` diff --git a/__pycache__/bitcoin_trading_simulation.cpython-312.pyc b/__pycache__/bitcoin_trading_simulation.cpython-312.pyc deleted file mode 100644 index 52abfc2edcf23a102e58b9d12c1192ee1c250d1b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8406 zcmbt3TWlLiazk?Xeu#QYCgmN=vMkDyWqEz;$J$<7PrsygFxJZEY-F=*c1jx)v3vy@u;Y9*m{HKiK+ay>)~~BEwFpKy1Kf$y1J^m^e=Y1g@7{n;YY#0ttN1JlA7KGb-POe13TuDO2_anB{<3!BoBhW))!Snd&#dtYK=I9gkg~=zf4pH{jK6z}v0NSzmur=y=Q6 zvQXpt5#^hAvWAdnGhCggAwcRznA^M=2cRumLicZmKHjPB$Xf#xO>9l^#A=~$W@}&R zTi9BlerPitfWEUC`uMFPbvNr~s9Hb?9ovk@>_Q{no$Su7smDRny>D$j#@1{}kw6sV zR+vYz)HXPH)wmvTcEMijnR<|U&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zLPae%^goZ_}%EQN5!E_@JZw{scAUwD!LV!ak;5Z70h2tR5n4l_FVzUu-A^7@^@|>x=JDQ$eb!Dws-$ z&LMweWmv(_2d7Z59yheMqI^tzNB^rSq8JAc4V<=QA)Y5=vr{Z&F4+J*9gfN51RI`; zJ6c-{7NHg0a>aZletRljepbotPzX%X5pFsfiEhW&n}m(Nx^pXom5@3V2M`ffwid5WWSEqY*(SaczonK74FAZ@uBb zaApR3uyqGFhzOBE^h~t|(0M7I9_1a8pk*gGFCXl{HvzKrp@5+56$p+C_ zUe?<*`(C zV!6X-q{C;#flIjouQcFYaSkt>1&3AcPWDM<&AGBRsjMxdmCD{;IG?xIBoF0WE%54> z>@5pDfL7zqRX0i1P3d<&efQD3nd^^EO4Ucfn&xXg>7Gx|KRTb)K6+oOeODx_z9r3x zj>HUdeF-iZxIcM!GC3+XgNO3wF8QQOK5^(LqJ>@|$G!&4_mf>YcdO)XP5VXHLDBN| z3V8_NqOBn{oGBL@ek5AnSs@QUH`?-wpqI_(`8=8T^VITKxd@Xa`o${{{Fyrf2{F diff --git a/__pycache__/test_bitcoin_trading.cpython-312.pyc b/__pycache__/test_bitcoin_trading.cpython-312.pyc deleted file mode 100644 index ecc297a91549969cdf54cd01999ccc2da4eaaba9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5175 zcmeHLU2GKB6~6Pkv+K2CZEUZ-HY`7-Y+#M~F$ocY1Z)B=EW|j3E<`lic&?gokYUvOKgEu^3qi5OCO{5#cpk}8ju>PNEK2akl~4^o^xks*Ear1 zr1Yu1mcMi7&OP_=-XKCn$^#OKG;D+EmlcOrKLg~@S?us!Z3NeVg-DB>1igCd};OMpII22^w(pkG%2 z1B3opz-j6-Ec>WCSC_kpbMB(dY^KMX2`A*?A26nk= z)Q)_jirH!dbhKqBjE_09qo?Tk!U{#?PpH4PjQZD8L3_icNTrwv|Ae( z*loj6sU9yy###mj9mz86_XqM@Ry)cT)(1`4+68cfd>h^}B9BxS>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?u#Zn({r&Z+>vPIm%pnu83P({@Oh_Rs zgbWc9xLgYBK~=@iTlWpc?3p$rY({ts;T?qc5YWEpd4MUxMrC;+#ruN+S_S(R;U<|E zeNuG3T98`i1ELh04~C>4&2L{X9hl!1klMNVFyiyDPde-lgk#_W(;ED=q{gZoB?pw? zak?juH#&T8YZ+a$+$$fcaT-^S@9f*oz4bAVbrd84_%+=1et>NvPDhhaEpNwB4!LJ@ zm}>BXgMSNs_?f_Q+#|B?5eYmZ{zs(d5efZ+T$mvjUWiGKJ3R5m!>#*gw(frczj|?p hpIzgL7#z<3O=u{Hp&KWDb^7DezdZ9yAYvn%>Oa%SLudd1 diff --git a/bitcoin_trading_simulation.py b/bitcoin_trading_simulation.py index c86be3e..6706418 100644 --- a/bitcoin_trading_simulation.py +++ b/bitcoin_trading_simulation.py @@ -1,9 +1,7 @@ -import argparse import numpy as np import pandas as pd import argparse - class Colors: HEADER = '\033[95m' BLUE = '\033[94m' @@ -21,18 +19,6 @@ def disable(cls): cls.ENDC = '' cls.BOLD = '' - -class Colors: - HEADER = '\033[95m' - BLUE = '\033[94m' - CYAN = '\033[96m' - GREEN = '\033[92m' - WARNING = '\033[93m' - FAIL = '\033[91m' - ENDC = '\033[0m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' - def simulate_bitcoin_prices(days=60, initial_price=50000, volatility=0.02): """ Simulates Bitcoin prices for a given number of days using Geometric Brownian Motion. @@ -47,7 +33,6 @@ def simulate_bitcoin_prices(days=60, initial_price=50000, volatility=0.02): prices.append(prices[-1] + price_change) return pd.Series(prices, name='Price') - def calculate_moving_averages(prices, short_window=7, long_window=30): """ Calculates short and long moving averages for a given price series. @@ -58,7 +43,6 @@ def calculate_moving_averages(prices, short_window=7, long_window=30): signals['long_mavg'] = prices.rolling(window=long_window, min_periods=1, center=False).mean() return signals - def generate_trading_signals(signals): """ Generates trading signals based on the Golden Cross strategy. @@ -70,12 +54,11 @@ def generate_trading_signals(signals): signals.loc[signals['short_mavg'] > signals['long_mavg'], 'signal'] = 1.0 # A Death Cross (sell signal) signals.loc[signals['short_mavg'] < signals['long_mavg'], 'signal'] = -1.0 - + # We create 'positions' to represent the trading action: 1 for buy, -1 for sell, 0 for hold signals['positions'] = signals['signal'].diff().shift(1) return signals - def simulate_trading(signals, initial_cash=10000, quiet=False): """ Simulates trading based on signals and prints a daily ledger. @@ -88,8 +71,6 @@ def simulate_trading(signals, initial_cash=10000, quiet=False): if not quiet: print(f"{Colors.HEADER}{Colors.BOLD}------ Daily Trading Ledger ------{Colors.ENDC}") - - print(f"\n{Colors.HEADER}{Colors.BOLD}------ Daily Trading Ledger ------{Colors.ENDC}") for i, row in signals.iterrows(): if i > 0: portfolio.loc[i, 'cash'] = portfolio.loc[i-1, 'cash'] @@ -100,33 +81,30 @@ def simulate_trading(signals, initial_cash=10000, quiet=False): btc_to_buy = portfolio.loc[i, 'cash'] / row['price'] portfolio.loc[i, 'btc'] += btc_to_buy portfolio.loc[i, 'cash'] -= btc_to_buy * row['price'] - print(f"{Colors.GREEN}đŸŸĸ Day {i}: Buy {btc_to_buy:.4f} BTC at ${row['price']:.2f}{Colors.ENDC}") + print(f"{Colors.GREEN}Day {i}: 💰 Buy {btc_to_buy:.4f} BTC at ${row['price']:.2f}{Colors.ENDC}") # Sell signal elif row['positions'] == -2.0: if portfolio.loc[i, 'btc'] > 0: cash_received = portfolio.loc[i, 'btc'] * row['price'] portfolio.loc[i, 'cash'] += cash_received - print(f"{Colors.FAIL}🔴 Day {i}: Sell {portfolio.loc[i, 'btc']:.4f} BTC at ${row['price']:.2f}{Colors.ENDC}") + print(f"{Colors.RED}Day {i}: 📉 Sell {portfolio.loc[i, 'btc']:.4f} BTC at ${row['price']:.2f}{Colors.ENDC}") portfolio.loc[i, 'btc'] = 0 portfolio.loc[i, 'total_value'] = portfolio.loc[i, 'cash'] + portfolio.loc[i, 'btc'] * row['price'] - if not quiet: - print(f"Day {i}: Portfolio Value: ${portfolio.loc[i, 'total_value']:.2f}, " - f"Cash: ${portfolio.loc[i, 'cash']:.2f}, BTC: {portfolio.loc[i, 'btc']:.4f}") - + print(f"Day {i}: Portfolio Value: ${portfolio.loc[i, 'total_value']:.2f}, Cash: ${portfolio.loc[i, 'cash']:.2f}, BTC: {portfolio.loc[i, 'btc']:.4f}") + return portfolio - if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Bitcoin Trading Simulation") - parser.add_argument("--days", type=int, default=60, help="Number of days to simulate") - parser.add_argument("--initial-cash", type=float, default=10000, help="Initial cash amount") - parser.add_argument("--initial-price", type=float, default=50000, help="Initial Bitcoin price") - parser.add_argument("--volatility", type=float, default=0.02, help="Price volatility") - parser.add_argument("--quiet", action="store_true", help="Suppress daily portfolio log") - parser.add_argument("--no-color", action="store_true", help="Disable colored output") + parser = argparse.ArgumentParser(description='Bitcoin Trading Simulation') + parser.add_argument('--days', type=int, default=60, help='Number of days to simulate') + parser.add_argument('--initial-cash', type=float, default=10000, help='Initial cash in portfolio') + parser.add_argument('--initial-price', type=float, default=50000, help='Initial Bitcoin price') + parser.add_argument('--volatility', type=float, default=0.02, help='Volatility of price changes') + parser.add_argument('--quiet', '-q', action='store_true', help='Suppress daily portfolio logs') + parser.add_argument('--no-color', action='store_true', help='Disable colored output') args = parser.parse_args() @@ -135,31 +113,33 @@ def simulate_trading(signals, initial_cash=10000, quiet=False): # Simulate prices prices = simulate_bitcoin_prices(days=args.days, initial_price=args.initial_price, volatility=args.volatility) - + # Calculate moving averages signals = calculate_moving_averages(prices) - + # Generate trading signals signals = generate_trading_signals(signals) - + # Simulate trading portfolio = simulate_trading(signals, initial_cash=args.initial_cash, quiet=args.quiet) - + # Final portfolio performance final_value = portfolio['total_value'].iloc[-1] initial_cash = args.initial_cash profit = final_value - initial_cash - + # Compare with buy and hold strategy - buy_and_hold_btc = args.initial_cash / prices.iloc[0] + buy_and_hold_btc = initial_cash / prices.iloc[0] buy_and_hold_value = buy_and_hold_btc * prices.iloc[-1] print(f"\n{Colors.HEADER}{Colors.BOLD}------ Final Portfolio Performance ------{Colors.ENDC}") print(f"Initial Cash: ${initial_cash:.2f}") print(f"Final Portfolio Value: ${final_value:.2f}") + if profit >= 0: - print(f"{Colors.GREEN}💰 Profit/Loss: ${profit:.2f}{Colors.ENDC}") + print(f"Profit/Loss: {Colors.GREEN}📈 ${profit:.2f}{Colors.ENDC}") else: - print(f"{Colors.FAIL}📉 Profit/Loss: ${profit:.2f}{Colors.ENDC}") + print(f"Profit/Loss: {Colors.RED}📉 ${profit:.2f}{Colors.ENDC}") + print(f"Buy and Hold Strategy Value: ${buy_and_hold_value:.2f}") print(f"{Colors.HEADER}-----------------------------------------{Colors.ENDC}") diff --git a/test.py b/test.py new file mode 100644 index 0000000..b326d87 --- /dev/null +++ b/test.py @@ -0,0 +1,3 @@ +# Filename: tests/test_sample.py +def test_example(): + assert 1 + 1 == 2 diff --git a/test_bitcoin.py b/test_bitcoin.py deleted file mode 100644 index 163248c..0000000 --- a/test_bitcoin.py +++ /dev/null @@ -1,35 +0,0 @@ -import pytest -from unittest.mock import patch -from bitcoin import get_bitcoin_price, calculate_value - -# Test 1: Verify the calculation logic -def test_calculate_value(): - """Ensure BTC to USD conversion math is correct.""" - price = 50000.0 - amount = 2.5 - expected = 125000.0 - assert calculate_value(amount, price) == expected - -# Test 2: Verify handling of zero amount -def test_calculate_value_zero(): - assert calculate_value(0, 50000.0) == 0.0 - -# Test 3: Mocking an API response -@patch('bitcoin.requests.get') -def test_get_bitcoin_price(mock_get): - """Simulate a successful API response from CoinDesk or similar.""" - # Mock the JSON return value - mock_get.return_value.json.return_value = { - "bpi": {"USD": {"rate_float": 62000.50}} - } - mock_get.return_value.status_code = 200 - - price = get_bitcoin_price() - assert price == 62000.50 - -# Test 4: Handling API failure -@patch('bitcoin.requests.get') -def test_get_price_api_error(mock_get): - mock_get.return_value.status_code = 404 - with pytest.raises(ConnectionError): - get_bitcoin_price() diff --git a/test_bitcoin_trading.py b/test_bitcoin_trading.py deleted file mode 100644 index e7eac6f..0000000 --- a/test_bitcoin_trading.py +++ /dev/null @@ -1,74 +0,0 @@ -import pytest -import pandas as pd -from bitcoin_trading_simulation import ( - simulate_trading, Colors, calculate_moving_averages, - generate_trading_signals, simulate_bitcoin_prices -) - - -@pytest.fixture -def reset_colors(): - # Save original colors - original_colors = { - 'HEADER': Colors.HEADER, - 'BLUE': Colors.BLUE, - 'GREEN': Colors.GREEN, - 'RED': Colors.RED, - 'ENDC': Colors.ENDC, - 'BOLD': Colors.BOLD, - } - yield - # Restore colors - Colors.HEADER = original_colors['HEADER'] - Colors.BLUE = original_colors['BLUE'] - Colors.GREEN = original_colors['GREEN'] - Colors.RED = original_colors['RED'] - Colors.ENDC = original_colors['ENDC'] - Colors.BOLD = original_colors['BOLD'] - - -def test_simulate_trading_quiet_mode(capsys): - """Test that quiet mode suppresses output.""" - signals = pd.DataFrame(index=range(5)) - signals['price'] = [100.0, 101.0, 102.0, 103.0, 104.0] - signals['positions'] = [0.0] * 5 - - simulate_trading(signals, initial_cash=1000, quiet=True) - - captured = capsys.readouterr() - assert captured.out == "" - - -def test_simulate_trading_verbose_mode(capsys): - """Test that verbose mode prints daily ledger.""" - signals = pd.DataFrame(index=range(2)) - signals['price'] = [100.0, 101.0] - signals['positions'] = [0.0, 0.0] - - simulate_trading(signals, initial_cash=1000, quiet=False) - - captured = capsys.readouterr() - assert "Daily Trading Ledger" in captured.out - assert "Portfolio Value" in captured.out - - -def test_colors_disable(reset_colors): - """Test that Colors.disable() clears color codes.""" - assert Colors.HEADER != "" - Colors.disable() - assert Colors.HEADER == "" - assert Colors.GREEN == "" - assert Colors.RED == "" - - -def test_simulation_integration(): - """Test full simulation pipeline with small parameters.""" - prices = simulate_bitcoin_prices(days=10, initial_price=100) - signals = calculate_moving_averages(prices, short_window=2, long_window=5) - signals = generate_trading_signals(signals) - portfolio = simulate_trading(signals, quiet=True) - - assert len(portfolio) == 10 - assert 'total_value' in portfolio.columns - assert 'btc' in portfolio.columns - assert 'cash' in portfolio.columns diff --git a/test_simulation.py b/test_simulation.py index 0f4f1f8..05a5106 100644 --- a/test_simulation.py +++ b/test_simulation.py @@ -4,30 +4,29 @@ from bitcoin_trading_simulation import simulate_bitcoin_prices, calculate_moving_averages, generate_trading_signals def test_simulate_bitcoin_prices(): - days = 10 - prices = simulate_bitcoin_prices(days=days, initial_price=50000) - assert len(prices) == days + prices = simulate_bitcoin_prices(days=10, initial_price=100, volatility=0.01) + assert len(prices) == 10 assert isinstance(prices, pd.Series) - assert prices.name == 'Price' + assert prices.iloc[0] == 100 def test_calculate_moving_averages(): - prices = pd.Series([100, 101, 102, 103, 104, 105, 106, 107, 108, 109], name='Price') + prices = pd.Series([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], name='Price') signals = calculate_moving_averages(prices, short_window=3, long_window=5) assert 'short_mavg' in signals.columns assert 'long_mavg' in signals.columns - assert not signals['short_mavg'].isnull().all() + assert len(signals) == 10 def test_generate_trading_signals(): - # Create dummy signals DataFrame - data = { + # create dummy signals dataframe + signals = pd.DataFrame({ 'price': [100, 101, 102, 103, 104], - 'short_mavg': [100, 101, 105, 102, 100], - 'long_mavg': [100, 100, 100, 103, 105] - } - signals = pd.DataFrame(data) - signals = generate_trading_signals(signals) + 'short_mavg': [100, 101, 102, 103, 104], + 'long_mavg': [99, 100, 101, 102, 103] + }) + # Here short > long, so signal should be 1.0 (buy) - assert 'signal' in signals.columns - assert 'positions' in signals.columns - # Check that positions are calculated (not all nan, though first might be) - assert signals['positions'].isin([0, 1, -1, 2, -2, np.nan]).any() + signals_with_logic = generate_trading_signals(signals) + assert 'signal' in signals_with_logic.columns + assert 'positions' in signals_with_logic.columns + # Check if logic is applied (dummy check) + assert (signals_with_logic['signal'] == 1.0).all()