예제 #1
0
    def test_get_reward(self, net_worths):
        portfolio = Portfolio(USD)
        performance = pd.DataFrame({'net_worth': net_worths})
        portfolio._performance = performance

        pct_chg = net_worths.pct_change()

        reward_scheme = SimpleProfit()
        assert reward_scheme.get_reward(portfolio) == pct_chg.iloc[
            -1]  # default window size 1

        reward_scheme.window_size = 3
        reward = ((1 + pct_chg.iloc[-1]) * (1 + pct_chg.iloc[-2]) *
                  (1 + pct_chg.iloc[-3])) - 1
        assert reward_scheme.get_reward(portfolio) == reward
예제 #2
0
    def test_reward_invalid_trade_type_input(self):
        scheme = SimpleProfit()
        scheme.reset()

        # Create the first trade
        trade_1 = Trade(0, "BTC", 1, 100, 1500)
        trade_2 = Trade(2, "BTC", TradeType.LIMIT_SELL, 100, 1300)

        scheme.get_reward(0, trade_1)
        reward1 = scheme.get_reward(2, trade_2)
        assert reward1 == -1015.5908812273915
def test_runs_with__external_feed_only(portfolio):

    df = pd.read_csv("tests/data/input/coinbase_(BTC,ETH)USD_d.csv").tail(100)
    df = df.rename({"Unnamed: 0": "date"}, axis=1)
    df = df.set_index("date")

    coinbase_btc = df.loc[:, [name.startswith("BTC") for name in df.columns]]
    coinbase_eth = df.loc[:, [name.startswith("ETH") for name in df.columns]]

    ta.add_all_ta_features(
        coinbase_btc,
        colprefix="BTC:",
        **{k: "BTC:" + k for k in ['open', 'high', 'low', 'close', 'volume']}
    )
    ta.add_all_ta_features(
        coinbase_eth,
        colprefix="ETH:",
        **{k: "ETH:" + k for k in ['open', 'high', 'low', 'close', 'volume']}
    )

    nodes = []
    with Module("coinbase") as coinbase:
        for name in coinbase_btc.columns:
            nodes += [Stream(name, list(coinbase_btc[name]))]
        for name in coinbase_eth.columns:
            nodes += [Stream(name, list(coinbase_eth[name]))]

    feed = DataFeed()(coinbase)

    action_scheme = ManagedRiskOrders()
    reward_scheme = SimpleProfit()

    env = TradingEnvironment(
        portfolio=portfolio,
        action_scheme=action_scheme,
        reward_scheme=reward_scheme,
        feed=feed,
        window_size=50,
        use_internal=False,
        enable_logger=False
    )

    done = False
    obs = env.reset()
    while not done:

        action = env.action_space.sample()
        obs, reward, done, info = env.step(action)

    n_features = coinbase_btc.shape[1] + coinbase_eth.shape[1]
    assert obs.shape == (50, n_features)
예제 #4
0
    def test_raise_reward(self):
        scheme = SimpleProfit()
        scheme.reset()

        # Create the first trade
        trade_1 = Trade(0, "BTC", TradeType.LIMIT_BUY, 100, 1000)
        trade_2 = Trade(2, "BTC", TradeType.LIMIT_SELL, 100, 1500)

        scheme.get_reward(0, trade_1)
        reward1 = scheme.get_reward(2, trade_2)
        
        assert reward1 == 1926.0370135765718
예제 #5
0
def test_init_multiple_exchanges(portfolio):

    action_scheme = ManagedRiskOrders()
    reward_scheme = SimpleProfit()

    env = TradingEnvironment(portfolio=portfolio,
                             action_scheme=action_scheme,
                             reward_scheme=reward_scheme,
                             window_size=50,
                             enable_logger=False)

    obs = env.reset()

    assert obs.shape == (50, 32)

    assert env.observation_space.shape == (50, 32)
예제 #6
0
def test_runs_with_only_internal_data_feed(portfolio):

    action_scheme = ManagedRiskOrders()
    reward_scheme = SimpleProfit()

    env = TradingEnvironment(portfolio=portfolio,
                             action_scheme=action_scheme,
                             reward_scheme=reward_scheme,
                             window_size=50,
                             enable_logger=False)

    done = False
    obs = env.reset()
    while not done:

        action = env.action_space.sample()
        obs, reward, done, info = env.step(action)

    assert obs.shape == (50, 32)
예제 #7
0
normalized_price = MinMaxNormalizer(price_columns)
normalized_volume = MinMaxNormalizer(volume_column)
sma = SimpleMovingAverage(columns=price_columns, window_size=50)
indicators = TAlibIndicator(
    indicators=["EMA", "RSI", "CCI", "Stochastic", "MACD"],
    lows=[30, -100, 20],
    highs=[70, 100, 80])
difference_all = FractionalDifference(difference_order=0.6)
feature_pipeline = FeaturePipeline(steps=[
    normalized_price, sma, difference_all, normalized_volume, indicators
])
exchange.feature_pipeline = feature_pipeline
# -------------------------- Action Schemes ------------------------#
action_scheme = DiscreteActions(n_actions=20, instrument='BTC/USD')
# -------------------------- Reward Schemes ------------------------#
reward_scheme = SimpleProfit()
# -------------------------- Live Exchange ------------------------#
# import ccxt
# from tensortrade.exchanges.live import CCXTExchange
# coinbase = ccxt.coinbasepro()
# exchange = CCXTExchange(exchange=coinbase, base_instrument='USD')
# -------------------------- Simulated Exchange ------------------------#
# df = pd.read_csv('./data/Coinbase_BTCUSD_1h.csv')
# exchange = SimulatedExchange(data_frame=df, base_instrument='USD',feature_pipeline=feature_pipeline)

# from tensortrade.exchanges.simulated import FBMExchange
# exchange = FBMExchange(base_instrument='BTC', timeframe='1h', feature_pipeline=feature_pipeline)
# #################### Creating an Environment ######################

environment = TradingEnvironment(exchange=exchange,
                                 action_scheme=action_scheme,