@steerprotocol/strategy-utils 0.0.5 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/assembly/index.ts +1 -1
- package/assembly/utils/Math.ts +36 -2
- package/assembly/utils/MovingAverages.ts +2 -0
- package/assembly/utils/Ranges.ts +29 -4
- package/assembly/utils/UniswapLiquidityUtils.ts +48 -0
- package/assembly/utils/console.ts +9 -0
- package/assembly/utils/index.ts +3 -1
- package/assembly/utils/types/Position.ts +8 -0
- package/assembly/utils/types/Price.ts +66 -0
- package/assembly/utils/types/index.ts +2 -0
- package/index.js +1 -1
- package/package.json +1 -1
- package/assembly/utils/Price.ts +0 -57
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
### [1.1.2-alpha.1](https://github.com/SteerProtocol/strategy-utils-assemblyscript/compare/v1.1.1...v1.1.2-alpha.1) (2022-08-15)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* :ambulance: Specific Index for parse prices ([0940640](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/09406407c7cb12bca2585976f3269414d9be731b))
|
|
7
|
+
|
|
8
|
+
### [1.1.1-alpha.1](https://github.com/SteerProtocol/strategy-utils-assemblyscript/compare/v1.1.0...v1.1.1-alpha.1) (2022-07-26)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* **devops:** :bug: Fix tests ([a2bb4d6](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/a2bb4d6bdc5e3dc8bbcf6be6f26c74b8167675a7))
|
|
15
|
+
|
|
16
|
+
## [1.1.0](https://github.com/SteerProtocol/strategy-utils-assemblyscript/compare/v1.0.0...v1.1.0) (2022-07-26)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* :sparkles: Additional Helper functions ([a783b06](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/a783b06c08ce11e50469c15e3f53224adf9710bb))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
* :bug: Hot Fix sync ([e635a70](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/e635a70951463271f227e996408d2bf4bb9c633d))
|
|
27
|
+
|
|
28
|
+
## 1.0.0 (2022-07-14)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* :sparkles: More utilities ([0bda6dd](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/0bda6dd58f395f09b252365f677d397b87f04e80))
|
|
34
|
+
|
|
1
35
|
## 1.0.0-alpha.1 (2021-12-09)
|
|
2
36
|
|
|
3
37
|
|
package/assembly/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './utils'
|
|
1
|
+
export * from './utils';
|
package/assembly/utils/Math.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function _getMax(arr: Array<f32>): f32 {
|
|
2
2
|
let max = arr[0];
|
|
3
3
|
for (let i = 1; i < arr.length; i++) {
|
|
4
4
|
if (arr[i] > max) {
|
|
@@ -6,4 +6,38 @@ export function getMax(arr: Array<f32>): f32 {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
return max;
|
|
9
|
-
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getMax(a: f32, b: f32): f32 {
|
|
12
|
+
if (a >= b) return a
|
|
13
|
+
return b
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export function _normalDensity(std: f32, mean: f32, x: f32): f32 {
|
|
18
|
+
return f32(
|
|
19
|
+
(f32(Math.E) ** (((x - mean) / std) ** 2 / -2) / std) *
|
|
20
|
+
Math.sqrt(2 * f32(Math.PI))
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function _standardDeviation(list:f32[]): f32 {
|
|
25
|
+
const mean = _mean(list)
|
|
26
|
+
const sqrdDiff: f32[] = []
|
|
27
|
+
for (let i = 0; i < list.length; i++){
|
|
28
|
+
sqrdDiff.push((list[i]-mean)*(list[i]-mean))
|
|
29
|
+
}
|
|
30
|
+
const variance = _mean(sqrdDiff)
|
|
31
|
+
const stddev = Math.sqrt(variance)
|
|
32
|
+
return f32(stddev)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// SMA simple moving average
|
|
36
|
+
export function _mean(list: f32[]): f32 {
|
|
37
|
+
const length = list.length;
|
|
38
|
+
let total: f32 = 0.0;
|
|
39
|
+
for (let i = 0; i < length; i++){
|
|
40
|
+
total += list[i]
|
|
41
|
+
}
|
|
42
|
+
return total / f32(length)
|
|
43
|
+
}
|
package/assembly/utils/Ranges.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Price } from "./Price";
|
|
2
|
-
import { getMax } from "./Math";
|
|
1
|
+
import { Price } from "./types/Price";
|
|
2
|
+
import { getMax, _mean, _getMax } from "./Math";
|
|
3
3
|
|
|
4
4
|
export function getAverageTrueRange(prices: Array<Price>, interval: i32): f32 {
|
|
5
5
|
let rangeSum: f32 = 0;
|
|
@@ -14,11 +14,36 @@ export function getAverageTrueRange(prices: Array<Price>, interval: i32): f32 {
|
|
|
14
14
|
const range2 = Math.abs(currentLow - previousClose);
|
|
15
15
|
const range3 = Math.abs(currentHigh - currentLow);
|
|
16
16
|
|
|
17
|
-
let max =
|
|
17
|
+
let max = _getMax([f32(range1), f32(range2), f32(range3)]);
|
|
18
18
|
rangeSum += max;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
return f32(rangeSum) / f32(prices.length - 1);
|
|
23
23
|
|
|
24
|
-
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export function trailingStop(percent: f32, prices: Price[]): f32 {
|
|
28
|
+
// Get the current price of the asset pair
|
|
29
|
+
const currentPrice = prices[prices.length - 1];
|
|
30
|
+
|
|
31
|
+
// Calculate the trailing stop price
|
|
32
|
+
const trailingStopPrice = currentPrice.close - (currentPrice.close * f32(percent / 100));
|
|
33
|
+
|
|
34
|
+
// Return the trailing stop price
|
|
35
|
+
return trailingStopPrice;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function trueRange(price: Price): f32{
|
|
39
|
+
const trueRange = getMax(price.high - price.low,getMax(f32(Math.abs(price.high-price.close)),f32(Math.abs(price.low-price.close))));
|
|
40
|
+
return trueRange;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function averageTrueRange(prices: Price[]): f32 {
|
|
44
|
+
const trueRanges: f32[] = []
|
|
45
|
+
for (let i = 0; i < prices.length; i++) {
|
|
46
|
+
trueRanges.push(trueRange(prices[i]));
|
|
47
|
+
}
|
|
48
|
+
return _mean(trueRanges);
|
|
49
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {Position } from "./types";
|
|
2
|
+
|
|
3
|
+
export function getTickSpacing(poolFee: i32): i32{
|
|
4
|
+
let spacing = 0;
|
|
5
|
+
switch (poolFee){
|
|
6
|
+
case 100:
|
|
7
|
+
spacing = 1;
|
|
8
|
+
break;
|
|
9
|
+
case 500:
|
|
10
|
+
spacing = 10;
|
|
11
|
+
break;
|
|
12
|
+
case 3000:
|
|
13
|
+
spacing = 60;
|
|
14
|
+
break;
|
|
15
|
+
default:
|
|
16
|
+
spacing = 200;
|
|
17
|
+
}
|
|
18
|
+
return spacing;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Function shaped for making positions with the UniLiquidityManager contract for ease
|
|
22
|
+
export function renderULMResult(positions: Array<Position>): string {
|
|
23
|
+
|
|
24
|
+
// Construct necessary object
|
|
25
|
+
const lowerTicks: Array<i32> = []
|
|
26
|
+
const upperTicks: Array<i32> = []
|
|
27
|
+
const weights: Array<i32> = []
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < positions.length; i++) {
|
|
30
|
+
lowerTicks.push(positions[i].startTick)
|
|
31
|
+
upperTicks.push(positions[i].endTick)
|
|
32
|
+
weights.push(positions[i].weight)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return `{"functionName":"tend(uint256,(int24[],int24[],uint16[]),bytes)",
|
|
36
|
+
"typesArray":["uint256","tuple(int24[],int24[],uint16[])","bytes"],
|
|
37
|
+
"valuesArray":[10000, [[`+lowerTicks.toString() +'],['+upperTicks.toString()+'],['+ weights.toString() +`]], "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000ffffffffffffffffffffffffffffffffffffffff"]
|
|
38
|
+
}`
|
|
39
|
+
// The bytes value here is a placeholder for encoding that gets replaced with time-sensitive data upon execution. It will actually be the swap amount for rebalancing (int256) and slippage limit (uint160)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// TODO: Might need to be rewritten for assets
|
|
43
|
+
// Price must be in the native token
|
|
44
|
+
// token0 for token1
|
|
45
|
+
export function getTickFromPrice(price: f32): f32 {
|
|
46
|
+
const tick = Math.log(price) / Math.log(f32(1.0001));
|
|
47
|
+
return f32(tick);
|
|
48
|
+
}
|
package/assembly/utils/index.ts
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { JSON } from "assemblyscript-json";
|
|
2
|
+
|
|
3
|
+
export class Price {
|
|
4
|
+
constructor(
|
|
5
|
+
public high: f32,
|
|
6
|
+
public low: f32,
|
|
7
|
+
public open: f32,
|
|
8
|
+
public close: f32
|
|
9
|
+
) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param _data data connector data array
|
|
15
|
+
* @param _index index of where the candles are in the array
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export function parsePrices(_data: String, _index: i32): Array<Price> {
|
|
19
|
+
const prices = _data;
|
|
20
|
+
// Parse an object using the JSON object
|
|
21
|
+
let jsonObj: JSON.Obj = <JSON.Obj>JSON.parse(prices);
|
|
22
|
+
const result: Array<Price> = [];
|
|
23
|
+
|
|
24
|
+
const data_arr = <JSON.Arr>jsonObj.getArr("data");
|
|
25
|
+
if (data_arr == null) {throw new Error()};
|
|
26
|
+
// First and only data result for this strategy is the candles
|
|
27
|
+
const val = <JSON.Arr>data_arr._arr[_index]
|
|
28
|
+
if (val != null) {
|
|
29
|
+
if (val.isArr) {
|
|
30
|
+
const pricesArray = (<JSON.Arr>val).valueOf();
|
|
31
|
+
|
|
32
|
+
for (let priceIndex = 0; priceIndex < pricesArray.length; priceIndex++) {
|
|
33
|
+
const price = pricesArray[priceIndex];
|
|
34
|
+
|
|
35
|
+
const candle = <JSON.Obj>JSON.parse(price.toString());
|
|
36
|
+
|
|
37
|
+
if (candle.isObj) {
|
|
38
|
+
const cl = candle.getValue("close");
|
|
39
|
+
const hi = candle.getValue("high");
|
|
40
|
+
const lo = candle.getValue("low");
|
|
41
|
+
const op = candle.getValue("open");
|
|
42
|
+
|
|
43
|
+
if (cl && hi && lo && op) {
|
|
44
|
+
//@ts-ignore
|
|
45
|
+
const close = f32(Number.parseFloat(cl.toString()));
|
|
46
|
+
//@ts-ignore
|
|
47
|
+
const high = f32(Number.parseFloat(hi.toString()));
|
|
48
|
+
//@ts-ignore
|
|
49
|
+
const low = f32(Number.parseFloat(lo.toString()));
|
|
50
|
+
//@ts-ignore
|
|
51
|
+
const open = f32(Number.parseFloat(op.toString()));
|
|
52
|
+
|
|
53
|
+
const obj: Price = new Price(high, low, open, close);
|
|
54
|
+
|
|
55
|
+
result.push(obj);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return [];
|
|
63
|
+
} else {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
}
|
package/index.js
CHANGED
package/package.json
CHANGED
package/assembly/utils/Price.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { JSON } from "assemblyscript-json";
|
|
2
|
-
|
|
3
|
-
export class Price {
|
|
4
|
-
constructor(
|
|
5
|
-
public high: f32,
|
|
6
|
-
public low: f32,
|
|
7
|
-
public open: f32,
|
|
8
|
-
public close: f32
|
|
9
|
-
) {}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function parsePrices(_prices: String): Array<Price> {
|
|
13
|
-
const prices = _prices;
|
|
14
|
-
// Parse an object using the JSON object
|
|
15
|
-
let jsonObj: JSON.Obj = <JSON.Obj>JSON.parse(prices);
|
|
16
|
-
const result: Array<Price> = [];
|
|
17
|
-
|
|
18
|
-
const val = jsonObj.getValue("data");
|
|
19
|
-
if (val != null) {
|
|
20
|
-
if (val.isArr) {
|
|
21
|
-
const pricesArray = (<JSON.Arr>val).valueOf();
|
|
22
|
-
|
|
23
|
-
for (let priceIndex = 0; priceIndex < prices.length; priceIndex++) {
|
|
24
|
-
const price = pricesArray[priceIndex];
|
|
25
|
-
|
|
26
|
-
const candle = <JSON.Obj>JSON.parse(price.toString());
|
|
27
|
-
|
|
28
|
-
if (candle) {
|
|
29
|
-
const cl = candle.getValue("close");
|
|
30
|
-
const hi = candle.getValue("high");
|
|
31
|
-
const lo = candle.getValue("low");
|
|
32
|
-
const op = candle.getValue("open");
|
|
33
|
-
|
|
34
|
-
if (cl && hi && lo && op) {
|
|
35
|
-
//@ts-ignore
|
|
36
|
-
const close = f32(Number.parseFloat(cl.toString()));
|
|
37
|
-
//@ts-ignore
|
|
38
|
-
const high = f32(Number.parseFloat(hi.toString()));
|
|
39
|
-
//@ts-ignore
|
|
40
|
-
const low = f32(Number.parseFloat(lo.toString()));
|
|
41
|
-
//@ts-ignore
|
|
42
|
-
const open = f32(Number.parseFloat(op.toString()));
|
|
43
|
-
|
|
44
|
-
const obj: Price = new Price(high, low, open, close);
|
|
45
|
-
|
|
46
|
-
result.push(obj);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return [];
|
|
54
|
-
} else {
|
|
55
|
-
return [];
|
|
56
|
-
}
|
|
57
|
-
}
|