Coder Social home page Coder Social logo

jookie / jojobot Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 86.08 MB

JojoFin is designed for developing financial trading strategies using deep reinforcement learning (DRL). It offers specialized tools and frameworks for quantitative finance, enabling practitioners to create, test, and implement strategies effectively.

Home Page: https://vercel.com/dovpeles-projects/jojo-bot

License: Other

TypeScript 43.18% CSS 0.36% JavaScript 0.35% Shell 0.34% Python 55.77%
ppo-agent bitcoin-wallet drl-algorithms machine-learning options-trading stock-market sharpe-ratio sharpe-ratios

jojobot's Introduction


AI StockBot
Jojo has three layers: market environments, agents, and applications. For a trading task (on the top), an agent (in the middle) interacts with a market environment (at the bottom), making sequential decisions.

OverviewFeaturesInterfacesQuickstartCredits Foundation

JojoFin

JojoFin is library designed to facilitate financial trading strategies using deep reinforcement learning (DRL). It is tailored specifically for quantitative finance, providing tools and frameworks for traderss to create, test, and implement trading dtrategis using DRL techniques.
Experiments of StockBot

Experiment of StockBot providing relevant, live, and interactive stock charts and interfaces

Overview

StockBot is an AI-powered chatbot that leverages Llama3 70b on JojoFam, Vercel’s AI SDK, and TradingView’s live widgets to respond in conversation with live, interactive charts and interfaces specifically tailored to your requests. JojoFam's speed makes tool calling and providing a response near instantaneous, allowing for a sequence of two API calls with separate specialized prompts to return a response.

Important

Note: StockBot may provide inaccurate information and does not provide investment advice. It is for entertainment and instructional use only.

Tutorial

Google Colab Notebooks

Examples for Stocks, Options, and Crypto in the notebooks provided below. Open them in Google Colab to jumpstart your journey!

Notebooks Open in Google Colab
Stocks Orders Open In Colab
Options Orders Open In Colab
Crypto Orders Open In Colab
Stock Trading Colab

Features

  • 🤖 Real-time AI Chatbot: Engage with AI powered by Llama3 70b to request stock news, information, and charts through natural language conversation
  • 📊 Interactive Stock Charts: Receive near-instant, context-aware responses with interactive TradingView charts that host live data
  • 🔄 Adaptive Interface: Dynamically render TradingView UI components for financial interfaces tailored to your specific query
  • JojoFam-Powered Performance: Leverage JojoFam's cutting-edge inference technology for near-instantaneous responses and seamless user experience
  • 🌐 Multi-Asset Market Coverage: Access comprehensive data and analysis across stocks, forex, bonds, and cryptocurrencies

Interfaces

Click to view Interfaces
Description Widget
Heatmap of Daily Market Performance
Visualize market trends at a glance with an interactive heatmap.
Heatmap of Daily Market Performance
Breakdown of Financial Data for Stocks
Get detailed financial metrics and key performance indicators for any stock.
Breakdown of Financial Data for Stocks
Price History of Stock
Track the historical price movement of stocks with customizable date ranges.
Price History of Stock
Candlestick Stock Charts for Specific Assets
Analyze price patterns and trends with detailed candlestick charts.
Candlestick Stock Charts for Specific Assets
Top Stories for Specific Stock
Stay informed with the latest news and headlines affecting specific companies.
Top Stories for Specific Stock
Market Overview
Shows an overview of today's stock, futures, bond, and forex market performance including change values, Open, High, Low, and Close values.
Market Overview
Stock Screener to Find New Stocks and ETFs
Discover new companies with a stock screening tool.
Stock Screener to Find New Stocks and ETFs
Trending Stocks
Shows the top five gaining, losing, and most active stocks for the day.
Trending Stocks
ETF Heatmap
Shows a heatmap of today's ETF market performance across sectors and asset classes.
ETF Heatmap

Publications

Click to view publications
Title Conference/Journal Link Citations Year
Dynamic Datasets and Market Environments for Financial Reinforcement Learning Machine Learning - Springer Nature paper code 7 2024
FinRL-Meta: FinRL-Meta: Market Environments and Benchmarks for Data-Driven Financial Reinforcement Learning NeurIPS 2022 paper code 37 2022
FinRL: Deep reinforcement learning framework to automate trading in quantitative finance ACM International Conference on AI in Finance (ICAIF) paper 49 2021
FinRL: A deep reinforcement learning library for automated stock trading in quantitative finance NeurIPS 2020 Deep RL Workshop paper 87 2020
Deep reinforcement learning for automated stock trading: An ensemble strategy ACM International Conference on AI in Finance (ICAIF) paper code 154 2020
Practical deep reinforcement learning approach for stock trading NeurIPS 2018 Workshop on Challenges and Opportunities for AI in Financial Services paper code 164 2018

Quickstart

Important

To use StockBot, you can use a hosted version at JojoFam-stockbot.vercel.app. Alternatively, you can run StockBot locally using the quickstart instructions.

You will need a JojoFam API Key to run the application. You can obtain one here on the JojoFam console.

To get started locally, you can run the following:

cp .env.example .env.local

Add your JojoFam API key to .env.local, then run:

pnpm install
pnpm dev

Your app should now be running on localhost:3000.

Changelog

See CHANGELOG.md to see the latest changes and versions. Major versions are archived.

Credits

This app was developed by Jojo Family at JojoFam and uses the AI Chatbot template created by Vercel: Github Repository.

Foundation

AI4Finance-Foundation

# Integrate RDL Trainer into JojoBot's Backend with Next.js and Python

Here's a structured approach to integrate an RDL trainer into your Next.js serverless backend for your financial data analysis application:

1. Serverless Next.js Architecture

You will use serverless functions with Vercel to run your backend code. Ensure that your Next.js project is configured for serverless deployment.

2. React Code in app and components Folders

Folder Structure

/jojobot
  /app
    /train
      page.tsx
  /components
    DataDisplay.tsx
  /public
  /styles
  /node_modules
  package.json
  next.config.js

React Component (DataDisplay.tsx)

// components/DataDisplay.tsx
import React, { useEffect, useState } from 'react';

const DataDisplay: React.FC = () => {
  const [data, setData] = useState<string | null>(null);

  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch('/api/train');
      const result = await response.json();
      setData(result.result);
    };

    fetchData();
    const interval = setInterval(fetchData, 60000); // Refresh every 1 minute

    return () => clearInterval(interval); // Cleanup on unmount
  }, []);

  return <div>{data}</div>;
};

export default DataDisplay;

Page Component (page.tsx)

// app/train/page.tsx
import React from 'react';
import DataDisplay from '../../components/DataDisplay';

const TrainPage: React.FC = () => {
  return (
    <div>
      <h1>Financial Data Analysis</h1>
      <DataDisplay />
    </div>
  );
};

export default TrainPage;

3. Python Script for Financial Data and Trading

train.py

# train.py
import finrl
from finrl.algos import PPO
from alpaca_trade_api import REST
import datetime
import pytz

# Initialize Alpaca API
api = REST('APCA_API_KEY_ID', 'APCA_API_SECRET_KEY', base_url='https://paper-api.alpaca.markets')

def main():
    # Retrieve historical data
    data = finrl.get_data()
    
    # Initialize PPO agent and train
    agent = PPO()
    agent.train(data)

    # Example action decision
    action = agent.predict(data)

    # Place an order based on the action
    symbol = 'AAPL'
    if action == 'buy':
        api.submit_order(
            symbol=symbol,
            qty=1,
            side='buy',
            type='limit',
            time_in_force='gtc',
            limit_price=150
        )
    elif action == 'sell':
        api.submit_order(
            symbol=symbol,
            qty=1,
            side='sell',
            type='limit',
            time_in_force='gtc',
            limit_price=155
        )
    # Implement 'hold' action if needed

if __name__ == "__main__":
    main()

4. Running Python Script Programmatically Every 1 Minute

Schedule Python Script Using cron (On Vercel)

If using Vercel, you might need to schedule the script externally (e.g., via a cron job on a separate server or using a cloud function with scheduling capability).

Alternatively, you can use a local scheduler if running the script in a local or cloud VM environment.

# Example cron job to run every minute (on a Unix-based system)
* * * * * /usr/bin/python3 /path/to/train.py

🌐 Sources

  1. workik.com - AI for Backend & Frontend Code Development
  2. nextjs.org - Server and Client Components
  3. docs.alpaca.markets - Orders at Alpaca
  4. medium.com - Proximal Policy Optimization(PPO) for trading environment

make the above code to results in the form of text and images that being saved to get saved locally and get displayed on the fromt end page

To modify the code so that the results of running the Python script are saved locally as text and images, and then displayed on the front end, follow the steps below.

1. Modify the Serverless Function to Save Results Locally

Update the serverless function to save the output as text and images on the server.

// pages/api/triggerScript.ts
import { NextApiRequest, NextApiResponse } from 'next';
import { exec } from 'child_process';
import path from 'path';
import fs from 'fs';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const outputDir = path.join(process.cwd(), 'public', 'results');
  if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
  }

  const scriptPath = '/path/to/train.py';
  exec(`python3 ${scriptPath}`, (error, stdout, stderr) => {
    if (error || stderr) {
      const errorMessage = error ? error.message : stderr;
      console.error(`Error: ${errorMessage}`);
      return res.status(500).json({ success: false, message: errorMessage });
    }

    // Save the text output
    const outputFilePath = path.join(outputDir, 'output.txt');
    fs.writeFileSync(outputFilePath, stdout);

    // Assume the Python script saves images in the outputDir
    const images = fs.readdirSync(outputDir).filter(file => file.endsWith('.png'));

    res.status(200).json({ success: true, message: 'Script executed', images });
  });
}

2. Modify the Python Script to Save Text and Images

Ensure that your Python script saves its output as text and images in the public/results directory. Here's a simple example:

# train.py
import matplotlib.pyplot as plt

# Sample data for demonstration purposes
data = [1, 2, 3, 4, 5]
output_text = "This is the result of the training process.\n"

# Save the output text
with open('public/results/output.txt', 'w') as f:
    f.write(output_text)

# Save a plot as an image
plt.plot(data)
plt.title("Sample Plot")
plt.savefig('public/results/plot.png')

3. Update the Frontend to Display Text and Images

Modify the DataDisplay component to fetch and display the saved text and images.

// components/DataDisplay.tsx
import React, { useState } from 'react';

const DataDisplay: React.FC = () => {
  const [isRunning, setIsRunning] = useState(false);
  const [output, setOutput] = useState<string | null>(null);
  const [images, setImages] = useState<string[]>([]);

  const startSchedule = () => {
    setIsRunning(true);
    const intervalId = setInterval(async () => {
      const response = await fetch('/api/triggerScript');
      const result = await response.json();

      if (result.success) {
        // Fetch the text output
        const textResponse = await fetch('/results/output.txt');
        const textData = await textResponse.text();
        setOutput(textData);

        // Update image paths
        const imagePaths = result.images.map((img: string) => `/results/${img}`);
        setImages(imagePaths);
      }
    }, 60000);

    (window as any).intervalId = intervalId;
  };

  const stopSchedule = () => {
    setIsRunning(false);
    clearInterval((window as any).intervalId);
  };

  return (
    <div>
      <h1>Python Script Runner</h1>
      <button onClick={startSchedule} disabled={isRunning}>
        Start Running Every 1 Minute
      </button>
      <button onClick={stopSchedule} disabled={!isRunning}>
        Stop Running
      </button>

      {output && (
        <div>
          <h2>Output:</h2>
          <pre>{output}</pre>
        </div>
      )}

      {images.length > 0 && (
        <div>
          <h2>Generated Images:</h2>
          {images.map((src, idx) => (
            <img key={idx} src={src} alt={`Result ${idx + 1}`} />
          ))}
        </div>
      )}
    </div>
  );
};

export default DataDisplay;

4. Use the DataDisplay Component in Your Page

Include the DataDisplay component in the desired page.

// app/train/page.tsx
import React from 'react';
import DataDisplay from '../../components/DataDisplay';

const TrainPage: React.FC = () => {
  return (
    <div>
      <h1>Financial Data Analysis</h1>
      <DataDisplay />
    </div>
  );
};

export default TrainPage;

Summary

  • The Python script runs every 1 minute when triggered by a button.
  • The results, including text and images, are saved to the public/results directory.
  • The front-end component fetches and displays the saved results on the page.

🌐 Sources

  1. stackoverflow.com - How to execute a function every 1 minute
  2. advsyscon.com - Python Job Scheduling with Cron
  3. discuss.python.org - Help me to initialize or trigger a python file to run every 2 hours

To set up your Next.js project to run a Python script, save the results locally, and display them on the front end, follow these steps:

1. Run the Python Script in Next.js:

  • Use Node.js’s child_process module to execute the Python script from within an API route in your Next.js project.
  • Example code (TypeScript):
import { exec } from 'child_process';
import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  exec('python3 ./../../scripts/train.py', (error, stdout, stderr) => {
    if (error) {
      res.status(500).json({ error: stderr });
    } else {
      // Save stdout to a file or return the output directly
      res.status(200).json({ message: 'Script executed successfully', output: stdout });
    }
  });
}

2. Save Results Locally:

  • You can save the output of the Python script to a file using Node.js’s fs module.
  • Example:
import { exec } from 'child_process';
import fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  exec('python3 ./../../scripts/train.py', (error, stdout, stderr) => {
    if (error) {
      res.status(500).json({ error: stderr });
    } else {
      // Save the output to a file
      fs.writeFileSync('./results/output.txt', stdout);
      res.status(200).json({ message: 'Script executed and result saved successfully' });
    }
  });
}

3. Display the Results on the Front End:

  • Create a React component to fetch and display the saved results.
import { useEffect, useState } from 'react';

function DataDisplay() {
  const [textResult, setTextResult] = useState('');

  useEffect(() => {
    fetch('/api/get-results')  // This API route should return the saved results
      .then((res) => res.text())
      .then((data) => setTextResult(data));
  }, []);

  return (
    <div>
      <h2>Results</h2>
      <p>{textResult}</p>
    </div>
  );
}

export default DataDisplay;

Summary

  • Run the Python script using a custom API route.
  • Save the script's output locally in a file.
  • Fetch and display the saved output in a front-end component.

This setup will allow your Next.js application to integrate Python scripts, process their results, and present them to users effectively.

🌐 Sources

jojobot's People

Contributors

jookie avatar

Stargazers

 avatar

Watchers

 avatar

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.