Tensorflow-bitcoin-robot: A Bitcoin price prediction robot based on the TensorFlow lstm model. __ Robot

Source: Internet
Author: User
Tags install matplotlib
Brief Introduction

Tensorflow-bitcoin-robot: A Bitcoin price prediction robot based on the TensorFlow lstm model.

The article includes a few parts:
1. Why try to do this project.
2. Why did you choose this model?
3. Where does the data from the model come from.
4. The optimization process of the model.
5. The direction in which the project can be further improved.

The recent performance of the digital currency, led by Bitcoin, can only be described in madness. The latest price quotes from the Bitcoin trading platform show that the day before that, the maximum price in the bitcoin is 29838.5 yuan, and the 30,000 Yuan mark is only a short distance away. Bitcoin recently hot market, attracted a lot of attention, there is an artificial intelligence seems omnipotent, then the question, can use artificial intelligence to carry on the bit currency transaction.

What model to use for price forecasting. Now popular deep neural networks, convolution neural networks, cyclic neural networks, because convolution neural networks are more suitable for processing images, cyclic neural networks are better suited to deal with serialized content, especially Lstm is an upgraded version of RNN.

LSTM (long short-term Memory) is a long-term short-term memory network, which is a kind of temporal recurrent neural network, which is suitable for processing and predicting important events with relatively long intervals and delays in time series. LSTM has many applications in the field of science and technology. The LSTM system can learn translation language, control robot, image analysis, document summary, speech recognition image recognition, handwriting recognition, control chat robot, predict disease, click rate and stock, synthesize music and so on task. The transaction record of Bitcoin is the addition of data on the event sequence, which can forecast the future price based on the past transaction record sequence, and the LSTM model is more suitable. The next price can be the result of the forecast.

data sets

The new question comes, where does the data come from.
The required data is a sequence containing the transaction price, which can then be intercepted as an input value, followed by a portion of the predicted value. After looking for a bit, the mainstream trading platform has provided some historical data, but not many. Finally, using the Btctrade, crawling with requests, it contains 50 transactions of Bitcoin.

Get a script for a dataset
Get_trades.py gets these transactions, converts them back to JSON, and displays them in a graphic format for next data analysis.

Dependencies that need to be installed before running:
In order to crawl data, you need to use the requests library, a very useful HTTP library. In order to visualize the transaction data, the matplotlib is used.

Pip Install requests
pip install Matplotlib

Model

rnn_predicter.py

Use the LSMT model. Intercepts 10 transactions as input, and if the 11th price is higher than the 10th, set the output to [1,0,0] if the lower is set to [0,0,1], if the same [0,1,0].

For I in Range (0,20):
    #print (Price)
    One_predictor=np.array (price[i:i+20],dtype=float)
    #print (one_ Predictor)
    Train_x.append (one_predictor)
    if (int (price[i+20)) >int (price[i+21)):
        Train_y.append ( Np.array ([1,0,0]))
    elif (int (price[i +)) = = Int (price[i +)):
        train_y.append (Np.array ([0,1,0]
    )) elif (int (price[i+20]) <int (price[i+21)):
        train_y.append (Np.array ([0,0,1]))

Next define the Model:
TensorFlow lstm model, you need to split the tensor into a sequence and then pass in the model. Otherwise, the return error is the x = Tf.unstack (x, N_steps, 1) in the code.

def RNN (x, Weights, biases):
    #首先把数据拆分为 n Sequences, each dimension (Batch_size, n_input)
    x = Tf.unstack (x, N_steps, 1)

    # Set A lstm cell
    Lstm_cell = rnn. Basiclstmcell (N_hidden, forget_bias=1.0)

    # obtains lstm output
    outputs, states = Rnn.static_rnn (Lstm_cell, X, dtype= Tf.float32)
    # Add a linear activation return
    Tf.matmul (outputs[-1], weights["out") + biases[' out ']
get results, define loss functions and optimization functions

How to optimize the model.
After the predicted value is obtained, a loss function is compared to the actual price. The loss function uses Softmax_cross_entropy_with_logits to calculate the difference between predictive and tagged values, and then uses Adamoptimizer to optimize the loss function optimization model.

pred = RNN (x, Weights, biases)
# Define loss and optimizer cost
= Tf.reduce_mean (tf.nn.softmax_cross_entropy_ With_logits (logits=pred, labels=y))
optimizer = Tf.train.AdamOptimizer (learning_rate=learning_rate). Minimize ( Cost)

# Evaluate model
correct_pred = tf.equal (Tf.argmax (pred,1), Tf.argmax (y,1))
accuracy = Tf.reduce_ Mean (Tf.cast (correct_pred, Tf.float32))
Project Open Source address and training results

https://github.com/TensorFlowNews/TensorFlow-Bitcoin-Robot/

Training equipment:

GeForce GTX 980 Ti

Training results:

Iter 998000, Minibatch loss= 0.730588 training, accuracy= 0.75000 optimization finished! subsequent update releases

http://www.tensorflownews.com/ Update Plan

Because trading platforms provide very few historical transactions, in order to further improve the effectiveness of training, the follow-up to continue to maintain their own historical transaction data or to find a better source of data. Another aspect is that after the model has been trained, it is preserved and can be used directly in the future. There are also for the model itself can do some optimization, now just forecast, rise, fall, maintain, follow-up can be a more sophisticated scoring, according to historical data back to test and so on.
Model persistence, training dataset persistence, testing data sets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.