Skip to content

ASX Gym Sample Agents

James Shen edited this page Jun 13, 2020 · 1 revision

The Env provides two sample agents for your reference when you design your own smarter agents:-)

Random Agent

class RandomAgent(DummyAgent):
    def __init__(self, env, min_volume=10, max_volume=100):
        self.env = env
        self.env_action = self.env.action_space.sample()
        self.min_volume = min_volume
        self.max_volume = max_volume

    def action(self):
        simulate_company_list = self.env.simulate_company_list
        company_count = len(simulate_company_list)
        self.env_action['company_count'] = company_count
        end = random.randint(0, 10)
        if end > 7:
            end_batch = 1
        else:
            end_batch = 0

        asx_action = AsxAction(end_batch)
        for c in range(len(simulate_company_list)):
            bet = random.randint(0, 10)
            company_id_index = random.randint(0, company_count) % company_count
            if bet > 7:
                stock_operation = BUY_STOCK
                price = 1000
            elif bet > 2:
                stock_operation = SELL_STOCK
                price = 1.0
            else:
                stock_operation = HOLD_STOCK
                price = 0
            volume = random.randint(self.min_volume, self.max_volume)
            asx_transaction = AsxTransaction(simulate_company_list[company_id_index],
                                             stock_operation, volume, price)
            asx_action.add_transaction(asx_transaction)

        asx_action.copy_to_env_action(self.env_action)
        return self.env_action

Random agent randomly buys and sell or hold a stock, default min-max volume is between 10 and 100.

Buy and keep Agent

from agents.dummy_agent import DummyAgent
from asx_gym.envs import BUY_STOCK, HOLD_STOCK
from asx_gym.envs import AsxAction, AsxTransaction


class BuyAndKeepAgent(DummyAgent):
    def __init__(self, env, company_id, min_volume=10, max_volume=100):
        self.env = env
        self.env_action = self.env.action_space.sample()
        self.min_volume = min_volume
        self.max_volume = max_volume
        self.company_id = company_id
        self.asx_hold_action = AsxAction(1)
        asx_transaction = AsxTransaction(company_id,
                                         HOLD_STOCK, 0, 0)
        self.asx_hold_action.add_transaction(asx_transaction)
        self.bought_stock = False

    def action(self):
        if not self.bought_stock:
            self.bought_stock = True
            asx_action = AsxAction(1)
            asx_transaction = AsxTransaction(self.company_id,
                                             BUY_STOCK, 0, 10000)
            asx_action.add_transaction(asx_transaction)
        else:
            asx_action = self.asx_hold_action

        asx_action.copy_to_env_action(self.env_action)
        return self.env_action

    def reset(self):
        self.bought_stock = False

This agent buy once and hold the stock. this agent can be used to check how stock price changes over time.

ASX GYM

Clone this wiki locally