|
| 1 | +##With this sample you`ll be able to: |
| 2 | +* get order book (Level II, market depth) from [BitMEX](https://www.bitmex.com) and save as json structures in day-lenght files |
| 3 | +* generate trading signals [BUY, SELL, WAIT] on orderbooks (not only BitMEX\`s) with or without help of sequential neural network |
| 4 | +* create training dataset for neural network from presaved orderbooks |
| 5 | +* create, train, save and restore neural network with input data of orderbooks (not only BitMEX`s) |
| 6 | +* perform backtest trading with output of trades |
| 7 | + |
| 8 | +###Limitations |
| 9 | +* [BitMEX](https://www.bitmex.com) only and [XBTUSD](https://www.bitmex.com/app/contract/XBTUSD) data fetching only are provided. The source connection code taken from [Python Adapter for BitMEX Realtime Data](https://github.com/BitMEX/api-connectors/tree/master/official-ws/python) |
| 10 | +* Code works correctly only for 100-depth order books. |
| 11 | +* Neural network has voluntaristic rigid architecture where you can operate only number of layers and neurons quantity. Input layer must contain 100 neurons, output layer 2 neurons. |
| 12 | +* No commissions, fees etc. are calculated while backtesting. Result trades must be extra analyzed. |
| 13 | + |
| 14 | + |
| 15 | +##Installation |
| 16 | +``` |
| 17 | +pip install -r requirements.txt |
| 18 | +``` |
| 19 | + |
| 20 | +##Use cases |
| 21 | +####Retrieving order books ([Bitmex](https://www.bitmex.com) only) |
| 22 | +Just run code below with your [API-key credentials to BitMEX](https://www.bitmex.com/app/apiKeysUsage). |
| 23 | +On every update of market 100-depth order book is writing to disk. |
| 24 | +[Bid-ask spread](https://en.wikipedia.org/wiki/Bid-ask_spread) is in the middle of order book. New trading day starts with new file. |
| 25 | + |
| 26 | +```python |
| 27 | +from BitmexOrderBookSaver import * |
| 28 | +api_key = '' |
| 29 | +api_secret = '' |
| 30 | +save_folder = '' |
| 31 | +bitmex = BitmexOrderBookSaver(api_key, api_secret, save_folder) |
| 32 | +print('Retrieving orderbooks market data. Press any key to stop') |
| 33 | +input() |
| 34 | +bitmex.exit() |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +####Dataset creation for neural network training |
| 39 | + |
| 40 | +```python |
| 41 | +from OrderBookContainer import * |
| 42 | + |
| 43 | +folder='' |
| 44 | +input_files = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))] |
| 45 | +for in_file in input_files: |
| 46 | +obc = OrderBookContainer(os.path.join(folder, in_file)) |
| 47 | +obc.create_training_dataset() |
| 48 | +``` |
| 49 | +As a result the script will create Datasets subfolder with \*.ds files. |
| 50 | + |
| 51 | + |
| 52 | +####Neural network creation, training and saving for next time use |
| 53 | + |
| 54 | +My goal is just to show that neural networks work without price movement analysis but only on current market timestamp (== order book) analysis. |
| 55 | + |
| 56 | +So, network gets only order book volumes as input and generates floating point value as output. **Really, there are no prices in input data!** |
| 57 | + |
| 58 | +- Is it possible to predict price movements without price analysis? :tw-1f62e: |
| 59 | +- Yes! :tw-1f60e: |
| 60 | + |
| 61 | +The code below will create three-layered feed-forward sequential network. I use [Keras framework](https://keras.io/). |
| 62 | + |
| 63 | +I use sigmoid activation function for all layers except for last one where softmax is used. |
| 64 | +The first layer consists of 100 neurons, one for each line in order book. |
| 65 | +The last layer must contain of 2 neurons because of two variants are possible - BUY and SELL. |
| 66 | + |
| 67 | +```python |
| 68 | +import TurexNetwork |
| 69 | + |
| 70 | +nwk = TurexNetwork.TurexNetwork() |
| 71 | +nwk.create_model((100, 50, 2)) |
| 72 | +datasets_folder='' |
| 73 | +nwk.train(datasets_folder) |
| 74 | +nwk.save_model('full_path_to_file.h5') |
| 75 | +``` |
| 76 | + |
| 77 | +####Trading signal generation |
| 78 | + |
| 79 | +You can generate trading signal with possible values of [BUY, SELL, WAIT] with order book analysis only. |
| 80 | +On every *orderbook* you get from exchange or read from file signal can be generated with code below. |
| 81 | +*threshold* is floating point value in range [0, 1]. The less the value the more signals you get. |
| 82 | +######Neural generator |
| 83 | + |
| 84 | +```python |
| 85 | +from Generators import sample_generator_n |
| 86 | +nwk = TurexNetwork.TurexNetwork() |
| 87 | +nwk.load_model('model_from_code_above.h5') |
| 88 | +signal = sample_generator_n(nwk, orderbook.volumes, threshold) |
| 89 | +``` |
| 90 | +######Simple generator |
| 91 | +```python |
| 92 | +from Generators import sample_generator |
| 93 | +signal = sample_generator(orderbook.volumes, threshold) |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +####Backtesting |
| 98 | +The mean of *threshold* is described above. |
| 99 | + |
| 100 | +```python |
| 101 | +import TurexNetwork |
| 102 | +import Generators |
| 103 | +from OrderBookContainer import * |
| 104 | + |
| 105 | +obc = OrderBookContainer('path_to_orderbook_file.txt') |
| 106 | + |
| 107 | +nwk = TurexNetwork.TurexNetwork() |
| 108 | +nwk.load_model('path_to_model_file.h5') |
| 109 | +threshold = 0.0 |
| 110 | +trades = obc.backtest_n(Generators.sample_neural_generator, nwk, threshold) |
| 111 | +#trades = obc.backtest_n(Generators.sample_generator, threshold) |
| 112 | +print(trades) |
| 113 | +``` |
0 commit comments