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

Implement buy twap strategy (closes #522) #548

Closed
wants to merge 6 commits into from

Conversation

debnil
Copy link
Contributor

@debnil debnil commented Oct 19, 2020

This PR implements the buy twap strategy with all necessary files and closes #522.

The following log runs confirm that the strategy successfully creates buy offers that fill previously made sell offers, through sell twap, with the same config:

2020/10/19 15:46:45 Starting fill tracker with 2 handlers
2020/10/19 15:46:45 Starting the trader bot...
2020/10/19 15:46:45 ----------------------------------------------------------------------------------------------------
2020/10/19 15:46:45 there were no trades, leaving lastCursor value as 5621167297667073-0
2020/10/19 15:46:46 synchronized state loading is disabled
2020/10/19 15:46:46  (base) assetA=native, maxA=8999.99997970, trustA=math.MaxFloat64
2020/10/19 15:46:46 (quote) assetB=COUPON:GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI, maxB=85.33766260, trustB=922337203685.47753906
2020/10/19 15:46:46 orderConstraints for trading pair XLM/COUPON: OrderConstraints[PricePrecision: 7, VolumePrecision: 7, MinBaseVolume: 0.0000001, MinQuoteVolume: <nil>]
2020/10/19 15:46:46 liabilities after resetting
2020/10/19 15:46:46 asset=base  , balance=8999.99997970, trust=math.MaxFloat64, minAccountBal=22.00000000, buyingLiabilities=0.00000000, sellingLiabilities=0.00000000
2020/10/19 15:46:46 asset=quote , balance=85.33766260, trust=922337203685.47753906, minAccountBal=0.08533766, buyingLiabilities=0.00000000, sellingLiabilities=0.00000000
2020/10/19 15:46:46 GetLevels, unix timestamp for 'now' in UTC = 1603147606 (2020-10-19 22:46:46.8704 +0000 UTC)
2020/10/19 15:46:46 volumeFilter = volume/daily/sell/base/10000.0/exact
2020/10/19 15:46:46 returning kraken API key at index 0
2020/10/19 15:46:47 (modifier: mid) price from exchange feed (kraken/XXLM/ZUSD/mid): bidPrice=0.085293, askPrice=0.085349, midPrice=0.085321, lastTradePrice=0.085287; price=0.085321
2020/10/19 15:46:47 bucketInfo: BucketInfo[UUID=e783c018475cd5beeafa6d9d0458ed69738bc896, date=2020-10-19, dayID=1 (Monday), bucketID=136, startTime=2020-10-19T22:40:00Z, endTime=2020-10-19T22:49:59Z, sizeSeconds=600, totalBuckets=144, totalBucketsToSell=138, dayBaseSoldStart=0.00000000, dayBaseCapacity=10000.00000000, totalBaseSurplusStart=9855.07246377, baseSurplusIncluded=9855.07246377, baseCapacity=9927.53623188, minOrderSizeBase=1985.50724638, DynamicBucketValues[isNew=true, isLast=false, roundID=0, dayBaseSold=0.00000000, dayBaseRemaining=10000.00000000, baseSold=0.00000000, baseRemaining=9927.53623188, bucketProgress=0.00%, bucketTimeElapsed=67.78%]]; roundInfo: RoundInfo[roundID=0, bucketID=136, bucketUUID=e783c018475cd5beeafa6d9d0458ed69738bc896, now=2020-10-19T22:46:46Z (day=Monday, secondsElapsedToday=82006), sizeBaseCapped=6304.77380840, price=0.08532100]
2020/10/19 15:46:47 levels returned (side = buy ): [{0.0853210 6304.7738084}]
2020/10/19 15:46:47 deleteSideStrategy: deleting 1 offers
2020/10/19 15:46:47 created 1 operations to prune excess offers
2020/10/19 15:46:47 lastFee <= maxOpFeeStroops; using last_ledger_base_fee of 100 stroops (maxOpFeeStroops = 5000)
2020/10/19 15:46:47 reloading sequence number
2020/10/19 15:46:47 tx XDR: AAAAAE9zGoPa0Ask5Xf/v4uTK0Yxrz/gvVCR5en7u7YiqilsAAAAZAAARv8AAAoVAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAEAAAAAhTStdpNWrQjnj6iRrzbV2X37s4sQ7VDENvLteKvUcC0AAAADAAAAAAAAAAJDT1VQT04AAAAAAAAAAAAAWMyxWhi4Ur+CAjyhVaKH7Ld0Hxwp8gv2MeI4ByH/t34AAAAAAAAAAAAApqEAB6EgAAAAAACjkxgAAAAAAAAAAiKqKWwAAABABzyTrmp6hqMU8h7t+74jAA5to/HQ/olbX88Dq/vbYd0oJRy6vxBRH2uAqtAKUo4b+GmSFamSxRuxcqARnUijAqvUcC0AAABAIOo3o5UX+YBF+g2nS4zrkQVxBWfKJLA2plIh2hcEtBxrxY0cYEkul0ehGsOa51wj1hrmyw66k7OXHy4Xzx/hBg==
2020/10/19 15:46:47 submitting tx XDR to network (async)
2020/10/19 15:46:48 liabilities after resetting
2020/10/19 15:46:48 asset=base  , balance=8999.99997970, trust=math.MaxFloat64, minAccountBal=22.00000000, buyingLiabilities=0.00000000, sellingLiabilities=0.00000000
2020/10/19 15:46:48 asset=quote , balance=85.33766260, trust=922337203685.47753906, minAccountBal=0.08533766, buyingLiabilities=0.00000000, sellingLiabilities=0.00000000
2020/10/19 15:46:48 buy  | create | new level=1 | priceQuote=11.72044397 | amtBase=6304.77380840
2020/10/19 15:46:48 we will oversell the asset 'COUPON:GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI', amountSelling = 73894.74816750, bal = 85.33766260, minAccountBal = 0.08533766, liabilities.Selling = 0.00000000
2020/10/19 15:46:48 computed remainder amount, constrained by selling capacity, returning sellingAmount=85.25232494, buyingAmount=7.27381362
2020/10/19 15:46:48 buy  | create | new level=1 | priceQuote=11.72044397 | amtBase=7.27381362
2020/10/19 15:46:48 buy , done creating preceding offers (numLevelsConsumed=1, hitCapacityLimit=true, numOps=1, newTopOfferPrice=0.0853210)
2020/10/19 15:46:48 liabilities at the end of a call to UpdateWithOps
2020/10/19 15:46:48 asset=base  , balance=8999.99997970, trust=math.MaxFloat64, minAccountBal=22.00000000, buyingLiabilities=7.27381362, sellingLiabilities=0.50000000
2020/10/19 15:46:48 asset=quote , balance=85.33766260, trust=922337203685.47753906, minAccountBal=0.08533766, buyingLiabilities=0.00000000, sellingLiabilities=85.25232494
2020/10/19 15:46:48 orderConstraintsFilter:  buying, baseAmount=7.27381361, quoteAmount=85.25232490, keep = true
2020/10/19 15:46:48 orderConstraintsFilter: dropped 0, kept 1 ops from original 1 ops, len(filteredOps) = 1
2020/10/19 15:46:48 created 1 operations to update existing offers
2020/10/19 15:46:48 lastFee <= maxOpFeeStroops; using last_ledger_base_fee of 100 stroops (maxOpFeeStroops = 5000)
2020/10/19 15:46:48 tx XDR: AAAAAE9zGoPa0Ask5Xf/v4uTK0Yxrz/gvVCR5en7u7YiqilsAAAAZAAARv8AAAoWAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAEAAAAAhTStdpNWrQjnj6iRrzbV2X37s4sQ7VDENvLteKvUcC0AAAADAAAAAkNPVVBPTgAAAAAAAAAAAABYzLFaGLhSv4ICPKFVoofst3QfHCnyC/Yx4jgHIf+3fgAAAAAAAAAAMtB48QABTUkAD0JAAAAAAAAAAAAAAAAAAAAAAiKqKWwAAABAb1CJyBE1109QMePtiHINvp9tp4MHFpzF+4mAn9L+GslYSTV6BLfpIbvkLDb6VA5aQp4MmiLK/VHSv+VJSy4HBKvUcC0AAABA1vT/bwLRWmpF8lkUtnw+1i04egtb9MKH2WvmIxsW3vyM52GgxwuxCUqQnjIPMrKAPnuzt3dDXlK2H8ChAPqcCw==
2020/10/19 15:46:48 submitting tx XDR to network (async)
2020/10/19 15:46:48 metric - not sending event metric of type 'ce:update' because metrics are disabled
2020/10/19 15:46:53 (async) tx confirmation hash: 571ba740326f1e894a6b2cd9ff4a65e116a24509c696d60120e3353e57e17b7d
2020/10/19 15:46:58 (async) tx confirmation hash: ec89e9da2fa45c3aa9983763060620c75133a16c504dfd75d66ef3ed001d2fd7
2020/10/19 15:46:58 ----------------------------------------------------------------------------------------------------
2020/10/19 15:46:58 sleeping for 4m46.95205609s...
2020/10/19 15:49:16 received fill: Trade[txid: 5621227427213313-0, orderId: , ts: 1603147616000, pair: XLM/COUPON, action: buy, type: limit, counterPrice: 0.0855074, baseVolume: 997.0169236, counterCost: 85.2523249, fee: 0.0000100]
2020/10/19 15:49:16 registered market in db: tradingMarket[ID=b0d66371cd, ExchangeName=sdex, BaseAsset=native, QuoteAsset=COUPON:GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI]
2020/10/19 15:49:16 wrote trade (txid=5621227427213313-0) to db
2020/10/19 15:49:16 updated lastCursor value to 5621227427213313-0

@debnil
Copy link
Contributor Author

debnil commented Oct 19, 2020

A draft version of this PR is #537. A new PR is opened here to remove prior comments.

@nikhilsaraf
Copy link
Contributor

@debnil the sample_twap.cfg needs the filter to say "buy" instead of "sell" otherwise it won't work properly -- i.e. since we are buying the base/quote asset we want the filter to be sensitive to increases in the value of the base/quote asset. That logic in the filter does not exist today.

We need to do two things in the following order (this will take 4 PRs):

  1. write tests for volumeFilter ([ad-hoc] [3](5) Add test for volumeFilter to ensure we correctly handle offers that will put us over #483). You can use tests in dailyVolumeByDate_test.go and TestMakeVolumeFilterConfig as inspiration.
  2. make a new PR with the infrastructure changes that I committed to this PR (1-4). this includes modifications to volumeFilter and so the newly added tests in step 1 should still pass. for reference, these modifications to volumeFilter is in the commit "4 - update structure of volumeFilter to make room for buy side". Add the missing test in TestDailyVolumeByDate_QueryRow marked as a TODO.
  3. implement the buy-side logic in volumeFilter and update the volumeFilter_test.go to include cases for the buy side.
  4. Finally, you can submit this PR and change sell to buy and it should work automatically.

PS: didn't realize this would be so many changes so apologies for messing up this PR with the commits.

@debnil
Copy link
Contributor Author

debnil commented Oct 20, 2020

Got it, I think that makes sense. Will get working on it!

@debnil
Copy link
Contributor Author

debnil commented Oct 26, 2020

Closing because once the other PRs mentioned above are done, the buy twap implementation will be very short and better to put in a different PR.

@debnil debnil closed this Oct 26, 2020
@debnil debnil deleted the debnil/buytwap-rfr branch October 26, 2020 17:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[8] buyTwap strategy
2 participants