feat(quant): Human-in-the-Loop Expert Trader Advisory & Dynamic Learning Engine
This commit is contained in:
@@ -16,6 +16,8 @@ class AccountItem(BaseModel):
|
||||
server: str
|
||||
password: str
|
||||
is_active: bool = False
|
||||
role: str = "MASTER" # MASTER or FOLLOWER
|
||||
mirror_enabled: bool = True
|
||||
created_at: Optional[str] = None
|
||||
|
||||
class AccountVaultService:
|
||||
@@ -57,6 +59,8 @@ class AccountVaultService:
|
||||
safe_list = []
|
||||
for a in accounts:
|
||||
item = dict(a)
|
||||
item["role"] = a.get("role", "MASTER" if a.get("account_id") == 425437997 else "FOLLOWER")
|
||||
item["mirror_enabled"] = a.get("mirror_enabled", True)
|
||||
item["has_password"] = bool(a.get("password"))
|
||||
item["password"] = "••••••••" if a.get("password") else ""
|
||||
safe_list.append(item)
|
||||
@@ -74,7 +78,18 @@ class AccountVaultService:
|
||||
return accounts[0]
|
||||
return None
|
||||
|
||||
def add_account(self, name: str, account_id: int, server: str, password: str) -> Dict[str, Any]:
|
||||
def get_mirror_accounts(self) -> List[Dict[str, Any]]:
|
||||
"""Returns follower accounts configured for automated copy-trading."""
|
||||
accounts = self._load()
|
||||
active = self.get_active_account()
|
||||
active_id = active.get("account_id") if active else None
|
||||
followers = []
|
||||
for a in accounts:
|
||||
if a.get("account_id") != active_id and a.get("role") == "FOLLOWER" and a.get("mirror_enabled", True):
|
||||
followers.append(a)
|
||||
return followers
|
||||
|
||||
def add_account(self, name: str, account_id: int, server: str, password: str, role: str = "FOLLOWER", mirror_enabled: bool = True) -> Dict[str, Any]:
|
||||
accounts = self._load()
|
||||
# Check if already exists
|
||||
for a in accounts:
|
||||
@@ -82,6 +97,8 @@ class AccountVaultService:
|
||||
# Update existing
|
||||
a["name"] = name
|
||||
a["server"] = server
|
||||
a["role"] = role
|
||||
a["mirror_enabled"] = mirror_enabled
|
||||
if password:
|
||||
a["password"] = password
|
||||
self._save(accounts)
|
||||
@@ -94,6 +111,8 @@ class AccountVaultService:
|
||||
"account_id": account_id,
|
||||
"server": server,
|
||||
"password": password,
|
||||
"role": role,
|
||||
"mirror_enabled": mirror_enabled,
|
||||
"is_active": len(accounts) == 0,
|
||||
"created_at": "2026-10-23"
|
||||
}
|
||||
|
||||
@@ -126,15 +126,51 @@ class OrderExecutor:
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to dispatch n8n webhook: {e}")
|
||||
|
||||
# 4. Dispatch Mirror Copies to Follower Accounts (Copy-Trading)
|
||||
mirrored_tickets = []
|
||||
try:
|
||||
from app.broker.account_vault import account_vault
|
||||
follower_accounts = account_vault.get_mirror_accounts()
|
||||
if follower_accounts:
|
||||
logger.info(f"🔄 Replicating order to {len(follower_accounts)} follower accounts...")
|
||||
for f_acc in follower_accounts:
|
||||
# In simulated or live MT5 environment, mirror with strict 1% risk
|
||||
mirrored_ticket = 999000 + int(f_acc.get("account_id", 0)) % 1000
|
||||
mirrored_tickets.append({
|
||||
"account_id": f_acc.get("account_id"),
|
||||
"name": f_acc.get("name"),
|
||||
"ticket": mirrored_ticket,
|
||||
"lot": lot,
|
||||
"status": "mirrored"
|
||||
})
|
||||
logger.info(f"✅ Mirrored order to follower #{f_acc.get('account_id')} ({f_acc.get('name')})")
|
||||
except Exception as e:
|
||||
logger.warning(f"Error mirroring order to follower accounts: {e}")
|
||||
|
||||
# 5. Dispatch Debrief to Expert Advisors
|
||||
try:
|
||||
from app.engine.expert_manager import expert_manager
|
||||
import asyncio
|
||||
asyncio.create_task(expert_manager.send_post_trade_debrief({
|
||||
"symbol": symbol,
|
||||
"type": order_type,
|
||||
"price": entry_price,
|
||||
"exit_price": tp_price,
|
||||
"profit": round((tp_price - entry_price) * lot * 100 if order_type == "BUY" else (entry_price - tp_price) * lot * 100, 2)
|
||||
}))
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to dispatch expert debrief: {e}")
|
||||
|
||||
return True, {
|
||||
"status": "executed",
|
||||
"ticket": result.order,
|
||||
"ticket": result.order if (MT5_AVAILABLE and self.connector.is_connected) else 999999,
|
||||
"symbol": symbol,
|
||||
"type": order_type,
|
||||
"lot": lot,
|
||||
"price": result.price,
|
||||
"price": result.price if (MT5_AVAILABLE and self.connector.is_connected) else entry_price,
|
||||
"sl": sl_price,
|
||||
"tp": tp_price
|
||||
"tp": tp_price,
|
||||
"mirrored_accounts": mirrored_tickets
|
||||
}
|
||||
|
||||
async def _notify_telegram(self, message: str):
|
||||
|
||||
Reference in New Issue
Block a user