Skip to main content

Exchange Simulator

Build Status codecov Python Version

Simulator of a currency exchange. Key features:

  • Very minimalistic (300 lines in code), written in a functional style;
  • You can download a sample data from poloniex.com or provide your data in .csv files;
  • You can set an initial balance, use basic functions like buy, sell, go to the next time step;
  • More advanced features like a total capital calculation, and make portfolio, more about that at the link below;
  • Trading fees and minimum order amounts are supported.

Full API documentation is here: https://sergei-bondarenko.github.io/xchg/

Quick start

The package is available on pypi.org, so you can install it via pip:

pip install xchg

Then we need a market data. For tutorial purposes you can download it with this command which will be available after package installation:

download_candles

It will download 50 candles for ETC, ETH, LTC and XMR cryptocurrencies from poloniex.com exchange and put it in sample_data/ directory in the current path. Or you can download sample_data/ directory from this repository.

Later you can view these .csv files and use your own data in the same format. You can have a different number of currecies, and different set of columns. The only mandatory column is "close" price, as it will used for trading. Just ensure that you have the same time range for different currencies and have no gaps in data.

Prices in .csv files are expressed in a base currency, which will be called cash (if you are curious, in the sample data cash currency is BTC).

Now let's trade!

from xchg import Xchg

# Set an initial balance. Let's set that we have only cash currency at the
# start.
balance = {'cash': 100, 'ETC': 0, 'ETH': 0, 'LTC': 0, 'XMR': 0}

# Set a trading fee which will be paid for each buy or sell trade (in this
# example it's 1%). You can set 0 if you don't want any fee to be paid.
fee = 0.01

# Set a minimum order size expressed in a cash currency. You can not place
# orders less than that value. You can use also set 0 here if you don't
# want to limit a minimum order size.
min_order_size = 0.001

# Create an exchange.
ex = Xchg(fee, min_order_size, data_path='sample_data/', balance=balance)

# Let's buy some coins.
ex = ex.buy('ETC', 7500)
ex = ex.buy('LTC', 15000)

print(ex.balance)
# Output: {'cash': 2.2024749999999926, 'ETC': 7425.0, 'ETH': 0.0, 'LTC': 14850.0, 'XMR': 0.0}

# All done, now we want to wait for 30 time periods.
for _ in range(30):
  ex = ex.next_step()

# Now we want to sell.
ex = ex.sell('ETC', 7425)
ex = ex.sell('LTC', 14850)

print(ex.balance)
# Output: {'cash': 101.1232084225, 'ETC': 0.0, 'ETH': 0.0, 'LTC': 0.0, 'XMR': 0.0}

# We made more than 1 BTC profit, yay!

For developers

Install testing modules:

pip install pytest pytest-flake8 pytest-cov

Use the following command to run tests locally:

pytest --flake8 --cov=xchg --cov-report term-missing -vvv

And do not forget to increase a version in xchg/__init__.py before commiting.

Release files for xchg 6.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xchg 6.0.1
File Size Uploaded
xchg-6.0.1.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xchg 6.0.1
File Interpreter ABI Platform
xchg-6.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / xchg-6.0.1.tar.gz

Download URL xchg-6.0.1.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6a7784fea11212362330c235f2438d443f06f3451bffd995c8a687097ec3ab72
BLAKE2b-256 checksum
How to use checksums
7fb154197b407ea47cd77744f4379c8ff5740d5b4d75067faf798b284b4a65bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release files / xchg-6.0.1-py3-none-any.whl

Download URL xchg-6.0.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad9950b951019b09ef0205488abf60c254c38977af16cdc93b32dc64444c739a
BLAKE2b-256 checksum
How to use checksums
677305e18ed1b1c84de32457f0e4d049050c5d8fbc30164f71b2367dca231635
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

6.0.1 This release

2 release files

6.0.0

2 release files

5.1.4

2 release files

5.1.3

2 release files

5.1.2

2 release files

5.1.1

2 release files

5.1.0

2 release files

5.0.2

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page