Skip to content

Commit

Permalink
Merge pull request #21 from h20liquidity/2024-07-08-stop-limit
Browse files Browse the repository at this point in the history
Stop Limit
  • Loading branch information
Siddharth2207 authored Jul 17, 2024
2 parents b5df67d + 9950e32 commit 60c5871
Show file tree
Hide file tree
Showing 2 changed files with 479 additions and 0 deletions.
269 changes: 269 additions & 0 deletions strategies/wlth/wlth-stop-limit.rain
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
# Stop Limit Orders.
#
# The stop price dictates the price whether the order is triggered, then the limit price dictates the price at which the order is filled.
# Once the market price reaches below the stop price, the order becomes a limit order and then the limit price dictates the price at
# which the order is filled.
# The only difference between a stop-limit and limit order is that the order won't even be visible until the stop price is reached.
# https://www.investopedia.com/terms/s/stop-limitorder.asp

networks:
base-community:
rpc: https://mainnet.base.org
chain-id: 8453
network-id: 8453
currency: ETH

subgraphs:
base-community: https://api.thegraph.com/subgraphs/name/h20liquidity/base-0x2aee87

orderbooks:
base-community:
address: 0x2AeE87D75CD000583DAEC7A28db103B1c0c18b76
network: base-community
subgraph: base-community

deployers:
base-community:
address: 0x56394785a22b3BE25470a0e03eD9E0a939C47b9b
network: base-community

tokens:
base-wlth:
network: base-community
address: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D
base-usdc:
network: base-community
address: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

orders:
# vault-id generated with `openssl rand -hex 32`
base-wlth-sell:
orderbook: base-community
inputs:
- token: base-usdc
vault-id: 0xe8c8cd1fae13ab9fa00c611a0d57cbd9298e939e34f1a38361fd37f917d4b065
outputs:
- token: base-wlth
vault-id: 0xe8c8cd1fae13ab9fa00c611a0d57cbd9298e939e34f1a38361fd37f917d4b065
base-wlth-buy:
orderbook: base-community
inputs:
- token: base-wlth
vault-id: 0x4767d92a5f01500424d2a2dd88964314f8a98a6b66bcf1db362b0ad9006c93e8
outputs:
- token: base-usdc
vault-id: 0x4767d92a5f01500424d2a2dd88964314f8a98a6b66bcf1db362b0ad9006c93e8

scenarios:
stop-limit-order:
network: base-community
deployer: base-community
orderbook: base-community
bindings:
# Ask for now, registry in future.
uniswap-words: 0xD6B34F97d4A8Cb38D0544dB241CB3f335866f490
orderbook-subparser: 0x8D96ea3EF24D7123882c51CE4325b89bc0d63f9e

# Uniswap V3 factory addresses and init code
uniswap-v3-factory: 0x33128a8fC17869897dcE68Ed026d694621f6FDfD
uniswap-v3-init-code: '[uniswap-v3-init-code]'

# Uniswap V3 pool fee
pool-fee: '[uniswap-v3-fee-medium]'
scenarios:
buy:
bindings:
# Input and Output token from perspective of the order.
input-token-address: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D
output-token-address: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

# Stop ratio, once market price reaches below the stop price order will be active.
stop-ratio: 45

# io-ratio and amount for first limit order.
limit-ratio-1: 46
output-amount-1: 1

# Binding to get current orcale price
get-oracle-price: '''get-oracle-price-univ3'

scenarios:
prod:
bindings:
plottables: '''plottables-prod'
get-trade-count: '''get-trade-count-prod'
ensure-stop-loss: '''ensure-stop-loss-increase'
metric:
runs: 1
bindings:
plottables: '''plottables-metric'
get-trade-count: '''get-trade-count-prod'
ensure-stop-loss: '''ensure-stop-loss-metric'
sell:
bindings:
# Input and Output token from perspective of the order.
input-token-address: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
output-token-address: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D

# Stop ratio, once market price reaches below the stop price order will be active.
stop-ratio: 0.022

# io-ratio and amount for first order.
limit-ratio-1: 0.02
output-amount-1: 50

# Binding to get current orcale price
get-oracle-price: '''get-oracle-price-univ3'

scenarios:
prod:
bindings:
plottables: '''plottables-prod'
get-trade-count: '''get-trade-count-prod'
ensure-stop-loss: '''ensure-stop-loss-decrease'
metric:
runs: 1
bindings:
plottables: '''plottables-metric'
get-trade-count: '''get-trade-count-prod'
ensure-stop-loss: '''ensure-stop-loss-metric'

charts:
Buy WLTH, sell USDC single limit order:
scenario: stop-limit-order.buy.metric
metrics:
- label: Stop Price
value: 0.6.2
unit-suffix: " WLTH/USDC"
- label: Price
value: 0.6.3
unit-suffix: " WLTH"
description: 'Unit price, WLTH per USDC (stack item 0.6.3)'
- label: Effective Price (as visible on dex tools)
value: 0.6.4
unit-suffix: " USDC"
description: 'Effective price in USDC denomination'
- label: Units
value: 0.6.5
unit-suffix: " USDC"
description: 'Units purchased (stack item 0.6.5)'
- label: Total Price
value: 0.6.6
unit-suffix: " WLTH"
description: 'Amount of WLTH recevied.'
Buy USDC, sell WLTH single limit order:
scenario: stop-limit-order.sell.metric
metrics:
- label: Stop Price
value: 0.6.2
unit-suffix: " USDC/WLTH"
- label: Price
value: 0.6.3
unit-suffix: " USDC"
description: 'Unit price, USDC per WLTH (stack item 0.6.3)'
- label: Effective Price (as visible on dex tools)
value: 0.6.3
unit-suffix: " USDC"
description: 'Effective price in USDC denomination'
- label: Units
value: 0.6.4
unit-suffix: " WLTH"
description: 'Units sold (stack item 0.6.4)'
- label: Total Price
value: 0.6.6
unit-suffix: " USDC"
description: 'Amount of USDC recevied.'

deployments:
base-wlth-buy:
scenario: stop-limit-order.buy.prod
order: base-wlth-buy
base-wlth-sell:
scenario: stop-limit-order.sell.prod
order: base-wlth-sell
---
#uniswap-words !The subparser for the Uniswap words
#orderbook-subparser !The subparser for the Orderbook words
#uniswap-v3-factory !Uniswap v3 factory address.
#uniswap-v3-init-code !Uniswap v3 init code hash.
#pool-fee !Uniswap v3 pool fees.

#input-token-address !Input token from the perspective of the order.
#output-token-address !Output token from the perspective of the order.

#stop-ratio !Ratio below which trade is active
#limit-ratio-1 !IO ratio for first limit order.
#output-amount-1 !Output amount for first limit order.

#get-oracle-price !Binding to get currect price from orcale.
#ensure-stop-loss !Binding to ensure stop loss.

#count-key "count-key"
#plottables !Binding to plot values.
#get-trade-count !Binding to get trade count.
#test-trade-count !Test trade count for plotting.

#plottables-prod
_ _: ;

#plottables-metric
amount io-ratio: ,
_: stop-ratio,
_: limit-ratio-1,
effective-limit-ratio-1: inv(limit-ratio-1),
_: output-amount-1,
input-amount-1: mul(limit-ratio-1 output-amount-1);

#get-trade-count-prod
trade-count-key: hash(order-hash() count-key),
trade-count: get(trade-count-key);

#get-ratio-amount
count: ,
output-amount: conditions(
equal-to(count 0) output-amount-1
"Max order count"
),
io-ratio: conditions(
equal-to(count 0) limit-ratio-1
"Max order count"
);

#get-oracle-price-univ3
_: uniswap-v3-quote-exact-input(
output-token-address input-token-address
1
uniswap-v3-factory uniswap-v3-init-code
pool-fee
);

#ensure-stop-loss-decrease
current-orcale-price: ,
:ensure(
less-than(current-orcale-price stop-ratio)
"Stop price."
);

#ensure-stop-loss-increase
current-orcale-price: ,
:ensure(
greater-than(current-orcale-price stop-ratio)
"Stop price."
);

#ensure-stop-loss-metric
_: ;

#calculate-io
using-words-from uniswap-words orderbook-subparser

orcale-price: call<'get-oracle-price>(),
:call<'ensure-stop-loss>(orcale-price),
trade-count: call<'get-trade-count>(),
final-amount final-ratio: call<'get-ratio-amount>(trade-count),
:call<'plottables>(final-amount final-ratio);

#handle-io
trade-count-key: hash(order-hash() count-key),
trade-count: get(trade-count-key),
:set(trade-count-key add(trade-count 1));
Loading

0 comments on commit 60c5871

Please sign in to comment.