Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Python CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Run tests with coverage
run: |
./tests.sh

- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: htmlcov
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
.idea/
deploy/tools/ansible/inventory/inventory.ini
reporting/trading-dashboard

# Python
__pycache__/
*.pyc
.coverage
htmlcov/
.pytest_cache/
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ watchfiles==0.21.0
websockets==12.0
wsproto==1.2.0
websocket-client~=1.8.0
cryptography==44.0.2
cryptography==44.0.2
pytest
pytest-cov
4 changes: 4 additions & 0 deletions src/logging_helper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# logging_utils.py
import logging
from logging.handlers import RotatingFileHandler
import os


def setup_logging(config_manager, app_name):
Expand All @@ -16,6 +17,9 @@ def setup_logging(config_manager, app_name):
# Configure logging with file name based on app_name
log_file_name = f"{logging_config['persistant']['log_path']}/{app_name}/{app_name}.log"

# Create directory if it doesn't exist
os.makedirs(os.path.dirname(log_file_name), exist_ok=True)

# Create a RotatingFileHandler
handler = RotatingFileHandler(log_file_name, maxBytes=2097152, backupCount=31)

Expand Down
2 changes: 1 addition & 1 deletion src/web_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

@app.middleware("http")
async def check_ip(request: Request, call_next):
client_ip = request.client.host
client_ip = request.headers.get("x-forwarded-for", request.client.host)
print(f"HERE {client_ip}")
if client_ip not in ALLOWED_IPS:
logging.warning(f"Forbidden access attempt from IP: {client_ip}")
Expand Down
2 changes: 1 addition & 1 deletion tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PYTHONPATH=./ pytest -vv tests/test_db_position_manager.py
PYTHONPATH=./:src/ pytest --cov=src --cov-report=term-missing --cov-report=html -vv tests/
123 changes: 123 additions & 0 deletions tests/test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"authentication": {
"saxo": {
"app_config_object": {
"AppName": "test_app",
"AppKey": "test_key",
"AppSecret": "test_secret",
"AuthorizationEndpoint": "https://sim.logonvalidation.net/authorize",
"TokenEndpoint": "https://sim.logonvalidation.net/token",
"GrantType": "Code",
"OpenApiBaseUrl": "https://gateway.saxobank.com",
"RedirectUrls": [
"http://localhost:8080/redirect"
]
}
},
"persistant": {
"token_path": "/tmp/token.json"
}
},
"logging": {
"level": "INFO",
"persistant": {
"log_path": "/tmp/logs"
}
},
"webserver": {
"persistant": {
"token_path": "/tmp/token.json"
},
"app_secret": "test_secret"
},
"rabbitmq": {
"hostname": "localhost",
"authentication": {
"username": "user",
"password": "password"
}
},
"duckdb": {
"persistant": {
"db_path": "/tmp/test.db"
}
},
"trade": {
"rules": [
{
"rule_type": "allowed_indices",
"rule_name": "allowed_indices",
"rule_config": {
"indice_ids": {}
}
},
{
"rule_type": "market_closed_dates",
"rule_name": "market_closed_dates",
"rule_config": {
"market_closed_dates": []
}
},
{
"rule_type": "signal_validation",
"rule_name": "signal_validation",
"rule_config": {
"max_signal_age_minutes": 10
}
},
{
"rule_type": "market_hours",
"rule_name": "market_hours",
"rule_config": {
"trading_start_hour": 8,
"trading_end_hour": 22,
"risky_trading_start_hour": 9,
"risky_trading_start_minute": 30
}
}
],
"config": {
"general": {
"timezone": "Europe/Paris",
"api_limits": {
"top_instruments": 200,
"top_positions": 200,
"top_closed_positions": 500
},
"retry_config": {
"max_retries": 3,
"retry_sleep_seconds": 1
},
"position_check": {
"check_interval_seconds": 60,
"timeout_seconds": 300
},
"websocket": {
"refresh_rate_ms": 1000
}
},
"turbo_preference": {
"exchange_id": "test_exchange",
"price_range": {
"min": 4,
"max": 15
}
},
"buying_power": {
"max_account_funds_to_use_percentage": 100,
"safety_margins": {
"bid_calculation": 1
}
},
"position_management": {
"performance_thresholds": {
"stoploss_percent": -20,
"max_profit_percent": 60
}
}
},
"persistant": {
"last_action_file": "/tmp/last_action.json"
}
}
}
45 changes: 28 additions & 17 deletions tests/test_configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,49 @@

class TestConfigurationManager:

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG"}, "rabbitmq": {"host": "localhost"}}')
def test_load_config_valid(self, mock_file):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.config_data["logging"]["level"] == "DEBUG"
assert config_manager.config_data["rabbitmq"]["host"] == "localhost"
def test_load_config_valid(self, mock_file, mock_exists):
with patch.object(ConfigurationManager, 'validate_config', return_value=None):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.config_data["logging"]["level"] == "DEBUG"
assert config_manager.config_data["rabbitmq"]["host"] == "localhost"

@patch("os.path.exists", return_value=False)
def test_load_config_file_not_found(self, mock_exists):
with pytest.raises(FileNotFoundError):
ConfigurationManager("dummy_path")

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG" "rabbitmq": {"host": "localhost"}}') # Malformed JSON
def test_load_config_malformed_json(self, mock_file):
def test_load_config_malformed_json(self, mock_file, mock_exists):
with pytest.raises(json.JSONDecodeError):
ConfigurationManager("dummy_path")

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG"}, "rabbitmq": {"host": "localhost"}}')
def test_get_config_value_existing_key(self, mock_file):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_config_value("logging.level") == "DEBUG"
def test_get_config_value_existing_key(self, mock_file, mock_exists):
with patch.object(ConfigurationManager, 'validate_config', return_value=None):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_config_value("logging.level") == "DEBUG"

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG"}, "rabbitmq": {"host": "localhost"}}')
def test_get_config_value_non_existing_key(self, mock_file):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_config_value("non.existing.key", default="default_value") == "default_value"
def test_get_config_value_non_existing_key(self, mock_file, mock_exists):
with patch.object(ConfigurationManager, 'validate_config', return_value=None):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_config_value("non.existing.key", default="default_value") == "default_value"

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG"}, "rabbitmq": {"host": "localhost"}}')
def test_get_logging_config(self, mock_file):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_logging_config() == {"level": "DEBUG"}
def test_get_logging_config(self, mock_file, mock_exists):
with patch.object(ConfigurationManager, 'validate_config', return_value=None):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_logging_config() == {"level": "DEBUG"}

@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data='{"logging": {"level": "DEBUG"}, "rabbitmq": {"host": "localhost"}}')
def test_get_rabbitmq_config(self, mock_file):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_rabbitmq_config() == {"host": "localhost"}
def test_get_rabbitmq_config(self, mock_file, mock_exists):
with patch.object(ConfigurationManager, 'validate_config', return_value=None):
config_manager = ConfigurationManager("dummy_path")
assert config_manager.get_rabbitmq_config() == {"host": "localhost"}
42 changes: 32 additions & 10 deletions tests/test_db_position_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ def test_append_performance_message(setup_temp_db):

# Insert dummy data into the database for testing
today = datetime.now().strftime('%Y/%m/%d')
from datetime import timedelta

open_data = {
"action": "long",
Expand Down Expand Up @@ -716,16 +717,37 @@ def test_append_performance_message(setup_temp_db):
last_best_7_days_percentages_on_max)

# Expected message
expected_message = f"\n--- Last 7 Days Performance real ---\n"
expected_message += f"{today}: 10.0%\n"
expected_message += f"\n--- Last 7 Days Performance best ---\n"
expected_message += f"{today}: 10.0%\n"
expected_message += f"\n--- Last 7 Days Performance, on max ---\n"
expected_message += f"{today}: 20.0%\n"
expected_message += f"\n--- Last 7 Days Performance, best on max ---\n"
expected_message += f"{today}: 20.0%\n"

assert message == expected_message, "The generated performance message should match the expected output."
expected_message = ""
for i in range(7):
day = (datetime.now() - timedelta(days=i)).strftime('%Y/%m/%d')
if i == 0:
expected_message += f"\n--- Last 7 Days Performance real ---\n"
expected_message += f"{day}: 10.00%\n"
else:
expected_message += f"{day}: 0.00%\n"
for i in range(7):
day = (datetime.now() - timedelta(days=i)).strftime('%Y/%m/%d')
if i == 0:
expected_message += f"\n--- Last 7 Days Performance best ---\n"
expected_message += f"{day}: 10.00%\n"
else:
expected_message += f"{day}: 0.00%\n"
for i in range(7):
day = (datetime.now() - timedelta(days=i)).strftime('%Y/%m/%d')
if i == 0:
expected_message += f"\n--- Last 7 Days Performance, on max ---\n"
expected_message += f"{day}: 20.00%\n"
else:
expected_message += f"{day}: 0.00%\n"
for i in range(7):
day = (datetime.now() - timedelta(days=i)).strftime('%Y/%m/%d')
if i == 0:
expected_message += f"\n--- Last 7 Days Performance, best on max ---\n"
expected_message += f"{day}: 20.00%\n"
else:
expected_message += f"{day}: 0.00%\n"

assert message.strip() == expected_message.strip()


def test_database_marked_as_corrupted(setup_temp_db):
Expand Down
42 changes: 27 additions & 15 deletions tests/test_saxo_api_action.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
import pytest
from unittest.mock import patch, MagicMock
from src.trade.api_actions import SaxoService
from src.trade.api_actions import TradingOrchestrator, InstrumentService, OrderService, PositionService, SaxoApiClient
from src.configuration import ConfigurationManager
from src.database import DbOrderManager, DbPositionManager, DbTradePerformanceManager, DbStrategySignalStatsManager
from src.rabbit_connection import RabbitConnection
from src.trading_rule import TradingRule
from src.database import DbOrderManager, DbPositionManager
from src.saxo_authen import SaxoAuth


@pytest.fixture
def saxo_service():
def trading_orchestrator():
config_manager = MagicMock(spec=ConfigurationManager)
def config_side_effect(key, default=None):
if key == "saxo_auth.env":
return "simulation"
if key == "trade.config.buying_power":
return {"safety_margins": {"bid_calculation": 1}, "max_account_funds_to_use_percentage": 100}
return MagicMock()

config_manager.get_config_value.side_effect = config_side_effect
db_order_manager = MagicMock(spec=DbOrderManager)
db_position_manager = MagicMock(spec=DbPositionManager)
rabbit_connection = MagicMock(spec=RabbitConnection)
trading_rule = MagicMock(spec=TradingRule)
return SaxoService(config_manager, db_order_manager, db_position_manager, rabbit_connection, trading_rule)
saxo_auth = MagicMock(spec=SaxoAuth)
api_client = SaxoApiClient(config_manager, saxo_auth)
instrument_service = InstrumentService(api_client, config_manager, "account_key")
order_service = OrderService(api_client, "account_key", "client_key")
position_service = PositionService(api_client, order_service, config_manager, "account_key", "client_key")
return TradingOrchestrator(instrument_service, order_service, position_service, config_manager, db_order_manager, db_position_manager)


@patch('src.trade.api_actions.pf.balances.AccountBalances')
@patch('src.trade.api_actions.SaxoService.saxo_client')
def test_calcul_bid_amount(mock_saxo_client, mock_account_balances, saxo_service):
@patch('src.trade.api_actions.PositionService.get_spending_power')
def test_calcul_bid_amount(mock_get_spending_power, trading_orchestrator):
# Mock the response from the Saxo API
mock_saxo_client.request.return_value = {"SpendingPower": 1000}
mock_get_spending_power.return_value = 1000
founded_turbo = {
"price": {
"Quote": {"Ask": 10}
"selected_instrument": {
"latest_ask": 10,
"decimals": 2
}
}

# Call the method
amount = saxo_service.calcul_bid_amount(founded_turbo)
amount = trading_orchestrator._calculate_bid_amount(founded_turbo, 1000)

# Assert the expected amount
assert amount == 99 # (1000 / 10) - 1 = 99
Loading