SkillAgentSearch skills...

Howtrader

Howtrader: A crypto quant framework for developing, backtesting, and executing your own trading strategies. Seamlessly integrates with TradingView and other third-party signals. Simply send a post request to automate trading and order placement. Supports Binance and Okex exchanges.

Install / Use

/learn @51bitquant/Howtrader

README

HowTrader

中文文档

What does Howtrader means? It means how to be a trader, especially a quant trader.

HowTrader crypto quantitative trading framework, and was forked from VNPY, so the core codes、functions and useages are pretty similar to VNPY, but would be more easy to install and use. By the way, Howtrader fixes some bugs, add more functions and strategies to framework. We extend the TradingView signals and grid strategies to Howtrader and more codes.

Howtrader VS VNPY

  1. some classes' definition are pretty different: for OrderData 、TradeData、ContractData, we replace float with Decimal to meet the precision, theses classes are defined in howtrader.trader.object module.

  2. on_order and on_trade update sequence are different: in VNPY, ,the on_order is always updated before the on_trade if the order was traded. And in the cta strategy, the self.pos(the position data) was calculated in the on_trade callback. So if we want to get the latest self.pos value, we may need to define a variable to calculate the latest position data. To solve this issue, we push the on_trade before the on_order callback. check out the code to find out the details:

        
        def on_order(self, order: OrderData) -> None:
         """on order update"""
         order.update_time = generate_datetime(time.time() * 1000)
         last_order: OrderData = self.get_order(order.orderid)
         if not last_order:
             self.orders[order.orderid] = copy(order)
             super().on_order(copy(order))
    
         else:
             traded: Decimal = order.traded - last_order.traded
             if traded < 0: # filter the order is not in sequence
                 return None
    
             if traded > 0:
                 trade: TradeData = TradeData(
                     symbol=order.symbol,
                     exchange=order.exchange,
                     orderid=order.orderid,
                     direction=order.direction,
                     price=order.price,
                     volume=traded,
                     datetime=order.update_time,
                     gateway_name=self.gateway_name,
                 )
    
                 super().on_trade(trade)
    
             if traded == 0 and order.status == last_order.status:
                 return None
    
             self.orders[order.orderid] = copy(order)
             super().on_order(copy(order))
    
     def get_order(self, orderid: str) -> OrderData:
         """get order by order id"""
         return self.orders.get(orderid, None)
    
    
  3. gateways are different: solve issues like disconnecting from exchange, and reconnecting.

  4. TradingView app to receive other 3rd party signals and algo trading strategies, checkout the module: howtrader.app.tradingview

Installation

the framework depends on pandas, Numpy libraries, so we higly recommend you to install Anaconda.

  1. Install Anaconda, the Anaconda download website is here, remember to click "Add Conda to System Path" in the process of Anaconda Installation. If you forget to do so, you might need to unistall and reinstall the Anaconda, or just search how to add conda to you system path, if you encounter the problem of "not found conda command".

    If you already install the anaconda before, you can simply update your conda by runing the following command:

    conda update conda

    conda update anaconda

  2. install git

Install git is pretty simple, just download git from [https://git-scm.com/downloads], then install it, remember to add git to your system path. If you're using MacOX, git was integrated into the system, so you may not need to install git. 3. create virtual env by conda

conda create -n mytrader python==3.9

mytrader is the name of your virtual env, if you have the mytrader virtual env before and the version is not 3.9, you can unistall it by following command:

conda remove -n mytrader --all

if you encounter an error, you may need to deactivate the mytrader by the command:

conda deactivate

then remove it:

conda remove -n mytrader --all

  1. activate your virtual env:

conda activate mytrader

  1. install howtrader

run the command:

pip install git+https://github.com/51bitquant/howtrader.git

if you want to update to the latest version, use the command:

pip install git+https://github.com/51bitquant/howtrader.git -U

if encounter the failure of installation TA-Lib, checkout the next step.

Install TA-Lib

If you can't install the howtrader in Window system, the main reason may be the TA-Lib, here we go in to the detail of installing TA-Lib:

  1. open this url in your browser:https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib

  2. search ta-lib in the website and download the TA-Lib.whl file: here we found the TA_Lib‑0.4.24‑cp39‑cp39‑win_amd64.whl in the website and we click download, for the TA_Lib-0.4.24, it means TA-Lib version is 0.4.24, cp39 is the python version is 3.9, amd64 your system version is 64 bit, choose the right one and download it.

  3. change your directory to your TA-Lib folder, and remember to activate your virtual env to mytrader(if you use mytrader as your vitrual env), then execute the following command:

pip install TA_Lib‑0.4.24‑cp39‑cp39‑win_amd64.whl

how-to-use

create a python proejct, and create a main.py file,and set your project's interpreter to the mytrader(the virtual env you just create and config).

then copy and paste the following code to main.py and run it, and you will see a howtrader folder beside the main.py, the howtrader folder contains the configs data(like exchange api key)、 your strategy settings and datas etc.



from howtrader.event import EventEngine, Event
from howtrader.trader.event import EVENT_TV_SIGNAL
from howtrader.trader.engine import MainEngine
from howtrader.trader.ui import MainWindow, create_qapp
from howtrader.trader.setting import SETTINGS
from howtrader.gateway.binance import BinanceUsdtGateway, BinanceSpotGateway, BinanceInverseGateway

from howtrader.app.cta_strategy import CtaStrategyApp
# from howtrader.app.data_manager import DataManagerApp
# from howtrader.app.data_recorder import DataRecorderApp
# from howtrader.app.algo_trading import AlgoTradingApp
# from howtrader.app.risk_manager import RiskManagerApp
# from howtrader.app.spread_trading import SpreadTradingApp
from howtrader.app.tradingview import TradingViewApp
from threading import Thread
import json
from flask import Flask, request

# create global event_engine
event_engine: EventEngine = EventEngine()
passphrase = SETTINGS.get("passphrase", "")
port = SETTINGS.get("port", 9999)

app = Flask(__name__)

@app.route('/', methods=['GET'])
def welcome():
    return "Hi, this is tv server!"

@app.route('/webhook', methods=['POST'])
def webhook():
    try:
        data = json.loads(request.data)
        if data.get('passphrase', None) != passphrase:
            return {"status": "failure", "msg": "passphrase is incorrect"}
        del data['passphrase'] # del it for safety.
        event:Event = Event(type=EVENT_TV_SIGNAL, data=data)
        event_engine.put(event)
        return {"status": "success", "msg": ""}
    except Exception as error:
        return {"status": "error", "msg": str(error)}

def start_tv_server():
    app.run(host="127.0.0.1", port=port)

def main():
    """"""
    qapp = create_qapp()
    main_engine = MainEngine(event_engine)

    main_engine.add_gateway(BinanceSpotGateway)
    main_engine.add_gateway(BinanceUsdtGateway)
    main_engine.add_gateway(BinanceInverseGateway)
    main_engine.add_app(CtaStrategyApp)
    main_engine.add_app(TradingViewApp)

    # if you don't use
    # main_engine.add_app(DataManagerApp)
    # main_engine.add_app(AlgoTradingApp)
    # main_engine.add_app(DataRecorderApp)
    # main_engine.add_app(RiskManagerApp)
    # main_engine.add_app(SpreadTradingApp)

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()

    t1 = Thread(target=start_tv_server)
    t1.daemon = True
    t1.start()

    qapp.exec()


if __name__ == "__main__":
    main()

how-to-crawl binance kline data

howdtrader use sliqte database as default, and also support mongodb and mysql if you want to use it. If you want to use other database, you can change the configuration in howtrader/vt_setting.json, here is the full configuration key, you can just config the corresponding values.

{
    "font.family": "", # set font family if display error
    "font.size": 12,

    "log.active": True,
    "log.level": CRITICAL,
    "log.console": True,
    "log.file": True,

    "email.server": "smtp.qq.com",
    "email.port": 465,
    "email.username": "",
    "email.password": "",
    "email.sender": "",
    "email.receiver": "",

    "order_update_interval": 600, # securing correct orders' status by synchronizing/updating orders through rest api
    "update_server_time_interval": 300,  # sync with server time
    "passphrase": "howtrader",  # tv passphrase
    "port": 9999, # tv server port

    "datafeed.name": "",
    "datafeed.username": "",
    "datafeed.password": "",

    "database.timezone": get_localzone_name(),
    "database.name": "sqlite",
    "database.database": "database.db",
    "database.host": "",
    "database.port": 0,
    "database.user": "",
    "database.password": ""
}


to crawl the Binance exchange kline data for backtesting, you just need to create a crawl_data.py file, just in the main.py directory. copy and paste the following codes:

"""
use the binance api to crawl data then save into the sqlite database.

"""

import pandas as pd
import time
from datetime import datetime
import requests
import pytz
from howtrader.trader.database import get_database, BaseDatabase

pd.set_option('expand_frame_repr', False)  
View on GitHub
GitHub Stars895
CategoryDevelopment
Updated44m ago
Forks337

Languages

Python

Security Score

100/100

Audited on Apr 1, 2026

No findings