Giter Club home page Giter Club logo

hft-ext's Introduction

hft-logo

GitHub license Discord GitHub issues GitHub forks GitHub stars PayPal


HFT-EXT

An extensible framework for high-frequency trading built on top of Alpaca and Yahoo Finance.

Table of Contents

Disclaimer

It goes without saying that past performance is not indicative of future results. This framework is not intended to be used for live trading. If you do use it for live trading, you do so at your own risk. The authors of this framework are not responsible for any losses incurred by using this framework. Please use this framework responsibly.

Why should I use HFT-EXT?

HFT-EXT is a framework that allows you to build your own high-frequency trading strategies. It is built on top of Alpaca and Yahoo Finance. It is designed to be extensible, so you can easily add your own strategies and indicators using Python. In addition to this, it is open-source, so you can contribute to the project and help make it better. It is designed to be compatible with both paper and live trading accounts, and is designed to be easy to use, so you can get started trading in no time.

hft-ext

I'm sold! How do I get started?

To get started, ensure you have Python 3.8 or higher installed. You can download Python here. Also, you must make a (free) account with Alpaca. You can do this here. This account will be used to make paper or live trades, and to retrieve information on your portfolio. You will also need to create an API public and secret key for your account. You can do this here. Make sure you save your API key and secret key somewhere safe. You will need them later.

First, you need to install the framework. You can do this by running the following command in your terminal:

git clone "https://github.com/finned-digital-solutions/hft-ext.git"

Then, you need to install the dependencies. You can do this by running the following command in your terminal:

cd hft-ext
pip install -r requirements.txt

Finally, you need to add your Alpaca API keys to the config.py file. We have a built-in script that will help you do this. You can run this script by running the following command in your terminal:

cd helpers
python auth.py

The script should look like this:

auth-example

From there, make sure you are back in the root directory cd .. and run the following command in your terminal:

python main.py

I have a working strategy! How do I deploy it to live trading?

You can deploy your strategy by changing the Paper variable to False in the config.py file. Please note that you will need to have a live trading account with Alpaca to do this. You can find more information about Alpaca's live trading accounts here.

How do I add my own strategies?

You can add your own strategies by adding a custom strategy to the scripts/strategies.py file. Strategies are defined as classes that inherit from the Strategy class. As such, they have several methods that need to be included in each strategy. These methods are:

  • _should_buy(): Responsible for determining whether or not the strategy should buy a new position.
  • _should_sell(): Responsible for determining whether or not the strategy should sell the current position.

Since they inherit from the Strategy class, make sure to include Strategy in the class definition. For example, if you want to create a strategy called MyCustomStrategyName, you would want to define it as follows:

class MyCustomStrategyName(Strategy):
  def _should_buy(self, past_prices:list) -> bool:
    ...
  def _should_sell(self, bought_price:float, current_price:float, past_prices:list) -> bool:
    ...

To then use your custom strategy, you have to instantiate it within scripts/strategies.py. This is done by adding the following line to the end of the file:

strategy = Strategy(MyCustomStrategyName())

Where MyCustomStrategyName is the name of your custom strategy class. Currently, the model used is ArimaStrategy, which is an ARIMA model that uses the previous day of data to predict the next day's fluctuations. you can see it instantiated in the scripts/strategies.py file as follows:

strategy = Strategy(ArimaStrategy())

The default strategy used is the ARIMA strategy. You can find the code for this strategy in the scripts/strategies.py file, within the ArimaStrategy class.

What about if I want to change the amount of shares I buy for each pricepoint?

By default, the model will buy a total of $200 worth of shares for each pricepoint. We recognize that this may be a bit too much for some people, so we have made it easy to change this. You can run the following command in your terminal to change the amount of shares you buy for each pricepoint:

cd helpers
python amount.py

The script should look like this:

amount-example

Which is also the default setting.

This is great, but I wish I could customize which tickers I want to trade.

You can customize which tickers you want to trade by adding them to the config.py file. We have a built-in script that will help you do this. You can run this script by running the following command in your terminal:

cd helpers
python tickers.py

The script should look like this:

tickers-example

As an aside, there are no limits to the number of tickers you can trade. However, the more tickers you trade, the more you risk running into API rate limits. If you do run into API rate limits, you can simply wait a few minutes and try again. If you want to avoid this, you can simply trade fewer tickers.

I want to get to modifying the framework, but I don't know where to start.

No worries, it's pretty simple to get started. The framework is built on top of the Alpaca and Yahoo Finance APIs. As such, you can find the documentation for these APIs here and here, respectively. An overview of the framework architecture can be found below.

Show Framework Architecture
|--+ hft-ext
   |--+ images: Contains images used in the README.md file.
   |--+ scripts: Contains the source code for the framework.
   |  |--+ finned-theme.json: Theme for the GUI using CustomTkinter.
   |  |--+ gui.py: Contains the GUI code.
   |  |--+ strategy.py: Contains the Strategy class and any custom strategies.
   |  |--+ trader.py: Contains the Trader class, and is responsible for trading/portfolio management.
   |--+ utils: Contains utility functions used throughout the framework.
   |  |--+ utils.py: Contains utility functions related to sqlite3.
   |  |--+ notifier.py: Contains the Notifier class, which is responsible for sending update messages between threads (e.g., GUI and Trader).
   |--+ helpers: Contains helper scripts for the framework.
   |  |--+ auth.py: Script for adding API keys to the config.py file.
   |  |--+ amount.py: Script for changing the amount of shares bought for each pricepoint.
   |  |--+ tickers.py: Script for adding tickers to the config.py file.
   |--+ config.py: Contains the configuration variables for the framework.
   |--+ CONTRIBUTING.md
   |--+ LICENSE
   |--+ README.md
   |--+ requirements.txt
   |--+ .gitignore
   |--+ main.py: The main entry point for the framework which calls the GUI and Trader classes.
   

If you want to share your custom strategies or implementations with the community, please feel free to open a pull request. To learn more about how to contribute, please read the CONTRIBUTING.md file.

I want to contribute!

That's great! We are always looking for new contributors. If you want to contribute, please read the CONTRIBUTING.md file. If you have any questions, please feel free to open an issue or contact us on Discord.

hft-ext's People

Contributors

flancast90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hft-ext's Issues

Error when installing requirement.txt : alpaca_trade_api==2.3.0

Hi,

I am trying to install requirement.txt and having trouble with alpaca_trade_api==2.3.0.
I see error when building aiohttp

Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/site-packages (from requests<3.0.0,>=2.27.1->alpaca-py==0.7.1->-r requirements.txt (line 1)) (2023.5.7)
Building wheels for collected packages: aiohttp
  Building wheel for aiohttp (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for aiohttp (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [107 lines of output]

.......................................

building 'aiohttp._websocket' extension
      creating build/temp.macosx-12-x86_64-cpython-311
      creating build/temp.macosx-12-x86_64-cpython-311/aiohttp
      clang -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -I/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c aiohttp/_websocket.c -o build/temp.macosx-12-x86_64-cpython-311/aiohttp/_websocket.o
      aiohttp/_websocket.c:198:12: fatal error: 'longintrepr.h' file not found
        #include "longintrepr.h"
                 ^~~~~~~~~~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for aiohttp
Failed to build aiohttp
ERROR: Could not build wheels for aiohttp, which is required to install pyproject.toml-based projects

I am installing on MAC. Have you come across this issue before?

Thanks,
Mahendra

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.