chore: initial local trading agent import
This commit is contained in:
commit
0591b75401
13 files changed
+1385
No files matched your search
+139
@@ -0,0 +1,139 @@
|
||||
import copy
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import bot
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
|
||||
|
||||
def test_config_check():
|
||||
r = subprocess.run([sys.executable, str(ROOT / 'bot.py'), '--config-check'], cwd=ROOT, capture_output=True, text=True, timeout=20)
|
||||
assert r.returncode == 0, r.stderr
|
||||
data = json.loads(r.stdout)
|
||||
assert data['ok'] is True
|
||||
assert 'symbols' in data['config']
|
||||
|
||||
|
||||
def test_once_live_public_data():
|
||||
# Coinbase scans can take longer when the watch list is large and the
|
||||
# service is also holding the file lock during its own cycle.
|
||||
r = subprocess.run([sys.executable, str(ROOT / 'bot.py'), '--once'], cwd=ROOT, capture_output=True, text=True, timeout=180)
|
||||
assert r.returncode == 0, r.stderr
|
||||
data = json.loads(r.stdout)
|
||||
assert 'equity_usd' in data
|
||||
state = json.loads((ROOT / 'data' / 'state.json').read_text())
|
||||
assert state['last_signals']
|
||||
|
||||
|
||||
def test_close_position_subtracts_buy_and_sell_fees(monkeypatch):
|
||||
events = []
|
||||
monkeypatch.setattr(bot, 'log_event', lambda ev: events.append(copy.deepcopy(ev)))
|
||||
st = {
|
||||
'cash_usd': 0.0,
|
||||
'realized_pnl_usd': 0.0,
|
||||
'daily': {'realized_pnl_usd': 0.0},
|
||||
'positions': {
|
||||
'TST-USD': {
|
||||
'qty': 10.0,
|
||||
'avg_entry': 1.0,
|
||||
'buy_fee_usd': 0.04,
|
||||
}
|
||||
},
|
||||
'last_trade_at': {},
|
||||
}
|
||||
cfg = {'take_profit_pct': 1.0, 'stop_loss_pct': 1.0, 'fee_bps': 40}
|
||||
ev = bot.close_position_if_needed(st, cfg, 'TST-USD', 1.02)
|
||||
assert ev is not None
|
||||
# gross gain = $0.20, sell fee = $0.0408, buy fee = $0.04, net = $0.1192
|
||||
assert round(ev['realized_pnl_usd'], 4) == 0.1192
|
||||
assert ev['buy_fee_usd'] == 0.04
|
||||
assert round(st['realized_pnl_usd'], 4) == 0.1192
|
||||
|
||||
|
||||
def test_close_shadow_position_subtracts_buy_and_sell_fees(monkeypatch):
|
||||
events = []
|
||||
monkeypatch.setattr(bot, 'log_event', lambda ev: events.append(copy.deepcopy(ev)))
|
||||
st = {
|
||||
'shadow_realized_pnl_usd': 0.0,
|
||||
'shadow_positions': {
|
||||
'TST-USD': {
|
||||
'qty': 10.0,
|
||||
'avg_entry': 1.0,
|
||||
'buy_fee_usd': 0.04,
|
||||
}
|
||||
},
|
||||
'shadow_last_trade_at': {},
|
||||
}
|
||||
cfg = {'take_profit_pct': 1.0, 'stop_loss_pct': 1.0, 'fee_bps': 40}
|
||||
ev = bot.close_shadow_position_if_needed(st, cfg, 'TST-USD', 1.02)
|
||||
assert ev is not None
|
||||
assert round(ev['realized_pnl_usd'], 4) == 0.1192
|
||||
assert ev['buy_fee_usd'] == 0.04
|
||||
assert round(st['shadow_realized_pnl_usd'], 4) == 0.1192
|
||||
|
||||
|
||||
def test_trailing_stop_locks_in_winner_before_full_take_profit(monkeypatch):
|
||||
events = []
|
||||
monkeypatch.setattr(bot, 'log_event', lambda ev: events.append(copy.deepcopy(ev)))
|
||||
st = {
|
||||
'cash_usd': 0.0,
|
||||
'realized_pnl_usd': 0.0,
|
||||
'daily': {'realized_pnl_usd': 0.0},
|
||||
'positions': {
|
||||
'TST-USD': {
|
||||
'qty': 10.0,
|
||||
'avg_entry': 1.0,
|
||||
'buy_fee_usd': 0.04,
|
||||
'peak_price': 1.02,
|
||||
}
|
||||
},
|
||||
'last_trade_at': {},
|
||||
}
|
||||
cfg = {
|
||||
'take_profit_pct': 3.0,
|
||||
'stop_loss_pct': 1.2,
|
||||
'fee_bps': 40,
|
||||
'trailing_stop_trigger_pct': 1.6,
|
||||
'trailing_stop_giveback_pct': 0.85,
|
||||
'breakeven_stop_trigger_pct': 1.4,
|
||||
'breakeven_stop_pct': 0.9,
|
||||
}
|
||||
ev = bot.close_position_if_needed(st, cfg, 'TST-USD', 1.011)
|
||||
assert ev is not None
|
||||
assert ev['reason'] == 'trailing_stop'
|
||||
assert ev['realized_pnl_usd'] > 0
|
||||
|
||||
|
||||
def test_breakeven_stop_cuts_roundtrip_before_it_turns_negative(monkeypatch):
|
||||
events = []
|
||||
monkeypatch.setattr(bot, 'log_event', lambda ev: events.append(copy.deepcopy(ev)))
|
||||
st = {
|
||||
'cash_usd': 0.0,
|
||||
'realized_pnl_usd': 0.0,
|
||||
'daily': {'realized_pnl_usd': 0.0},
|
||||
'positions': {
|
||||
'TST-USD': {
|
||||
'qty': 10.0,
|
||||
'avg_entry': 1.0,
|
||||
'buy_fee_usd': 0.04,
|
||||
'peak_price': 1.015,
|
||||
}
|
||||
},
|
||||
'last_trade_at': {},
|
||||
}
|
||||
cfg = {
|
||||
'take_profit_pct': 3.0,
|
||||
'stop_loss_pct': 1.2,
|
||||
'fee_bps': 40,
|
||||
'trailing_stop_trigger_pct': 1.6,
|
||||
'trailing_stop_giveback_pct': 0.85,
|
||||
'breakeven_stop_trigger_pct': 1.4,
|
||||
'breakeven_stop_pct': 0.9,
|
||||
}
|
||||
ev = bot.close_position_if_needed(st, cfg, 'TST-USD', 1.009)
|
||||
assert ev is not None
|
||||
assert ev['reason'] == 'breakeven_stop'
|
||||
assert ev['realized_pnl_usd'] > 0
|
||||
Reference in new issue
Block a user