agent_system / forces.py
janggi3's picture
Upload folder using huggingface_hub
d96a847 verified
Raw History Blame Contribute Delete
4.3 kB
# file: agent_system/forces.py
from core import AssetAllocation
class BaseForce:
def __init__(self, name, tags=None, allocation=None):
self.name = name
self.tags = tags if tags else set()
self.allocation = allocation if allocation else AssetAllocation({})
# Add id to satisfy Force interface if needed by some components,
# though core.Force is a dataclass, duck typing usually works.
self.id = name
def get_energy(self, context):
"""Higher energy = Higher penalty (Brain avoids this)"""
raise NotImplementedError("Each force must implement get_energy")
class LongForce(BaseForce):
def __init__(self):
super().__init__(
name="LONG",
tags={"momentum", "aggressive", "bull_trend", "long"},
allocation=AssetAllocation({"BTC": 1.0, "USDT": 0.0})
)
def get_energy(self, context):
tags = context.get('tags', set())
metrics = context.get('metrics', {})
ema_delta = metrics.get('ema_delta', 0.0)
# Base Energy
energy = 10.0
if "bull_trend" in tags:
# The stronger the Bull Delta, the lower (better) the energy
# e.g., if delta is 0.05 (5%), energy becomes 0.0 - (0.05 * 100) = -5.0
energy = 0.0 - (ema_delta * 100.0)
if "bear_trend" in tags:
energy = 20.0
if "crash" in tags:
energy = 100.0
return energy
class ShortForce(BaseForce):
def __init__(self):
super().__init__(
name="SHORT",
tags={"defensive", "short", "bear_trend"},
# Shorting mechanism depends on exchange.
# For simplicity here, we might map this to holding USDT (Risk Off)
# OR actual shorting if 'execution' supports it.
# Let's map it to USDT but with 'short' tag so Brain selects it in Bear.
# Ideally: allocation={"BTC": -1.0} but simple spot portfolio can't do -1.
# REVISION: Let's assume we just go to CASH in bear market for this simple agent,
# unless we want to use margin. Let's start with Cash-heavy for ShortForce
# or actually defined as a "Hedge" (Sell BTC).
allocation=AssetAllocation({"BTC": 0.0, "USDT": 1.0})
)
# NOTE: In a real Short strategy, allocation might be negative.
# But if we are Spot trading, "Short" means "Sell everything".
# If we have a dedicated "CashForce", ShortForce is redundant unless it means *margin short*.
# Let's keep it 100% USDT for now to be safe (Bear = Cash).
def get_energy(self, context):
tags = context.get('tags', set())
metrics = context.get('metrics', {})
ema_delta = metrics.get('ema_delta', 0.0)
if "bear_trend" in tags:
# If Bearish, the more negative the delta, the better the energy for Short/Cash
# e.g., if delta is -0.05, energy becomes 0.0 + (-0.05 * 100) = -5.0
return 0.0 + (ema_delta * 100.0)
return 20.0
class CashForce(BaseForce):
def __init__(self):
super().__init__(
name="CASH",
tags={"safe", "stable", "risk_off"},
allocation=AssetAllocation({"BTC": 0.0, "USDT": 1.0})
)
self.is_cash = True
def get_energy(self, context):
tags = context.get('tags', set())
# Cash is the 'Safety Net'
if "high_vol" in tags or "crash" in tags or "bear_trend" in tags:
return -5.0 # Very attractive during chaos
return 2.0 # Slight cost in bull markets to favor LongForce
class ETHForce(BaseForce):
def __init__(self):
super().__init__(
name="ETH_LONG",
tags={"ethereum", "momentum", "long", "aggressive"},
allocation=AssetAllocation({"ETH": 1.0, "USDT": 0.0})
)
def get_energy(self, context):
# We can eventually add ETH specific metrics here.
# For now, it shares some logic with BTC (LongForce) or we give it base logic.
tags = context.get('tags', set())
if "bull_trend" in tags:
return 5.0 # Base attraction in bulls
return 15.0 # neutral/penalty