1. Range-breaking Strategy
This example highlights how to create a strategy by adopting custom rules
Open position Long Position: Break above the last 60-minute peak Short Position: Break below the last 60-minute trough Close position Close long Position: Break below the 10-minute peak Close short Position: Break above the 10-minute trough
1.1 Rules on new creating
1. Long position opening
Conditions price("BTC_USDT", 0, "usdt") > max_price("BTC_USDT", 60, "usdt") and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") == 0
(1)price("BTC_USDT", 0, "usdt") > max_price("BTC_USDT", 60, "usdt") This condition means that the current BTC perpetual contract price is greater than the highest price in the last 60 minutes.
(2)and order_collateral("usdt") ==0 This condition means that there are no pending orders for the current strategy; order_collateral is a predefined function, which obtains the margin of the unfilled orders. If the return value of this function is 0, it means that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") == 0 This condition means that the current position size of the strategy is 0, and position is a predefined function. It indicates the position in specified market for the current strategy.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5X; Side: Long; Limit price: bid_price("BTC_USDT", 1, "usdt"); Size: 1.

2. Short position opening-short position opening conditions
Conditions price("BTC_USDT", 0, "usdt") < min_price("BTC_USDT", 60, "usdt") and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") == 0
(1)price("BTC_USDT", 0, "usdt") < min_price("BTC_USDT", 60, "usdt") This condition means that the current BTC perpetual contract price is lower than the lowest price in the last 60 minutes.
(2)and order_collateral("usdt") ==0 This condition means that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") == 0 This condition means that the current position size for the strategy is 0.
Execution strategy
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5x; Dide: Short; Limit price: ask_price("BTC_USDT", 1, "usdt"); Size: 1.

3. Closing long conditions -close long position
Conditions price("BTC_USDT", 0, "usdt") < min_price("BTC_USDT", 10, "usdt") and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") > 0
(1)price("BTC_USDT", 0, "usdt") < min_price("BTC_USDT", 10, "usdt") This condition means that the current BTC perpetual contract price is lower than the lowest price in the last 10 minutes.
(2)and order_collateral("usdt") ==0 This condition indicates that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") > 0 This condition implies that the strategy has already set up positions, based on which the positions will be closed.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5x; Side: Short; Limit price: ask_price("BTC_USDT", 1, "usdt"); Size: 1.

4. Close short conditions
Conditions price("BTC_USDT", 0, "usdt") > max_price("BTC_USDT", 10, "usdt") and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") < 0
(1)price("BTC_USDT", 0, "usdt") > max_price("BTC_USDT", 10, "usdt") This condition means that the current BTC perpetual contract price is higher than the highest price in the last 10 minutes.
(2)and order_collateral("usdt") ==0 This condition indicates that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") < 0 This condition implies that the strategy has already set up positions, based on which the positions will be closed.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5x; Side: Long; Limit price: ask_price("BTC_USDT", 1, "usdt"); Size: 1.

1.2 Rules on strategy creating
Establish the opening conditions and closing conditions in turn by adding the "long position opening conditions," "short position opening conditions," "close long position conditions" and "close short position conditions" in turn.

2. Extremum Breakout Strategy
This example highlights how to create a strategy by using custom variable(s)
Open Long/Close Short
Where: pricet represents the last price at the current moment
closet-1 represents the closing price at the previous moment
Open Short/Close Long
2.1New variable
Variable name: range
Condition
true
Variable value
max_price("BTC_USDT", 10, "usdt") - min_price("BTC_USDT", 10, "usdt")

2.2 Rules on new creating
1. Open long conditions
Conditions price("BTC_USDT", 0, "usdt")>price("BTC_USDT", 1, "usdt")+get_value("range")*0.5 and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") == 0
(1)price("BTC_USDT", 0, "usdt")>price("BTC_USDT", 1, "usdt")+get_value("range")*0.5
This condition indicates open long positions as the following rule:
Get the value of "range" variable by "get_value" predefined fuction
(2)and order_collateral("usdt") ==0 This condition indicates that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") == 0 This condition means that there are opening positions for the strategy.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5x; Side: Long; Limit price: bid_price("BTC_USDT", 1, "usdt"); Size: 1.

2. Open short conditions
Conditions
price("BTC_USDT", 0, "usdt")<price("BTC_USDT", 1, "usdt") - get_value("range")0.5 and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") == 0
(1)price("BTC_USDT", 0, "usdt")<price("BTC_USDT", 1, "usdt") - get_value("range")0.5
This condition indicates open short positions as the following rule:
Get the value of "range" variable by "get_value" predefined fuction.
(2)and order_collateral("usdt") ==0 This condition indicates that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") == 0 This condition means that there are no opening positions for the strategy.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5x; Side: Short; Limit price: ask_price("BTC_USDT", 1, "usdt"); Size: 1.

3. Close long conditions
Conditions price("BTC_USDT", 0, "usdt")<price("BTC_USDT", 1, "usdt") - get_value("range")*0.5 and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") > 0
(1)price("BTC_USDT", 0, "usdt")<price("BTC_USDT", 1, "usdt") - get_value("range")*0.5
This condition indicates close long positions as the following rule:
Get the value of "range" variable by "get_value" predefined fuction
(2)and order_collateral("usdt") ==0 This condition means that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") > 0 This condition implies that the strategy has already set up positions, based on which the positions will be closed.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5X; Side: Short; Limit price: ask_price("BTC_USDT", 1, "usdt"); Size: 1.

4. Close short conditions
Conditions price("BTC_USDT", 0, "usdt")>price("BTC_USDT", 1, "usdt")+get_value("range")*0.5 and order_collateral("usdt") ==0 and position("BTC_USDT", "usdt") < 0
(1)price("BTC_USDT", 0, "usdt")>price("BTC_USDT", 1, "usdt")+get_value("range")*0.5
This condition indicates close short positions as the following rule:
Get the value of "range" variable by "get_value" predefined fuction
(2)and order_collateral("usdt") ==0 This condition indicates that there are no pending orders for the current strategy.
(3)and position("BTC_USDT", "usdt") < 0 This condition implies that the strategy has already set up positions, based on which the positions will be closed.
Execution logic
Type: Contract Trading; Market: BTC/USDT Perpetual Contract; Order Type: Limit; Leverage: 5X; Side: Long; Limit price: bid_price("BTC_USDT", 1, "usdt");Size: 1.

2.3 Rules on strategy creating
When using custom variables, the custom variables should be executed before the conditions in the execution sequence. For example, the "range" custom variable should be executed first, followed by the opening and closing conditions in the order 2 to 5.

Gate reserves the final right to interpret the product.
