Norman Fung

Here’s the code, I think it should be rather self explanatory, it uses bisection search to look for the block given a particular cutoff (Timestamp, not Block Number), have a look at the referenced article from http://amsi.org.au/ if you haven’t done it before.

Usage should also be apparent from unit tests:

Please note:

a. search_step_size is “initial” step size. Set this to a bigger number so things converges more quickly. When the search passes the target block for the first time, it’d automatically half the step size to recursively converge more accurately to target block.

b. if precision_sec is too small, your search may not converge.

c. Real life application? https://norman-lm-fung.medium.com/web3-py-fetching-all-transfer-events-on-single-tokens-from-a-given-timestamp-90bd6ec08e33

--

--

Lets start with simplest commands SET/GET to show concept of name space in Redis. You see ‘ns1:key1’ separated by colon and can be hierarchical.

127.0.0.1:6379> set 'ns1:key1' 'val1'
OK
127.0.0.1:6379> set 'ns1:key2' 'val2'
OK
127.0.0.1:6379> get 'ns1:key1'
"val1"
127.0.0.1:6379> get 'ns1:key2'
"val2"
127.0.0.1:6379> set 'ns1:l1:key1' 'val1_ns1_l1'
OK
127.0.0.1:6379> set 'ns1:l1:key2' 'val2_ns1_l1'
OK

From Redis RDB (Free on Linux), “namespaces” are represented by folders.

Redis command reference: https://redis.io/commands

--

--

Below I used Binance inverse future as example. But you can run calc_pos_pnl essentially on any public tradable instruments with marked prices: Spot pairs crypto or fiat, stocks and ETFs, commodities …

Now, further our example, here’s how you can wire it up with FTX.

In terms of performance, for ~20 privater trades:

2021–10–10 08:43:25,654 _fetch_my_trades returned in 10416.828393936157 ms
2021–10–10 08:43:30,711 _fetch_ohlcv returned in 5039.645671844482 ms
2021–10–10 08:43:35,821 _fetch_ohlcv returned in 5109.45200920105 ms
2021–10–10 08:43:40,828 _fetch_ohlcv returned in 5005.392789840698 ms
2021–10–10 08:43:45,931 _fetch_ohlcv returned in 5103.818655014038 ms
2021–10–10 08:43:45,934 calc_pos_pnl returned in 0.9934902191162109 ms

You see most of the time spent in REST calls to FTX, not the calc_pos_pnl, which took 1 ms for 20 private trades.

The timer function here:

--

--

Norman Fung

Norman Fung

#python #web3 #crypto #trading #nft #blockchain #smartcontract #journey2makemoneyscientifically