Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
merge buy and sell calculate io
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Mar 7, 2024
1 parent f664523 commit 625ed9c
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/trend-trader.rain
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
---
#reserve-decimals !The decimals of the reserve token to be tracking price against.
#mean-cooldown !The mean cooldown time in seconds.
#bounty !The bounty to offer for the trade as normalized 18 decimal reserve amount.
#jittery-binomial-bits !The number of bits to use for the binomial distribution.
#reserve-token !The token that is considered the reserve which defines trade sizings and that TKN trends against.
#reserve-decimals !The decimals of the reserve token.

#times !The expression to get the last time and now as unix timestamps in seconds.

#real-times
last-time:
get(order-hash()),
Expand All @@ -18,6 +15,7 @@
#test-times
_ _: test-last-time test-now;

#jittery-binomial-bits !The number of bits to use for the binomial distribution.
#jittery-binomial
seed:,
binomial18-10:
Expand All @@ -29,6 +27,7 @@
jittery-combined-normalized:
decimal18-div(jittery-combined decimal18-add(1e18 int-to-decimal18(jittery-binomial-bits)));

#mean-cooldown !The mean cooldown time in seconds.
#ensure-cooldown
last-time
now:,
Expand Down Expand Up @@ -64,11 +63,11 @@
#mean-reserve-amount18 !The mean amount of reserve to offer per trade as normalized 18 decimals.
#trend-up-factor !The factor to multiply the trend with when the trend is up as normalized 18 decimals.
#trend-down-factor !The factor to multiply the trend with when the trend is down as normalized 18 decimals.
#calculate-reserve
#calculate-reserve-amount18
last-time now: call<times>,
:call<'ensure-cooldown>(last-time now),
random-multiplier18: call<'jittery-binomial>(last-time),
jittered-amount-reserve18: decimal18-mul(
jittered-reserve-amount18: decimal18-mul(
decimal18-mul(mean-reserve-amount18 2e18)
random-multiplier18
),
Expand All @@ -78,26 +77,31 @@
decimal18-power(trend-ratio trend-up-factor)
decimal18-power(trend-ratio trend-down-factor)
),
amount-reserve18: decimal18-mul(jittered-amount-reserve18 skewed-trend-ratio),
amount-reserve: decimal18-scale-n<reserve-decimals>(amount-reserve18);
reserve-amount18: decimal18-mul(jittered-reserve-amount18 skewed-trend-ratio);

#sell-calculate-io
trend-ratio _ amount-reserve18 amount-reserve: call<'calculate-reserve>(),
tkn-amount18 last-price-timestamp: uniswap-v2-quote-exact-output<1>(
output-token() input-token() amount-reserve
),
:ensure(less-than(last-price-timestamp now) "Sell price change."),
order-output-max18: tkn-amount18,
io-ratio: decimal18-div(decimal18-sub(amount-reserve18 bounty) order-output-max18);

#buy-calculate-io
trend-ratio _ amount-reserve18 amount-reserve: call<'calculate-reserve>(),
#calculate-tkn-amount18
reserve-amount18:,
now: call<times>(),
tkn-amount18 last-price-timestamp: uniswap-v2-quote-exact-input<1>(
output-token() input-token() amount-reserve
output-token() input-token() decimal18-scale-n<reserve-decimals>(reserve-amount18)
),
:ensure(less-than(last-price-timestamp now) "Buy price change."),
order-output-max18: decimal18-add(amount-reserve18 bounty),
io-ratio: decimal18-div(tkn-amount18 order-output-max18);
:ensure(less-than(last-price-timestamp now) "Price change in same block."),
_: tkn-amount18;

#bounty !The bounty to offer for the trade as normalized 18 decimal reserve amount.

#calculate-io
reserve-amount18: call<'calculate-reserve-amount18>(),
tkn-amount18: call<'calculate-tkn-amount18>(reserve-amount18),
is-buying-tkn: equal-to(output-token() reserve-token),
amount: if(
is-buying-tkn
decimal18-add(reserve-amount18 bounty)
tkn-amount18),
io-ratio: if(
is-buying-tkn
decimal18-div(tkn-amount18 amount)
decimal18-div(decimal18-sub(reserve-amount18 bounty) amount));

#handle-io
:ensure(
Expand Down

0 comments on commit 625ed9c

Please sign in to comment.