<jupyter_start><jupyter_text>Trading ContextIn this notebook we will explain how to use a TensorTrade `TradingContext`. In addition we will also explain the backbone behind TensorTrade's major components and how they are used inside the framework. We will begin with an easy example of how to use the most basic context, this involves specifying what `base_intrument` we are using and what `instruments` we are exchanging. By default, the framework sets these as `USD` and `BTC`, however, we would like to specify our instruments as `BTC` and `ETH`. We also don't want to have to put them as parameters to every component we make because we know it will be the same for all components. Therefore, we use a trading context to set these parameters across the entire framework.<jupyter_code>import tensortrade as td

from tensortrade.environments import TradingEnvironment

config = {
    "base_instrument": "USD",
    "instruments": ["BTC", "ETH"]
}

with td.TradingContext(**config):
    
    env = TradingEnvironment(exchange='simulated',
                             action_scheme='discrete',
                             reward_scheme='simple')<jupyter_output><empty_output><jupyter_text>Special KeysThe following keys are considered special configuration keys:  *  `shared`  *  `exchanges`  *  `actions`  *  `rewards`  *  `features`  *  `slippage`  There is one for each major component of the library. This ensures that each component gets the parameters it needs. The ones that get shared across components get stored on either the top level of this dictionary or inside the `shared` special key. After the creating the configuration dictionary, all that needs to be done is to use a `TradingContext` in a `with` statement. Any components that are defined under the `with` statement are injected with shared parameters as well as the parameters for the special configuration corresponding to the component that has been initialized.  Initializing a Trading Environment from a ConfigurationFor example, this configuration sets up a trading environment using a `DiscreteActions` with 24 actions.<jupyter_code>config = {
    "base_instrument": "USD",
    "instruments": ["BTC", "ETH"],
    "actions": {
        "n_actions": 24
    }
}

with TradingContext(**config):
    
    env = TradingEnvironment(exchange='simulated',
                         action_scheme='discrete',
                         reward_scheme='simple')
config = {
    "base_instrument": "USD",
    "instruments": ["BTC", "ETH"],
    "actions": {
        "n_actions_per_instrument": 25,
        'max_allowed_slippage_percent': 5.0
    }
}

with TradingContext(**config):
    
    action_scheme = MultiDiscreteActions()<jupyter_output><empty_output><jupyter_text>Initialize Live Trading Environment with Credentials<jupyter_code>config = {
    "base_instrument": "USD",
    "instruments": ["BTC", "ETH"],
    "actions": {
        "n_actions": 25,
        'max_allowed_slippage_percent': 5.0
    },
    "exchanges": {
        "credentials": {
            "key": "<KEY>",
            "b64secret": "<KEY>",
            "passphrase": "<PASSWORD>"
        }
    }
}


with TradingContext(**config):
    
    env = TradingEnvironment(exchange='coinbase',
                         action_scheme='discrete',
                         reward_scheme='simple')
config = {
    "base_instrument": "USD",
    "instruments": ["BTC", "ETH"],
    "actions": {
        "n_actions": 25,
        'max_allowed_slippage_percent': 5.0
    }
}

exchange = StochasticExchange(base_price=2, base_volume=2)

with TradingContext(**config):
    
    env = TradingEnvironment(exchange=exchange,
                         action_scheme='discrete',
                         reward_scheme='simple')<jupyter_output><empty_output><jupyter_text>Initialize from FileThe two file format that are currently supported for creating trading context are JSON and YAML. The following are examples of what these files might look like for customization of action scheme and exchange parameters. YAML```YAMLbase_instrument: "EURO"instruments: ["BTC", "ETH"]actions:  n_actions: 24  max_allowed_slippage_percent: 5.0exchanges:  credentials:    api_key: "487r63835t4323"    api_secret_key: "do8u43hgiurwfnlveio"    min_trade_price: 1e-7    max_trade_price: 1e7    min_trade_size: 1e-4    max_trade_size: 1e4``` JSON```JSON{    "base_instrument": "EURO",    "instruments": ["BTC", "ETH"],    "exchanges": {      "commission_percent": 0.5,      "base_precision": 0.3,      "instrument_precision": 10,      "min_trade_price": 1e-7,      "max_trade_price": 1e7,      "min_trade_size": 1e-4,      "max_trade_size": 1e4,      "initial_balance": 1e5,      "window_size": 5,      "should_pretransform_obs": true,      "max_allowed_slippage_percent": 3.0,      "slippage_model": "uniform"}```<jupyter_code>yaml_path = "data/config/configuration.yaml"
json_path = "data/config/configuration.json"

with TradingContext.from_yaml(yaml_path):
    
    env = TradingEnvironment(exchange='fbm',
                         action_scheme='discrete',
                         reward_scheme='simple')
    
with TradingContext.from_json(json_path):
    
    env = TradingEnvironment(exchange='fbm',
                         action_scheme='discrete',
                         reward_scheme='simple')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10065727699530516, 'fraction_numerical': 0.012394366197183098, 'mean_word_length': 3.3943894389438944, 'pattern_counts': {'":': 38, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '46550', 'license': 'Apache-2.0', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1490, 'n_words': 455, 'path': 'examples/use_trading_context[outdated].ipynb', 'repo_name': 'NunoEdgarGFlowHub/tensortrade', 'size': '5367'}