question-mark

Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extracting results for quantstats

See original GitHub issue

Issue Description

Hello, after creating a model and running results = model.learn(int(1000)) how do I use the results to compare with a benchmark in quantstats? Currently the results doesn’t hold the data that quantstats expect to be able to use in conjunction with qs.reports.html(results, "SPY", output="D:\ReinforcementLearning\BaseLines\Trading\Myreport.html")

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
AminHPcommented, Jul 28, 2020

Hi again. Can you try this code and check if it works?

# Imports
import numpy as np
import pandas as pd

import gym
import gym_anytrading
import quantstats as qs

from stable_baselines import A2C
from stable_baselines.common.vec_env import DummyVecEnv

import matplotlib.pyplot as plt


# Create Env
df = gym_anytrading.datasets.STOCKS_GOOGL.copy()
df.index = pd.to_datetime(df.index)
env_maker = lambda: gym.make('stocks-v0', df=df, window_size=10, frame_bound=(100, 5000))
env = DummyVecEnv([env_maker])


# Train Env
policy_kwargs = dict(net_arch=[64, 'lstm',dict(vf=[128, 128, 128], pi=[64, 64])])
model = A2C('MlpLstmPolicy', env, verbose=1, policy_kwargs=policy_kwargs)
model.learn(total_timesteps=1)


# Test Env 
env = env_maker()
observation = env.reset()

profits = []

while True:
    observation = observation[np.newaxis, ...]
    # action = env.action_space.sample()
    action, _states = model.predict(observation)

    observation, reward, done, info = env.step(action)
    profits.append(info['total_profit'])

    # env.render()
    if done:
        print("info:", info)
        break


# Plot Results
plt.cla()
env.render_all()
plt.show()


# Analysis Using quantstats
qs.extend_pandas()

net_worth = pd.Series(profits, index=df.index[-len(profits):])
returns = net_worth.pct_change().iloc[1:]

qs.reports.full(returns)
qs.reports.html(returns, output="report.html")
1reaction
sword134commented, Jul 29, 2020

@AminHP just ran the test code you posted. Damn you are an absolute champ. Going to implement this with my own my RL now and I just want to thank you a lot! Perhaps for even easier implementation it would be possible to get the returns variable through a profit function that could easily be built into gym-anytrading.

Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extracting results from stable baselines environment · Issue #38
Hello, after creating a model in the stable baselines library anytrading (https://github.com/AminHP/gym-anytrading) and running the ...
Read more >
QuantStats: Portfolio Analytics with Python Tutorial - YouTube
QuantStats : Portfolio Analytics with Python Tutorial. Watch later ... An error occurred while retrieving sharing information.
Read more >
How to use the quantstats.stats function in QuantStats - Snyk
To help you get started, we've selected a few quantstats.stats examples, based on popular ways it is used in public projects.
Read more >
The easiest way to evaluate the performance of trading ...
... library and provide performance results based only on a provided series of returns. ... We can now inspect the extracted elements:
Read more >
Pulling All Sorts of Financial Data in Python [Updated for 2021]
But there is a work around. The Quantstat Github repo written by Ran Aroussi provides a Python library that connects to the Yahoo...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found