@steerprotocol/strategy-utils 0.0.3 → 1.1.1

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 CHANGED
@@ -1,3 +1,22 @@
1
+ ## [1.1.0](https://github.com/SteerProtocol/strategy-utils-assemblyscript/compare/v1.0.0...v1.1.0) (2022-07-26)
2
+
3
+
4
+ ### Features
5
+
6
+ * :sparkles: Additional Helper functions ([a783b06](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/a783b06c08ce11e50469c15e3f53224adf9710bb))
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * :bug: Hot Fix sync ([e635a70](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/e635a70951463271f227e996408d2bf4bb9c633d))
12
+
13
+ ## 1.0.0 (2022-07-14)
14
+
15
+
16
+ ### Features
17
+
18
+ * :sparkles: More utilities ([0bda6dd](https://github.com/SteerProtocol/strategy-utils-assemblyscript/commit/0bda6dd58f395f09b252365f677d397b87f04e80))
19
+
1
20
  ## 1.0.0-alpha.1 (2021-12-09)
2
21
 
3
22
 
package/assembly/index.ts CHANGED
@@ -1 +1 @@
1
- export * from './utils'
1
+ export * from './utils';
@@ -1,4 +1,4 @@
1
- export function getMax(arr: Array<f32>): f32 {
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
+ }
@@ -1,4 +1,6 @@
1
1
 
2
+
3
+ // WARNING: classes will initially be allocated 1 page of memory, you will likely need to add size or flatten these classes
2
4
  export class SMA {
3
5
  private readonly prices: f32[] = [];
4
6
  private interval: i32 = 0;
@@ -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 = getMax([f32(range1), f32(range2), f32(range3)]);
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
+ }
@@ -0,0 +1,9 @@
1
+ declare function log(s: string): void;
2
+
3
+ export class console {
4
+ constructor() {}
5
+
6
+ static log(s: string): void {
7
+ log(s);
8
+ }
9
+ }
@@ -1,4 +1,6 @@
1
1
  export * from './Math';
2
2
  export * from './MovingAverages';
3
- export * from './Price';
4
3
  export * from './Ranges';
4
+ export * from './UniswapLiquidityUtils';
5
+ export * from './console';
6
+ export * from './types';
@@ -0,0 +1,8 @@
1
+ export class Position
2
+ {
3
+ constructor(
4
+ public startTick : i32,
5
+ public endTick : i32,
6
+ public weight : i32)
7
+ {}
8
+ }
@@ -0,0 +1,60 @@
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 data_arr = <JSON.Arr>jsonObj.getArr("data");
19
+ if (data_arr == null) {throw new Error()};
20
+ // First and only data result for this strategy is the candles
21
+ const val = <JSON.Arr>data_arr._arr[0]
22
+ if (val != null) {
23
+ if (val.isArr) {
24
+ const pricesArray = (<JSON.Arr>val).valueOf();
25
+
26
+ for (let priceIndex = 0; priceIndex < pricesArray.length; priceIndex++) {
27
+ const price = pricesArray[priceIndex];
28
+
29
+ const candle = <JSON.Obj>JSON.parse(price.toString());
30
+
31
+ if (candle.isObj) {
32
+ const cl = candle.getValue("close");
33
+ const hi = candle.getValue("high");
34
+ const lo = candle.getValue("low");
35
+ const op = candle.getValue("open");
36
+
37
+ if (cl && hi && lo && op) {
38
+ //@ts-ignore
39
+ const close = f32(Number.parseFloat(cl.toString()));
40
+ //@ts-ignore
41
+ const high = f32(Number.parseFloat(hi.toString()));
42
+ //@ts-ignore
43
+ const low = f32(Number.parseFloat(lo.toString()));
44
+ //@ts-ignore
45
+ const open = f32(Number.parseFloat(op.toString()));
46
+
47
+ const obj: Price = new Price(high, low, open, close);
48
+
49
+ result.push(obj);
50
+ }
51
+ }
52
+
53
+ return result;
54
+ }
55
+ }
56
+ return [];
57
+ } else {
58
+ return [];
59
+ }
60
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Position';
2
+ export * from './Price';
package/index.js CHANGED
@@ -2,7 +2,7 @@ const fs = require("fs");
2
2
  import * as AsBind from "as-bind/dist/as-bind.cjs.js";
3
3
 
4
4
  const imports = {
5
- Logger: { // File which you are injecting
5
+ console: { // File which you are injecting
6
6
  log(strPtr) {
7
7
  console.log(strPtr)
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerprotocol/strategy-utils",
3
- "version": "0.0.3",
3
+ "version": "1.1.1",
4
4
  "description": "Strategy utilities library for Steer Protocol strategies",
5
5
  "main": "assembly/index.ts",
6
6
  "types": "assembly/index.ts",
@@ -1,25 +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<JSON.Value>{
13
- const prices = _prices;
14
- // Parse an object using the JSON object
15
- let jsonObj: JSON.Obj = <JSON.Obj>JSON.parse(prices);
16
-
17
- const val = jsonObj.getValue("data");
18
- if (val != null) {
19
- if (val.isArr) {
20
- const pricesArray = (<JSON.Arr>val).valueOf();
21
- return pricesArray;
22
- }
23
- }
24
- return [];
25
- };