SwapOrder.inverted

This commit is contained in:
tim
2024-10-28 13:23:06 -04:00
parent 8a34c55fe0
commit 5e91b0ff43
10 changed files with 64 additions and 48 deletions

View File

@@ -2,6 +2,7 @@ import {DISTANT_FUTURE, DISTANT_PAST} from "@/blockchain/orderlib.js";
import {allocationText, DLine, HLine} from "@/charts/shape.js";
import {createShape, deleteShapeId} from "@/charts/chart.js";
import {timestamp} from "@/misc.js";
import {useChartOrderStore} from "@/orderbuild.js";
export class OrderShapes {
constructor(symbol, orderStatus) {
@@ -30,6 +31,11 @@ export class OrderShapes {
class TrancheShapes {
constructor(symbol, orderStatus, trancheIndex) {
// todo validate base/quote
if (symbol.inverted !== orderStatus.order.inverted) {
console.log('OrderShapes.createLine(): symbol has wrong inverson for this order')
return
}
this.symbol = symbol
this.status = orderStatus
this.trancheIndex = trancheIndex
@@ -68,18 +74,18 @@ class TrancheShapes {
* 10 ** -(amountIsBase ? this.symbol.base.d : this.symbol.quote.d)
const weight = Number(filledAmount) / Number(this.status.order.amount)
const amountSymbol = amountIsBase ? this.symbol.base.s : this.symbol.quote.s
// console.log('fillpoint', buy, filledAmount, this.status.order, this.symbol)
console.log('fillpoint', buy, filledAmount, this.status.order, this.symbol)
const time = f.time
const out = Number(f.filledOut) / (1-this.status.order.route.fee/1000000)
let price = out / Number(f.filledIn)
if (buy)
price = 1/price
price *= scale
// console.log('price', price)
console.log('price', price)
const channel = buy?'low':'high';
const text = (buy ? 'Buy ' : 'Sell ') + allocationText(weight, amount, amountSymbol, '\n')
const s = createShape(buy?'arrow_up':'arrow_down', {time, price}, {channel,text,lock:true})
// console.log('created fill shape at', time, price)
console.log('created fill shape at', time, price)
this.fills.push(s)
}
@@ -89,21 +95,18 @@ class TrancheShapes {
const symbol = this.symbol
const scale = 10**symbol.decimals;
const buy = status.order.tokenIn === this.symbol.quote.a
const inverted = buy
amount = Number(amount)
* 10 ** -(buy ? this.symbol.base.d : this.symbol.quote.d)
const amountSymbol = buy ? this.symbol.base.s : this.symbol.quote.s
const color = buy ? 'green' : 'red'
if (intercept !== 0 || slope !== 0) {
// console.log('tranche line', intercept, slope)
console.log('tranche line', intercept, slope)
// line active
if (slope === 0) {
let price = intercept
if (inverted)
price = 1/price
price *= scale
// horizontal line
// console.log('hline', price, inverted)
console.log('hline', price)
const model = {price, color, maxAllocation: status.order.amount, amount, amountSymbol};
const s = new HLine(model, null, null, null, true)
this.shapes.push(s)
@@ -111,16 +114,12 @@ class TrancheShapes {
// diagonal line
let startTime = t.startTime
if (startTime === DISTANT_PAST)
startTime = 1231006505 // use Bitcoin genesis as the drawing point's start time. unnecessary.
startTime = timestamp() - useChartOrderStore().intervalSecs * 20 // 20 bars ago
let endTime = t.endTime
if (endTime === DISTANT_FUTURE)
endTime = timestamp() // use "now" as the drawing point's time
let startPrice = (intercept + slope * startTime);
let endPrice = (intercept + slope * endTime);
if (inverted) {
startPrice = 1/startPrice
endPrice = 1/endPrice
}
startPrice *= scale
endPrice *= scale
console.log('dline', startTime, endTime, DISTANT_FUTURE, startPrice, endPrice)