Extract stake_currency logic from tooltip functions

This commit is contained in:
Matthias 2023-10-21 16:51:27 +02:00
parent 46261f5cc6
commit 43dd865ad4

View File

@ -3,30 +3,35 @@ import { roundTimeframe } from '@/shared/timemath';
import { Order, PairHistory, Trade, BTOrder } from '@/types'; import { Order, PairHistory, Trade, BTOrder } from '@/types';
import { ScatterSeriesOption } from 'echarts'; import { ScatterSeriesOption } from 'echarts';
function buildTooltipCost(trade: Trade, order: Order | BTOrder): string { function buildTooltipCost(order: Order | BTOrder, stakeCurrency: string): string {
return `${order.ft_order_side === 'buy' ? '+' : '-'}${formatPriceCurrency( return `${order.ft_order_side === 'buy' ? '+' : '-'}${formatPriceCurrency(
'cost' in order ? order.cost : order.amount * order.safe_price, 'cost' in order ? order.cost : order.amount * order.safe_price,
trade.quote_currency ?? '<stake_currency>', stakeCurrency,
)}`; )}`;
} }
function buildToolTip(trade: Trade, order: Order | BTOrder, side: string): string { function buildToolTip(
trade: Trade,
order: Order | BTOrder,
side: string,
stakeCurrency: string,
): string {
return `${trade.is_short ? 'Short' : 'Long'} ${side} return `${trade.is_short ? 'Short' : 'Long'} ${side}
${formatPercent(trade.profit_ratio)} ${ ${formatPercent(trade.profit_ratio)} ${
trade.profit_abs trade.profit_abs ? '(' + formatPriceCurrency(trade.profit_abs, stakeCurrency) + ')' : ''
? '(' +
formatPriceCurrency(trade.profit_abs, trade.quote_currency ?? '<stake_currency>') +
')'
: ''
} }
${buildTooltipCost(trade, order)} ${buildTooltipCost(order, stakeCurrency)}
Enter-tag: ${trade.enter_tag ?? ''} Enter-tag: ${trade.enter_tag ?? ''}
Exit-Tag: ${trade.exit_reason ?? ''}`; Exit-Tag: ${trade.exit_reason ?? ''}`;
} }
function buildAdjustmentToolTip(trade: Trade, order: Order | BTOrder): string { function buildAdjustmentToolTip(
trade: Trade,
order: Order | BTOrder,
stakeCurrency: string,
): string {
return `${trade.is_short ? 'Short' : 'Long'} adjustment return `${trade.is_short ? 'Short' : 'Long'} adjustment
${buildTooltipCost(trade, order)} ${buildTooltipCost(order, stakeCurrency)}
Enter-tag: ${trade.enter_tag ?? ''}`; Enter-tag: ${trade.enter_tag ?? ''}`;
} }
@ -66,6 +71,7 @@ export function getTradeEntries(dataset: PairHistory, trades: Trade[]) {
if (trade.orders) { if (trade.orders) {
for (let i = 0; i < trade.orders.length; i++) { for (let i = 0; i < trade.orders.length; i++) {
const order: Order | BTOrder = trade.orders[i]; const order: Order | BTOrder = trade.orders[i];
const stakeCurrency = trade.quote_currency ?? '<stake_currency>';
if ( if (
order.order_filled_timestamp && order.order_filled_timestamp &&
roundTimeframe(dataset.timeframe_ms ?? 0, order.order_filled_timestamp) <= roundTimeframe(dataset.timeframe_ms ?? 0, order.order_filled_timestamp) <=
@ -81,7 +87,7 @@ export function getTradeEntries(dataset: PairHistory, trades: Trade[]) {
order.ft_order_side == 'sell' ? 180 : 0, order.ft_order_side == 'sell' ? 180 : 0,
trade.is_short ? SHORT_COLOR : LONG_COLOR, trade.is_short ? SHORT_COLOR : LONG_COLOR,
trade.is_short ? 'Short' : 'Long', trade.is_short ? 'Short' : 'Long',
buildToolTip(trade, order, 'entry'), buildToolTip(trade, order, 'entry', stakeCurrency),
]); ]);
// Trade exit // Trade exit
} else if (i === trade.orders.length - 1 && trade.close_timestamp) { } else if (i === trade.orders.length - 1 && trade.close_timestamp) {
@ -99,7 +105,7 @@ export function getTradeEntries(dataset: PairHistory, trades: Trade[]) {
trade.is_short ? SHORT_COLOR : LONG_COLOR, trade.is_short ? SHORT_COLOR : LONG_COLOR,
// (trade.profit_abs ?? 0) > 0 ? '#31e04b' : '#fc0505', // (trade.profit_abs ?? 0) > 0 ? '#31e04b' : '#fc0505',
formatPercent(trade.profit_ratio, 2), formatPercent(trade.profit_ratio, 2),
buildToolTip(trade, order, 'exit'), buildToolTip(trade, order, 'exit', stakeCurrency),
]); ]);
} }
} }
@ -112,7 +118,7 @@ export function getTradeEntries(dataset: PairHistory, trades: Trade[]) {
order.ft_order_side == 'sell' ? 180 : 0, order.ft_order_side == 'sell' ? 180 : 0,
trade.is_short ? SHORT_COLOR : LONG_COLOR, trade.is_short ? SHORT_COLOR : LONG_COLOR,
'', '',
buildAdjustmentToolTip(trade, order), buildAdjustmentToolTip(trade, order, stakeCurrency),
]); ]);
} }
} }