70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
"""Tests for Grid strategy."""
|
|
|
|
import pytest
|
|
from decimal import Decimal
|
|
from src.strategies.grid.grid_strategy import GridStrategy
|
|
|
|
|
|
def test_grid_strategy_initialization():
|
|
"""Test Grid strategy initializes correctly."""
|
|
strategy = GridStrategy("Test Grid", {
|
|
"grid_spacing": 1,
|
|
"num_levels": 10,
|
|
"profit_target": 2
|
|
})
|
|
assert strategy.name == "Test Grid"
|
|
assert strategy.grid_spacing == Decimal("0.01")
|
|
assert strategy.num_levels == 10
|
|
|
|
|
|
def test_grid_levels_calculation():
|
|
"""Test grid levels are calculated correctly."""
|
|
strategy = GridStrategy("Test Grid", {
|
|
"grid_spacing": 1,
|
|
"num_levels": 5,
|
|
"center_price": 100
|
|
})
|
|
|
|
strategy._update_grid_levels(Decimal("100"))
|
|
assert len(strategy.buy_levels) == 5
|
|
assert len(strategy.sell_levels) == 5
|
|
|
|
# Buy levels should be below center
|
|
assert all(level < Decimal("100") for level in strategy.buy_levels)
|
|
# Sell levels should be above center
|
|
assert all(level > Decimal("100") for level in strategy.sell_levels)
|
|
|
|
|
|
def test_grid_buy_signal():
|
|
"""Test grid generates buy signal at lower level."""
|
|
strategy = GridStrategy("Test Grid", {
|
|
"grid_spacing": 1,
|
|
"num_levels": 5,
|
|
"center_price": 100,
|
|
"position_size": Decimal("0.1")
|
|
})
|
|
|
|
# Price at buy level
|
|
signal = strategy.on_tick("BTC/USD", Decimal("99"), "1h", {})
|
|
assert signal is not None
|
|
assert signal.signal_type.value == "buy"
|
|
|
|
|
|
def test_grid_profit_taking():
|
|
"""Test grid takes profit at target."""
|
|
strategy = GridStrategy("Test Grid", {
|
|
"grid_spacing": 1,
|
|
"num_levels": 5,
|
|
"profit_target": 2
|
|
})
|
|
|
|
# Simulate position
|
|
entry_price = Decimal("100")
|
|
strategy.positions[entry_price] = Decimal("0.1")
|
|
|
|
# Price with profit
|
|
signal = strategy.on_tick("BTC/USD", Decimal("102"), "1h", {})
|
|
assert signal is not None
|
|
assert signal.signal_type.value == "sell"
|
|
assert entry_price not in strategy.positions # Position removed
|