Tensorflow| Simple application of approaching stock prices through Tensorflow

Tensorflow| Simple application of approaching stock prices

Today I want to share a simple program of approaching stock prices through tensorflow. It is a very simple case but It could help us understand the principle of Tensorflow more clearly.

Step1: Import the packages.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

Step2: Import the data of stock prices
Here I have imported the opening and closing prices of the stock market for 15 working days.

data = np.linspace(1,15,15)
endPrice = np.array([111.6,108.9,108.6,108.8,113.0,109.9,113.5,112.5,123.8,114.4,118.4,119.8,116.6,117.0,119.1])
beginPrice = np.array([113.0,109.0,110.0,108.0,107.5,111.7,113.9,112.0,114.5,122.1,113.0,117.0,118.5,115.0,117.5])

Step 3.Create a chart of stock market.
After we imported the data of stock prices, we created a bar chart to represent the stock price data we imported. Red represents the closing price above the opening price and green represents the closing price below the opening price.

plt.figure()
for i in range(0,15):
    dataOne = np.zeros([2])
    dataOne[0]=i
    dataOne[1]=i
    prizeOne = np.zeros([2])
    prizeOne[0] = beginPrice[i]
    prizeOne[1]=endPrice[i]
    if endPrice[i]>beginPrice[i]:
        plt.plot(dataOne,prizeOne,'r',lw=8)#draw the red bar.
    else:
        plt.plot(dataOne,prizeOne,'g',lw=8)#draw the green bar.
plt.show()

在这里插入图片描述
Step 4.Normalization operation
We normalize the data to improve the accuracy of the calculation results.
Here the price is divided into 14 servings and 200 servings, you can customize the number of splits according to your actual data.

dataNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range (0,15):
    dataNormal[i,0] = i/14.0
    priceNormal[i,0] = endPrice[i]/200.0

Step 5. Use placeholder to load our data.

x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])

Step 6. Define the hidden layer.

w1 = tf.Variable(tf.random_uniform([1,10],0,1))#Initial weight
b1 = tf.Variable(tf.zeros([1,10]))#Additional constant
wb1 = tf.matmul(x,w1)+b1. # wb1=x*w1+b1
layer1 = tf.nn.relu(wb1)#Excitation function

Step 7.Define the output layer.
The principle is the same as the hidden layer.

w2= tf.Variable(tf.random_uniform([10,1],0,1))#Initial weight
b2 = tf.Variable(tf.zeros([15,1]))#Additional constant
wb2 = tf.matmul(layer1,w2)+b2 # wb2=x*w2+b2
layer2 = tf.nn.relu(wb2)#Excitation function

Step 8.Define the loss & Use gradient descent method to reduce loss.

loss = tf.reduce_mean(tf.square(y-layer2))
#define the loss
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
#Gradient descent method, each drop 0.1 
#Function: reduce loss

Step 9. Run the artificial network.

with tf.Session()as sess:
    sess.run(tf.global_variables_initializer())#
    for i in range(10000):
        sess.run(train_step,feed_dict={x:dataNormal,y:priceNormal})
        #to Get optimized w1,b1,w2,b2
    preds = sess.run(layer2,feed_dict={x:dataNormal})
    #use the w and b we got to calculate the layer2
    predPrice =np.zeros([15,1])
    for i in range(0,15):
        predPrice[i,0]=(preds*200)[i,0] 
        #Anti-normalization
    plt.plot(data,predPrice,'b',lw=1)
    #draw the predict lines of stock prices of color blue.
plt.show()

result:
在这里插入图片描述
Conclusion:
The number of samples is only fifteen, and the training time is too long, the method is not practical. However, in the process of preliminary learning tensorflow, this simple program has a good effect on understanding the principles of neural networks. Besides, you can adjust some parameters yourself to try different effects and then find the best training results.

Thank you for reading!

--credit by dora 2020.4.22


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值