Last active
July 11, 2019 12:11
-
-
Save sushantkumar23/7b07f3d82ec78a28665b5ae010b18e44 to your computer and use it in GitHub Desktop.
SineTradeEnv-v0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# trade_env.py | |
import numpy as np | |
import gym | |
class SineEnv(gym.Env): | |
""" | |
An Env that trades for n steps and returns rewards and observations by following | |
a sine curve | |
""" | |
def __init__(self, n=200, offset=3, amplitude=0.6, wavelength=0.1): | |
self.n = n | |
self.offset = offset | |
self.amplitude = amplitude | |
self.wavelength = wavelength | |
self.num_actions = 3 | |
self.series = self.offset + self.amplitude * np.sin(self.wavelength * np.arange(self.n)) | |
self.done = False | |
def step(self, action): | |
""" | |
The step function takes action and returns the next obs, reward, done and info | |
""" | |
self.index += 1 | |
obs = self.series[self.index] | |
step_return = np.log(self.series[self.index]/self.series[self.index-1]) | |
reward = (action - 1) * step_return | |
if self.index >= (self.n-1): | |
self.done = True | |
info = { | |
'return': step_return | |
} | |
return obs, reward, self.done, info | |
def reset(self): | |
self.index = 0 | |
self.done = False | |
obs = self.series[self.index] | |
return obs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment