DAY 15
0

# 一、實作內容

• 主要功能

1. 可以紀錄當下狀態和以前狀態的環境
1. 每次給定一個action都會更新狀態和返回對這個動作的評價分數
• 對於評價分數

• 我將(現在的資產總和-上一次的資產總和) 得到的分數，+越多代表目前獲利越多，反之亦然
• 之後可能會修正，最主要是到真正應用的時候會再次修改

# 二、程式碼

• 環境程式碼段
``````import numpy as np

class stockEvn_single:
__lot = 0
__mean_price = 0
__state_assets = 0

def __init__(self, money=1000):
self.__money = money
self.__money_init = money
self.__state_assets = self.getAssets()

def getInfo(self):
print('stock count : ', self.__lot, ', stock value : ', self.__mean_price, ', money leave : ', self.__money,
', assets', self.__state_assets)

def doAction(self, stock_price, action, s_prop):
'''
stock_price : the price of stock
action : 0, sell; 1, do nothing, 2, buy
s_prop : a float between 0-1
'''
return self.__calculate_price(stock_price=stock_price, action=action, s_prop=s_prop)

def __calculate_price(self, stock_price, action, s_prop):
if action == 0:
can_sell = self.__lot
sell = int(s_prop * can_sell)
self.__doSell(stock_price=stock_price, quan=sell)
elif action == 1:
self.__doNothing()
elif action == 2:
now_state = self.__getAssetsState(stock_price=stock_price)
loss = now_state - self.__state_assets
self.__state_assets = now_state
return loss

def __doSell(self, stock_price, quan):
if quan < 1:
return self.__doNothing()
else:
if stock_price < self.__mean_price:
loss = 1
else:
loss = 0
self.__lot -= quan
self.__money += (quan * stock_price)
return

if quan < 1:
return self.__doNothing()
else:
if stock_price > self.__mean_price:
loss = 1
else:
loss = 0
value_stock = self.__lot * self.__mean_price
self.__lot += quan
value_stock += (quan * stock_price)
self.__mean_price = value_stock / self.__lot
self.__money -= (quan * stock_price)
return

def __doNothing(self):
return

def getAssets(self):
return self.__money + self.__lot * self.__mean_price

def __getAssetsState(self, stock_price):
return self.__lot * stock_price + self.__money
``````
• 簡單測試
``````env = stockEvn_single()
env.getInfo()

get_loss = []
get_loss.append(env.doAction(stock_price=200, action=2, s_prop=0.6))
env.getInfo()
get_loss.append(env.doAction(stock_price=210, action=2, s_prop=1))
env.getInfo()
get_loss.append(env.doAction(stock_price=250, action=2, s_prop=1))
env.getInfo()
get_loss.append(env.doAction(stock_price=340, action=0, s_prop=0.6))
env.getInfo()
get_loss.append(env.doAction(stock_price=390, action=2, s_prop=1))
env.getInfo()
get_loss.append(env.doAction(stock_price=300, action=0, s_prop=0))
env.getInfo()
print('loss = ', get_loss)
``````
• 輸出
``````stock count :  0 , stock value :  0 , money leave :  1000 , assets 1000

stock count :  3 , stock value :  200.0 , money leave :  400 , assets 1000
stock count :  4 , stock value :  202.5 , money leave :  190 , assets 1030
stock count :  4 , stock value :  202.5 , money leave :  190 , assets 1190
stock count :  2 , stock value :  202.5 , money leave :  870 , assets 1550
stock count :  4 , stock value :  296.25 , money leave :  90 , assets 1650
stock count :  4 , stock value :  296.25 , money leave :  90 , assets 1290
loss =  [0, 30, 160, 360, 100, -360]
``````
• 上方內容是指每次的`doAction`會觸發一次更新，loss就是跟上一次的總資產做比較，較低就是負值。我原本是跟「購入時的價值」做比較，但比較不直觀，之後如果訓練要用到在改變XD

AI可以分析股票嗎?30