Pair summary -> script setup

This commit is contained in:
Matthias 2023-02-27 20:21:45 +01:00
parent 0d59031d72
commit d09dc48944

View File

@ -25,12 +25,12 @@
</b-list-group> </b-list-group>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import { formatPercent, timestampms } from '@/shared/formatters'; import { formatPercent, timestampms } from '@/shared/formatters';
import { Lock, Trade } from '@/types'; import { Lock, Trade } from '@/types';
import TradeProfit from '@/components/ftbot/TradeProfit.vue'; import TradeProfit from '@/components/ftbot/TradeProfit.vue';
import ProfitPill from '@/components/general/ProfitPill.vue'; import ProfitPill from '@/components/general/ProfitPill.vue';
import { defineComponent, computed } from 'vue'; import { computed } from 'vue';
import { useBotStore } from '@/stores/ftbotwrapper'; import { useBotStore } from '@/stores/ftbotwrapper';
interface CombinedPairList { interface CombinedPairList {
@ -44,101 +44,90 @@ interface CombinedPairList {
tradeCount: number; tradeCount: number;
} }
export default defineComponent({ const props = defineProps({
name: 'PairSummary', // TOOD: Should be string list
components: { TradeProfit, ProfitPill }, pairlist: { required: true, type: Array as () => string[] },
props: { currentLocks: { required: false, type: Array as () => Lock[], default: () => [] },
// TOOD: Should be string list trades: { required: true, type: Array as () => Trade[] },
pairlist: { required: true, type: Array as () => string[] }, sortMethod: { default: 'normal', type: String },
currentLocks: { required: false, type: Array as () => Lock[], default: () => [] }, backtestMode: { required: false, default: false, type: Boolean },
trades: { required: true, type: Array as () => Trade[] }, });
sortMethod: { default: 'normal', type: String }, const botStore = useBotStore();
backtestMode: { required: false, default: false, type: Boolean }, const combinedPairList = computed(() => {
}, const comb: CombinedPairList[] = [];
setup(props) {
const botStore = useBotStore();
const combinedPairList = computed(() => {
const comb: CombinedPairList[] = [];
props.pairlist.forEach((pair) => { props.pairlist.forEach((pair) => {
const trades: Trade[] = props.trades.filter((el) => el.pair === pair); const trades: Trade[] = props.trades.filter((el) => el.pair === pair);
const allLocks = props.currentLocks.filter((el) => el.pair === pair); const allLocks = props.currentLocks.filter((el) => el.pair === pair);
let lockReason = ''; let lockReason = '';
let locks; let locks;
// Sort to have longer timeframe in front // Sort to have longer timeframe in front
allLocks.sort((a, b) => (a.lock_end_timestamp > b.lock_end_timestamp ? -1 : 1)); allLocks.sort((a, b) => (a.lock_end_timestamp > b.lock_end_timestamp ? -1 : 1));
if (allLocks.length > 0) { if (allLocks.length > 0) {
[locks] = allLocks; [locks] = allLocks;
lockReason = `${timestampms(locks.lock_end_timestamp)} - ${locks.reason}`; lockReason = `${timestampms(locks.lock_end_timestamp)} - ${locks.reason}`;
} }
let profitString = ''; let profitString = '';
let profit = 0; let profit = 0;
let profitAbs = 0; let profitAbs = 0;
trades.forEach((trade) => { trades.forEach((trade) => {
profit += trade.profit_ratio; profit += trade.profit_ratio;
profitAbs += trade.profit_abs ?? 0; profitAbs += trade.profit_abs ?? 0;
}); });
const tradeCount = trades.length; const tradeCount = trades.length;
const trade = tradeCount ? trades[0] : undefined; const trade = tradeCount ? trades[0] : undefined;
if (trades.length > 0) { if (trades.length > 0) {
profitString = `Current profit: ${formatPercent(profit)}`; profitString = `Current profit: ${formatPercent(profit)}`;
} }
if (trade) { if (trade) {
profitString += `\nOpen since: ${timestampms(trade.open_timestamp)}`; profitString += `\nOpen since: ${timestampms(trade.open_timestamp)}`;
} }
comb.push({ pair, trade, locks, lockReason, profitString, profit, profitAbs, tradeCount }); comb.push({ pair, trade, locks, lockReason, profitString, profit, profitAbs, tradeCount });
}); });
if (props.sortMethod === 'profit') { if (props.sortMethod === 'profit') {
comb.sort((a, b) => { comb.sort((a, b) => {
if (a.profit > b.profit) { if (a.profit > b.profit) {
return -1; return -1;
}
return 1;
});
} else {
// sort Pairs: "with open trade" -> available -> locked
comb.sort((a, b) => {
if (a.trade && !b.trade) {
return -1;
}
if (a.trade && b.trade) {
// 2 open trade pairs
return a.trade.trade_id > b.trade.trade_id ? 1 : -1;
}
if (!a.locks && b.locks) {
return -1;
}
if (a.locks && b.locks) {
// Both have locks
return a.locks.lock_end_timestamp > b.locks.lock_end_timestamp ? 1 : -1;
}
return 1;
});
} }
return comb; return 1;
}); });
const tableFields = computed(() => { } else {
return [ // sort Pairs: "with open trade" -> available -> locked
{ key: 'pair', label: 'Pair' }, comb.sort((a, b) => {
{ if (a.trade && !b.trade) {
key: 'locks.lock_end_timestamp', return -1;
label: 'Lock', }
formatter: (value) => (value ? `${timestampms(value)}` : ''), if (a.trade && b.trade) {
}, // 2 open trade pairs
{ return a.trade.trade_id > b.trade.trade_id ? 1 : -1;
key: 'trade.current_profit', }
label: 'Position', if (!a.locks && b.locks) {
formatter: (value) => formatPercent(value, 3), return -1;
}, }
]; if (a.locks && b.locks) {
// Both have locks
return a.locks.lock_end_timestamp > b.locks.lock_end_timestamp ? 1 : -1;
}
return 1;
}); });
return { }
combinedPairList, return comb;
tableFields, });
botStore, const tableFields = computed(() => {
}; return [
}, { key: 'pair', label: 'Pair' },
{
key: 'locks.lock_end_timestamp',
label: 'Lock',
formatter: (value) => (value ? `${timestampms(value)}` : ''),
},
{
key: 'trade.current_profit',
label: 'Position',
formatter: (value) => formatPercent(value, 3),
},
];
}); });
</script> </script>