Skip to content

Commit

Permalink
feat: add day change rate to obs
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshub committed Mar 23, 2020
1 parent 7a8e145 commit 6370099
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 21 deletions.
4 changes: 3 additions & 1 deletion rlenv/StockTradingEnv0.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MAX_SHARE_PRICE = 5000
MAX_OPEN_POSITIONS = 5
MAX_STEPS = 20000
MAX_DAY_CHANGE = 1

INITIAL_ACCOUNT_BALANCE = 10000

Expand All @@ -30,7 +31,7 @@ def __init__(self, df):

# Prices contains the OHCL values for the last five prices
self.observation_space = spaces.Box(
low=0, high=1, shape=(6, 6), dtype=np.float16)
low=0, high=1, shape=(7, 6), dtype=np.float16)

def _next_observation(self):
# Get the stock data points for the last 5 days and scale to between 0-1
Expand All @@ -40,6 +41,7 @@ def _next_observation(self):
self.df.loc[self.current_step: self.current_step + 5, 'Low'].values / MAX_SHARE_PRICE,
self.df.loc[self.current_step: self.current_step + 5, 'Close'].values / MAX_SHARE_PRICE,
self.df.loc[self.current_step: self.current_step + 5, 'Volume'].values / MAX_NUM_SHARES,
self.df.loc[self.current_step: self.current_step + 5, 'Change'].values / MAX_DAY_CHANGE,
])

# Append additional data and scale each value to between 0-1
Expand Down
293 changes: 273 additions & 20 deletions vis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,39 +180,292 @@
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"execution_count": 47,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Close</th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Volume</th>\n",
" <th>Change</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>1942.000000</td>\n",
" <td>1942.000000</td>\n",
" <td>1942.000000</td>\n",
" <td>1942.000000</td>\n",
" <td>1.942000e+03</td>\n",
" <td>1942.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>416.342152</td>\n",
" <td>415.675396</td>\n",
" <td>421.311972</td>\n",
" <td>410.796777</td>\n",
" <td>4.143622e+06</td>\n",
" <td>0.001037</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>308.651366</td>\n",
" <td>308.184666</td>\n",
" <td>311.933793</td>\n",
" <td>304.812292</td>\n",
" <td>2.680067e+06</td>\n",
" <td>0.020511</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>107.960000</td>\n",
" <td>108.050000</td>\n",
" <td>109.480000</td>\n",
" <td>107.280000</td>\n",
" <td>8.148700e+05</td>\n",
" <td>-0.123000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>176.577500</td>\n",
" <td>176.465000</td>\n",
" <td>178.675000</td>\n",
" <td>173.372500</td>\n",
" <td>2.462500e+06</td>\n",
" <td>-0.009800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>249.850000</td>\n",
" <td>249.550000</td>\n",
" <td>253.665000</td>\n",
" <td>246.575000</td>\n",
" <td>3.520000e+06</td>\n",
" <td>0.000150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>657.252500</td>\n",
" <td>654.292500</td>\n",
" <td>668.830000</td>\n",
" <td>645.120000</td>\n",
" <td>4.957500e+06</td>\n",
" <td>0.011375</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>1233.750000</td>\n",
" <td>1231.000000</td>\n",
" <td>1241.610000</td>\n",
" <td>1228.060000</td>\n",
" <td>3.181000e+07</td>\n",
" <td>0.103700</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"(5, 6)"
" Close Open High Low Volume \\\n",
"count 1942.000000 1942.000000 1942.000000 1942.000000 1.942000e+03 \n",
"mean 416.342152 415.675396 421.311972 410.796777 4.143622e+06 \n",
"std 308.651366 308.184666 311.933793 304.812292 2.680067e+06 \n",
"min 107.960000 108.050000 109.480000 107.280000 8.148700e+05 \n",
"25% 176.577500 176.465000 178.675000 173.372500 2.462500e+06 \n",
"50% 249.850000 249.550000 253.665000 246.575000 3.520000e+06 \n",
"75% 657.252500 654.292500 668.830000 645.120000 4.957500e+06 \n",
"max 1233.750000 1231.000000 1241.610000 1228.060000 3.181000e+07 \n",
"\n",
" Change \n",
"count 1942.000000 \n",
"mean 0.001037 \n",
"std 0.020511 \n",
"min -0.123000 \n",
"25% -0.009800 \n",
"50% 0.000150 \n",
"75% 0.011375 \n",
"max 0.103700 "
]
},
"execution_count": 44,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('./data/600519.csv')\n",
"df_mt = pd.read_csv('./data/600519.csv')\n",
"\n",
"MAX_ACCOUNT_BALANCE = 2147483647\n",
"MAX_NUM_SHARES = 2147483647\n",
"MAX_SHARE_PRICE = 5000\n",
"MAX_OPEN_POSITIONS = 5\n",
"MAX_STEPS = 20000\n",
"df_mt.describe()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Unnamed: 0</th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Volume</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>5255.000000</td>\n",
" <td>5255.000000</td>\n",
" <td>5255.000000</td>\n",
" <td>5255.000000</td>\n",
" <td>5255.000000</td>\n",
" <td>5.255000e+03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>2627.000000</td>\n",
" <td>158.335855</td>\n",
" <td>160.109968</td>\n",
" <td>156.388133</td>\n",
" <td>158.286548</td>\n",
" <td>2.122191e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>1517.132163</td>\n",
" <td>161.309153</td>\n",
" <td>162.588024</td>\n",
" <td>159.766714</td>\n",
" <td>161.195754</td>\n",
" <td>1.824189e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>0.000000</td>\n",
" <td>12.990000</td>\n",
" <td>13.190000</td>\n",
" <td>12.720000</td>\n",
" <td>13.120000</td>\n",
" <td>7.025000e+05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>1313.500000</td>\n",
" <td>37.940000</td>\n",
" <td>38.580000</td>\n",
" <td>37.160000</td>\n",
" <td>37.980000</td>\n",
" <td>6.705350e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>2627.000000</td>\n",
" <td>106.370000</td>\n",
" <td>108.000000</td>\n",
" <td>104.880000</td>\n",
" <td>106.130000</td>\n",
" <td>1.677300e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>3940.500000</td>\n",
" <td>187.765000</td>\n",
" <td>189.405000</td>\n",
" <td>185.635000</td>\n",
" <td>187.560050</td>\n",
" <td>2.976013e+07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>5254.000000</td>\n",
" <td>702.410000</td>\n",
" <td>705.070000</td>\n",
" <td>699.570000</td>\n",
" <td>702.100000</td>\n",
" <td>1.895606e+08</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Unnamed: 0 Open High Low Close \\\n",
"count 5255.000000 5255.000000 5255.000000 5255.000000 5255.000000 \n",
"mean 2627.000000 158.335855 160.109968 156.388133 158.286548 \n",
"std 1517.132163 161.309153 162.588024 159.766714 161.195754 \n",
"min 0.000000 12.990000 13.190000 12.720000 13.120000 \n",
"25% 1313.500000 37.940000 38.580000 37.160000 37.980000 \n",
"50% 2627.000000 106.370000 108.000000 104.880000 106.130000 \n",
"75% 3940.500000 187.765000 189.405000 185.635000 187.560050 \n",
"max 5254.000000 702.410000 705.070000 699.570000 702.100000 \n",
"\n",
" Volume \n",
"count 5.255000e+03 \n",
"mean 2.122191e+07 \n",
"std 1.824189e+07 \n",
"min 7.025000e+05 \n",
"25% 6.705350e+06 \n",
"50% 1.677300e+07 \n",
"75% 2.976013e+07 \n",
"max 1.895606e+08 "
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_ap = pd.read_csv('./data/AAPL.csv')\n",
"\n",
"current_step = 164\n",
"df.loc[current_step:current_step+5, 'Open'].values.shape\n",
"frame = np.array([\n",
" df.loc[current_step: current_step + 5, 'Open'].values / MAX_SHARE_PRICE,\n",
" df.loc[current_step: current_step + 5, 'High'].values / MAX_SHARE_PRICE,\n",
" df.loc[current_step: current_step + 5, 'Low'].values / MAX_SHARE_PRICE,\n",
" df.loc[current_step: current_step + 5, 'Close'].values / MAX_SHARE_PRICE,\n",
" df.loc[current_step: current_step + 5, 'Volume'].values / MAX_NUM_SHARES,\n",
" ])\n",
"frame.shape"
"df_ap.describe()"
]
}
],
Expand Down

0 comments on commit 6370099

Please sign in to comment.