def test_difference_not_inplace(self, data_frame, exchange):
        transformer = FractionalDifference(difference_order=0.5, inplace=False)

        transformed_frame = transformer.transform(data_frame,
                                                  exchange.generated_space)

        expected_data_frame = pd.DataFrame([{
            'open': 100,
            'low': 50,
            'high': 250,
            'close': 200,
            'open_diff_0.5': -26.20469322,
            'low_diff_0.5': -46.15180724,
            'high_diff_0.5': 33.63664884,
            'close_diff_0.5': 13.68953482,
        }, {
            'open': 200,
            'low': 150,
            'high': 350,
            'close': 300,
            'open_diff_0.5': 134.53651465,
            'low_diff_0.5': 118.24976426,
            'high_diff_0.5': 183.39676584,
            'close_diff_0.5': 167.11001545,
        }])

        assert np.allclose(expected_data_frame.values,
                           transformed_frame.values)
    def test_incremental_difference_inplace_false(self, data_frame,
                                                  reference_frame):

        frame_tail = data_frame.tail(200)

        transformer = FractionalDifference(difference_order=0.5, inplace=False)

        transformed_frame = transformer.transform(frame_tail)
        assert transformed_frame is not None
        assert isinstance(frame_tail, pd.DataFrame)
    def test_transform_space(self, data_frame):
        transformer = FractionalDifference(
            difference_order=0.5, inplace=False, all_column_names=data_frame.columns)

        low = np.array([1E-3, ] * 4 + [1E-3, ])
        high = np.array([1E3, ] * 4 + [1E3, ])

        input_space = Box(low=low, high=high, dtype=np.float16)

        transformed_space = transformer.transform_space(input_space)

        assert transformed_space != input_space
    def test_incremental_difference(self, data_frame, exchange):
        transformer = FractionalDifference(difference_order=0.5, inplace=True)

        transformed_frame = transformer.transform(data_frame,
                                                  exchange.generated_space)

        expected_data_frame = pd.DataFrame([{
            'open': -26.20469322,
            'low': -46.15180724,
            'high': 33.63664884,
            'close': 13.68953482,
        }, {
            'open': 134.53651465,
            'low': 118.24976426,
            'high': 183.39676584,
            'close': 167.11001545,
        }])

        assert np.allclose(expected_data_frame.values,
                           transformed_frame.values)

        next_frame = pd.DataFrame([{
            'open': 200,
            'low': 150,
            'high': 350,
            'close': 300,
        }, {
            'open': 300,
            'low': 250,
            'high': 450,
            'close': 400,
        }])

        transformed_frame = transformer.transform(next_frame,
                                                  exchange.generated_space)

        expected_data_frame = pd.DataFrame([{
            'open': 127.785105,
            'low': 87.031409,
            'high': 250.046192,
            'close': 209.292496,
        }, {
            'open': 185.484853,
            'low': 166.817514,
            'high': 241.486873,
            'close': 222.819533,
        }])

        assert np.allclose(expected_data_frame.values,
                           transformed_frame.values)
    def test_incremental_difference(self, data_frame, reference_frame):
        frame_tail = data_frame.tail(200)

        transformer = FractionalDifference(difference_order=0.5, inplace=True)

        significant = 3

        transformed_frame = transformer.transform(frame_tail)

        reference_frame = reference_frame.round(significant)
        transformed_frame = transformed_frame.round(significant)

        close_1 = transformed_frame.Close.values
        close_2 = reference_frame.Close.values

        is_valid = (close_1 == close_2).all()

        assert transformed_frame is not None
        assert is_valid
    def test_select_correct_columns(self, data_frame, exchange):
        transformer = FractionalDifference(
            columns=['open', 'close'], difference_order=0.5, inplace=True)

        transformed_frame = transformer.transform(data_frame, exchange.generated_space)

        expected_data_frame = pd.DataFrame([{
            'open': -26.20469322,
            'low': 50,
            'high': 250,
            'close': 13.68953482,
        },
            {
            'open': 134.53651465,
            'low': 150,
            'high': 350,
            'close': 167.11001545,
        }])

        assert np.allclose(expected_data_frame.values, transformed_frame.values)
    def test_difference_inplace(self, data_frame):
        transformer = FractionalDifference(
            difference_order=0.5, inplace=True, all_column_names=data_frame.columns)

        transformed_frame = transformer.transform(data_frame)

        expected_data_frame = pd.DataFrame([{
            'open': -26.20469322,
            'low': -46.15180724,
            'high': 33.63664884,
            'close': 13.68953482,
        },
            {
            'open': 134.53651465,
            'low': 118.24976426,
            'high': 183.39676584,
            'close': 167.11001545,
        }])

        assert np.allclose(expected_data_frame.values, transformed_frame.values)
    def test_incremental_transform(self, data_frame):
        difference_all = FractionalDifference(
            difference_order=0.5,
            inplace=True,
            all_column_names=data_frame.columns)

        feature_pipeline = FeaturePipeline(steps=[difference_all])

        transformed_frame = feature_pipeline.transform(data_frame)

        expected_data_frame = pd.DataFrame([{
            'open': -26.20469322,
            'low': -46.15180724,
            'high': 33.63664884,
            'close': 13.68953482,
        }, {
            'open': 134.53651465,
            'low': 118.24976426,
            'high': 183.39676584,
            'close': 167.11001545,
        }])

        assert np.allclose(expected_data_frame.values,
                           transformed_frame.values)

        next_frame = pd.DataFrame([{
            'open': 200,
            'low': 150,
            'high': 350,
            'close': 300,
        }, {
            'open': 300,
            'low': 250,
            'high': 450,
            'close': 400,
        }])

        transformed_frame = feature_pipeline.transform(next_frame)

        expected_data_frame = pd.DataFrame([{
            'open': 127.785105,
            'low': 87.031409,
            'high': 250.046192,
            'close': 209.292496,
        }, {
            'open': 185.484853,
            'low': 166.817514,
            'high': 241.486873,
            'close': 222.819533,
        }])

        assert np.allclose(expected_data_frame.values,
                           transformed_frame.values)
Exemplo n.º 9
0
    def test_transform_space(self, data_frame, exchange):
        difference_all = FractionalDifference(difference_order=0.5, inplace=False)

        feature_pipeline = FeaturePipeline(steps=[difference_all])

        low = np.array([1E-3, ] * 4 + [1E-3, ])
        high = np.array([1E3, ] * 4 + [1E3, ])

        input_space = Box(low=low, high=high, dtype=np.float16)

        transformed_space = feature_pipeline.transform_space(
            input_space, exchange.generated_columns)

        assert transformed_space != input_space
Exemplo n.º 10
0
def get_env(file_path):
    df = load_data(file_path)

    normalize = MinMaxNormalizer(inplace=True)
    difference = FractionalDifference(difference_order=0.6, inplace=True)
    feature_pipeline = FeaturePipeline(steps=[normalize, difference])

    reward_strategy = SimpleProfitStrategy()
    action_strategy = DiscreteActionStrategy(n_actions=20, instrument_symbol='BTC/USDT')

    exchange = SimulatedExchange(base_instrument='USDT',
                                 should_pretransform_obs=True,
                                 feature_pipeline=feature_pipeline
                                 )
    exchange.data_frame = df[:STEP]
    environment = TradingEnvironment(exchange=exchange,
                                     action_strategy=action_strategy,
                                     reward_strategy=reward_strategy,
                                     feature_pipeline=feature_pipeline)

    return environment
import tensorflow
assert tensorflow.__version__ == '1.13.1'

from tensortrade.strategies import TensorforceTradingStrategy
from tensortrade.rewards import DirectProfitStrategy
from tensortrade.actions import FutureActionStrategy
reward_strategy = DirectProfitStrategy()
action_strategy = FutureActionStrategy()

#%% Feature Pipeline
from tensortrade.features.stationarity import FractionalDifference
from tensortrade.features.scalers import MinMaxNormalizer
from tensortrade.features import FeaturePipeline

normalize = MinMaxNormalizer(inplace=True)
difference = FractionalDifference(difference_order=0.6, inplace=True)
feature_pipeline = FeaturePipeline(steps=[])

#%% Data Input
from tensortrade.exchanges.simulated.future_exchange import FutureExchange
data = pd.read_csv('TA.csv', index_col=0)

data = data[data.index % 30 == 0]
data = data.reset_index(drop=True)

exchange = FutureExchange(data,
                          base_instrument='RMB',
                          exclude_close=True,
                          initial_balance=10000,
                          should_pretransform_obs=False)
Exemplo n.º 12
0
df = pd.read_csv('./data/btc_ohclv_1h.csv')

exchange = SimulatedExchange(data_frame=df, base_instrument='USD')

# Feature Pipelines

from tensortrade.features import FeaturePipeline
from tensortrade.features.scalers import MinMaxNormalizer
from tensortrade.features.stationarity import FractionalDifference
from tensortrade.features.indicators import SimpleMovingAverage

price_columns = ['open', 'high', 'low', 'close']

normalize_price = MinMaxNormalizer(price_columns)
moving_averages = SimpleMovingAverage(price_columns)
difference_all = FractionalDifference(difference_order=0.6)

feature_pipeline = FeaturePipeline(
    steps=[normalize_price, moving_averages, difference_all])

exchange.feature_pipeline = feature_pipeline

# Action Strategies

from tensortrade.actions import DiscreteActionStrategy

action_strategy = DiscreteActionStrategy(n_actions=20, instrument_symbol='BTC')

# Reward Strategies

from tensortrade.rewards import SimpleProfitStrategy