Perpetual Market Making
Perpetual market making allows Hummingbot users to market make on supported derivatives exchanges. Position management features are introduced to configure the bot further to make managing positions easier and remove the need to manually interact with the derivative exchange.
Like pure market making strategy, it keeps placing limit buy and sell orders on the order book and waits for other participants (takers) to fill its orders.
In this document, we will explain how the strategy works by dividing it into three behaviors:
- Creating orders to open a position
- Opening positions after a filled order event
- Creating orders to close a position
- Make sure to connect to an exchange supported by Perpetual Market Making strategy
- Run the
createcommand and enter
- Enter the name of the connector and the trading pair you want to use with this strategy
- Enter how much leverage you want to use. Leverage allows you to open a position with a fraction of a cost. The higher the leverage, the higher the risk. Please manage accordingly.
- Select a
Hedgemode. These are both explained further in their respective sections below.
- Enter the bid and ask spreads from mid price of your limit orders (for opening a position)
- Enter how many seconds you want orders to refresh when not filled
- Enter the size of orders you want to place in
- Select a
- If you selected
Profit_taking, enter the spreads of your profit taking orders to close a long or short position in
- If you selected
Trailing_stop, enter the spread when you want the bot to start trailing as
ts_activation_spreadand the exit price as
- If you selected
- Enter the spread from entry price to close a position with a stop loss order
- Choose between limit or market order when
close_position_order_typevalue for stop loss and trailing stop orders
- The strategy configuration file is saved in the Hummingbot folder
Initially, limit buy and sell orders are created based on
ask_spread settings. This determines the entry price of the position to be opened when these orders are filled.
What happens if both my orders are not filled?
If the orders are not filled when they reach
order_refresh_time, they are cancelled and a new set of orders are created to ensure our orders are refreshed based on specified bid/ask spread.
With the exception of
order_refresh_tolerance_pct value, if the orders are within the tolerable change in spread then they are not cancelled. A log message on the right pane will show “Not cancelling active orders since difference between the new order prices and current order prices is within order_refresh_tolerance_pct”.
The strategy uses the filled order’s price as the entry price of the position. The number of positions you can open depends on the
If one of my orders is filled, what happens to the opposite order that was not filled?
One-way mode, if an order is filled the opposite order is cancelled. For example, if your buy order is filled it opens a LONG position, cancels the opening sell order and creates a closing sell order.
Hedge mode, if one order is filled the opposite order is not cancelled. For example, if your buy order is filled it opens a LONG position and creates a closing sell order. The opening sell order initially created remains active, waiting to get filled to open a SHORT position simultaneously.
One-way mode, only one position can be opened i.e. you only have either a LONG or a SHORT position.
bid_spread: 0.01 ask_spread: 1.00 order_refresh_time: 10 position_mode: One-way
Using the sample configuration above, we will try to get our buy order only filled on purpose.
If the buy order is filled then the opposite sell order is cancelled and a long position is opened. On the other hand, if your sell order is filled first, the opposite buy order is cancelled and opens a short position.
Hedge mode, two positions can be opened simultaneously i.e. you can have one LONG and one SHORT position.
bid_spread: 0.01 ask_spread: 0.05 order_refresh_time: 10 position_mode: Hedge
Using the sample configuration above, we will try to get our buy order filled first on purpose.
If the buy order is filled, a long position is opened and the opposite sell order is not cancelled. If the sell order eventually gets filled even with an open long position, a short position will be opened.
Positions are closed in three different ways:
- Profit taking orders are filled (for
- Market falls to or below the estimated exit price (for
- Stop loss is triggered because profitability falls to or below the
Why is my bot not creating orders to close a position?
The strategy includes a logic that it will not create these orders when the position's profitability is at a loss (PNL at negative value). In the sample screenshot below, you can see that we have an open long position but no profit taking orders because PNL (ROE%) is at a loss.
These are the logs in Hummingbot. Notice our position was opened at 03:52 when the buy order was filled but only created a profit taking sell order after 8 minutes when our PNL went up to a positive value.
Hummingbot creates a sell order to close a long position and a buy order to close a short position. If these orders are filled, the position is closed.
position_management: Profit_taking long_profit_taking_spread: 5 short_profit_taking_spread: 1
If the price of your filled buy order is 10,000 then it opens a long position with that entry price. If your
long_profit_taking_spread is set to 5% it will create a profit taking sell order at 10,500.
If the price of your filled sell order is 10,000 it opens a short position. The bot will create a profit taking buy order at 9,900 because our
short_profit_taking_spread is set to 1% value.
This position management allows users to maximize the profitability of a position. When an order is filled and a position is opened, it waits for PNL to reach the
ts_activation_spread to start trailing. If this value is set to 0, the bot starts trailing immediately as soon as the order is filled.
Trailing is when Hummingbot monitors for the market’s peak price and determines your exit price based on
ts_activation_spread: 10 ts_callback_rate: 5
Let’s say your buy order is filled again at 10,000 and opens a long position. It waits for the best sell price to reach 11,000 (10% from the entry price) before the bot can start trailing by monitoring for the peak price. The market went down to 10,500 the next minute and went up again to 12,000. Our new peak price is now 12,000 and our exit price is 11,400 (5% callback rate).
When the market falls to or below 11,400 the bot will create a limit or market (depending on
close_position_order_type value) sell order to close the position.
Estimated exit price is Nill USDT
NILL means the exit price is not profitable based on the current peak price.
If the position’s profitability (PNL) is equal to or below the
stop_loss_spread, the bot will create a limit or market order (depending on
close_positon_order_type value) at the current mark price to close the position.
If there are outstanding limit orders, they are cancelled and replaced with a stop loss order.
Most of us are aware that submitting a market order instantly executes a trade by taking the best order in the order book while a limit order is an order that you place on the order book (taker) with a specific limit price, waiting for someone to fill that order therefore adding liquidity to the market.
Taker fees are typically higher than maker fees in most exchanges.
close_position_order_type parameter, some users use
LIMIT order type for stop loss or trailing stop orders to close a position to take advantage of the maker fees, while some users use
MARKET order type in case of network connectivity problems.
If you’re running the bot or strategy on a machine with poor network connection, chances are the order book prices will not be updated right away which could affect your limit order prices and your positions might be closed at the profitability you were not expecting.
Using market order type helps ensure that your positions are closed at mark price but paying higher fees.
Hummingbot prompts to enter the values for these parameters when creating the strategy.
|The derivative exchange where you want to trade|
|Token trading pair for the exchange e.g. BTC-USDT|
|How much the position is increased by|
|Determines the number of positions that can be opened simultaneously. |
Refer to this section for more information.
|How far away from the price reference (by default, mid price) to place the buy order|
|How far away from the price reference (by default, mid price) to place the sell order|
|Time (in seconds) to cancel active orders and place new ones with specified spreads.|
|The size or amount of your bid and ask orders|
|Which position management feature to use. |
Current available options:
|When using profit taking mode, the spread of profit taking order from the entry price to close a long position.|
|When using profit taking mode, the spread of profit taking order from the entry price to close a short position.|
|Used with trailing stop position management, the profitability % of your position for the bot to start trailing.|
|Used with trailing stop position management, the callback % from the peak price to determine your position's exit price.|
|Triggers to close a position by creating a stop loss order when profitability % falls below this value.|
|The order type used (limit or market) when using trailing stop or when a stop loss is triggered. |
Refer to this section for more information.
Currently, only the following parameters can be reconfigured without stopping the bot. The changes will take effect in the next order refresh.