Coder Social home page Coder Social logo

Comments (6)

windowshopr avatar windowshopr commented on July 30, 2024

Just tried to increase the look back period in the stock code from 5 to 6 and receive the same error. I know it has to do with the size of the array, but don't know how to fix it. Thanks!

from stock-trading-environment.

fightthepower avatar fightthepower commented on July 30, 2024

I just look through your code and I think the error is in here shape=(8, 6) . If I am correct shape's format is (row,column) that is, it should be (6 days, 8 features) in your case its (8 features, 6 days).

from stock-trading-environment.

windowshopr avatar windowshopr commented on July 30, 2024

@fightthepower Thanks for your input! Using just the stock code, when you change the lookback period from a 5 to a 6 for each, and then change it to read something like shape=(7, 6), I get the same error message. Try it out and let me know what you think, I'd really appreciate the help here :)

from stock-trading-environment.

fightthepower avatar fightthepower commented on July 30, 2024

I initially thought the observations were stock prices and volume alone but it seems it also include the portfolio details also. The real problem was the line where we are appending the frame and the balance details. Because I really didn't understand the math behind it , take this solution a bit carefully. I was able to run the code with these changes,

# Global variable
LOOKBACK_PERIOD=10 

# Observation space
self.observation_space = spaces.Box(
            low=0, high=1, shape=(LOOKBACK_PERIOD,6), dtype=np.float16)

# Method _next_observation with commented lines
def _next_observation(self):
    # Get the stock data points for the last 5 days and scale to between 0-1
    frame = np.array([
        self.df.loc[self.current_step: self.current_step +
                    LOOKBACK_PERIOD-1, 'Open'].values / MAX_SHARE_PRICE,
        self.df.loc[self.current_step: self.current_step +
                    LOOKBACK_PERIOD-1, 'High'].values / MAX_SHARE_PRICE,
        self.df.loc[self.current_step: self.current_step +
                    LOOKBACK_PERIOD-1, 'Low'].values / MAX_SHARE_PRICE,
        self.df.loc[self.current_step: self.current_step +
                    LOOKBACK_PERIOD-1, 'Close'].values / MAX_SHARE_PRICE,
        self.df.loc[self.current_step: self.current_step +
                    LOOKBACK_PERIOD-1, 'Volume'].values / MAX_NUM_SHARES,
    ])

    # Append additional data and scale each value to between 0-1
    # obs = np.append(frame, [[
        # self.balance / MAX_ACCOUNT_BALANCE,
        # self.max_net_worth / MAX_ACCOUNT_BALANCE,
        # self.shares_held / MAX_NUM_SHARES,
        # self.cost_basis / MAX_SHARE_PRICE,
        # self.total_shares_sold / MAX_NUM_SHARES,
        # self.total_sales_value / (MAX_NUM_SHARES * MAX_SHARE_PRICE),
    # ]], axis=0)
    
    obs=frame

    return obs

# Conditional statement
if self.current_step > len(self.df.loc[:, 'Open'].values) - LOOKBACK_PERIOD:
    self.current_step = 0

# Set the current step to a random point within the data frame
self.current_step = random.randint(0, len(self.df.loc[:, 'Open'].values) - LOOKBACK_PERIOD)

I have comment out the line in _next_observation method which was used to append frame and portfolio details and I really don't know if this has any adverse effect on the portfolio calculations.

from stock-trading-environment.

windowshopr avatar windowshopr commented on July 30, 2024

Thanks a lot @fightthepower . It does work. I'm going to leave the issue open to see if the author has any insights about the change maybe, or how to add a bigger dataset with a longer lookback period like I'm doing. Really appreciate it! I'll try to play with it now.

from stock-trading-environment.

windowshopr avatar windowshopr commented on July 30, 2024

@fightthepower Actually, I found another great project from this author here that uses bitcoin data for a similar purpose:

https://github.com/notadamking/Bitcoin-Trader-RL

You can set a lookback period in the environment there, so I'm going to be playing with that one and see if I can merge the two methodologies together for my dataset. Thanks for your help!

from stock-trading-environment.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.