Move more checks from market into checks library
parent
da67177a18
commit
dd3b3521d9
@ -0,0 +1,57 @@
|
|||||||
|
from core.exchanges import BaseExchange
|
||||||
|
|
||||||
|
|
||||||
|
class FakeExchange(BaseExchange):
|
||||||
|
def call_method(self, request):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_account(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_instruments(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_currencies(self, currencies):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_supported_assets(self, response=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_balance(self, return_usd=False):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_market_value(self, symbol):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def post_trade(self, trade):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close_trade(self, trade_id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_trade(self, trade_id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def update_trade(self, trade):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def cancel_trade(self, trade_id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_position_info(self, symbol):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_all_positions(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_all_open_trades(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close_position(self, side, symbol):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close_all_positions(self):
|
||||||
|
pass
|
@ -1,26 +1,106 @@
|
|||||||
|
from datetime import time
|
||||||
|
|
||||||
|
import freezegun
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from core.models import (
|
||||||
|
Account,
|
||||||
|
Hook,
|
||||||
|
OrderSettings,
|
||||||
|
RiskModel,
|
||||||
|
Signal,
|
||||||
|
Strategy,
|
||||||
|
TradingTime,
|
||||||
|
User,
|
||||||
|
)
|
||||||
from core.trading import checks
|
from core.trading import checks
|
||||||
|
|
||||||
from core.models import TradingTime, Strategy, OrderSettings, User
|
|
||||||
|
|
||||||
class ChecksTestCase(TestCase):
|
class ChecksTestCase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.time_8 = time(8, 0, 0)
|
||||||
|
self.time_16 = time(16, 0, 0)
|
||||||
self.user = User.objects.create_user(
|
self.user = User.objects.create_user(
|
||||||
username="testuser", email="test@example.com", password="test"
|
username="testuser", email="test@example.com", password="test"
|
||||||
)
|
)
|
||||||
self.order_settings = OrderSettings.objects.create(user=self.user, name="Default")
|
self.account = Account.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
name="Test Account",
|
||||||
|
exchange="fake",
|
||||||
|
)
|
||||||
|
self.order_settings = OrderSettings.objects.create(
|
||||||
|
user=self.user, name="Default"
|
||||||
|
)
|
||||||
self.trading_time_now = TradingTime.objects.create(
|
self.trading_time_now = TradingTime.objects.create(
|
||||||
user=self.user,
|
user=self.user,
|
||||||
name="Test Trading Time",
|
name="Test Trading Time",
|
||||||
start_day=1, # Monday
|
start_day=1, # Monday
|
||||||
start_time="08:00",
|
start_time=self.time_8,
|
||||||
end_day=1, # Monday
|
end_day=1, # Monday
|
||||||
end_time="16:00",
|
end_time=self.time_16,
|
||||||
|
)
|
||||||
|
self.risk_model = RiskModel.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
name="Test Risk Model",
|
||||||
|
max_loss_percent=50,
|
||||||
|
max_risk_percent=10,
|
||||||
|
max_open_trades=10,
|
||||||
|
max_open_trades_per_symbol=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.strategy = Strategy.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
name="Test Strategy",
|
||||||
|
account=self.account,
|
||||||
|
order_settings=self.order_settings,
|
||||||
|
risk_model=self.risk_model,
|
||||||
|
active_management_enabled=True,
|
||||||
)
|
)
|
||||||
|
self.strategy.trading_times.set([self.trading_time_now])
|
||||||
|
self.strategy.save()
|
||||||
|
|
||||||
|
@freezegun.freeze_time("2023-02-13T09:00:00") # Monday at 09:00
|
||||||
|
def test_within_trading_times_pass(self):
|
||||||
|
self.assertTrue(checks.within_trading_times(self.strategy))
|
||||||
|
|
||||||
self.strategy = Strategy.objects.create(user=self.user, name="Test Strategy", )
|
@freezegun.freeze_time("2023-02-13T17:00:00") # Monday at 17:00
|
||||||
|
def test_within_trading_times_fail(self):
|
||||||
|
self.assertFalse(checks.within_trading_times(self.strategy))
|
||||||
|
|
||||||
|
def test_within_callback_price_deviation_fail(self):
|
||||||
|
price_callback = 100
|
||||||
|
current_price = 200
|
||||||
|
self.assertFalse(
|
||||||
|
checks.within_callback_price_deviation(
|
||||||
|
self.strategy, price_callback, current_price
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_within_callback_price_deviation_pass(self):
|
||||||
|
price_callback = 100
|
||||||
|
current_price = 100.5
|
||||||
|
self.assertTrue(
|
||||||
|
checks.within_callback_price_deviation(
|
||||||
|
self.strategy, price_callback, current_price
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_within_trends(self):
|
||||||
|
hook = Hook.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
name="Test Hook",
|
||||||
|
)
|
||||||
|
signal = Signal.objects.create(
|
||||||
|
user=self.user,
|
||||||
|
name="Test Signal",
|
||||||
|
hook=hook,
|
||||||
|
type="trend",
|
||||||
|
)
|
||||||
|
self.strategy.trend_signals.set([signal])
|
||||||
|
self.strategy.trends = {"EUR_USD": "buy"}
|
||||||
|
self.strategy.save()
|
||||||
|
self.assertTrue(checks.within_trends(self.strategy, "EUR_USD", "buy"))
|
||||||
|
self.assertFalse(checks.within_trends(self.strategy, "EUR_USD", "sell"))
|
||||||
|
|
||||||
def test_within_trading_times(self):
|
self.assertIsNone(checks.within_trends(self.strategy, "EUR_XXX", "buy"))
|
||||||
pass
|
self.assertIsNone(checks.within_trends(self.strategy, "EUR_XXX", "sell"))
|
||||||
|
@ -1,3 +1,16 @@
|
|||||||
class ActiveManagement(object):
|
class ActiveManagement(object):
|
||||||
def __init__(self, strategy):
|
def __init__(self, strategy):
|
||||||
self.strategy = strategy
|
self.strategy = strategy
|
||||||
|
|
||||||
|
def run_checks(self):
|
||||||
|
pass
|
||||||
|
# Trading Time
|
||||||
|
# Max loss
|
||||||
|
# Trends
|
||||||
|
# Asset Groups
|
||||||
|
# Position Size
|
||||||
|
# Protection
|
||||||
|
# Max open positions
|
||||||
|
# Max open positions per asset
|
||||||
|
# Max risk
|
||||||
|
# Crossfilter
|
||||||
|
Loading…
Reference in New Issue