Skip to content

Commit

Permalink
stop limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Jul 8, 2024
1 parent 1e350a2 commit 0072107
Show file tree
Hide file tree
Showing 2 changed files with 407 additions and 0 deletions.
266 changes: 266 additions & 0 deletions strategies/stop-limit.rain
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
# Limit Order Strategy
# 5 buy and sell targets
# Each target you set buy price and amount and sell price and amount
# Target Network: Base
# Quote (Input / Incoming): USDC or WLTH
# Base (Output / Outgoing): WLTH or USDC
# Token contract: https://basescan.org/address/0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D
# Token github: NA
# Liquidity protocol: Uniswap V3
# Liquidity pool address: https://www.dextools.io/app/en/base/pair-explorer/0x1536ee1506e24e5a36be99c73136cd82907a902e?t=1717921711270
# Liquidity pool fee: 0.3%

networks:
base-community:
rpc: https://rpc.ankr.com/base/8e4cac5728d95471ae55724953c7ae6f19a227ac05146276f09717692b732dae
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: '[uniswap-v3-factory]'
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: 35
ouptut-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-prod'
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.05

# io-ratio and amount for first order.
limit-ratio-1: 0.02
ouptut-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-prod'
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.3.2
unit-suffix: " WLTH/USDC"
- label: Price
value: 0.3.3
unit-suffix: " WLTH"
description: 'Unit price, WLTH per USDC (stack item 0.3.2)'
- label: Effective Price (as visible on dex tools)
value: 0.3.4
unit-suffix: " USDC"
description: 'Effective price in USDC denomination'
- label: Units
value: 0.3.5
unit-suffix: " USDC"
description: 'Units purchased (stack item 0.3.3)'
- label: Total Price
value: 0.3.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.3.2
unit-suffix: " USDC/WLTH"
- label: Price
value: 0.3.3
unit-suffix: " USDC"
description: 'Unit price, USDC per WLTH (stack item 0.3.2)'
- label: Effective Price (as visible on dex tools)
value: 0.3.3
unit-suffix: " USDC"
description: 'Effective price in USDC denomination'
- label: Units
value: 0.3.4
unit-suffix: " WLTH"
description: 'Units sold (stack item 0.3.3)'
- label: Total Price
value: 0.3.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.
#ouptut-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),
_: ouptut-amount-1,
input-amount-1: mul(limit-ratio-1 ouptut-amount-1);

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

#get-ratio-amount
trade-count: call<'get-trade-count>(),
ouput-amount: conditions(
equal-to(trade-count 0) ouptut-amount-1
"Max order count"
),
io-ratio: conditions(
equal-to(trade-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-prod
current-orcale-price: ,
:ensure(
less-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>(),
:ensure<'ensure-stop-loss>(orcale-price),
final-amount
final-ratio: call<'get-ratio-amount>(),
: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 0072107

Please sign in to comment.