< All Topics
Print

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 AILidex, 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, okAILidex & OKX swap markets,

Make sure to enable hedge / long/short mode on the exchange. For okAILidex 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.

Balance settings
Setting Description
Trading limit
Minimum amount to invest per buy order.

On an USDT-BTC pair, a trading limit of 20 means that every buy order is for 20 USDT. This setting applies for buy orders on the long pair.

On okAILidex and OKX this amount is in contracts.

Trading limit short
Minimum amount to invest per sell order.

On an USDT-BTC pair, a trading limit of 20 means that every sell order is for 20 USDT. This setting applies for sell orders on the short pair.

On okAILidex and OKX this amount is in contracts.

Max buy count
Limits maximum position size.

When max is reached, the strategy goes in close only mode. A value of 40 means that the total position size may not exceed 40x the amount set as ‘trading limit’.

Max unhedged
Limits maximum position size difference.

When max is reached, the strategy goes in close only mode.

Example: max unhedged set to 40 and is reached, long is at 50x tl position, short at 10x tl. Long can now not grow further until the short grows too.

First trades multiplier
Increase invest in first few trades

Multiplies the order size for the first x trades on the long side.

Set ‘auto’ to use default behavior. Auto mode generates multiplier values between 1.5 and 5, mostly depending on the imbalance between both sides.

With a manually set value, you use 2 as multiplier value, it means that eligible orders get fired for tl * 2.

First trades multiplier (short)
Increase invest in first few trades

Multiplies the order size for the first x trades on the short side.

Set ‘auto’ to use default behavior. Auto mode generates multiplier values between 1.5 and 5, mostly depending on the imbalance between both sides.

With a manually set value, you use 2 as multiplier value, it means that eligible orders get fired for tl * 2.

First trades threshold (long)
Limit increased invest

Sets a position size threshold under which first trades multiplier applies.

Set ‘auto’ to use default behavior. In auto mode it will usually allow 2 to 3 trades with first trades multiplier, and continues with 1x tl trades after that.

When you set 10 as value, first trades multiplier applies until the current position is tl * 10.

First trades threshold (short)
Limit increased invest

Sets a position size threshold under which first trades multiplier applies.

Set ‘auto’ to use default behavior. In auto mode it will usually allow 2 to 3 trades with first trades multiplier, and continues with 1x tl trades after that.

When you set 10 as value, first trades multiplier applies until the current position is tl * 10.

Customize grid settings
Setting Description
Period
Sets the chart timeframe

Sets which candles are used to calculate auto step size and trailing range. A lower value increases trading frequency and risk.

Use trend
Pause trading on the losing side

Gives the strategy the option to pick a side and manipulate trading limit, based on trend data gathered on different timeframe charts.

During stronger trends, expect this mechanism to pause dca on the losing side for longer periods of time. Strategy behavior is fundamentally different with or without this option.

Trend variant
Data source to use for market direction

Sets the data source to use for determining market direction.

Epected behavior in 4h mode:

When it detects a phase where it trades long only:

Trading limit for longs multiplied by 2
Shorts may only close, not dca
When it detects a phase where it trades short only:

Trading limit forshorts multiplied by 2
Longs may only close, not dca
When it detects an undecided zone:

Both long and short use regular trading limit
Both sides may dca and close
During stronger trends, expect this mechanism to pause dca on the losing side for longer periods of time (think weeks, not days or hours). Strategy behavior is fundamentally different with or without this option.

Epected behavior in 1h_15m mode:

When it detects a phase where it trades long only:

TL for longs are multiplied by 2
Shorts may only close, not dca
When it detects a phase where it trades short only:

TL for shorts are multiplied by 2
Longs may only close, not dca
When it detects an undecided zone:

Both sides may only close, not dca
Epected behavior in custom mode:

Save your own trend value as an override called CUSTOM_DIRECTION. Trading behavior is like in 4h trend variant, just with your own trend definitions:

CUSTOM_DIRECTION value 1 for long
CUSTOM_DIRECTION value -1 for short
CUSTOM_DIRECTION value 0 for both sides
Auto step size
Enables using automatic step size.

Lets the bot automatically determine a grid step size that fits the pairs current volatility, Based on Average True Range (ATR).

Step size
Sets a manual step size.

Manually defined grid step size for buy and sell orders, when auto step size is not enabled.

Normally set as a price: setting 500 on USDT-BTC makes the bot trade each time price moves 500 USDT.

When using Pct step size, the value represents a percentage: setting 2 will lead to having steps of 2% of current price.

Pct step size
Use a percentage based step size.

When enabled, the value for step size represents a percentage.

Enforce step size.
Predictable minimum distance between orders.

Used to ensure orders are not placed at a rate worse than step target.

Price trailing can sometimes finish at a rate worse than the current grid step up or down. With this option enabled, orders may only get fired when trailing finishes at a better rate than the current step target. This can be useful for very precise balance management.

Minimum step up pct
Minimum percentage size for steps up

To overrule automatic step size. Setting 1 means that a step up is set to 1% of price, unless auto step size is already higher than 1%

Minimum step down pct
Minimum percentage size for steps down

To overrule automatic step size. Setting 1 means that a step down is set to 1% of price, unless auto step size is already higher than 1%

Minimum step asymmetric
To use min step size on one side only

When enabled, min step size (either up or down) is only applied in the dca direction of the currently smallest position.

Example: if short position is smaller than long position, then ‘min step up pct’ is applied to steps up, while ‘min step down pct’ is not applied to steps down

Advanced settings
Setting Description
Stop after close long
To stop trading the long side after the current position closes.

Averaging down of the current position continues, partial close orders do not make the bot stop afterwards.

Stop after close short
To stop trading the short side after the current position closes.

Averaging down of the current position continues, partial close orders do not make the bot stop afterwards.

Buy enabled
Allow or disallow placing buy orders on the long side.

When disabled, no buy orders will be placed at all.

Sell enabled
Allow or disallow placing sell orders on the short side.

When disabled, no sell orders will be placed at all.

Close long enabled
Allow or disallow placing close orders on the long side.

When disabled, no close orders will be placed at all.

Close short enabled
Allow or disallow placing close orders on the short side.

When disabled, no close orders will be placed at all.

SMA period
How many candles are used for calculating support and resistance levels.

By default 50 candles are used to calculate support and resistance. In this strategy, support and resistance are used to determine appropriate trailing ranges.

ATR period
How many candles are used for calculating ATR.

Number of candles used to calculate Average True Range, which is used to calculate auto step size. Default value of 50 should usually not need tuning.

Buydown exponent
Exponent to use in the built-in exponential buydown function that forces a distance between dca trades and avg entry price.

Use this code example to experiment what different values result in. When in doubt, use default!

Set 0 to completely disable this feature.

Instant open threshold (long)
Minimum long position size, to use always in position trading on short side

Behavior of ‘always in position’ trading is to immediately start trailing for a new position after completely closing one.

When set to 50, the short side starts always in position trading when long position is more than 50x TL in size. Cannot be used together with enforce step size

Instant open threshold (short)
Minimum short position size, to use always in position trading on long side

Behavior of ‘always in position’ trading is to immediately start trailing for a new position after completely closing one.

When set to 50, the long side starts always in position trading when short position is more than 50x TL in size. Cannot be used together with enforce step size

Static close qty
When enabled, every close order uses the same order quantity: 1x trading limit.

Panic close
Close everything immediately

When enabled, positions on both sides are closed immediately.

Table of Contents