Kismuz / btgym

Scalable, event-driven, deep-learning-friendly backtesting library
https://kismuz.github.io/btgym/
GNU Lesser General Public License v3.0
979 stars 259 forks source link

episode image not rendered #103

Closed deandreee closed 5 years ago

deandreee commented 5 years ago
Running environment:

Ubuntu 16.04

Files or part of package has been run:

I'm trying to run basic examples from rendering_howto.ipynb

Expected behaviour:

Both images/modes rendered - human and episode

Actual behaviour:

human image is rendered, but episode is empty See here

Steps to reproduce:

Example code (basically from here - https://github.com/Kismuz/btgym/blob/master/examples/rendering_howto.ipynb)

import random
import os

import numpy as np

import sys

sys.path.insert(0, "..")

import IPython.display as Display
import PIL.Image as Image

from gym import spaces
from btgym import BTgymEnv

def show_rendered_image(rgb_array):
    """
    Convert numpy array to RGB image using PILLOW and
    show it inline using IPykernel.
    """
    img = Image.fromarray(rgb_array)
    Display.display(img)
    img.show()

def render_all_modes(env):
    """
    Retrieve and show environment renderings
    for all supported modes.
    """
    for mode in env.metadata["render.modes"]:
        print("[{}] mode:".format(mode))
        show_rendered_image(env.render(mode))

def take_some_steps(env, some_steps):
    """Just does it. Acting randomly."""
    for step in range(some_steps):
        rnd_action = env.action_space.sample()
        o, r, d, i = env.step(rnd_action)
        if d:
            print("Episode finished,")
            break
    print(step + 1, "steps made.\n")

env = BTgymEnv(
    filename="./examples/data/DAT_ASCII_EURUSD_M1_2016.csv",
    state_shape={
        "raw": spaces.Box(low=-100, high=100, shape=(30, 4), dtype=np.float32)
    },
    skip_frame=5,
    start_cash=100,
    render_ylabel="Price Lines",
    render_size_episode=(12, 8),
    render_size_human=(8, 3.5),
    render_size_state=(10, 3.5),
    render_dpi=75,
    verbose=0,
)

o = env.reset()
take_some_steps(env, 10000)
render_all_modes(env)
Kismuz commented 5 years ago

@deandreee , - thank you for spotting this; fixed; please update and try it again.

deandreee commented 5 years ago

@Kismuz thanks for a quick fix, image now renders. Unfortunately it's a bit distorted, labels on top of chart, scale numbers cut etc (see here). I tried playing with render_size/dpi but not much luck. Any advice on this? I see that example image (here https://github.com/Kismuz/btgym/blob/master/examples/rendering_howto.ipynb) is also a bit distorted, but not as bad as mine.

Kismuz commented 5 years ago

@deandreee , yes indeed; it can be controlled to some degree by setting bigger render size and resolution size.I wouldn't recommend doing something other than that but one can pass full stack of backtrader options found in PlotScheme object, see:

https://www.backtrader.com/docu/plotting/plotting.html

https://github.com/Kismuz/btgym/blob/master/btgym/rendering/renderer.py#L36

deandreee commented 5 years ago

@Kismuz thanks, I tried playing with multiple params but unfortunately saw no improvements. One thing I noticed - looked like dpi was not affecting episode but was affecting human, for example, when I changed dpi to 30, human image got really small but episode stayed the same, see here. I tried debugging, but as far as I could see, dpi was passed all the way to the end (to self.cerebro.plot() in DrawCerebro), so not sure where the problem is.

Anyway, I think you can close this issue, since the original problem was solved. I'll probably do some more research on this later but for now I'll focus more on the basics. Just started researching RL for my trading and looks like I need to go with more barebones approach, integrating existing frameworks might be to steep learning curve, will try something like raw Gym/Tensorforce/etc.

nodeflow-ch commented 3 years ago

@Kismuz first of all - really great package! Appreciate your efforts that went into this.

Actually I have got the same issue as @deandreee, even after upgrading to the latest version (deinstalled and pulled the latest version from master):

I ran your rendering_howto and got this after a finished episode: image

Thanks for your help