diagonal line support

This commit is contained in:
Tim Olson
2023-12-19 17:07:08 -04:00
parent 9199d31e77
commit 8007f63469
10 changed files with 170 additions and 52 deletions

View File

@@ -4,7 +4,7 @@ import {useOrderStore} from "@/store/store.js";
import {encodeIEE754} from "@/blockchain/common.js";
export function applyLimit(tranche, price=null, isAbove=null) {
export function applyLimit(tranche, price=null, isMinimum=null) {
if( price === null ) {
const os = useOrderStore()
price = os.limitPrice
@@ -12,38 +12,62 @@ export function applyLimit(tranche, price=null, isAbove=null) {
return
}
applyLine(tranche, price, 0, isAbove)
applyLine(tranche, price, 0, isMinimum)
}
export function applyLinePoints(tranche, date0, price0, date1, price1, isAbove=null) {
const os = useOrderStore()
if( !date0 || !price0 && price0!==0 || !date1 || !price1 && price1!==0 )
return
function computeInterceptSlope(date0, price0, date1, price1) {
if (!date0 || !price0 && price0 !== 0 || !date1 || !price1 && price1 !== 0)
throw Error(`invalid line points data ${date0} ${price0} ${date1} ${price1}`)
const t0 = timestamp(date0);
const t1 = timestamp(date1);
const slope = (price1-price0)/(t1-t0)
if (t0 === t1)
throw Error("line points' times must be different")
const slope = (price1 - price0) / (t1 - t0)
const intercept = price1 - slope * t1
applyLine(tranche, intercept, slope, isAbove)
return [intercept, slope]
}
export function applyLine(tranche, intercept, slope, isAbove=null) {
export function linePointsValue(date0, price0, date1, price1, unixTime=null) {
if(unixTime===null)
unixTime = timestamp()
try {
const [intercept, slope] = computeInterceptSlope(date0, price0, date1, price1)
return intercept + unixTime * slope
}
catch (e) {
return null
}
}
export function applyLinePoints(tranche, date0, price0, date1, price1, isMinimum=null) {
const [intercept, slope] = computeInterceptSlope(date0, price0, date1, price1);
applyLine(tranche, intercept, slope, isMinimum)
}
export function applyLine(tranche, intercept, slope, isMinimum=null) {
console.log('intercept, slope', intercept, slope)
// intercept and slope are still in "human" units of decimal-adjusted prices
const os = useOrderStore()
const route = os.route
const inverted = routeInverted(route)
const scale = 10 ** (os.tokenA.decimals - os.tokenB.decimals)
const m = encodeIEE754(inverted ? scale / slope : slope / scale)
const b = encodeIEE754(inverted ? scale / intercept : intercept / scale)
if( isAbove === null )
isAbove = os.limitIsMinimum ^ inverted
if( isAbove ) {
let m = inverted ? -scale / slope : slope / scale
let b = inverted ? scale / intercept : intercept / scale
const cur = b + timestamp() * m
console.log('inverted b, m, cur', inverted, b, m, cur)
m = encodeIEE754(m)
b = encodeIEE754(b)
if( isMinimum === null )
isMinimum = os.limitIsMinimum
console.log('limit is minimum', isMinimum)
if (isMinimum) {
tranche.minIntercept = b;
tranche.minSlope = m;
}
else {
} else {
tranche.maxIntercept = b;
tranche.maxSlope = m;
}