Git Product home page Git Product logo

whook's Introduction

whook

WHOOK is a web hook for handling Tradingview Alerts to crypto exchanges in perpetual USDT futures.

Whook prioritizes reliability over speed. If you're looking for high frequency trading Whook is not for you. Whook will do everything it can to fullfill orders, including resending rejected orders until they time out (currently 40 seconds), reducing the quantity of the order when the balance is not enough and dividing the order in two at reversing positions when there's not enough balance for doing it at once.

Whook only makes market orders and limit orders. Take profit and stop loss are not supported.
Whook only uses one-side mode. Hedge mode is not supported.

You don't need to be a programmer nor know how to clone a repository to use Whook. All you need is to download the main.py file and follow the instructions below.

Disclaimer: This project is for my personal use. I'm not taking feature requests.

Currently supported exchanges:

ALERT SYNTAX

  • Symbol format:: ETHUSDT, ETHUSDT.P, ETH/USDT, ETH/USDT:USDT. All these formats will be accepted.

  • Account id: Just add the id you create for the account. No command associated. Account id must include at least one non-numeric character and obviously it shouldn't be the same as any of the command names.

  • Commands:
    buy - places buy order.
    sell - places sell order.
    position or pos - goes to a position of the given value. Use a positive value for Long and a negative value for Short.
    close - closes the position (position 0 also does it).
    limit:[customID]:[price] - Combined with buy/sell commads creates a limit order. The three fields must be separated by a colon with no spaces.
    Every limit order must have assigned its own unique ID so it can be identified for cancelling it
    cancel:[customID] - Cancels a limit order by its customID. The symbol is required in the order.
    cancel:all - Special keyword which cancels all orders from that symbol at once.

  • Quantities:
    [value] - quantity in base currency. Just the number without any extra character. Base currency is the coin you're trading.
    [value]$ - quantity in USDT. No command associated. Just the number and the dollar sign.
    [value]@ - quantity in contracts. No command associated. Just the number and the 'at' sign.
    [value]% - quantity as percentage of total USDT balance. Use a negative value for shorts when using the position command.
    All quantity types are interchangeable. All can be used with buy/sell/position commands.

  • Leverage:
    [value]x or x[value] - The x identifies this value as the leverage.

Examples:

  • Buy command using USDT:
    [account_id] [symbol] [command] [value in USDT] [leverage] - myKucoinA ETH/USDT buy 300$ x3

  • Position command using contracts:
    [symbol] [command] [value in contracts] [leverage] [account_id] - ETH/USDT position -500@ x3 myKucoinA
    Notice: This is a short position. For a long position use a positive value. Same goes when the value is in USDT
    The value of a contract differs from exchange to exchange. You have to check it in the exchange under contract information
    Example of a position alert from a strategy in Tradingview:
    myKucoinA {{ticker}} pos {{strategy.position_size}} x3
    This alert is all you should really need for running 90% of the strategies in TV

  • Sell command using base currency:
    [account_id] [symbol] [command] [value in USDT] [leverage] - myKucoinA ETH/USDT sell 0.25 x3
    This would sell 0.25ETH

  • Close position
    [account_id] [symbol] [command] [percentage] - myKucoinA ETH/USD close 33.33%
    The percentage parameter is optional. If not included it will close the full position.

  • Limit buy command using USDT:
    [account_id] [symbol] [command] [value in USDT] [leverage] [limit:[customID]:[price]] - myKucoinA ETH/USDT buy 300$ x3 limit:myid002:1012
    Will open a buy order at 1012. The management of the customID falls on you if you ever want to cancel it. Remember you can't open 2 orders with the same customID
    Some exchange peculiarities to be aware of:
    Bybit will not accept the same customID twice, even if the previous order is already cancelled.
    Coinex only accepts numeric customIDs.

  • Cancel limit order:
    [account_id] [symbol] [cancel:[customID]] - myKucoinA ETH/USDT cancel:myid002
    [account_id] [symbol] [cancel:all] - myKucoinA ETH/USDT cancel:all all orders from this symbol

Several orders can be included in the same alert, separated by line breaks. For example, you can send the orders for 2 different accounts inside the same alert. (the console will be a little messy when doing this, but the logs will be clean)

It's possible to add comments inside the alert message. The comment must be in a new line and begin with a double slash '//'. Why? You ask. Because I often forget the setting I used when I created the alert! Whook will simply ignore that line when parsing the alert.

HOW TO INSTALL AND RUN

Windows:
  • Download and install python. During the installation make sure to enable the system PATH option and at the end of the installation allow it to unlimit windows PATH length
  • Open the windows cmd prompt (type cmd in the windows search at the taskbar). Install the required python modules by typing "pip install ccxt" and "pip install flask" in the cmd prompt.

With these you can already run the script, but it won't have access online. For giving it access to the internet I recommend to use:

  • ngrok. Create a free ngrok account. Download the last version of ngrok and unzip it. In the ngrok website they provide an auth key, copy it. Launch the software and paste the auth code into the ngrok console (with the authcode ngrok will be able to stay open forever). Then type in the ngrok console: "ngrok http 80". This will create an internet address for your webhook. You have to add /whook at the end of it to comunicate with the Whook server. This will be the address you introduce in the tradingview alert

Note: Ngrok now allows free accounts to create one static domain. It allows to close Ngrok and keep the same address the next time you open it, which is nice.

Example of an address: https://e579-139-47-50-49.ngrok-free.app/whook

  • You can launch the script by double clicking main.py (as long as you enabled the PATH options at installing python). If for some reason Windows failed to associate .py files with python.exe you can create a .bat file inside the same directory as main.py with this inside
    @echo off
    python.exe main.py
    pause

CONFIGURATION - API KEYS

When you first launch the script it will create an accounts.json file in the script directory and exit with a 'no accounts found' error. This file is a template to configure the accounts API data. This file can contain as many accounts as you want separated by commas. It looks like this:

[
  {
   "ACCOUNT_ID":"your_account_name",
   "EXCHANGE":"exchange_name",
   "API_KEY":"your_api_key",
   "SECRET_KEY":"your_secret_key",
   "PASSWORD":"your_API_password",
   "MARGIN_MODE":"isolated"
  }
]

You have to fill your API_KEY and SECRET_KEY information in the accounts.json file.
The ACCOUNT_ID field is the name you give to the account. It's to be included in the alert message to identify the alert target account.
The PASSWORD field is required by Kucoin and Bitget but other exchanges may or may not use it. If your exchange doesn't give you a password when creating the API key just leave the field blank.
The MARGIN_MODE field defines the margin mode in which the account will operate. Valid names "isolated" or "cross". Defaults to isolated. It's only allowed to define it in a per account basis. There's no support to define it per symbol.
The EXCHANGE field is self explanatory. Valid exchange names are:

  • "kucoinfutures"
  • "bitget"
  • "coinex"
  • "bingx
  • "okx"
  • "okxdemo"(for testnet)
  • "bybit"
  • "bybitdemo"(for testnet)
  • "binance"
  • "binancedemo"(for testnet)
  • "krakenfutures"
  • "krakendemo"(for testnet)
  • "phemex"
  • "phemexdemo" (for testnet)

There is also one optional key: 'SETTLE_COIN' for cases where you want to trade non-USDT pairs (or non-USD in the case of Kraken). Different settle coins can't be combined, tho. Whook will only use one at once per account. If you want to trade in several settle coins you can create an account for each settle coin (they can reuse the same API keys).

HOW TO HOST IN AWS

(the easy way)

You can host a server in AWS EC2 for free. It can be a linux server or a windows server. You can find many tutorials in Youtube on how to do it. Here's a (slightly outdated) tutorial for windows: https://youtu.be/9z5YOXhxD9Q.
I host it in a Windows_server 2022 edition which was the latest at the time of writing this readme.

Once you have your virtual machine running follow the steps in the section above ("How to intall and run").

I'm not a linux user so I struggled to open the ports in the Linux virtual machine. If you have experience in Linux this may be easy to you.

KNOWN BUGS

  • Kraken: Whook is unable to set the margin mode. It will use whatever is set in the exchange for that symbol.
  • Kraken can't check leverage boundaries. If a order exceeds the maximum leverage the console may spam until the order times out.
  • BingX: Limit orders aren't setting the custom ID. They can only be cancelled using cancel:all
  • Things will most likely go south if you have a position with a leverage and you order the same position with a different leverage. Some exchanges may take the leverage change as you trying to change the leverage of the current position but not changing the amount of contracts. The order will go through, but the resulting position will depend on the exchange. I'll try to handle it but it's not a big priority for me.

TO DO LIST

  • Split whook in 2 files, one containing the accounts class. So it can be imported by other scripts to create orders directly. In short, for bots.
  • Add some account configuration optional keys. Like order timeout, alert timeout, margin mode...
  • 'pricelock' a made up mode to attempt to place market orders as limit orders.
  • Create some form of past trades storage better than the logs. Usable for trading performance analytics.

whook's People

Contributors

germangar avatar

Stargazers

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

Watchers

 avatar

whook's Issues

Whook not working bybit is that correct?

Im getting:

whook\main.py'

17:22:47 * Initializing account: [ WHOOK ] in [ bybit ]
Account creating failed: bybit {"retCode":10002,"retMsg":"invalid request, please check your server timestamp or recv_window param. req_timestamp[1694100204684],server_timestamp[1694100301862],recv_window[5000]","result":{},"retExtInfo":{},"time":1694100301862}

  • FATAL ERROR: No valid accounts found. Please edit 'accounts.json' and introduce your API keys

I filled in a Random account_id name and changed the exchange to bybit.
Added a API key and secret en removed Password info. But still isnt working.

[
{
"EXCHANGE":"bybit",
"ACCOUNT_ID":"WHOOK",
"API_KEY":"APIKEYYYYYYY****",
"SECRET_KEY":"APISECRET*****",
"PASSWORD":""
}
]

Any help please like to test this out :P

Requesting Exchanges (info)

Adding new exchanges is a pretty simple proccess at this point. I will accept requests, but some conditions need to be met.

  • The exchange needs to be supported by ccxt.
  • If the exchange has a testnet I'll straight up give it a try.
  • If the exchange doesn't have a testnet I'll ask the person requesting it to create a subaccount, move 15USDT there and share the subaccount API keys with me (privately! trading permission only). I'll probably spend 2 or 3 USDT in trading fees during the implementation. Once it's done the subaccount can be deleted.

Note: I will not add Mexc. I already did, only to find out the exchange doesn't allow to place orders since 2022.

Undefined positionContracts in parseAlert

line 1419, in parseAlert
command = 'sell' if positionContracts > quantity else 'buy'
TypeError: '>' not supported between instances of 'NoneType' and 'float'
command = 'sell' if positionContracts > quantity else 'buy'

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.