Skip to main content

Stepgridhedge

This strategy borrows the grid logic from the stepgrid strategy and uses it to trade both the long and short side of a futures market in hedge mode. The strategy uses the same order triggers as stepGrid does on spot, but in quite a different way.

To trade in hedge mode in Gunbot, the two sides act as independent trading pairs like USDT-BTC-LONG and USDT-BTC-SHORT. Both sides can have a position at the same time.

This strategy runs on the long side, meaning that strategy settings and targets only exist on this pair. It trades both longs and shorts using these targets.

note

Currently supported on Bybit futures, okGunbot & OKX swap markets,

Make sure to enable hedge / long/short mode on the exchange. For okGunbot and OKX, also make sure to set the account units to contracts in your profile settings on the exchange.

Trading behavior

The strategy is quite complex, but it can be summarized like this:

Price hits sell step:

  • is there a long that can be closed at profit? do that
  • else: dca short or skip step

Price hits buy step:

  • is there a short that can be closed at profit? do that
  • else: dca long or skip step

Grid targets are placed around the last order rate of the newest order of either the long or short side. If a step gets skipped, targets get placed around the skipped rate. Additionally, there are some none configurable methods that prevent dca orders to try to not accumulate too much too fast.

Let's go over a few example trades to explain what happens and how to read the strategy charts:

Short side is in position and price is better than average entry

About to close short

The chart of the short pair shows past trades and the average entry price of the current position. In this case, price is profitable and the bot can soon begin closing the short position. There is no ROE target, 'profitable' is meant as just the fact that trading fees are covered.

Long side chart shows the current targets

About to close short, targets visible

The long side chart shows the current targets. In this particular case the long side has a position that is quite far from average entry and is not allowed to further increase the long position size. Because the long side may not DCA at the moment, hitting the next lower target will cause a partial close on the short side.

DCA target exceeded, trailing starts

About to close short, trailing

Price has now dropped below the DCA target. This triggers an automatic price trailing process. As soon as trailing finishes, the short side is going to take some profits.

Trailing finished

New trading targets

Trailing is now finished, new targets are placed on the long pair around the rate where trailing finished. Since the expected next order was on the short side, no new orders show on the long side.

Partial short close

Partial close

On the short side, a partial close order has appeared. The position size has been reduced by about 30%. There still remains an open short position.

Fast forward a few trades

Partial close

Going forward in time a bit, this is how the short side played out. It is still in a short position, has placed several partial close orders and has been adding to the position each time the upper step was hit. By adding to the position even while it is in profit, the strategy can stay active a lot longer during one directional price movement.

tip

The above is just one example, it is not that easy to illustrate this strategy in a way that explains all possible situations. Expect to often see one side profitably trading for some time, while the opposing side will have a position it slowly averages down.

StepGridHedge settings

Balance settings

LabelConfig ParameterDefault ValueDetailed Description
Trading limitTRADING_LIMIT20The minimum amount you can invest for each order. For Bybit, this represents the notional value, and for OKX / okGunbot, it represents contracts.
Trading limit shortTRADING_LIMIT_SHORT20The minimum amount you can invest for each short order. Similar to the trading limit, for Bybit, this represents the notional value, and for OKX / okGunbot, it represents contracts.
Max buy countMAX_BUY_COUNT75This setting limits the maximum position size. When the maximum is reached, the strategy goes into close-only mode. For example, if the maximum is set to 40, your total position size cannot exceed 40 times the amount set as the 'trading limit'.
Max unhedgedMAX_UNHEDGED75This setting limits the maximum position size difference from the other side, applying only to orders that add to a position. For instance, if 'max unhedged' is set to 40 and is reached with a long position at 50x the trading limit and short at 10x, the long position cannot grow further until the short position also increases.
First trades multiplier (long)FIRST_TRADES_MULTIPLIERautoMultiplies the order size for the first few trades on the long side. In 'auto' mode, it automatically generates multiplier values between 1.5 and 5, mostly depending on the imbalance between both sides. If you set a manual value, such as 2, it means that eligible orders will be executed for twice the trading limit.
First trades threshold (long)FIRST_TRADES_THRESHOLDautoSets a position size threshold under which the first trades multiplier applies. In 'auto' mode, this usually allows 2 to 3 trades with an increased multiplier before continuing with trades at 1x the trading limit. If manually set to 10, the multiplier applies until the current position reaches 10 times the trading limit.
First trades multiplier (short)FIRST_TRADES_MULTIPLIER_SHORTautoSimilar to the long side setting, this multiplies the order size for the first few trades on the short side. The behavior in 'auto' mode and manual settings is the same as for the long side.
First trades threshold (short)FIRST_TRADES_THRESHOLD_SHORTautoThis setting works similarly to its counterpart for the long side, setting a threshold under which the first trades multiplier applies, with identical behavior in 'auto' mode and when manually set.

Customize grid

LabelConfig ParameterDefault ValueDetailed Description
PeriodPERIOD15Sets which candle period is used to calculate auto step size and trailing range. A lower value like 15 increases trading frequency and risk by using shorter duration candles.
Use trendUSE_TRENDfalseThis option allows the strategy to choose a trading side based on trend data from multiple timeframe charts, altering the trading limit accordingly. For instance, during a long-only phase, the trading limit for longs might be doubled while short orders can only close positions, and vice versa for a short-only phase. In undecided market conditions, both sides operate with the regular trading limit and can both dca and close positions.
Trend variantTREND_VARIANT4hSelects the data source for determining market direction, with options including '4h', '1h_15m', and 'custom'. Each setting affects trading behavior differently, such as modifying trading limits based on perceived market trends.
Auto step sizeAUTO_STEP_SIZEtrueWhen enabled, the bot automatically determines an appropriate grid step size based on the current volatility of the trading pair.
Step sizeSTEP_SIZE500Sets a fixed grid step size for buy and sell orders when auto step size is not enabled. For example, setting 500 on USDT-BTC configures the bot to trade each time the price moves by 500 USDT.
Pct step sizePCT_STEP_SIZEfalseEnables the step size to be defined as a percentage rather than a fixed amount, affecting how grid steps are calculated relative to price changes.
Enforce step sizeENFORCE_STEPfalseEnsures that the defined step size is adhered to strictly. This can be particularly useful for maintaining precise balance management, as it prevents orders from being executed unless they meet or exceed the targeted step improvement.
Minimum step up pctMIN_STEP_UP_PCT0Sets a minimum percentage for step size increases, to override the automatic step size if necessary. For instance, a setting of 1% ensures that the step up will not be less than 1% of the price, unless the auto step size already exceeds this value.
Minimum step down pctMIN_STEP_DOWN_PCT0Similar to the minimum step up percentage, this setting establishes a floor for step size reductions, ensuring that step downs are not less than a set percentage of the price, unless the auto step size is higher.
Minimum step asymmetricMIN_STEP_ASYMMETRICtrueWhen enabled, this setting applies the minimum step size only in the direction of dollar-cost averaging (DCA) for the currently smaller position. For example, if the short position is smaller than the long, the 'min step up pct' will apply to steps up, but not to steps down.

Advanced

LabelConfig ParameterDefault ValueDetailed Description
Stop after close (long)STOP_AFTER_CLOSE_LONGfalseWhen enabled, this setting stops all long trading activities once the current long position has been completely closed, preventing any new long positions from being opened until re-enabled.
Stop after close (short)STOP_AFTER_CLOSE_SHORTfalseOperates like its counterpart for long positions, stopping all short trading activities once the current short position is fully closed, to prevent opening new short positions until it is re-enabled.
Buy enabledBUY_ENABLEDtrueThis setting allows you to enable or disable the placement of long orders, providing control over whether new long positions can be initiated.
Sell enabledSELL_ENABLEDtrueSimilar to 'Buy enabled', this controls whether you can initiate new short positions by enabling or disabling the placement of short orders.
Close long enabledCLOSE_LONG_ENABLEDtrueAllows you to enable or disable the ability to partially or fully close long positions, providing flexibility in position management.
Close short enabledCLOSE_SHORT_ENABLEDtrueAllows you to enable or disable the ability to partially or fully close short positions, similar to the setting for long positions.
Panic closePANIC_CLOSEfalseEnables the immediate closure of all open positions at market price, useful in situations where a rapid exit from the market is necessary.
SMA periodSMAPERIOD50Defines the number of candles used to calculate support and resistance levels, which are then used to calculate trailing ranges. The default setting of 50 is generally adequate, but can be adjusted based on specific trading needs.
ATR periodATR_PERIOD50Sets the number of candles used to calculate the Average True Range (ATR), which is then used to determine auto step size. The default setting is typically suitable for most situations.
Buydown exponentBUYDOWN_EXPONENT0.5Determines the exponent used in the exponential buydown function, which helps manage the distance between DCA trades and the average entry price. A tool is available for experimenting with different values to find the most suitable setting for your strategy: use this code example to experiment. When in doubt, use default!
Instant open threshold (long)INSTANT_OPEN_THRESHOLD_LONG99999Specifies the minimum size of a long position required to engage 'always in position' trading on the short side. This behavior ensures continuous market engagement by starting to trail for a new position immediately after one is closed. Note: This setting is incompatible with 'enforce step size'.
Instant open threshold (short)INSTANT_OPEN_THRESHOLD_SHORT99999Operates similarly to the long threshold, specifying the minimum size of a short position required for continuous trading on the long side. This facilitates an aggressive trading posture by ensuring immediate market re-engagement after a position closure. Note: Incompatible with 'enforce step size'.
Static close qtySTATIC_CLOSE_QTYfalseWhen enabled, every order to close a position uses the same quantity: one times the trading limit. This can simplify the management of closing trades, making them more predictable.
Static close qty allow lossSTATIC_CLOSE_QTY_LOSSfalseThis setting, when enabled and combined with 'static close qty', allows the strategy to place partial close orders at a loss under certain conditions, such as when both trading sides have negative unrealized profits and the cumulative profit history is positive on the side being closed. This can help manage risks and recover from adverse market movements more effectively.
tip

Other parameters

Besides settings mentioned on this page, no other strategy setting has any effect on stepgridhedge.