frequi_origin/src/stores/pairlistConfig.ts

223 lines
6.3 KiB
TypeScript
Raw Normal View History

import { defineStore } from 'pinia';
import { useBotStore } from './ftbotwrapper';
2023-05-30 11:41:37 +00:00
import {
ExchangeSelection,
MarginMode,
2023-05-30 11:41:37 +00:00
Pairlist,
PairlistConfig,
PairlistParamType,
PairlistPayloadItem,
PairlistsPayload,
TradingMode,
2023-05-30 11:41:37 +00:00
} from '@/types';
import { computed, ref, toRaw } from 'vue';
import { showAlert } from './alerts';
2023-05-29 22:24:48 +00:00
export const usePairlistConfigStore = defineStore(
'pairlistConfig',
() => {
const botStore = useBotStore();
2023-05-29 22:24:48 +00:00
const evaluating = ref<boolean>(false);
const intervalId = ref<number>();
const stakeCurrency = ref<string>(botStore.activeBot?.stakeCurrency ?? 'USDT');
2023-05-29 22:24:48 +00:00
const whitelist = ref<string[]>([]);
2023-05-30 11:28:55 +00:00
const blacklist = ref<string[]>([]);
const customExchange = ref<boolean>(false);
const selectedExchange = ref<ExchangeSelection>({
2023-06-04 13:45:49 +00:00
exchange: botStore.activeBot?.botState.exchange ?? '',
trade_mode: {
2023-06-04 13:45:49 +00:00
trading_mode: botStore.activeBot?.botState.trading_mode ?? TradingMode.SPOT,
margin_mode:
botStore.activeBot?.botState.trading_mode === TradingMode.FUTURES
? MarginMode.ISOLATED
: MarginMode.NONE,
},
});
2023-05-29 22:24:48 +00:00
const config = ref<PairlistConfig>({ name: '', pairlists: [] });
const savedConfigs = ref<PairlistConfig[]>([]);
2023-05-29 22:24:48 +00:00
const firstPairlistIsGenerator = computed<boolean>(() => {
// First pairlist must be a generator
if (config.value.pairlists[0]?.is_pairlist_generator) {
return true;
}
return false;
});
2023-05-29 22:24:48 +00:00
const pairlistValid = computed<boolean>(() => {
return firstPairlistIsGenerator.value && config.value.pairlists.length > 0;
});
2023-05-30 11:41:37 +00:00
const configJSON = computed(() => {
return JSON.stringify(configToPayloadItems(), null, 2);
});
2023-05-30 15:51:19 +00:00
const isSavedConfig = computed(
() => savedConfigs.value.findIndex((c) => c.name === config.value.name) > -1,
);
2023-05-29 22:24:48 +00:00
function addToConfig(pairlist: Pairlist, index: number) {
pairlist = structuredClone(toRaw(pairlist));
for (const param in pairlist.params) {
pairlist.params[param].value = pairlist.params[param].default
? pairlist.params[param].default.toString()
: '';
}
config.value.pairlists.splice(index, 0, pairlist);
}
2023-05-29 22:24:48 +00:00
function removeFromConfig(index: number) {
config.value.pairlists.splice(index, 1);
}
2023-06-01 23:44:04 +00:00
function saveConfig(name: string) {
2023-05-29 22:24:48 +00:00
const i = savedConfigs.value.findIndex((c) => c.name === config.value.name);
2023-06-01 23:44:04 +00:00
config.value.name = name;
2023-05-29 22:24:48 +00:00
if (i > -1) {
2023-05-30 10:42:17 +00:00
savedConfigs.value[i] = config.value;
2023-05-29 22:24:48 +00:00
} else {
2023-05-30 10:42:17 +00:00
savedConfigs.value.push(config.value);
2023-05-29 22:24:48 +00:00
}
2023-05-30 10:42:17 +00:00
}
2023-06-01 23:44:04 +00:00
function cloneConfig() {
config.value = {
name: '',
pairlists: structuredClone(toRaw(config.value.pairlists)),
};
}
2023-05-30 15:44:38 +00:00
function deleteConfig() {
const i = savedConfigs.value.findIndex((c) => c.name === config.value.name);
if (i > -1) {
savedConfigs.value.splice(i, 1);
config.value = { name: '', pairlists: [] };
}
}
2023-05-30 10:42:17 +00:00
function newConfig() {
config.value = { name: '', pairlists: [] };
}
function selectConfig(selected: PairlistConfig) {
config.value = structuredClone(toRaw(selected));
}
2023-05-30 11:28:55 +00:00
function addToBlacklist() {
blacklist.value.push('');
}
function removeFromBlacklist(index: number) {
blacklist.value.splice(index, 1);
}
2023-05-30 11:41:37 +00:00
async function startPairlistEvaluation() {
const payload: PairlistsPayload = configToPayload();
evaluating.value = true;
try {
const { job_id: jobId } = await botStore.activeBot.evaluatePairlist(payload);
console.log('jobId', jobId);
intervalId.value = setInterval(async () => {
const res = await botStore.activeBot.getBackgroundJobStatus(jobId);
if (!res.running) {
clearInterval(intervalId.value);
const wl = await botStore.activeBot.getPairlistEvalResult(jobId);
2023-05-31 18:22:36 +00:00
evaluating.value = false;
if (wl.status === 'success') {
whitelist.value = wl.result.whitelist;
} else if (wl.error) {
showAlert(wl.error, 'danger');
evaluating.value = false;
}
2023-05-31 18:22:36 +00:00
}
}, 1000);
} catch (error) {
showAlert('Evaluation failed', 'danger');
evaluating.value = false;
}
2023-05-30 11:41:37 +00:00
}
function convertToParamType(type: PairlistParamType, value: string) {
if (type === PairlistParamType.number) {
return Number(value);
} else if (type === PairlistParamType.boolean) {
return Boolean(value);
} else {
return String(value);
}
}
function configToPayload(): PairlistsPayload {
2023-05-30 11:41:37 +00:00
const pairlists: PairlistPayloadItem[] = configToPayloadItems();
const config: PairlistsPayload = {
2023-05-30 11:41:37 +00:00
pairlists: pairlists,
stake_currency: stakeCurrency.value,
2023-05-30 11:41:37 +00:00
blacklist: blacklist.value,
};
console.log('asdf');
if (customExchange.value) {
console.log('setting custom exchange props');
config.exchange = selectedExchange.value.exchange;
config.trading_mode = selectedExchange.value.trade_mode.trading_mode;
config.margin_mode = selectedExchange.value.trade_mode.margin_mode;
}
return config;
2023-05-30 11:41:37 +00:00
}
function configToPayloadItems() {
const pairlists: PairlistPayloadItem[] = [];
config.value.pairlists.forEach((config) => {
const pairlist = {
method: config.name,
};
for (const key in config.params) {
const param = config.params[key];
if (param.value) {
pairlist[key] = convertToParamType(param.type, param.value);
}
}
pairlists.push(pairlist);
});
return pairlists;
}
2023-05-29 22:24:48 +00:00
return {
evaluating,
whitelist,
config,
2023-05-30 11:41:37 +00:00
configJSON,
2023-05-29 22:24:48 +00:00
savedConfigs,
2023-05-30 11:28:55 +00:00
blacklist,
2023-05-29 22:24:48 +00:00
startPairlistEvaluation,
addToConfig,
removeFromConfig,
saveConfig,
2023-06-01 23:44:04 +00:00
cloneConfig,
2023-05-30 15:44:38 +00:00
deleteConfig,
2023-05-30 10:42:17 +00:00
selectConfig,
newConfig,
2023-05-30 11:28:55 +00:00
addToBlacklist,
removeFromBlacklist,
2023-05-30 15:51:19 +00:00
isSavedConfig,
2023-05-29 22:24:48 +00:00
firstPairlistIsGenerator,
pairlistValid,
stakeCurrency,
customExchange,
selectedExchange,
2023-05-29 22:24:48 +00:00
};
},
{
persist: {
key: 'pairlist-configs',
2023-05-30 11:28:55 +00:00
paths: ['savedConfigs', 'blacklist'],
2023-05-29 22:24:48 +00:00
},
},
);