Skip to content

Commit

Permalink
TraderAgent -> BaseTraderAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmc committed Dec 12, 2023
1 parent ef02b65 commit 11b6bd0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
6 changes: 3 additions & 3 deletions pdr_backend/trader/approach1/trader_agent1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

from pdr_backend.ppss.ppss import PPSS
from pdr_backend.models.feed import Feed
from pdr_backend.trader.trader_agent import TraderAgent
from pdr_backend.trader.base_trader_agent import BaseTraderAgent


@enforce_types
class TraderAgent1(TraderAgent):
class TraderAgent1(BaseTraderAgent):
"""
@description
TraderAgent Naive CCXT
Naive trader agent.
- Market order buy-only
- Doesn't save client state or manage pending open trades
- Only works with MEXC. How to improve:
Expand Down
17 changes: 3 additions & 14 deletions pdr_backend/trader/approach2/trader_agent2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,14 @@
Order,
create_order,
)
from pdr_backend.trader.trader_agent import TraderAgent
from pdr_backend.trader.base_trader_agent import BaseTraderAgent


@enforce_types
class TraderAgent2(TraderAgent):
class TraderAgent2(BaseTraderAgent):
"""
@description
TraderAgent Naive CCXT
This is a naive algorithm. It will simply:
1. If open position, close it
2. If new prediction up, open long
3. If new prediction down, open short
You can improve this by:
1. Improving the type of method to buy/exit (i.e. limit)
2. Improving the buy Conditional statement
3. Enabling buying and shorting
4. Using SL and TP
Trader agent that's slightly less naive than agent 1.
"""

def __init__(self, ppss: PPSS):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


# pylint: disable=too-many-instance-attributes
class TraderAgent:
class BaseTraderAgent:
def __init__(
self,
ppss: PPSS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
INIT_BLOCK_NUMBER,
setup_take_step,
)
from pdr_backend.trader.trader_agent import TraderAgent
from pdr_backend.trader.base_trader_agent import BaseTraderAgent


@enforce_types
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
def test_trader_agent_constructor(check_subscriptions_and_subscribe_mock):
do_constructor(TraderAgent, check_subscriptions_and_subscribe_mock)
do_constructor(BaseTraderAgent, check_subscriptions_and_subscribe_mock)


@enforce_types
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
def test_trader_agent_run(check_subscriptions_and_subscribe_mock):
do_run(TraderAgent, check_subscriptions_and_subscribe_mock)
do_run(BaseTraderAgent, check_subscriptions_and_subscribe_mock)


@enforce_types
@pytest.mark.asyncio
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
async def test_trader_agent_take_step(
check_subscriptions_and_subscribe_mock,
monkeypatch,
):
agent = setup_take_step(
TraderAgent,
BaseTraderAgent,
check_subscriptions_and_subscribe_mock,
monkeypatch,
)
Expand All @@ -57,7 +57,7 @@ def custom_do_trade(feed, prediction):


@pytest.mark.asyncio
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
async def test_process_block_at_feed( # pylint: disable=unused-argument
check_subscriptions_and_subscribe_mock,
monkeypatch,
Expand All @@ -73,7 +73,7 @@ async def test_process_block_at_feed( # pylint: disable=unused-argument
monkeypatch,
)

agent = TraderAgent(ppss, custom_do_trade)
agent = BaseTraderAgent(ppss, custom_do_trade)

feed_addr = feed.address
agent.prev_traded_epochs_per_feed.clear()
Expand Down Expand Up @@ -113,22 +113,22 @@ async def _do_trade(feed, prediction): # pylint: disable=unused-argument


@enforce_types
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
def test_save_and_load_cache(
check_subscriptions_and_subscribe_mock,
): # pylint: disable=unused-argument
feed, ppss = mock_feed_ppss("5m", "binance", "BTC/USDT")
inplace_mock_feedgetters(ppss.web3_pp, feed) # mock publishing feeds

agent = TraderAgent(ppss, custom_do_trade, cache_dir=".test_cache")
agent = BaseTraderAgent(ppss, custom_do_trade, cache_dir=".test_cache")

agent.prev_traded_epochs_per_feed = {
feed.address: [1, 2, 3],
}

agent.update_cache()

agent_new = TraderAgent(ppss, custom_do_trade, cache_dir=".test_cache")
agent_new = BaseTraderAgent(ppss, custom_do_trade, cache_dir=".test_cache")
assert agent_new.prev_traded_epochs_per_feed[feed.address] == [3]
cache_dir_path = (
Path(os.path.dirname(os.path.abspath(__file__))).parent.parent
Expand All @@ -140,14 +140,14 @@ def test_save_and_load_cache(


@pytest.mark.asyncio
@patch.object(TraderAgent, "check_subscriptions_and_subscribe")
@patch.object(BaseTraderAgent, "check_subscriptions_and_subscribe")
async def test_get_pred_properties(
check_subscriptions_and_subscribe_mock,
): # pylint: disable=unused-argument
feed, ppss = mock_feed_ppss("5m", "binance", "BTC/USDT")
inplace_mock_feedgetters(ppss.web3_pp, feed) # mock publishing feeds

agent = TraderAgent(ppss)
agent = BaseTraderAgent(ppss)
check_subscriptions_and_subscribe_mock.assert_called_once()

agent.get_pred_properties = Mock()
Expand Down

0 comments on commit 11b6bd0

Please sign in to comment.