massive Shape rework: keep both tvPoints/Props and ourPoints/Props; delegate model updates to subclasses; DCA/VLine working but Ladder/HLine not done.

This commit is contained in:
Tim
2024-04-16 16:25:31 -04:00
parent 8befffe1c5
commit 65be28fb0a
11 changed files with 874 additions and 208 deletions

View File

@@ -4,6 +4,10 @@ import {useOrderStore, useStore} from "@/store/store.js";
import {encodeIEE754} from "@/common.js";
import {defineStore} from "pinia";
import {computed, ref} from "vue";
import Color from "color";
export const MIN_EXECUTION_TIME = 60 // give at least one full minute for each tranche to trigger
function unimplemented() { throw Error('Unimplemented') }
@@ -13,7 +17,10 @@ function unimplemented() { throw Error('Unimplemented') }
// for instantiating the UI component for a given builder dictionary, based on its builder.component field.
export function newBuilder( component, options = {}) {
const id = uuid()
return {id, component, options, points: {}, shapes: {}, props: {}, build: unimplemented}
return {
id, component, options,
allocation: 1.0, points: {}, shapes: {}, props: {}, valid: false,
}
}
// Orders hold an amount and builders
@@ -44,6 +51,7 @@ export const useChartOrderStore = defineStore('chart_orders', () => {
const selectedOrder = ref(null)
const selectedSymbol = ref(null)
const selectedPool = ref(null)
const intervalSecs = ref(0)
const baseToken = computed(()=>selectedSymbol.value === null ? null : selectedSymbol.value.base)
const quoteToken = computed(()=>selectedSymbol.value === null ? null : selectedSymbol.value.quote)
const price = computed(() => {
@@ -87,7 +95,7 @@ export const useChartOrderStore = defineStore('chart_orders', () => {
}
return {
chartReady, selectedSymbol, selectedPool, baseToken, quoteToken, price,
chartReady, selectedSymbol, selectedPool, intervalSecs, baseToken, quoteToken, price,
orders, drawing, drawingCallbacks, newOrder, removeOrder, resetOrders,
}
})
@@ -222,3 +230,51 @@ export function timesliceTranches() {
return ts
}
export function builderDefaults(builder, defaults) {
for (const k in defaults)
if (builder[k] === undefined)
builder[k] = defaults[k] instanceof Function ? defaults[k]() : defaults[k]
}
export function linearWeights(n, s) {
if (n === 1) return [1]
const result = []
if (s === 0) {
// equal weighted
for (let i = 0; i < n; i++)
result.push(1 / n)
} else if (s === 1) {
result.push(1)
for (let i = 1; i < n; i++)
result.push(0)
} else if (s === -1) {
for (let i = 1; i < n; i++)
result.push(0)
result.push(1)
} else {
for (let i = 0; i < n; i++)
result.push((1 - s * (2 * i / (n - 1) - 1)) / n)
}
// console.log('weights', result)
return result
}
export function weightColors(weights, color) {
const c = new Color(color).rgb()
const max = Math.max(...weights)
const ns = weights.map((w) => w / max) // set largest weight to 100%
const adj = ns.map((w) => c.alpha(Math.pow(w, 0.67))) // https://en.wikipedia.org/wiki/Stevens's_power_law
return adj.map((a) => a.string())
}
export function allocationText(amount, weight, symbol) {
// console.log('weight', weight, alloc, props.amount)
const a = amount * weight
return `${(weight * 100).toFixed(1)}% = ${a.toLocaleString('fullwide')} ${symbol}`
}
export function deleteBuilder(order, builder) {
order.builders = order.builders.filter((b) => b !== builder)
// if (props.deleteShapes) // todo is this redundant?
// props.deleteShapes()
}