frequi_origin/src/components/ftbot/TradeList.vue

289 lines
8.9 KiB
Vue
Raw Normal View History

2020-05-04 18:34:59 +00:00
<template>
2021-07-11 18:43:19 +00:00
<div class="h-100 overflow-auto w-100">
2021-07-04 14:12:13 +00:00
<b-table
ref="tradesTable"
small
hover
2021-07-17 15:31:01 +00:00
stacked="md"
2022-11-19 13:41:04 +00:00
:items="[...trades]"
2021-07-04 14:12:13 +00:00
:fields="tableFields"
show-empty
:empty-text="emptyText"
:per-page="perPage"
:current-page="currentPage"
primary-key="botTradeId"
2021-07-04 14:12:13 +00:00
selectable
select-mode="single"
2021-08-09 17:56:11 +00:00
:filter="filterText"
2021-07-04 14:12:13 +00:00
@row-contextmenu="handleContextMenuEvent"
@row-clicked="onRowClicked"
@row-selected="onRowSelected"
>
<template #cell(actions)="row">
2022-09-08 14:20:35 +00:00
<b-button
:id="`btn-actions_${row.index}`"
class="btn-xs"
size="sm"
title="Actions"
href="#"
>
2021-11-27 08:15:21 +00:00
<ActionIcon :size="16" title="Actions" />
2021-07-04 14:12:13 +00:00
</b-button>
2021-11-27 08:15:21 +00:00
<b-popover :target="`btn-actions_${row.index}`" triggers="focus" placement="left">
<trade-actions
:trade="row.item"
2022-04-19 04:33:25 +00:00
:bot-api-version="botStore.activeBot.botApiVersion"
@deleteTrade="removeTradeHandler"
2022-08-03 04:57:17 +00:00
@forceExit="forceExitHandler"
2022-08-03 18:09:45 +00:00
@forceExitPartial="forceExitPartialHandler"
/>
2021-11-27 08:15:21 +00:00
</b-popover>
2021-07-04 14:12:13 +00:00
</template>
<template #cell(pair)="row">
<span>
{{
`${row.item.pair}${
row.item.open_order_id === undefined || row.item.open_order_id === null ? '' : '*'
}`
}}
2021-07-04 14:12:13 +00:00
</span>
</template>
2022-02-27 15:30:59 +00:00
<template #cell(trade_id)="row">
{{ row.item.trade_id }}
2022-04-09 08:43:46 +00:00
{{
2022-04-19 04:33:25 +00:00
botStore.activeBot.botApiVersion > 2.0 && row.item.trading_mode !== 'spot'
2022-04-09 08:43:46 +00:00
? '| ' + (row.item.is_short ? 'Short' : 'Long')
: ''
}}
2022-02-27 15:30:59 +00:00
</template>
<template #cell(profit)="row">
2021-09-04 08:35:44 +00:00
<trade-profit :trade="row.item" />
</template>
2021-07-04 14:12:13 +00:00
<template #cell(open_timestamp)="row">
<DateTimeTZ :date="row.item.open_timestamp" />
</template>
<template #cell(close_timestamp)="row">
<DateTimeTZ :date="row.item.close_timestamp" />
</template>
</b-table>
2021-08-09 17:56:11 +00:00
<div class="w-100 d-flex justify-content-between">
<b-pagination
v-if="!activeTrades"
v-model="currentPage"
:total-rows="rows"
:per-page="perPage"
aria-controls="my-table"
></b-pagination>
2022-11-18 16:10:06 +00:00
<b-form-input
2021-08-09 17:56:11 +00:00
v-if="showFilter"
v-model="filterText"
type="text"
placeholder="Filter"
size="sm"
style="width: unset"
/>
</div>
2022-11-19 13:39:46 +00:00
<force-exit-form v-if="activeTrades" v-model="forceExitVisible" :trade="feTrade" />
</div>
2020-05-04 18:34:59 +00:00
</template>
2020-06-29 19:14:16 +00:00
<script lang="ts">
2020-06-29 19:29:09 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2021-07-01 05:15:11 +00:00
import { formatPercent, formatPrice } from '@/shared/formatters';
import { MultiDeletePayload, MultiForcesellPayload, Trade } from '@/types';
2021-11-27 08:15:21 +00:00
import ActionIcon from 'vue-material-design-icons/GestureTap.vue';
2021-07-01 05:15:11 +00:00
import DateTimeTZ from '@/components/general/DateTimeTZ.vue';
2021-09-04 08:35:44 +00:00
import TradeProfit from './TradeProfit.vue';
import TradeActions from './TradeActions.vue';
2022-08-03 18:09:45 +00:00
import ForceExitForm from '@/components/ftbot/ForceExitForm.vue';
2020-06-29 19:14:16 +00:00
2022-08-03 18:09:45 +00:00
import { defineComponent, ref, computed, watch, getCurrentInstance, nextTick } from 'vue';
2022-04-19 04:33:25 +00:00
import { useBotStore } from '@/stores/ftbotwrapper';
2020-06-29 19:14:16 +00:00
2022-04-16 18:27:33 +00:00
export default defineComponent({
name: 'TradeList',
2022-08-03 18:09:45 +00:00
components: { ActionIcon, DateTimeTZ, TradeProfit, TradeActions, ForceExitForm },
2022-04-16 18:27:33 +00:00
props: {
trades: { required: true, type: Array as () => Array<Trade> },
title: { default: 'Trades', type: String },
stakeCurrency: { required: false, default: '', type: String },
activeTrades: { default: false, type: Boolean },
showFilter: { default: false, type: Boolean },
multiBotView: { default: false, type: Boolean },
emptyText: { default: 'No Trades to show.', type: String },
},
2022-07-07 18:44:19 +00:00
setup(props) {
const root = getCurrentInstance();
2022-04-19 04:33:25 +00:00
const botStore = useBotStore();
2022-04-16 18:27:33 +00:00
const currentPage = ref(1);
const selectedItemIndex = ref();
const filterText = ref('');
2022-08-03 18:09:45 +00:00
const feTrade = ref<Trade>({} as Trade);
2022-04-16 18:27:33 +00:00
const perPage = props.activeTrades ? 200 : 15;
const tradesTable = ref<HTMLFormElement>();
2022-11-19 13:39:46 +00:00
const forceExitVisible = ref(false);
2022-04-16 18:27:33 +00:00
const openFields: Record<string, string | Function>[] = [{ key: 'actions' }];
const closedFields: Record<string, string | Function>[] = [
{ key: 'close_timestamp', label: 'Close date' },
{ key: 'exit_reason', label: 'Close Reason' },
];
const formatPriceWithDecimals = (price) => {
2022-04-19 04:33:25 +00:00
return formatPrice(price, botStore.activeBot.stakeCurrencyDecimals);
2022-04-16 18:27:33 +00:00
};
const rows = computed(() => {
return props.trades.length;
});
const tableFields: Record<string, string | Function>[] = [
props.multiBotView ? { key: 'botName', label: 'Bot' } : {},
{ key: 'trade_id', label: 'ID' },
{ key: 'pair', label: 'Pair' },
{ key: 'amount', label: 'Amount' },
{
key: 'stake_amount',
label: 'Stake amount',
formatter: (value: number) => formatPriceWithDecimals(value),
2021-07-17 14:00:15 +00:00
},
2022-04-16 18:27:33 +00:00
{
key: 'open_rate',
label: 'Open rate',
formatter: (value: number) => formatPrice(value),
},
{
key: props.activeTrades ? 'current_rate' : 'close_rate',
label: props.activeTrades ? 'Current rate' : 'Close rate',
formatter: (value: number) => formatPrice(value),
},
{
key: 'profit',
label: props.activeTrades ? 'Current profit %' : 'Profit %',
formatter: (value: number, key, item: Trade) => {
const percent = formatPercent(item.profit_ratio, 2);
return `${percent} ${`(${formatPriceWithDecimals(item.profit_abs)})`}`;
},
},
{ key: 'open_timestamp', label: 'Open date' },
...(props.activeTrades ? openFields : closedFields),
];
2022-08-03 04:57:17 +00:00
const forceExitHandler = (item: Trade, ordertype: string | undefined = undefined) => {
2022-07-09 18:04:22 +00:00
root?.proxy.$bvModal
2022-08-03 04:50:39 +00:00
.msgBoxConfirm(
`Really exit trade ${item.trade_id} (Pair ${item.pair}) using ${ordertype} Order?`,
)
2022-04-16 18:27:33 +00:00
.then((value: boolean) => {
if (value) {
const payload: MultiForcesellPayload = {
tradeid: String(item.trade_id),
botId: item.botId,
};
if (ordertype) {
payload.ordertype = ordertype;
}
2022-04-19 04:33:25 +00:00
botStore
.forceSellMulti(payload)
2022-04-16 18:27:33 +00:00
.then((xxx) => console.log(xxx))
.catch((error) => console.log(error.response));
2021-11-27 08:15:21 +00:00
}
2022-04-16 18:27:33 +00:00
});
};
2020-06-29 19:14:16 +00:00
2022-08-03 18:09:45 +00:00
const forceExitPartialHandler = (item: Trade) => {
feTrade.value = item;
2022-11-19 13:41:04 +00:00
forceExitVisible.value = true;
2022-08-03 18:09:45 +00:00
};
2022-04-16 18:27:33 +00:00
const handleContextMenuEvent = (item, index, event) => {
// stop browser context menu from appearing
if (!props.activeTrades) {
return;
}
event.preventDefault();
// log the selected item to the console
console.log(item);
};
const removeTradeHandler = (item) => {
console.log(item);
2022-07-09 18:04:22 +00:00
root?.proxy.$bvModal
2022-04-16 18:27:33 +00:00
.msgBoxConfirm(`Really delete trade ${item.trade_id} (Pair ${item.pair})?`)
.then((value: boolean) => {
if (value) {
const payload: MultiDeletePayload = {
tradeid: String(item.trade_id),
botId: item.botId,
};
2022-04-19 04:33:25 +00:00
botStore.deleteTradeMulti(payload).catch((error) => console.log(error.response));
2022-04-16 18:27:33 +00:00
}
});
};
2022-11-19 13:41:04 +00:00
const onRowClicked = (item) => {
2022-04-16 18:27:33 +00:00
// Only allow single selection mode!
2022-11-19 13:41:04 +00:00
if (item && item.trade_id !== botStore.activeBot.detailTradeId) {
2022-04-19 04:33:25 +00:00
botStore.activeBot.setDetailTrade(item);
2022-04-16 18:27:33 +00:00
} else {
2022-04-19 04:33:25 +00:00
botStore.activeBot.setDetailTrade(null);
2022-04-16 18:27:33 +00:00
}
};
const onRowSelected = () => {
2022-04-19 04:33:25 +00:00
if (botStore.activeBot.detailTradeId) {
const itemIndex = props.trades.findIndex(
(v) => v.trade_id === botStore.activeBot.detailTradeId,
);
2022-04-16 18:27:33 +00:00
if (itemIndex >= 0) {
tradesTable.value?.selectRow(itemIndex);
} else {
console.log(`Unsetting item for tradeid ${selectedItemIndex.value}`);
selectedItemIndex.value = undefined;
2020-08-29 15:18:56 +00:00
}
2022-04-16 18:27:33 +00:00
}
};
2022-04-19 04:33:25 +00:00
watch(
() => botStore.activeBot.detailTradeId,
2022-04-26 20:00:21 +00:00
(val) => {
2022-04-19 04:33:25 +00:00
const index = props.trades.findIndex((v) => v.trade_id === val);
// Unselect when another tradeTable is selected!
if (index < 0) {
tradesTable.value?.clearSelected();
}
},
);
2022-04-16 18:27:33 +00:00
return {
2022-04-19 04:33:25 +00:00
botStore,
2022-04-16 18:27:33 +00:00
currentPage,
selectedItemIndex,
filterText,
perPage,
tableFields,
rows,
tradesTable,
2022-08-03 04:57:17 +00:00
forceExitHandler,
2022-08-03 18:09:45 +00:00
forceExitPartialHandler,
2022-04-16 18:27:33 +00:00
handleContextMenuEvent,
removeTradeHandler,
onRowClicked,
onRowSelected,
2022-08-03 18:09:45 +00:00
feTrade,
2022-11-19 13:39:46 +00:00
forceExitVisible,
2022-04-16 18:27:33 +00:00
};
},
});
2020-05-04 18:34:59 +00:00
</script>
<style lang="scss" scoped>
.card-body {
padding: 0 0.2em;
2020-07-11 18:00:13 +00:00
}
.table-sm {
font-size: $fontsize-small;
}
2020-09-12 14:39:52 +00:00
.btn-xs {
padding: 0.1rem 0.25rem;
font-size: 0.75rem;
}
</style>