Git Product home page Git Product logo

stock-buy-sell-dynamic-programming-fintech's Introduction

Fintech: Best Time to Buy and Sell Stock with Transaction Fee

  • Find the best time to buy and sell stock with transaction fee using Dynamic Programming, implementation in Python.

Algorithm

  • Use Dynamic Programming to compute to optimal action sequence along a give price vector.
  • DP records the following at each time t:
    • optimal value of money, and
    • optimal value of stock, and
    • the previous action that lead to this optimal value
  • DP initialization:
    • money = original capital, and
    • stock = buy stock with original capital
  • DP recursion:
    • calculate optimal money:
      • money = hold yesterday's money, or
      • money = sell all your stocks today
    • calculate optimal stock:
      • stock = hold yesterday's stock, or
      • stock = buy stock today with all your money
  • DP trace back:
    • You must sell on the last day to maximize profit
    • trace the previous action that lead to this optimal value
  • Action smoothing:
    • if previous action is the same as the current action, then the action choosen is "hold".

Result

  • Testing on the SPY dataset, return rate is: 212.88675299365727

Usage:

  • Use default:
python3 optimal_stock_action.py
  • Run with customized dataset and transaction fee value:
python3 optimal_stock_action.py ./data.csv 0.01

stock-buy-sell-dynamic-programming-fintech's People

Contributors

andi611 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.