"use strict"

const timeStart = Date.now()
const pvVersion = chrome.runtime.getManifest().version
const pvName = chrome.runtime.getManifest().name
const pvBuilt = chrome.runtime.getManifest().version_name.substr(-11, 10)
const pvShortU = pvName.replace(/[^A-Z]/g, '')
const pvShortL = pvShortU.toLowerCase()
const pvCode = pvShortU.charCodeAt(0)-0x41
const ls = localStorage
const bg = chrome.extension.getBackgroundPage()
const log = bg.log
StorageInternal.connect(bg)
Permissions.connect(bg)
window.GUID = bg.GUID
window.broker = bg.broker

const q = sel => document.querySelector(sel)
const qa = sel => document.querySelectorAll(sel)
const qi = sel => document.getElementById(sel)

const firstAlertId = 1540353600000
const resetProps = ["recv", "fired", "locks", "recvNum", "firedNum", "autoNum", "intNum", "manualNum", "errNum", "time", "timeAvg", "timeMin", "timeMax", "slip"]
let state = {}

const s2ms = 1000
const m2ms = 60 * s2ms
const h2ms = 60 * m2ms
const d2ms = 24 * h2ms

const autoInterval = 1 * m2ms
const autoPosUpdate = 5 * m2ms

navigator && navigator.keyboard && navigator.keyboard.getLayoutMap().then(m => window.qwertz = m.get('KeyZ') === 'y')
document.addEventListener('DOMContentLoaded', run.bind(window, init))

chrome.runtime.onMessage.addListener(function(msg, sender){
	void chrome.runtime.lastError
	if (!msg || !msg.method || (sender && sender.tab)) return

	if (msg.method === 'positions') {
		opt.posEx = msg.value
		saveOptions(), filterPosChanged()
		run(updatePos, undefined, undefined, msg)
		return
	} else if (msg.method === 'refresh') {
		initLicense(), initExchanges()
		return
	}
	const alert = getAlerts(msg.value)
	if (!alert) return

	switch(msg.method) {
		case 'alert.toggle':
			const form = q(`#alert-${alert.id}`)
			if (form) {
				form.classList[alert.enabled ? 'remove' : 'add']('disabled')
				q(`#alert-${alert.id}-enabled`).checked = alert.enabled
			}
			break

		case 'alert.interval':
			const interval = q(`#alert-${alert.id}-autoInt`)
			if (interval) {
				interval.value = alert.autoInt
				q(`#alert-${alert.id}-auto`).checked = alert.auto
			}
			break
	}
})

window.onbeforeunload = function(ev) {
	if (Array.prototype.reduce.call(qa('#main-links [data-page=alerts], #exchange-links a'), (res, el) => res || el.classList.contains('unsaved'), false)) {
		return "Unsaved changes!"
	}
}

function* createAccount() {
	exchangeAccount.call(this)
}

function* saveAccounts() {
	const forms = getState(this).content.querySelectorAll("form")
	let newAccounts = []
	let newCreds = {}
	let success = true

	for (const form of forms) {
		let creds = formData(form)
		if (!creds.account) {
			continue
		}
		creds.account = creds.account.trim()
		newAccounts.push(creds.account)

		try {
			if (newCreds.getProp(creds.account)) {
				throw new ReferenceError("Account already exists: "+creds.account)
			}
			creds = this.setCredentials(creds.account, creds)
			newCreds[creds.account] = creds
		} catch (err) {
			yield error.bind(this, err.message)
			success = false
		}
	}
	if (!success) return false

	// $$$$ api-fy
	const alias = this.getAlias()
	let stored = broker.getAccounts()
	if (stored[alias]) {
		for (const account in stored[alias]) {
			--bg.accountNum
			if (!newAccounts.getProp(account)) {
				this.removeAccount(account)
				this.removeBalance(account)
			} else if (!newCreds.getProp(account)) {
				// $$$$ should never reach? (only if sync update inbetween UI load and save?)
				newCreds[account] = stored[alias][account]
			}
		}
	}
	bg.accountNum += newAccounts.length
	stored[alias] = newCreds
	broker.setAccounts(stored)
	q(`[data-page=exchange-${alias.toLowerCase()}] span`).classList.value = `glyphicon glyphicon-ok text-success`
	let posEx = q(`#posEx+ul [data-value=${this.getAlias(-1)}]`)
	posEx = posEx && posEx.parentElement
	posEx && posEx.classList.remove('disabled')

	log.info(`Saved ${newAccounts.length} API keys for ${this.getName()}: `+newAccounts.join(', '))
	toastr.success(`Saved ${newAccounts.length} ${this.getName()} accounts!`)
	q(`[data-page=exchange-${this.getAlias().toLowerCase()}]`).classList.remove('unsaved')
	return true
}

function* testAccount(el) {
	if (!(yield* saveAccounts.call(this))) {
		return false
	}
	const data = formData(el.closest("form"))

	try {
		log.info(`Testing API key ${data.account} on ${this.getName()} ...`);

		// $$$$ api-fy
		const cmd = new Alert({desc: "ub", sym: this.getTestSymbol()}, {a: data.account}).commands[0]
		yield* this.executeCommand(cmd, `TEST @ ${data.account}`)

		ok(`The API key for ${data.account} was successfully tested!<br/>
			<br/>
			<b><u>NOTE:</u> PLEASE MAKE SURE TRADING PRIVILIGES FOR API KEY ARE SET CORRECTLY!`)

		log.success(`API key ${data.account} successfully tested!`)
	} catch (ex) {
		// $$$ err.includes timestamp => sync & try one more time
/*			if (opt.timeSync && exchange && exchange.checkTime && err.includesNoCase('timestamp') && Date.now() - lastTimeSync > streamRetryTimeout) {
				logWarn && log.warn(pre+`Time sync issue detected, trying automatic time sync...`)
				lastTimeSync = Date.now()
				yield* exchange.checkTime()
			}
*/
		const err = getError(ex)
		error(`The following error occured testing account ${data.account}<br/>
			<br/>
			<b>${err}</b><br/>
			<br/>
			<u>Note:</u> Please check our <a href="https://wiki.profitview.app/faq" target="_blank">FAQ <span class="glyphicon glyphicon-new-window" aria-hidden="true"></span></a> for common errors!`)

		log.error("Error: "+err)
	}
}

function* removeAccount(el, ev) {
	const alias = this.getAlias()
	const form = el.closest("form")
	if (!form) return
	const curr = form.querySelector("[name='account']").value
	const account = !this.hasAccount(curr) && el.dataset.account || curr
	const onlyBalance = ev.shiftKey
	if (yield yesno.bind(this, `Do you really want to remove ${onlyBalance?'all stored balance info for ':''}the account <b>${account}</b> from ${this.getName()}?<br/>
		<br/>
		<u>Note:</u> This action cannot be undone!`)) {

		!onlyBalance && el.closest("form").remove()
		// $$$$ api-fy
		if (account && this.hasAccount(account)) {
			this.removeBalance(account)
			if (onlyBalance) return
			this.removeAccount(account)
			const alias2 = this.getAlias(-1)
			localStore.remove("positions", alias2, account)
			bg.positions && bg.positions[alias2] && delete bg.positions[alias2][account]
			--bg.accountNum

			const saved = this.getAccounts()
			q(`[data-page=exchange-${alias.toLowerCase()}] span`).classList.value = `glyphicon glyphicon-ok text-${!saved.length ? 'muted' : 'success'}`
			if (!saved.length) {
				let posEx = q(`#posEx+ul [data-value=${this.getAlias(-1)}]`)
				posEx = posEx && posEx.parentElement
				posEx && posEx.classList.add('disabled')
			}
		}
		if (!qa(`#exchange-${alias.toLowerCase()} form[data-account]`).length) {
			exchangeAccount.call(this)
		}
	}
}

function* cloneAccount(el, ev) {
	const creds = formData(el.closest('form'))
	const name = `${creds.account}-copy`
	const account = ev && ev.ctrlKey ? name : (yield ask.bind(this, "Create a copy with the following name:<br/><br/>", `Clone account "${creds.account}"`, name))
	if (account) {
		exchangeAccount.call(this, account, creds)
		q(`[data-page=exchange-${this.getAlias().toLowerCase()}]`).classList.add('unsaved')
	}
}

function* copyAccount(el, ev) {
	const creds = formData(el.closest('form'))
	if (ev && ev.ctrlKey || (yield yesno.bind(this, `Do you really want to copy the credentials of the ${this.getName()} account <b>${creds.account}</b> to the clipboard?`))) {
		clipboardWrite("text/plain", stringifyPretty(creds))
	}
}

function* pasteAccount(el, ev) {
	navigator.clipboard.readText().then(text => run(doPasteAccount.bind(this, el, ev), text)).catch(err => {})
}

function* doPasteAccount(el, ev, text) {
	let creds = null
	try {
		creds = JSON.parse(text)
	} catch(ex) {}

	if (!creds || !creds.account || !creds.public) {
		return yield error.bind(this, `Clipboard data is not a valid ${pvName} API account!`)
	}
	const form = el.closest('form')
	const oldCreds = form && formData(form)
	const action = !oldCreds || ev && ev.shiftKey ? 3 : ev && ev.ctrlKey ? 1 : (yield onetwo.bind(this, `Do you want to overwrite the ${this.getName()} account <b>${oldCreds.account}</b> with the credentials of the account <b>${creds.account}</b> – <u>including</u> the account name or only its key/secret?`, "Overwrite w/Name", "Only Key/Secret"))

	if (action) {
		if (action === 3) {
			exchangeAccount.call(this, creds.account, creds)
		} else {
			for (const field in creds) {
				if (oldCreds[field] === undefined || action === 2 && field === "account") continue
				form[field].value = creds[field]
			}
		}
		q(`[data-page=exchange-${this.getAlias().toLowerCase()}]`).classList.add('unsaved')
		toastr.success(`Pasted credentials ${action === 3 ? 'for':'into'} account ${action === 2 && oldCreds.account || creds.account}`)
	}
}

function onClick(ev) {
	let el = ev.target
	if (['SPAN', 'INPUT', 'TD'].includes(el.nodeName) && el.parentNode && !el.dataset.action) {
		el = el.parentNode
	}
	if (!['A', 'BUTTON', 'SPAN', 'LABEL', 'TR'].includes(el.nodeName) || el.classList.contains('disabled')) return
	if (el.target === "_blank") return
	if (el.dataset.href) window.open(el.dataset.href, el.dataset.target)

	const data = el.dataset
	if (data.page) {
		ev.preventDefault()
		return openPage(data.page)
	} else if (data.action) {
		if (typeof window[data.action] !== "function") {
			throw new ReferenceError("Action not found: "+data.action)
		}

		let cb = window[data.action]
		if (data.exchange) {
			const ex = broker.getByAlias(data.exchange)
			if (!ex) {
				throw new ReferenceError("Exchange Alias was not found: "+data.exchange)
			}
			cb = cb.bind(ex)
		}
		ev.preventDefault()
		return run(cb, el, ev)
	}
}

function exchangeAccount(account, creds) {
	const isNew = account === undefined || creds
	const state = getState(this)

	if (isNew && !creds) {
		account = "*"
		state.content.querySelectorAll("input[name='account']").forEach(e => e.value.trim() === '*' && (account = ''))
	}
	const access = new Template("exchange-access", state.content, state.data, {
		"access.#": (state.content.getElementsByTagName("form") || []).length,
		"access.account": account,
		"access.order": this.canListPos(),
	}).render()

	creds = creds || this.getAccountRaw(account) || {}
	Object.each(this.getFields(), (key, field) => {
		new Template(`exchange-access-${field.type === 'switch' ? 'switch' : 'field'}`, access, {
			"field.label": field.label,
			"field.message": field.message || '',
			"field.tooltip": field.tooltip || '',
			"field.type": field.type || 'password',
			"field.key": key,
			"field.value": (creds[key] === undefined ? field.default : creds[key]) || ''
		}).render()
	})
	$("input[name='account']", state.content).on('change.account', function(ev){
		const name = ev.target.value.replace(Exchange.illegalAcc, '-').trim()
		ev.target.value = name === '' ? ev.target.defaultValue : name
	})
	$("form[data-account] input", state.content).on('input.unsaved change.unsaved', function(ev){
		q(`[data-page=exchange-${state.data["exchange.id"]}]`).classList.add('unsaved')
	})

	if (isNew) {
		const scroll = $(".scroll-container", state.content)
		scroll.animate({
			scrollTop: scroll[0].scrollHeight - scroll[0].clientHeight
		}, 200)
		$("input[name='account']", state.content).focus()
	}
}

function initExchanges() {
	qa("#exchange-links li").forEach(el => el.remove())
	qa("article.exchange-page").forEach(el => el.remove())
	if (!broker) return

	const exes = broker.getAll()
	for (const ex of exes) {
		const id = ex.getAlias().toLowerCase()
		const state = getState(ex)

		state.content = qi("exchange-" + id)
		if (!state.content) {
			new Template("#exchange-page", null, state.data).render()

			let options = syncStore.get("options_"+id) || defExchangeOpt
			$(`#exchange-${id}-options input`).each(function(i, el){
				if (el.name && options.hasOwnProperty(el.name)) {
					el[el.type === 'checkbox' ? 'checked' : 'value'] = options[el.name]
				}
				$(el).on('change.exopt', function(ev){
					if (this.type === 'number') {
						this.value = Math.min(Math.max(this.value, this.min !== "" ? this.min : -Number.MAX_VALUE), this.max !== "" ? this.max : Number.MAX_VALUE)
					}
					syncStore.set("options_"+id, formData(ev.target.closest('form')))
				})
			})
			state.content = qi("exchange-"+id)

			new Template("#exchange-link", null, state.data).render()
		}

		let icon = "remove text-danger"
		const perm = ex.hasPermission()
		const sub = ex.hasSubscription()
		state.permissions.granted = perm && sub

		if (!perm || !sub) {
			new Template(!perm ? "#alert-permissions-missing" : "#alert-license-missing", null, state.data).render(state.content.querySelector("h1"))
		} else {
			const p5 = "hidHRmNXpoYWEzemg"
			const p6 = _("ZDh6d21oOTJ3")
			const p9 = localStore.get('perms')
			const p10 = "Zmo4Z2UzeXpneX"
			if ((ex.getSubscriptions().active.length || ex.getSubscriptions().inactive.length) && 
				(!p9 || !p9.permissions || Object.values(p9.permissions).indexOf(p6 + _(p10 + p5)) < 0)) {
				new Template(_("I2FsZXJ0LWxpY2Vuc2UtbWlzc2luZw"), null, state.data).render(state.content.querySelector("h1"))
				state.permissions.granted = !true
			}
		}

		const pos = ex.canListPos() && q(`[name=positions][data-id=${ex.getAlias(-1)}]`)
		let posEx = pos && q(`#posEx+ul [data-value=${ex.getAlias(-1)}]`)
		posEx = posEx && posEx.parentElement
		const btns = state.content.querySelectorAll(".btn")
		for (const btn of btns) {
			btn.classList.toggle("hide", !state.permissions.granted)
		}
		if (state.permissions.granted) {
			const accounts = ex.getAccounts()
			icon = "ok text-success"
			if (!accounts.length) {
				icon = "ok text-muted"
				exchangeAccount.call(ex)
				pos && pos.classList.add('noacc')
				posEx && posEx.classList.add('disabled')
			} else {
				for (const account of accounts) {
					exchangeAccount.call(ex, account)
					pos && pos.classList.remove('noacc')
				}
				posEx && posEx.classList.remove('disabled')
			}
			pos && pos.classList.remove('noperm')
		} else {
			state.content.querySelectorAll("form").forEach(el => el.remove())
			pos && pos.classList.add('noperm')
			posEx && posEx.classList.add('disabled')
		}
		q(`[data-page=exchange-${id}] span`).classList.value = "glyphicon glyphicon-"+icon
	}

	if (currentPage.startsWith('exchange-')) {
		q(`article[id=${currentPage}]`).classList.remove('hide')
		q(`.nav [data-page=${currentPage}]`).parentNode.classList.add('active')
	}
}

function getState(ex) {
	const alias = ex.getAlias()
	if (!state[alias]) {
		state[alias] = {
			content: null,
			permissions: {connected: false, granted: false},
			data: {
				"exchange.alias": alias,
				"exchange.syntax": ex.getAlias(-1),
				"exchange.id": alias.toLowerCase(),
				"exchange.name": ex.getName(),
				"exchange.attr": ex.hasMargin() ? (ex.hasSpot() ? "MS" : "M") : "",
				"exchange.attrtitle": ex.hasMargin() ? (ex.hasSpot() ? "Margin and Spot trading" : "Margin trading") : "",
				"exchange.url": ex.getWebsite(),
			}
		}
	}
	return state[alias]
}

function scrollLogChanged() {
	if (opt.scrollLog) {
		const scroll = $('#log-container')
		scroll.stop(true).animate({
			scrollTop: scroll[0].scrollHeight - scroll[0].clientHeight
		}, 200)
	}
}

function onlyOneAlertChanged() {
	if (opt.onlyOneAlert) {
		run(minAlert, "min")
		currentAlert && run(minAlert, q(`[data-alert='${currentAlert}'][data-action='minAlert']`))
	}
}

function debugLogChanged() {
	log.setLevel(opt.debugLog ? log.level.DEBUG : log.level.INFO)
}

function disableAll(val = !opt.disableAll) {
	if (opt.disableAll != val) {
		opt.disableAll = val
		saveOptions(), disableAllChanged()
	}
}

function disableAllChanged() {
	bg.disableAllChanged(opt.disableAll)
}

function onlyStreamChanged() {
	run(bg.toggleTradingViewStream, opt.onlyStream)
}

function* showFilterToggle() {
	opt.showFilter = !opt.showFilter
	saveOptions(), showFilterChanged()
}

function showFilterChanged() {
	q("#alertlist").classList[opt.showFilter ? "remove" : "add"]("hideFilter")
}

function filterChanged(name) {
	const forms = qa("#alertlist form")
	if (!forms || !forms.length) return

	if (["searchName", "searchSyntax", "onlyActAlert", "onlyAutoAlert"].includes(name)) {
		let shown = 0
		let first = 0
		let current = currentAlert
		const searchName = wildcardExp(opt.searchName, true, true)// RegExp(opt.searchName, 'i')
		const searchSyntax = wildcardExp(opt.searchSyntax, true, true)// RegExp(opt.searchSyntax, 'i')

		for (const form of forms) {
			const visible = (!opt.onlyActAlert || !form.classList.contains('disabled')) && 
				(!opt.onlyAutoAlert || form.auto.checked) && 
				(!opt.searchName || searchName.test(form.names.value) || searchName.test(form.id.value)) && 
				(!opt.searchSyntax || searchSyntax.test(form.commands.value))

			form.classList.add(visible ? 'visible' : 'hidden')
			form.classList.remove(visible ? 'hidden' : 'visible')

			if (visible) {
				++shown
				if (!first) {
					first = form.dataset.alert
				}
			} else if (form.dataset.alert == current) {
				current = 0
			}
		}
		currentAlert = current || first
		q("#filterInfo").innerHTML = `Showing ${shown} of ${forms.length}`

		if (!current) {
			qa("#alertlist form.selected").forEach(el => el.classList.remove('selected'))
			const curr = q(`[data-alert='${currentAlert}']`)
			curr && curr.classList.add('selected')
		}
	} else {
		function get(form, first, second, def) {
			const alert = getAlerts(form.dataset.alert)
			def = def ? '0'+form[def].value : (-alert.sort || 0)
			return (alert && (alert[first] || (second && alert[second]))) || def
		}

		const up = opt.sortRev ? -1 : 1
		const down = opt.sortRev ? 1 : -1
		const by = {
			"custom": (a, b) => Number(a.sort.value) > Number(b.sort.value) ? up : down,
			"name": (a, b) => a.names.value > b.names.value ? up : down,
			"created": (a, b) => Number(a.id.value) < Number(b.id.value) ? up : down,
			"recv": (a, b) => get(a, "recv", "fired", "names") < get(b, "recv", "fired", "names") ? up : down,
			"fired": (a, b) => get(a, "fired", "recv", "names") < get(b, "fired", "recv", "names") ? up : down,
			"recvnum": (a, b) => get(a, "recvNum") < get(b, "recvNum") ? up : down,
			"firednum": (a, b) => get(a, "firedNum") < get(b, "firedNum") ? up : down,
			"autonum": (a, b) => get(a, "autoNum") < get(b, "autoNum") ? up : down,
			"intnum": (a, b) => get(a, "intNum") < get(b, "intNum") ? up : down,
			"manualnum": (a, b) => get(a, "manualNum") < get(b, "manualNum") ? up : down,
			"errnum": (a, b) => get(a, "errNum") < get(b, "errNum") ? up : down,
			"timelast": (a, b) => get(a, "time") < get(b, "time") ? up : down,
			"timeavg": (a, b) => get(a, "timeAvg") < get(b, "timeAvg") ? up : down,
			"timemin": (a, b) => get(a, "timeMin") < get(b, "timeMin") ? up : down,
			"timemax": (a, b) => get(a, "timeMax") < get(b, "timeMax") ? up : down
		}[opt.sort || "custom"]

		const list = forms[0].parentElement
		Array.prototype.slice.call(forms).sort(by).forEach(form => list.appendChild(form))

		if (currentAlert) {
			const el = isAlertMin(currentAlert) ? q(`#alert-${currentAlert}-names`) : 
				q(`#alert-${currentAlert}-commands`).nextSibling.firstChild
			el && el.focus()
		}
	}
}

function initOptions(isUpdate) {
	$('[data-opt]').each(function(i, el){
		const name = el.dataset.opt
		if (name && opt.hasOwnProperty(name)) {
			if (el.type === 'checkbox') {
				el.checked = opt[name]
			} else if (el.type === 'button') {
				const item = el.nextElementSibling.querySelector(`[data-value='${opt[name]}' i]`)
				el.innerHTML = ((item && item.innerHTML) || opt[name])+'<span class="caret"></span>'
			} else {
				el.value = opt[name]
			}
		}

		!isUpdate && el.type !== 'button' && $(el).off('change.opt').on('change.opt', function(ev){
			if (this.dataset.warn && this.checked) {
				ok(this.dataset.warn)
			}
			if (this.type === 'number') {
				this.value = Math.min(Math.max(this.value, this.min !== "" ? this.min : -Number.MAX_VALUE), this.max !== "" ? this.max : Number.MAX_VALUE)
			} else if (this.type === 'text' && this.dataset.default && !this.value.trim()) {
				this.value = this.defaultValue
			}
			opt[name] = 
				this.type === 'checkbox' ? this.checked : 
				this.type === 'number' ? (this.step && this.step.includes('.') ? parseFloat(this.value) || 0 : parseInt(this.value) || 0) : 
				this.value.trim()
			saveOptions()

			if (!this.dataset.live) {
				const changed = (this.dataset.change || name)+'Changed'
				window[changed] && window[changed](name)
			}
		})

		!isUpdate && el.type === 'button' && $(el.nextElementSibling).find('a').off('click.opt').on('click.opt', function(ev){
			if (this.parentElement.classList.contains('disabled')) return false
			const btn = this.parentElement.parentElement.previousElementSibling
			btn.innerHTML = this.innerHTML+'<span class="caret"></span>'
			$(btn).dropdown("toggle")
			opt[name] = this.dataset.value
			saveOptions()

			const changed = (btn.dataset.change || name)+'Changed'
			window[changed] && window[changed](name)
			return false
		})

		!isUpdate && el.type === 'text' && el.dataset.live && $(el).off('keyup.opt').on('keyup.opt', function(ev){
			opt[name] = this.value.trim()

			const changed = (this.dataset.change || name)+'Changed'
			window[changed] && window[changed](name)
		})
	})

	if (!isUpdate) {
		state.initOptions = Date.now()

		setInterval(function(){
			if (ls.opt_ > state.initOptions) {
				state.initOptions = ls.opt_
				loadOptions()
				initOptions(true)
				opt.expConfSave && doConfigExport("Options changed")
			}
		}, 300)
	}
}

function renderPositions(alias, exPos) {
	const table = q(`tbody[data-id=${alias}]`)
	if (!table) return

	const list = new Template("position")
	let updated = 0
	let posNum = 0

	Object.each(exPos, (acc, pos) => {
		const accS = smartTrim(acc, 31)
		if (pos.error) {
			const err = getError(pos.error, undefined, false)
			list.renderBuffer(`<tr class="error"><td>${accS}</td><td colspan="15">ERROR: ${err}</td></tr>`)
			return
		}
		if (pos.list && pos.list.length) {
			pos.list.forEach(p => list.renderBuffer(Object.assign({cross: false, isolated: false}, p, {
				isProfit: p._profit > 0,
				isHidden: opt.posSym && !p._symbol.includesNoCase(opt.posSym),
				acc: acc, accS: accS,
				_hedge: p._hedge === "both" ? '' : p._hedge,
				_oneway: p._hedge === "both" ? p._hedge : '',
				_sl: p._sl || '–', _tp: p._tp || '–',
				_mark: toDecimal(Number(p._mark)) || '–',
				_liq: toDecimal(Number(p._liq)) || '–',
				_size: toDecimal(p._size),
				_sizeUSD: p._sizeUSD ? Number(p._sizeUSD).toFixed(2) : '–',
				_sizeCoin: p._sizeCoin ? Number(p._sizeCoin).toFixed(4) : '–',
				relSize: (p.relSize * 100).toFixed(2),
				leverage: `${Number(p.leverage || 1).toFixed(2).replace('.00', '')}x`,
				_profit: toDecimal(p._profit),
				pnl: (p.pnl * 100).toFixed(2),
				pnlLev: (p.pnlLev * 100).toFixed(2),
				pnlTotal: (p.pnlTotal * 100).toFixed(2),
				_date: p._date ? formatDateSP(p._date) : '–',
				age: p._date ? formatDurP(tillNow(p._date)) : '–',
				ccy: p.ccy || p.currency || '',
				currency: Exchange.formatCcy(p.ccy || p.currency || ''),
				ex: alias, empty: false
			})))
			posNum += pos.list.length
		} else {
			list.renderBuffer({
				isHidden: opt.posSym !== "" || !opt.posNone,
				acc: acc, accS: accS, _symbol: '–', _side: '–', _hedge: '', _oneway: '', _entry: '–', _mark: '–', _liq: '–', _size: '–', _sizeUSD: '–', _sizeCoin: '–', relSize: 0, leverage: 0, 
				_profit: '–', pnl: 0, pnlLev: 0, pnlTotal: 0, _date: '–', age: '–', ccy: '', currency: '',
				ex: alias, empty: true, cross: false, isolated: false
			})
		}
		updated = Math.max(updated, pos.updated || 0)
	})

	if (updated) {
		let since = (Date.now() - updated) / 1000
		since = since > 2 ? `(${formatDur(since)} ago)` : ''
		list.renderBuffer(`<tr class="footer"><td colspan="16" title="Last updated at"><span class="glyphicon glyphicon-time"></span>  ${formatDateS(updated)} <span data-since="${updated}">${since}</span></td></tr>`)
	}
	$(".tooltip").tooltip('destroy')
	table.innerHTML = list.getBuffer() || `<tr class="footer"><td colspan="16">NO DATA YET!</td></tr>`
	table.closest('.panel').classList[!list.getBuffer() || !posNum ? 'add' : 'remove'](opt.posEx || opt.posEmpty ? '-' : 'hide', 'empty')	// $$$ check filtering!
}

let updatingPositions = 0
let updatePosLast = 0

function* updatePos(el, ev = {}, msg) {
	const panel = el && el.closest('.panel')
	if (panel) {
		if (panel.classList.contains('noperm')) {
			error(`Please grant necessary permissions for ${panel.dataset.id} under "Setup: Permissions"!`); return
		}
		if (panel.classList.contains('noacc')) {
			error(`No accounts configured for ${panel.dataset.id} – please check "Options / Accounts"!`); return
		}
	}
	// $$$ make async; updatingPositions => counter for # updating exchanges?
	if (updatingPositions) return
	++updatingPositions
	qa("[data-action=updatePos]").forEach(el => el.disabled = true)
	el && q("#positions").classList.add('loading')

	yield* bg.updatePositions(!opt.posEx && !opt.posEmpty && !ev.ctrlKey ? 
		Array.prototype.map.call(qa("[name=positions]:not(.hide)"), ex => ex.dataset.id) : 
		!opt.posEx /*|| ev.ctrlKey*/ ? null : [opt.posEx], 
		undefined, undefined, renderPositions, undefined, !(ev.ctrlKey || opt.posNone))

	q("#positions").classList.remove('loading')
	qa("[data-action=updatePos]").forEach(el => el.disabled = false)
	--updatingPositions
	updatePosLast = Date.now()

	if (msg && msg.do) {
		const btn = q(`tr[data-ex='${msg.value}'][data-acc='${msg.acc || '*'}' i] button[data-do='${msg.do}']`)
		btn && run(changePos, btn, {shiftKey: msg.shiftKey, ctrlKey: msg.ctrlKey})
	}
}

function filterPosChanged(name) {
	if (name === "posSym") {
		const name = opt.posSym.toUpperCase()
		qa("[name=positions] tbody tr").forEach(row => {
			const sym = row.dataset.sym
			sym && row.classList[sym.includes(name) ? 'remove' : 'add']('hide')
		})
		// $$$ hide exchanges if !opt.posEx && !opt.posEmpty && no pos
	} else if (!opt.posEx) {
		qa("[name=positions]:not(.noperm):not(.noacc)").forEach(el => el.classList.remove('hide'))
		!opt.posEmpty && bg.positions.updated && qa("[name=positions].empty").forEach(el => el.classList.add('hide'))
		// $$$ if all empty => show all
	} else {
		qa("[name=positions]").forEach(el => el.classList.add('hide'))
		const sel = q(`[name=positions][data-id=${opt.posEx}]`)
		if (!sel) {
			opt.posEx = ""
			saveOptions()
			filterPosChanged()
			return
		}
		sel.classList.remove('hide')
	}
	qa("[name=positions] tr.empty").forEach(el => el.classList[opt.posNone ? 'remove' : 'add']('hide'))
	enableBtn('posEmpty', !opt.posEx)
}

function* copySyntax(el) {
	clipboardWrite("text/plain", q("#ordSyntax").innerHTML)
}

function* changePos(el, ev) {
	// $$$$ give closing orders id (eg "exit")
	// $$$$ don't cancel all orders for pos close first by default? (unless market close)?
	const act = ev && ev.act || el.dataset.do
	if (!act) return
	if (!el.dataset.ex) el = el.closest("tr")
	const pos = el && el.dataset && Object.map(el.dataset, (key, val) => val === '–' ? '' : val) || {}
	const isAll = !pos.ex
	const isOpen = act === 'add'
	const isAdd = isOpen && pos.side
	const isFlip = act === 'flip'
	const isClose = act === 'close' || act === 'market'
	const isMarket = act === 'market' || ev && ev.altKey
	const isCancel = act === 'cancel' || ev && ev.ctrlKey
	if (isAdd && ev && ev.shiftKey) {
		pos.side = pos.size = pos.entry = pos.sym = ""
	}
	const isNew = isOpen && !pos.side
	// $$$$$ isClose && isAll ==> handle different position sides +/- ???
	const isStops = (pos.side || (isOpen && !isAdd) || (isClose && isAll)) && /*!isOnlyCancel &&*/ ev && ev.shiftKey
	const isOnlyCancel = !isOpen && !isFlip && !isStops && isCancel
	const cancelType = ev && ev.cancelType
	const what = isOnlyCancel ? "Orders" : "Position"
	const title = (isOnlyCancel ? "Cancel" : {close: isMarket ? "Market Close" : "Close", add: isNew ? "Open <b>New</b>" : "Add to", flip: "Flip", market: "Market Close"}[act])+' '+
		(isAll ? `<b>ALL ${what}${what.endsWith('s')?'':'s'}</b>!` : `${isNew || isOnlyCancel ? '' : pos.side.toUpperCase()+' '}${what} on ${pos.ex}${isNew ? '' : ':'+pos.sym} @ ${pos.acc}`)
	const btn = isOnlyCancel ? "CANCEL" : {add: isNew ? "OPEN" : "ADD", flip: "FLIP"}[act] || "CLOSE"
	let allAcc = ""

	pos.balance = ''
	if (isAll) {
		qa("[name=positions]:not(.hide) tr.long:not(.hide), [name=positions]:not(.hide) tr.short:not(.hide)").forEach(el => {
			const data = el.dataset
			allAcc += `${allAcc.length?',':''}${data.ex}:${data.sym}:${data.acc}`
		})
		if (!allAcc) {
			ok("No open positions listed – please refresh or change your selection!")
			return
		}
		pos.size = pos.entry = pos.sym = "n/a"
		pos.lev = ""
		pos.hasSpot = true
	} else {
		if (!pos.acc) { // Accounts => +Order
			const data = formData(el.closest("form"))
			pos.acc = data.account
		}
		// $$$ update when symbol selected/changed!
		const ex = broker.getByAlias(pos.ex)
		const alias = ex.getAlias()
		pos.hasSpot = ex.hasSpot()
		pos.hasStopsOnFill = ex.hasStopsOnFill()
		pos.hasReducePosOnly = ex.hasReducePosOnly()
		let balance = ex.getBalanceRaw(pos.acc)
		let currency = pos.ccy || (ex.hasSpot() && (ex.getDefaultSymbol()+(alias.startsWith("OKEX")?'':marginSuffix))) || balance.lastSymbol
		// $$$$ check for margin/iso suffix
		balance = balance[currency] || balance[(currency = pos.sym)]
		if (balance) {
			const total = Number(balance.balance || balance.Balance || balance.total || 0)
			const available = Number(balance.available || balance.Available || 0)
			const digits = total > 999 ? 2 : total > 99 ? 3 : 4
			currency = Exchange.formatCcy(currency)
			pos.balance = (available === total ? total.toFixed(digits) : available.toFixed(digits-1)+' / '+total.toFixed(digits-1))+' '+currency
		}
		// $$$ get from symbol for new pos!
		if (pos.lev === "0") pos.lev = ({"SIMPLEFX": 400, "SIMPLEFX-DEMO": 400/*, "KRAKENFT": 50, "KRAKENFT-DEMO": 50*/})[pos.ex] || opt.posL
		else if (pos.lev) pos.lev = parseFloat(pos.lev) || opt.posL
	}
	pos.isAll = isAll
	pos.isClose = !isOpen
	pos.noSide = isAll || !isNew
	pos.short = !isAll && pos.side === 'short'
	pos.long = !isAll && (pos.side === 'long' || (isNew && !pos.short))
	pos.market = isMarket || isFlip
	// $$$$ save +/-/% price offset btn as well
	pos.p = isOpen ? opt[isNew ? 'posP' : 'posPA'] || 0 : opt.posPC || 0
	pos.q = isFlip ? 200 : isOpen ? opt[isNew ? 'posQ' : 'posQA'] : isCancel && !isStops ? 0 : 100
	const tmp = new Template("#changePos")
	tmp.renderBuffer(pos)

	function updateOrder(ev) {
		const f = formData(q(".order form"))
		f.ordId = f.ordId.replace(Command.illegalId, '-')
		f.ordSym && q(".order .bootbox-accept").classList.remove('disabled')
		let doStops = f.ordSL || f.ordTP

		enableBtn("ordPx", !(!isOpen && doStops) && !f.ordMarket)
		enableBtn("ordPxType", !(!isOpen && doStops) && !f.ordMarket)
		enableBtn("ordPxRef", !(!isOpen && doStops) && (!f.ordMarket || f.ordTrig))
		if (f.ordMarket && !f.ordTrig) setRadio("ordPxRef", (f.ordPxRef = ""))
		enableBtn("posPost", !f.ordMarket)
		enableBtn("ordMarket", !(!isOpen && doStops))

		if (doStops) q("#ordTrig").checked = f.ordTrig = false
		enableBtn("ordTrig", !doStops)
		enableBtn("ordTrigPx", f.ordTrig)
		enableBtn("ordTrigPxType", f.ordTrig)

		enableBtn("ordSLPx", f.ordSL)
		enableBtn("ordSLPxType", f.ordSL)
		enableBtn("ordSLPxRef", f.ordSL)
		enableBtn("ordSLType", f.ordSL)
		enableBtn("ordTPPx", f.ordTP)
		enableBtn("ordTPPxType", f.ordTP)
		enableBtn("ordTPPxRef", f.ordTP)
		enableBtn("ordTPType", f.ordTP)
		let stopsReduce = !pos.isClose && opt.posStop
		const stopsOnFill = stopsReduce && pos.hasStopsOnFill
		if (stopsOnFill && (!f.ordSL || f.ordSLType === "market") && (!f.ordTP || f.ordTPType === "market")) {
			doStops = false
		}
		if (pos.hasReducePosOnly) {
			enableBtn("posStop", pos.hasStopsOnFill)
			stopsReduce = false
		} else if (stopsReduce && f.ordTPType === 'limit' && !pos.side) {
			q("#ordTPType-1").click()
			f.ordTPType = "stop"
		}
		pos.isClose && enableBtn("posStop", false)

		const isShort = f.ordSide === 'short'
		const longPos = isShort ? -1 :  1
		const longNeg = isShort ?  1 : -1
		if (f.ordPxType !== '@') f.ordPx *= longPos
		if (f.ordTrigPxType !== '@') f.ordTrigPx *= longPos
		if (f.ordTPPxType !== '@') f.ordTPPx *= longPos
		if (f.ordSLPxType !== '@') f.ordSLPx *= longNeg

		let syntax = isAll ? (opt.posPar ? 'ap=' : 'acc=')+allAcc+'\n' : `acc=${pos.ex}:${f.ordSym/*||(isOnlyCancel?'*':'')*/}:${pos.acc}\n`
		if (isOpen) {
			syntax += 
				// $$$ cancel=open || cancel=order id=ENTRY
				(f.ordSide ? `${f.ordSide}=${f.ordQty}${f.ordQtyType === '%' ? '%' : f.ordQtyType ? ` unit=${f.ordQtyType}` : ''} ` : '')+
				(f.ordLev !== "" ? `lev=${f.ordLev} ` : '')+
				(f.ordTrig /*&& f.ordTrigPx*/ ? `stop=${f.ordTrigPxType === '@' ? '@' : ''}${f.ordTrigPx}${f.ordTrigPxType === '@' ? '' : f.ordTrigPxType} ` : '')+
				(!f.ordMarket && f.ordPx ? `price=${f.ordPxType === '@' ? '@' : ''}${f.ordPx}${f.ordPxType === '@' ? '' : f.ordPxType} ` : '')+
				(f.ordPxRef ? `pr=${f.ordPxRef} ` : '')+
				(f.ordMarket ? `market ` : '')+
				(!f.ordMarket && opt.posPost ? `post ifreject=retry maxtime=${opt.posTime||1}m${doStops ? ' iftime=abort' : ''} ` : '')+
				(!f.ordSide ? `quantity=${f.ordQty}${f.ordQtyType === '%' ? '%' : f.ordQtyType ? ` unit=${f.ordQtyType}` : ''} ` : '')+
				(f.ordQtyType === '%' && f.ordQtyRef !== 'free' ? `yield=${f.ordQtyRef} ` : '')+
				(f.ordId || !f.ordMarket ? `id=${f.ordId || 'ENTRY'} ` : '')+
				(stopsOnFill && f.ordSL && f.ordSLType !== 'stop'   ? `sl=${f.ordSLPxType === '@' ? '@' : ''}${f.ordSLPx}${f.ordSLPxType === '@' ? '' : f.ordSLPxType} ${f.ordSLPxRef === 'pos' ? 'slref=price ':''}` : '')+
				(stopsOnFill && f.ordTP && f.ordTPType === 'market' ? `tp=${f.ordTPPxType === '@' ? '@' : ''}${f.ordTPPx}${f.ordTPPxType === '@' ? '' : f.ordTPPxType} ${f.ordTPPxRef === 'pos' ? 'tpref=price ':''}` : '')+
				(doStops && stopsReduce ? 'save' : '')+
				`\n`
			if (doStops && !stopsReduce) syntax += f.ordMarket ? `wait=2s\n` : 
				`iforder=retry id=${f.ordId || 'ENTRY'} maxtime=${opt.posTime||1}m iftime=cancelall cancelid=${f.ordId || 'ENTRY'} log=cond\n`
		} else if (!doStops) {
			// $$$ f.ordMarket ? => cancel needed eg for OKX BTCUSDT margin pos w open limit!
			if (/*f.ordMarket &&*/ f.ordQty >= 100 && f.ordQtyType ==='%') syntax += `cancel${pos.hasSpot ? ' margin':''}\n`
			else if (f.ordClear || f.ordClearL) syntax += `cancel${f.ordClearL ? (f.ordClear ? '' : '=limit') : '=stops'}${pos.hasSpot ? ' margin':''}\n`
			f.ordQty && (syntax += 
				`${f.ordMarket ? 'market' : ''}close`+
				(f.ordSide ? `=${f.ordSide} ` : ' ')+
				(!f.ordMarket && f.ordPx ? `price=${f.ordPxType === '@' ? '@' : ''}${f.ordPx}${f.ordPxType === '@' ? '' : f.ordPxType} ` : '')+
				(!f.ordMarket && opt.posPost ? `post ifreject=retry maxtime=${opt.posTime||1}m ` : '')+
				((!f.ordTrig && f.ordPxRef) || (f.ordTrig && !f.ordPxRef) ? `pr=${f.ordPxRef || (isShort ? 'ask' : 'bid')} ` : '')+
				(f.ordTrig /*&& f.ordTrigPx*/ ? `stop=${f.ordTrigPxType === '@' ? '@' : ''}${f.ordTrigPx}${f.ordTrigPxType === '@' ? '' : f.ordTrigPxType} ` : '')+
				(f.ordQty !== 100 || f.ordQtyType !== '%' ? `quantity=${f.ordQty}${f.ordQtyType === '%' ? '%' : ''} ` : '')+
				(f.ordQtyType && f.ordQtyType !== '%' ? `unit=${f.ordQtyType} ` : '')+
				(f.ordQtyType === '%' && f.ordQtyRef !== 'pos' ? `yield=${f.ordQtyRef} ` : '')+
				(f.ordQtyType === '%' && f.ordQty > 100 ? `reduce=0 ` : '')+
				(f.ordId ? `id=${f.ordId} ` : '')+
				`\n`)
		}
		if (doStops) {
			if (f.ordClear || f.ordClearL) syntax += `cancel${f.ordClearL ? (f.ordClear ? '' : '=limit') : f.ordSL && f.ordTP ? '=stops' : f.ordSL ? '=sl' : '=tp'}${pos.hasSpot ? ' margin':''}\n`
			if (f.ordSL && f.ordQty) {
				const type = pos.hasStopsOnFill ? 'so' : 'sl'
				if (!stopsOnFill || f.ordSLType !== 'market') {
					syntax += 
						(isOpen && stopsReduce ? `${isShort ? 'long' : 'short'}=lastqty@ reduce ` : 
						`close`+
						(f.ordSide ? `=${f.ordSide} ` : ' '))+
						(!isOpen && (f.ordQty !== 100 || f.ordQtyType !== '%') ? `quantity=${f.ordQty}${f.ordQtyType === '%' ? '%' : f.ordQtyType ? ` unit=${f.ordQtyType}` : ''} ` : '')+
						`${f.ordSLType !== 'market' ? 'price' : type}=${f.ordSLPxType === '@' ? '@' : ''}${f.ordSLPx}${f.ordSLPxType === '@' ? '' : f.ordSLPxType} `+
						(f.ordSLPxRef !== 'pos' ? (isOpen && stopsReduce ? '' : `pr=${isShort ? 'ask' : 'bid'} `) : isOpen && stopsReduce ? 'pr=lastprice ':'')+
						(f.ordSLType !== 'market' ? `${type}=${f.ordLmtDist*longNeg}${f.ordLmtDistType} ${type}ref=price ` : '')+
						(f.ordSLType === 'market' ? `market ` : ``)+
						`id=${!isOpen && f.ordId ? f.ordId+'-' : ''}${!f.ordSLType ? 'L' : ''}SL `+
						(isOpen && stopsReduce ? '' : `expect `)+
						(isOpen && !f.ordMarket && !stopsReduce ? `retries=3 ` : '')+
						`\n`
				}
				if (!f.ordSLType && !stopsOnFill) {
					syntax += 
						(isOpen && stopsReduce ? `${isShort ? 'long' : 'short'}=lastqty@ reduce ` : 
						`close`+
						(f.ordSide ? `=${f.ordSide} ` : ' '))+
						(!isOpen && (f.ordQty !== 100 || f.ordQtyType !== '%') ? `quantity=${f.ordQty}${f.ordQtyType === '%' ? '%' : f.ordQtyType ? ` unit=${f.ordQtyType}` : ''} ` : '')+
						`price=${f.ordSLPxType === '@' ? '@' : ''}${f.ordSLPx}${f.ordSLPxType === '@' ? '' : f.ordSLPxType} `+
						(f.ordSLPxRef !== 'pos' ? (isOpen && stopsReduce ? '' : `pr=${isShort ? 'ask' : 'bid'} `) : isOpen && stopsReduce ? 'pr=lastprice ':'')+
						`${type}=${f.ordStpDist*longNeg}${f.ordStpDistType} ${type}ref=price `+
						`market `+
						`id=${!isOpen && f.ordId ? f.ordId+'-' : ''}MSL `+
						(isOpen && stopsReduce ? '' : `expect `)+
						(isOpen && !f.ordMarket && !stopsReduce ? `retries=3 ` : '')+
						`\n`
				}
			}
			if (f.ordTP && f.ordQty && (!stopsOnFill || f.ordTPType !== 'market')) {
				const type = pos.hasStopsOnFill ? 'so' : 'tp'
				syntax += 
					(isOpen && stopsReduce ? `${isShort ? 'long' : 'short'}=lastqty@ reduce ` : 
					`close`+
					(f.ordSide ? `=${f.ordSide} ` : ' '))+
					(!isOpen && (f.ordQty !== 100 || f.ordQtyType !== '%') ? `quantity=${f.ordQty}${f.ordQtyType === '%' ? '%' : f.ordQtyType ? ` unit=${f.ordQtyType}` : ''} ` : '')+
					`${f.ordTPType === 'market' ? type : 'price'}=${f.ordTPPxType === '@' ? '@' : ''}${f.ordTPPx}${f.ordTPPxType === '@' ? '' : f.ordTPPxType} `+
					(f.ordTPType === 'stop' ? (f.ordTPPxRef !== 'pos' ? (isOpen && stopsReduce ? '' : `pr=${isShort ? 'ask' : 'bid'} `) : isOpen && stopsReduce ? 'pr=lastprice ':'') : 
						(f.ordTPType !== 'market' && f.ordTPPxRef === 'pos' ? `pr=${isOpen && stopsReduce ? 'lastprice' : 'pos'} ` : ''))+
					(f.ordTPType === 'market' ? 'market ' : f.ordTPType === 'stop' ? `${type}=${f.ordLmtDist*longNeg}${f.ordLmtDistType} ${type}ref=price ` : '')+
					`id=${!isOpen && f.ordId ? f.ordId+'-' : ''}TP `+
					(isOpen && stopsReduce ? '' : `expect `)+
					(isOpen && !f.ordMarket && !stopsReduce ? `retries=3 ` : '')+
					`\n`
			}
		}
		q("#ordSyntax").innerHTML = syntax
	}

	bootbox.dialog({
		title: `<span class="glyphicon glyphicon-alert"></span>  ${title}  <span class="glyphicon glyphicon-alert"></span>`,
		className: 'order',
		message: tmp.getBuffer(),
		size: 'large',
		onEscape: true,
		buttons: {
			ok: {
				label: `<span class="glyphicon glyphicon-ok"></span> ${!isOnlyCancel ? 'PLACE ':''}${btn} ${isAll || isOnlyCancel ? '<b>ORDERS</b>' : 'ORDER'}`, className: 'btn-success'+(isNew || isOnlyCancel ? ' disabled' : ''),
				callback: ev => {
					const syntax = q("#ordSyntax").innerHTML
					const f = formData(q(".order form"))
					const side = f.ordSide || ''
					const name = isAll ? `ALL-${btn}` : `${f.ordSym||'ALL'}-${side.toUpperCase()}-${btn}`

					let delay = 100
					if(opt.posLog) {
						openPage('log')
						delay = opt.showDebug ? 500 : 250
					}
					setTimeout(run.bind(bg, bg.runCommandsCheck, name, {isManual: true, name: name, desc: syntax, side: side, 
						log: opt.posOnlyErr ? Command.log.warn/*Command.log.err?*/ : Command.log.all/*Command.log.nosyntax?*/}, opt.posTest), delay)
				}
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel', className: 'btn-danger'
			},
		},
		onShow: ev => {
			if (isOpen) {
				q("input[name=ordQty]").dataset.opt = isNew ? 'posQ' : 'posQA'
				q("input[name=ordPx]").dataset.opt = isNew ? 'posP' : 'posPA'
			} else {
				q("input[name=ordPx]").dataset.opt = 'posPC'
			}
			initOptions()
			if (isStops) {
				q("#ordSL").checked = q("#ordTP").checked = true
				if (isClose && isMarket) {
					q("#ordSLType-2").click(), q("#ordTPType-2").click()
				}
			}
			if (isCancel) {
				if (!cancelType || cancelType == 1) q("#ordClear").checked = true
				if (!cancelType || cancelType == 2) q("#ordClearL").checked = true
			}
			updateOrder()
			qa(".order form .btn-group .btn-default").forEach(el => el.addEventListener('click', function(){setTimeout(updateOrder, 50)}))
			qa(".order input.form-control, .order form input[type=checkbox], .order form input[type=hidden]").forEach(el => el.addEventListener('change', function(){
				window.clearTimeout(window._updateOrder)
				window._updateOrder = setTimeout(updateOrder, 50)
			}))
			qa(".order input.form-control").forEach(el => el.addEventListener('keyup', function(){
				window.clearTimeout(window._updateOrder)
				window._updateOrder = setTimeout(updateOrder, 100)
			}))
		},
		onShown: ev => ev.currentTarget.querySelector('.bootbox-cancel').focus()
	})
}

function initExchangeList() {
	let exHTML = ''
	let marginHTML = ''
	if (!broker) return

	const exes = broker.getAll()
	for (const ex of exes) {
		const alias = ex.getAlias(-1)
		const entry = `<li><a href="#" data-value="${alias}">${ex.getName()}</a></li>`
		exHTML += entry

		if (ex.canListPos()) {
			marginHTML += entry
			new Template("#exchange-positions", null, {
				"exchange.id": alias,
				"exchange.name": ex.getName(),
			}).render()
			renderPositions(alias, bg.positions[alias])
		}
	}

	["telebotEx", "defExchange", "posEx"].forEach(name => 
		q('#'+name).nextElementSibling.insertAdjacentHTML('afterbegin', name === 'posEx' ? marginHTML : exHTML))
}

function* handleAutoExports() {
	const now = new Date()
	setTimeout(run.bind(window, handleAutoExports), autoInterval - (now % autoInterval) + opt.fireOffset * 1000)

	if (opt.expConfAuto && intervalMatch(opt.expConfAutoInt, now)) {
		doConfigExport("Interval "+opt.expConfAutoInt)
	}
	if (opt.expLogAuto && intervalMatch(opt.expLogAutoInt, now)) {
		doLogExport(true, "Interval "+opt.expLogAutoInt)
	}
	if (opt.expBalAuto && intervalMatch(opt.expBalAutoInt, now)) {
		doBalanceExport(opt.expBalXLS ? "excel" : "csv", "Interval "+opt.expBalAutoInt)
	}
	if (opt.expBalMail && intervalMatch(opt.expBalMailInt, now)) {
		doBalanceExport("email", "Interval "+opt.expBalMailInt, opt.expBalMailTo)
	}
	if (opt.expPosAuto && intervalMatch(opt.expPosAutoInt, now)) {
		if (!(currentPage === "positions" && opt.posAuto && opt.posRate) && Date.now() - updatePosLast > autoPosUpdate) {
			log.notice(`[Auto Export => Interval ${opt.expPosAutoInt}] Updating all position info before export...`)
			yield* updatePos()
		}
		doPosExport(opt.expPosXLS ? "excel" : "csv", undefined, "Interval "+opt.expPosAutoInt)
	}
	if (opt.expPosMail && intervalMatch(opt.expPosMailInt, now)) {
		if (!(currentPage === "positions" && opt.posAuto && opt.posRate) && Date.now() - updatePosLast > autoPosUpdate) {
			log.notice(`[Auto Export => Interval ${opt.expPosMailInt}] Updating all position info before sending...`)
			yield* updatePos()
		}
		doPosExport("email", undefined, "Interval "+opt.expPosMailInt, opt.expPosMailTo)
	}
}

function initMenus() {
	$.contextMenu2({
		selector: '#alertlist form',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			runLong: {name: "Run (Long)  <code class=hk>Ctrl+L</code>", isHtmlName: true, icon: "glyphicon-play", action: "runAlert", idx: 2},
			runShort: {name: "Run (Short)  <code class=hk>Ctrl+P</code>", isHtmlName: true, icon: "glyphicon-play", action: "runAlert", idx: 3},
			testLong: {name: "Test (Long)", icon: "glyphicon-question-sign", action: "runAlert", idx: 4},
			testShort: {name: "Test (Short)", icon: "glyphicon-question-sign", action: "runAlert", idx: 5},
			s1: "-----",
			lock: {name: "Lock...", icon: "glyphicon-lock", action: "setLock"},
			unlock: {name: "Unlock...", icon: "glyphicon-remove-sign", action: "setLock", idx: 1},
			interval: {name: "Edit interval...", icon: "glyphicon-time", action: "confInterval"},
			stats: {name: "Reset stats", icon: "glyphicon-adjust", action: "resetAlertStats"},
			s2: "-----",
			copy: {name: "Copy (Clipboard)", icon: "glyphicon-copy", action: "copyAlert"},
			paste: {name: "Paste (Clipboard)", icon: "glyphicon-paste", action: "pasteAlert"},
			s3: "-----",
			delete: {name: "Delete  <code class=hk>Ctrl+Q</code>", isHtmlName: true, icon: "delete", action: "removeAlert"},
			clone: {name: "Clone", icon: "copy", action: "cloneAlert"},
			add: {name: "Add new  <code class=hk>Ctrl+D</code>", isHtmlName: true, icon: "add", action: "createAlert", global: true},
		},
		callback: function(key, item) {
			item = item.items[key]
			const alert = this[0].dataset.alert
			const btn = qa((item.global ? '' : `[data-alert='${alert}']`)+`[data-action='${item.action}']`)[item.idx || 0] || q("#alert-"+alert)
			run(window[item.action], btn, {})
		}
	})
	$.contextMenu2({
		selector: '.alert-editor',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			// $$$$$
			// Ctrl+Alt + Mouse => select in parallel multi line
			// Ctrl+Alt+Right => select next match in parallel/multi
			// Alt+E => goto next error
			undo: {name: "Undo  <code class=hk>Ctrl+Z</code>", isHtmlName: true, icon: "glyphicon-chevron-left"},
			redo: {name: "Redo  <code class=hk>Ctrl+Shift+Z</code>", isHtmlName: true, icon: "glyphicon-chevron-right"},
			s1: "-----",
			cut: {name: "Cut  <code class=hk>Ctrl+X</code>", isHtmlName: true, icon: "cut"},
			copy: {name: "Copy  <code class=hk>Ctrl+C</code>", isHtmlName: true, icon: "copy"},
			paste: {name: "Paste  <code class=hk>Ctrl+V</code>", isHtmlName: true, icon: "paste"},
			all: {name: "Select all  <code class=hk>Ctrl+A</code>", isHtmlName: true, icon: "glyphicon-ok-sign"},
			s2: "-----",
			find: {name: `Find...  <code class=hk>Ctrl+F</code>`, isHtmlName: true, icon: "glyphicon-search"},
			// Ctrl+K => findNext
			replace: {name: `Replace...  <code class=hk>Ctrl+H</code>`, isHtmlName: true, icon: "glyphicon-"},
			s3: "-----",
			moveLinesUp: {name: `Move Up  <code class=hk>Alt+↑</code>`, isHtmlName: true, icon: "glyphicon-"},
			moveLinesDown: {name: `Move Down  <code class=hk>Alt+↓</code>`, isHtmlName: true, icon: "glyphicon-"},
			blockIndent: {name: `Block Indent  <code class=hk>Ctrl+${window.qwertz?'`':']'}</code>`, isHtmlName: true, icon: "glyphicon-"},
			blockOutdent: {name: `Block Outdent  <code class=hk>Ctrl+${window.qwertz?'?':'['}</code>`, isHtmlName: true, icon: "glyphicon-"},
//			gotoLine: {name: `Go to line...  `, isHtmlName: true, icon: "glyphicon-"},
			toUpperCase: {name: `Uppercase  <code class=hk>Ctrl+U</code>`, isHtmlName: true, icon: "glyphicon-"},
			toLowerCase: {name: `Lowercase  <code class=hk>Ctrl+Shift+U</code>`, isHtmlName: true, icon: "glyphicon-"},
			s4: "-----",
			toggleCommentLines: {name: `Toggle Comment  <code class=hk>Ctrl+${window.qwertz?'#':'/'}</code>`, isHtmlName: true, icon: "glyphicon-pause"},
			lookupSymbol: {name: "Insert Symbol...  <code class=hk>Alt+S</code>", isHtmlName: true, icon: "glyphicon-search"},
		},
		callback: function(key, item) {
			const editor = ace.edit(this[0])
			const session = editor.getSession()
			switch (key) {
				case "find":
				case "replace":
					ace.config.loadModule("ace/ext/searchbox", function(e){e.Search(editor, key === "replace")})
					break
				case "cut":
				case "copy":
					document.execCommand(key)
					break
				case "paste":
					navigator.clipboard.readText().then(text => {
						session.insert(session.selection.getCursor(), text)
					})
					break
				case "all":
					session.selection.selectAll()
					break
				case "lookupSymbol":
					run(window[key], session, this[0].firstChild)
					break
				default:
					editor[key]()
					break
			}
			this[0].firstChild.focus()
		}
	})
/*	$.contextMenu2({
		selector: "button[data-action='tempAlert']",
		trigger: 'left',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			3: {
				name: "General",
				items: {
					4: {name: "Market Entry (unified)"},
					5: {name: "Simple Limit Entry (unified)"},
					6: {name: "Update TP/SL"},
				}
			},
			6: {
				name: "Checks",
				items: {
					7: {name: "Abort if order w ID found"},
					8: {name: "Abort if order w ID NOT found"},
				}
			},
		},
		callback: function(key, item) {
		}
	})*/
	$.contextMenu2({
		selector: '.exchange-page form[data-exchange]',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			copy: {name: "Copy (Clipboard)", icon: "glyphicon-copy", action: "copyAccount"},
			paste: {name: "Paste (Clipboard)", icon: "glyphicon-paste", action: "pasteAccount"},
			s1: "-----",
			clone: {name: "Clone", icon: "copy", action: "cloneAccount"},
			delete: {name: "Delete  <code class=hk>Ctrl+Q</code>", isHtmlName: true, icon: "delete", action: "removeAccount"},
			add: {name: "Add new  <code class=hk>Ctrl+D</code>", isHtmlName: true, icon: "add", action: "createAccount", global: true},
			s2: "-----",
			test: {name: "Test API", icon: "glyphicon-refresh", action: "testAccount"},
			order: {name: "Order (Wizard)", icon: "add", action: "changePos"},
			delete2: {name: "Delete Balances", icon: "delete", action: "removeAccount", shift: true},
			s0: "-----",
			note: {name: "<code>Ctrl</code> – skip confirm", disabled: true, isHtmlName: true},
		},
		callback: function(key, item, ev) {
			item = item.items[key]
			const acc = this[0].dataset.account
			const ex = this[0].dataset.exchange
			if (!ex) return
			const btn = q((item.global ? `[data-exchange='${ex}']` : `[data-account='${acc}'][data-exchange='${ex}'] `)+`[data-action='${item.action}']`)
			const cb = window[item.action].bind(broker.getByAlias(ex))
			item.shift && (ev.shiftKey = true)
			run(cb, btn, ev)
		}
	})
	$.contextMenu2({
		selector: '#positions tr',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			refresh: {name: "Refresh", icon: "glyphicon-refresh", action: "updatePos"},
			details: {name: "Show Details", icon: "glyphicon-search", action: "posInfo"},
			s1: "-----",
			close: {name: "Close...", icon: "glyphicon-minus", action: "changePos", do: "close"},
			add: {name: "Add...", icon: "add", action: "changePos", do: "add"},
			flip: {name: "Flip...", icon: "glyphicon-resize-vertical", action: "changePos", do: "flip"},
			market: {name: "Market Close...", icon: "glyphicon-remove-sign", action: "changePos", do: "close", act: "market"},
			s2: "-----",
			cancel: {name: "Cancel Orders...", icon: "delete", action: "changePos", do: "close", ctrl: true},
			cancel1: {name: "Cancel Stops...", icon: "glyphicon-chevron-right", action: "changePos", do: "close", ctrl: true, cancelType: 1},
			cancel2: {name: "Cancel Limit...", icon: "glyphicon-chevron-left", action: "changePos", do: "close", ctrl: true, cancelType: 2},
			s3: "-----",
			open: {name: "Open New...", icon: "add", action: "changePos", do: "add", shift: true},
			stops: {name: "TP / SL...", icon: "glyphicon-adjust", action: "changePos", do: "close", shift: true},
		},
		callback: function(key, item) {
			item = item.items[key]
			const btn = this[0].querySelector(`[data-action='${item.action}']${item.do ? `[data-do='${item.do}']`:''}`) || this[0]
			run(window[item.action], btn, {shiftKey: item.shift, ctrlKey: item.ctrl, cancelType: item.cancelType, act: item.act})
		}
	})
	$.contextMenu2({
		selector: 'body',
		animation: {duration: 0, show: 'show', hide: 'hide'},
		zIndex: 99999,
		items: {
			disable: {name: "Pause/Resume App", icon: "glyphicon-pause", action: "disableAll", direct: true},
			so: "-----",
			pause: {name: "Pause Alerts & App", icon: "glyphicon-pause", action: "handbrake", shift: true},
			stop: {name: "Stop Alerts & App", icon: "glyphicon-stop", action: "handbrake", shift: true},
			s1: "-----",
			market: {name: "Refresh Market Info", icon: "glyphicon-refresh", action: "clearSymbols"},
			time: {name: "Force Time Sync", icon: "glyphicon-time", action: "timeSync"},
			s2: "-----",
			restart: {name: "Restart App", icon: "glyphicon-repeat", action: "restartApp"},
			exportConf: {name: "Export Config  <code class=hk>Ctrl+E</code>", isHtmlName: true, icon: "glyphicon-open", action: "exportConfig"},
			importConf: {name: "Import Config  <code class=hk>Ctrl+O</code>", isHtmlName: true, icon: "glyphicon-save", action: "importConfig"},
			s3: "-----",
			exportBal1: {name: "Export Balances (CSV)", icon: "glyphicon-open", action: "exportBalances", format: "csv"},
			exportBal2: {name: "Export Balances (Excel)", icon: "glyphicon-open", action: "exportBalances", format: "excel"},
			copyBal: {name: "Copy Balances (Clipboard)", icon: "glyphicon-copy", action: "exportBalances", format: "csv", shift: true},
			clearBal: {name: "Clear Balances", icon: "glyphicon-adjust", action: "resetBalances"},
			s4: "-----",
			export: {name: "Export Log", icon: "glyphicon-floppy-save", action: "exportLog"},
			clear: {name: "Clear Log", icon: "delete", action: "clearLog"},
			scroll: {name: "Toggle Scroll", icon: "glyphicon-resize-vertical", opt: "scrollLog"},
			s0: "-----",
			note: {name: "<code>Ctrl</code> – skip confirm", disabled: true, isHtmlName: true},
		},
		callback: function(key, item, ev) {
			item = item.items[key]
			if (item.opt) {
				opt[item.opt] = !opt[item.opt]
				saveOptions()
				const changed = item.opt + 'Changed'
				window[changed] && window[changed]()
			}
			if (item.action) {
				const btn = q(`[data-action='${item.action}']${item.format ? `[data-format='${item.format}']`:''}`)
				const keys = {shiftKey: item.shift, ctrlKey: item.ctrl || ev.ctrlKey}
				item.direct ? window[item.action]() : run(window[item.action], btn, keys)
			}
		}
	})
}

function* init() {
	toastr.options = {
		positionClass: 'toast-top-center', 
		showDuration: 250, hideDuration: 500, 
		timeOut: 5000, extendedTimeOut: 1000, 
		showEasing: 'swing', hideEasing: 'swing'
	}
	window.onresize = onResize
	q("#ver").innerText = pvVersion
	document.addEventListener('click', onClick)

	initExchangeList()
	initOptions()
	initMenus()

	initExchanges()
	initPermissions()
	initLicense()

	let langTools = ace.require("ace/ext/language_tools")
	const syntaxCommands = [
		// $$$ move to Command.js
		{value: 'a=', meta: 'Account name(s) of the API credentials you want to use for this or following commands (comma separated, default: *)'},
		{value: 'abort=', meta: 'Shorthand for do=abort'},
		{value: 'above=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'aboveeq=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'acc=', meta: 'Account name(s) of the API credentials you want to use for this or following commands (comma separated, default: *)'},
		{value: 'account=', meta: 'Account name of the API credentials you want to use for this or following commands (default: *)'},
		{value: 'alertcheck=', meta: 'alert | Process this line only if alert is found and enabled – or process ife=/ifd= (if specified) according to alert state!'},
		{value: 'amount=', meta: '#(-#), %(-%), customX, _name | Portion of your balance or position (!) you want to affect (add "e" for y=equity, "p" for y=pos, "$" for u=currency)'},
		{value: 'ap=', meta: 'Use instead of a= to run ALL following commands in parallel on the given account(s) (comma separated)'},
		{value: 'b=', score: 5, meta: 'buy, long; sell, short | Side of the market you want to place your order on or check for'},
		{value: 'back=', score: 4, meta: 'Shorthand for jump=back'},
		{value: 'below=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'beloweq=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'book=', score: 3, meta: 'buy, long; sell, short | Side of the market you want to place your order on or check for'},
		{value: 'bound=', score: 2, meta: '#(-#), %(-%), @abs | OANDA only: Worst market price allowed for an order to be filled at'},
		{value: 'bc=', score: 1, meta: '0, 1 | Use last cached balance info for an account if possible, otherwise current balance is queried!'},
		{value: 'buy=', score: 0, meta: 'Shorthand for side=buy quantity=... (default 100%)'},
		{value: 'c=', score: 27, meta: 'order, pos | Cancel open orders or close open positions according to other specified parameters (also: tp/sl/open/limit/close/stops/long/short/buy/sell)'},
		{value: 'cached=', score: 26, meta: 'Shorthand for cachedprice=1 cachedbalance=1 (specify 0 to disable for this command)'},
		{value: 'cachedbalance=', score: 25, meta: '0, 1 | Use last cached balance info for an account if possible, otherwise current balance is queried!'},
		{value: 'cachedprice=', score: 24, meta: '0, 1 | Use last cached price data (ticker) for a symbol if possible, otherwise new ticker data is queried!'},
		{value: 'cancel=', score: 23, meta: 'order (def), pos | Cancel open orders or close open positions according to other specified parameters (also: tp/sl/open/limit/close/stops/long/short/buy/sell)'},
//		{value: 'cancelall=', score: 22, meta: 'Shorthand for cancel=order symbol=*'},
		{value: 'cancelid=', score: 21, meta: 'name(s) | Only cancel orders with matching custom ID(s) (comma separated) for conditional actions cancel/cancelall/cancelside (eg. ifnopos=cancelall)'},
		{value: 'cb=', score: 20, meta: '0, 1 | Use last cached balance info for an account if possible, otherwise current balance is queried!'},
		{value: 'cbrate=', score: 19, meta: '# | Binance Futures only: Callback rate (distance) for trailing stops (min 0.1, max 5; 5 = 5%)'},
		{value: 'ch=', score: 18, meta: 'order, pos | Check for a matching order or position according to other specified parameters'},
		{value: 'cha=', score: 17, meta: 'alert | Process this line only if alert is found and enabled – or process ife=/ifd= (if specified) according to alert state!'},
		{value: 'chid=', score: 16, meta: 'name(s) | Check if order with matching custom ID(s) (comma separated) exists before closing or checking a position'},
		{value: 'check=', score: 15, meta: 'order (def), pos | Check for a matching order or position according to other specified parameters'},
		{value: 'checkalert=', score: 14, meta: 'alert | Process this line only if alert is found and enabled – or process ife=/ifd= (if specified) according to alert state!'},
		{value: 'checkid=', score: 13, meta: 'name(s) | Check if order with matching custom ID(s) (comma separated) exists before closing or checking a position'},
		{value: 'checklock=', score: 12, meta: '1|2,Sym,TF | Check if symbol is locked (2=ign. timeframe), optionally specify "exchange:symbol" and timeframe (minutes)'},
		{value: 'checkorder=', score: 11, meta: 'Shorthand for check=order (optionally specify side or order type eg. checkorder=stop)'},
		{value: 'checkpos=', score: 10, meta: 'Shorthand for check=position (optionally specify side to check eg. checkpos=long)'},
		{value: 'cid=', score: 9, meta: 'name(s) | Check if order with matching custom ID(s) (comma separated) exists before closing or checking a position | alt: ID(s) to filter for cancel conditionals'},
		{value: 'close=', score: 8, meta: 'order, pos (def) | Cancel open orders or close open positions according to other specified parameters (also: tp/sl/open/limit/close/stops/long/short/buy/sell)'},
		{value: 'closeall=', score: 7, meta: 'Shorthand for close=position symbol=* quantity=... (default 100%)'},
		{value: 'closeid=', score: 6, meta: 'name(s) | Custom ID to use for close position conditional actions closeside/closeall (eg. ifnoorder=closeall)'},
		{value: 'closemarket=', score: 5, meta: 'Shorthand for close=position type=market quantity=... (default 100%) (optionally specify side to close eg. closemarket=long)'},
		{value: 'closepos=', score: 4, meta: 'Shorthand for close=position quantity=... (default 100%) (optionally specify side to close eg. closepos=long)'},
		{value: 'cm=', score: 3, meta: '#(-#), %(-%) | Cancel / Close / Check Maximum (see cmo for ordering)'},
		{value: 'cmcid=', score: 2, meta: '<id> | Specifies the CoinMarketCap ticker to query for min/max price change checks (default: bitcoin)'},
		{value: 'cmo=', score: 1, meta: 'newest, oldest; lowest, highest; smallest, biggest; random | Sort order for cancel/close/check'},
		{value: 'convert=', meta: 'src:dest | FTX only: Convert wallet balance of currency "src" into currency "dest" (comma separated list, amount via q=)'},
		{value: 'cp=', meta: '0, 1 | Use last cached price data (ticker) for a symbol if possible, otherwise new ticker data is queried!'},
		{value: 'cr=', meta: '0, 1 | Inverse the filtering of orders (c/ch=order) or positions (c/ch=pos)'},
		{value: 'custom=', meta: 'name(s) | Custom ID for order placement or to target order(s) by for cancelation/check'},
		{value: 'delay=', meta: 'seconds, % | Pauses execution between previous and this command (line) for the specified time (supports time units eg. 2.5h = 150m)'},
		{value: 'd=', meta: '0, 1 | Disables placement and cancelation of real orders – helpful for debugging'},
		{value: 'debug=', meta: '0, 1 | Disables placement and cancelation of real orders – helpful for debugging'},
		{value: 'disabled=', meta: '0, 1 | Disables placement and cancelation of real orders – helpful for debugging'},
		{value: 'disable=', meta: `alert | Disable given ${pvShortU} Alert or current running alert if none is specified, use "${pvShortU}" or "APP" to disable ${pvShortU}`},
		{value: 'do=', meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'e=', meta: 'BitMEX, Binance, ... | Receiving exchange for this command (default: taken from TV alert!)'},
		{value: 'echo=', meta: 'Shorthand for notify=log:...'},
		{value: 'enable=', meta: `alert | Enable given ${pvShortU} Alert or current running alert if none is specified, use "${pvShortU}" or "APP" to enable ${pvShortU}`},
		{value: 'end=', meta: 'Shorthand for jump=back'},
		{value: 'eid=', meta: 'name(s) | Only cancel orders with matching custom ID(s) (comma separated) for err=cancel/cancelall/cancelside or to use for closing orders err=closesall/closeside'},
		{value: 'err=', meta: '#, label | On error: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'errid=', meta: 'name(s) | Only cancel orders with matching custom ID(s) (comma separated) for err=cancel/cancelall/cancelside or to use for closing orders err=closesall/closeside'},
		{value: 'error=', meta: '#, label | On error: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'errorid=', meta: 'name(s) | Only cancel orders with matching custom ID(s) (comma separated) for err=cancel/cancelall/cancelside or to use for closing orders err=closesall/closeside'},
		{value: 'errorp=', meta: '#(-#), %(-%), @abs | If error handling is set to close positions: amount to overcut/undercut current bid/ask price'},
		{value: 'errorprice=', meta: '#(-#), %(-%), @abs | If error handling is set to close positions: amount to overcut/undercut current bid/ask price'},
		{value: 'ep=', meta: '#(-#), %(-%), @abs | If error handling is set to close positions: amount to overcut/undercut current bid/ask price'},
		{value: 'ex=', meta: 'BitMEX, Binance, ... | Receiving exchange for this command (default: taken from TV alert!)'},
		{value: 'exit=', meta: 'Shorthand for do=abort'},
		{value: 'exitall=', meta: 'Terminate all currently running alerts'},
		{value: 'exchange=', meta: 'BitMEX, Binance, ... | Receiving exchange for this command (default: taken from TV alert!)'},
		{value: 'expect=', meta: '# | Command expected to return at least # order(s) or none (0), otherwise treat as error (skip retries with negative #)'},
		{value: 'fallback=', meta: '#, label | Fallback action if other jump labels fail: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'fb=', meta: '#, label | Fallback action if other jump labels fail: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'fifo=', meta: '0, 1 | SimpleFX only: Set FIFO order flag (true by default)'},
		{value: 'fill=', meta: '0, 1 | FTX only: Set retryUntilFilled flag (true by default)'},
		{value: 'filtertrades=', meta: '0, 1 | OANDA only: Apply cm/maximum and cmo/order options per trade for close=pos (instead of per position)'},
		{value: 'fixed=', meta: '#(-#) | Fixed price for which to place an order or close a position'},
		{value: 'fixedp=', meta: '#(-#) | Fixed price for which to place an order or close a position'},
		{value: 'fixedprice=', meta: '#(-#) | Fixed price for which to place an order or close a position'},
		{value: 'fp=', meta: '#(-#) | Fixed price for which to place an order or close a position'},
		{value: 'from=', meta: 'seconds, % | Specify starting time window for PNL query; default from now, see since= (supports time units eg. -2d = back 2 days from now)'},
		{value: 'ft=', meta: '0, 1 | OANDA only: Apply cm/maximum and cmo/order options per trade for close=pos (instead of per position)'},
		{value: 'getbalance=', meta: 'Shorthand for updatebalance=1 save=1'},
		{value: 'getorder=', meta: 'Shorthand for check=order save=1'},
		{value: 'getpos=', meta: 'Shorthand for check=pos save=1'},
		{value: 'getprice=', meta: 'Shorthand for updateprice=1 save=1'},
		{value: 'go=', meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'goto=', meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'gsl=', meta: '0, 1 | OANDA only: Place stop (sl=) as "guaranteed stop loss" (for higher fees)'},
		{value: 'gt=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'gte=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'guaranteed=', meta: '0, 1 | OANDA only: Place stop (sl=) as "guaranteed stop loss" (for higher fees)'},
		{value: 'h=', meta: '#(-#), %(-%) | Bitfinex: 1 = hidden | BitMEX, Deribit, KuCoin FT: portion to be visible, 0 = hidden | Binance: portion to be invisible'},
		{value: 'hidden=', meta: '#(-#), %(-%) | Bitfinex: 1 = hidden | BitMEX, Deribit, KuCoin FT: portion to be visible, 0 = hidden | Binance: portion to be invisible'},
		{value: 'id=', meta: 'name(s) | Custom ID for order placement or to target order(s) by for cancelation/check'},
		{value: 'iceberg=', meta: '#(-#), %(-%) | Bitfinex: 1 = hidden | BitMEX, Deribit, KuCoin FT: portion to be visible, 0 = hidden | Binance: portion to be invisible'},
		{value: 'interval=', meta: `[alert,]interval | Configure ${pvShortU} Alert or current running alert if none specified to run in given interval (+x = x minutes from now)`},
		{value: 'ifbg=', meta: '#, label | If current pos/order size is BIGGER than saved one: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifbigger=', meta: '#, label | If current pos/order size is BIGGER than saved one: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifbuy=', meta: '#, label | If BUY order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifd=', meta: '#, label | If alert specified with cha=xxx is DISABLED: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifdisabled=', meta: '#, label | If alert specified with cha=xxx is DISABLED: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ife=', meta: '#, label | If alert specified with cha=xxx is ENABLED: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifenabled=', meta: '#, label | If alert specified with cha=xxx is ENABLED: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iferror=', meta: '#, label | On error: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iffound=', meta: '#, label | If AT LEAST ONE found (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifl=', meta: '#, label | If LONG order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iflong=', meta: '#, label | If LONG order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifmatch=', meta: '#, label | If AT LEAST ONE found (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifminsp=', meta: '#, label | If bid-ask spread is TOO LOW (see minsp=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifminspread=', meta: '#, label | If bid-ask spread is TOO LOW (see minspread=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifmaxsp=', meta: '#, label | If bid-ask spread is TOO HIGH (see maxsp=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifmaxspread=', meta: '#, label | If bid-ask spread is TOO HIGH (see maxspread=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifn=', meta: '#, label | If NONE found/matched (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifnone=', meta: '#, label | If NONE found/matched (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifnomatch=', meta: '#, label | If NONE found/matched (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifnoorder=', meta: '#, label | Shorthand for check=order ifnone=... | cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifnopos=', meta: '#, label | Shorthand for check=pos ifnone=... | cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifnot=', meta: '#, label | If NONE found/matched (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifo=', meta: '#, label | If AT LEAST ONE found (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifopen=', meta: '#, label | If AT LEAST ONE found (or placed): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iforder=', meta: '#, label | Shorthand for check=order iffound=... | cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifp=', meta: '#, label | If found order was PARTIALLY filled: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifpartial=', meta: '#, label | If found order was PARTIALLY filled: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifpos=', meta: '#, label | Shorthand for check=pos iffound=... | cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifr=', meta: '#, label | If type=post order is immediately rejected: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifreject=', meta: '#, label | If type=post order is immediately rejected: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifrejected=', meta: '#, label | If type=post order is immediately rejected: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifs=', meta: '#, label | If SHORT order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifsell=', meta: '#, label | If SELL order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifshort=', meta: '#, label | If SHORT order or position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifsl=', meta: '#, label | If slippage TOO HIGH (see msl=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifslip=', meta: '#, label | If slippage TOO HIGH (see msl=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifsm=', meta: '#, label | If current pos/order size is SMALLER than saved one: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ifsmaller=', meta: '#, label | If current pos/order size is SMALLER than saved one: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ift=', meta: '#, label | If more time than maxtime has elapsed: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftf=', meta: '#, label | If maxtime has elapsed AND PARTIALLY filled order found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftnf=', meta: '#, label | If maxtime has elapsed AND UNFILLED order found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftime=', meta: '#, label | If more time than maxtime has elapsed: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftimefill=', meta: '#, label | If maxtime has elapsed AND PARTIALLY filled order found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftimenofill=', meta: '#, label | If maxtime has elapsed AND UNFILLED order found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftimenopos=', meta: '#, label | If maxtime has elapsed AND NO position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftimeout=', meta: '#, label | If more time than maxtime has elapsed: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftimepos=', meta: '#, label | If maxtime has elapsed AND position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftnp=', meta: '#, label | If maxtime has elapsed AND NO position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'iftp=', meta: '#, label | If maxtime has elapsed AND position found: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ismargin=', meta: '0, 1 | Place as margin order instead of spot; alternative to b=long/short'},
		{value: 'iso=', meta: '0, 1 | Binance / KuCoin: Use isolated margin (instead of crossed) with spot margin order'},
		{value: 'isolated=', meta: '0, 1 | Binance / KuCoin: Use isolated margin (instead of crossed) with spot margin order'},
		{value: 'jd=', meta: 'seconds | Pauses execution between this and the next command IF a conditional jump is made (negative value = only for back jumps)'},
		{value: 'jump=', meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'jumpdelay=', meta: 'seconds | Pauses execution between this and the next command IF a conditional jump is made (negative value = only for back jumps)'},
		{value: 'l=', score: 9, meta: 'Leverage, multiplying your available balance while increasing risk (!) | BitMEX, Bybit, Phemex: 0 = cross'},
		{value: 'lc=', score: 8, meta: '# >= 1 | Leverage calculation factor to use instead of specified or detected leverage (override!)'},
		{value: 'limit=', score: 7, meta: 'Shorthand for type=limit'},
		{value: 'lq=', score: 6, meta: '0, 1 | Use previously saved "leftover" quantity from an unfilled or only partially filled order (see save=)'},
		{value: 'lt=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'lte=', meta: '#(-#), %(-%), customX, _name | Filter positions (c/ch=pos) by their average entry price or orders (c/ch=order) by their limit or market stop price'},
		{value: 'leftover=', score: 5, meta: '0, 1 | Use previously saved "leftover" quantity from an unfilled or only partially filled order (see save=)'},
		{value: 'lev=', score: 4, meta: 'Leverage, multiplying your available balance while increasing risk (!) | BitMEX, Bybit, OKX, Phemex: 0 = cross'},
		{value: 'levcalc=', score: 3, meta: '# >= 1 | Leverage calculation factor to use instead of specified or detected leverage (override!)'},
		{value: 'leverage=', score: 2, meta: 'Leverage, multiplying your available balance while increasing risk (!) | BitMEX, Bybit, OKX, Phemex: 0 = cross'},
		{value: 'loan=', score: 1, meta: 'BTC, BNB, ... | Binance: margin borrow on specified assets (comma separated, use q=#/% for amount)'},
		{value: 'lock=', meta: '1|0,Sym,TF | Lock (1) or Unlock (0) symbol, optionally specify "exchange:symbol" or custom name and timeframe (minutes)'},
		{value: 'lockcheck=', meta: '1|2,Sym,TF | Check if symbol is locked (2=ign. timeframe), optionally specify "exchange:symbol" or custom name and timeframe (minutes)'},
		{value: 'log=', meta: 'all, nosyntax, nodebug, cond, warn, err, debug, none | Loglevel to use for this command'},
		{value: 'loglevel=', meta: 'all, nosyntax, nodebug, cond, warn, err, debug, none | Loglevel to use for this command'},
		{value: 'long=', meta: 'Shorthand for side=long quantity=... (default 100%) (margin trade!)'},
		{value: 'loop=', meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'lr=', meta: 'customX, _name | Specifies from which custom plot data to take an optional leverage multiplier'},
		{value: 'lref=', meta: 'customX, _name | Specifies from which custom plot data to take an optional leverage multiplier'},
		{value: 'margin=', meta: '0, 1 | Place as margin order instead of spot; alternative to b=long/short'},
		{value: 'margintype=', meta: 'isolated/iso, crossed/cross; borrow, repay | BinanceFT/Bybit/KrakenFT/OKX/Phemex: Margin type to switch to, Binance: borrow = auto borrow, repay = auto repay'},
		{value: 'market=', meta: 'Shorthand for type=market'},
		{value: 'marketclose=', meta: 'Shorthand for close=position type=market quantity=... (default 100%) (optionally specify side to close eg. closepos=long)'},
		{value: 'marketcloseall=', meta: 'Shorthand for close=position symbol=* type=market quantity=... (default 100%) (optionally specify side to close eg. marketcloseall=long)'},
		{value: 'marketbuy=', meta: 'Shorthand for side=buy type=market'},
		{value: 'marketsell=', meta: 'Shorthand for side=sell type=market'},
		{value: 'marketlong=', meta: 'Shorthand for side=long type=market'},
		{value: 'marketshort=', meta: 'Shorthand for side=short type=market'},
		{value: 'minb=', meta: '#, % | Minimum available balance, if below treat as error'},
		{value: 'minbalance=', meta: '#, % | Minimum available balance, if below treat as error'},
		{value: 'maxb=', meta: '#, % | Maximum available balance, if above treat as error'},
		{value: 'maxbalance=', meta: '#, % | Maximum available balance, if above treat as error'},
		{value: 'minhvd=', meta: '# | Minimum daily historical BTC volatility, if below treat as error'},
		{value: 'mindailyvola=', meta: '# | Minimum daily historical BTC volatility, if below treat as error'},
		{value: 'maxhvd=', meta: '# | Maximum daily historical BTC volatility, if above treat as error'},
		{value: 'maxdailyvola=', meta: '# | Maximum daily historical BTC volatility, if above treat as error'},
		{value: 'minhvw=', meta: '# | Minimum weekly historical BTC volatility, if below treat as error'},
		{value: 'minweeklyvola=', meta: '# | Minimum weekly historical BTC volatility, if below treat as error'},
		{value: 'maxhvw=', meta: '# | Maximum weekly historical BTC volatility, if above treat as error'},
		{value: 'maxweeklyvola=', meta: '# | Maximum weekly historical BTC volatility, if above treat as error'},
		{value: 'minhvm=', meta: '# | Minimum monthly historical BTC volatility, if below treat as error'},
		{value: 'minmonthlyvola=', meta: '# | Minimum monthly historical BTC volatility, if below treat as error'},
		{value: 'maxhvm=', meta: '# | Maximum monthly historical BTC volatility, if above treat as error'},
		{value: 'maxmonthlyvola=', meta: '# | Maximum monthly historical BTC volatility, if above treat as error'},
		{value: 'minp=', meta: '# | Minimum absolute price (if p is specified as percentage)'},
		{value: 'minprice=', meta: '# | Minimum absolute price (if p is specified as percentage)'},
		{value: 'maxp=', meta: '# | Maximum absolute price (if p is specified as percentage)'},
		{value: 'maxprice=', meta: '# | Maximum absolute price (if p is specified as percentage)'},
		{value: 'minpl=', meta: '#(-#), %(-%) | Filter open positions by profit (only if above) – use either absolute contracts/units or %'},
		{value: 'minpnl=', meta: '#(-#), %(-%) | Filter open positions by profit (only if above) – use either absolute contracts/units or %'},
		{value: 'maxpl=', meta: '#(-#), %(-%) | Filter open positions by profit (only if below) – use either absolute contracts/units or %'},
		{value: 'maxpnl=', meta: '#(-#), %(-%) | Filter open positions by profit (only if below) – use either absolute contracts/units or %'},
		{value: 'minq=', meta: '# | Minimum absolute quantity (if q is specified as percentage)'},
		{value: 'minquantity=', meta: '# | Minimum absolute quantity (if q is specified as percentage)'},
		{value: 'maxq=', meta: '# | Maximum absolute quantity (if q is specified as percentage)'},
		{value: 'maxquantity=', meta: '# | Maximum absolute quantity (if q is specified as percentage)'},
		{value: 'mins=', meta: '#(-#), %(-%) | Filter open positions by size (only if above) – use either absolute contracts/units or %'},
		{value: 'minsr=', meta: 'customX, _name | Specifies from which custom plot data to take an optional minsize multiplier'},
		{value: 'minsize=', meta: '#(-#), %(-%) | Filter open positions by size (only if above) – use either absolute contracts/units or %'},
		{value: 'minsizeref=', meta: 'customX, _name | Specifies from which custom plot data to take an optional minsize multiplier'},
		{value: 'max=', meta: '# | Maximum number of iterations for a specific jump (when used with do/goto/jump/loop) or current command'},
		{value: 'maxcount=', meta: '# | Maximum number of iterations for a specific jump (when used with do/goto/jump/loop) or current command'},
		{value: 'maxs=', meta: '#(-#), %(-%) | Filter open positions by size (only if below) – use either absolute contracts/units or %'},
		{value: 'maxsr=', meta: 'customX, _name | Specifies from which custom plot data to take an optional maxsize multiplier'},
		{value: 'maxsize=', meta: '#(-#), %(-%) | Filter open positions by size (only if below) – use either absolute contracts/units or %'},
		{value: 'maxsizeref=', meta: 'customX, _name | Specifies from which custom plot data to take an optional maxsize multiplier'},
		{value: 'maxslip=', meta: '#(-#), %(-%) | Compare current price to last cached price or OHLC / custom plot, only place order if not further <b>against</b> us than specified'},
		{value: 'maxslipref=', meta: 'ask, bid, mid, last, pos, _name | Price reference for slippage check (see msl=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'minsp', meta: '#(-#), %(-%) | Compare current ticker bid-ask (or OHLC / custom plot) spread, only place order if spread <b>higher</b> than specified value'},
		{value: 'minspread', meta: '#(-#), %(-%) | Compare current ticker bid-ask (or OHLC / custom plot) spread, only place order if spread <b>higher</b> than specified value'},
		{value: 'maxsp', meta: '#(-#), %(-%) | Compare current ticker bid-ask (or OHLC / custom plot) spread, only place order if spread <b>lower</b> than specified value'},
		{value: 'maxspread', meta: '#(-#), %(-%) | Compare current ticker bid-ask (or OHLC / custom plot) spread, only place order if spread <b>lower</b> than specified value'},
		{value: 'maxtime=', meta: '#(-#), %(-%) | Maximum time allowed since initial firing of alert in either seconds or as % of bar time'},
		{value: 'maximum=', score: -1, meta: '#(-#), %(-%) | Cancel / Close / Check Maximum (see cmo for ordering)'},
		{value: 'min1h=', meta: '# | Check CoinMarketCap price ticker for minimum hourly price change percentage – if under treat as error'},
		{value: 'max1h=', meta: '# | Check CoinMarketCap price ticker for maximum hourly price change percentage – if over treat as error'},
		{value: 'min24h=', meta: '# | Check CoinMarketCap price ticker for minimum daily price change percentage – if under treat as error'},
		{value: 'max24h=', meta: '# | Check CoinMarketCap price ticker for maximum daily price change percentage – if over treat as error'},
		{value: 'min7d=', meta: '# | Check CoinMarketCap price ticker for minimum weekly price change percentage – if under treat as error'},
		{value: 'max7d=', meta: '# | Check CoinMarketCap price ticker for maximum weekly price change percentage – if over treat as error'},
		{value: 'mc=', meta: 'Shorthand for close=position type=market quantity=... (default 100%) (optionally specify side to close eg. mc=long)'},
		{value: 'mca=', meta: 'Shorthand for close=position symbol=* type=market quantity=... (default 100%) (optionally specify side to close eg. mca=long)'},
		{value: 'msl=', meta: '#(-#), %(-%) | Compare current price to last cached price or OHLC / custom plot, only place order if not further <b>against</b> us than specified'},
		{value: 'mslr=', meta: 'ask, bid, mid, last, pos, _name | Price reference for slippage check (see msl=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'mt=', meta: 'isolated/iso, crossed/cross; borrow, repay | BinanceFT/Bybit/KrakenFT/OKX/Phemex: Margin type to switch to, Binance: borrow = auto borrow, repay = auto repay'},
		{value: 'n=', meta: 'target:"message":"subject":rcpt, ... | Send custom notification to targets (d = discord, e = email, i = ifttt, l = log, s = sms/twilio, t = telegram)'},
		{value: 'name=', meta: 'name(s) | Custom ID for order placement or to target order(s) by for cancelation/check'},
		{value: 'notify=', meta: 'target:"message":"subject":rcpt, ... | Send custom notification to targets (d = discord, e = email, i = ifttt, l = log, s = sms/twilio, t = telegram)'},
		{value: 'nr=', meta: '#, label | If NOTHING open/found AT ALL: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'nonmatch=', meta: '0, 1 | Inverse the filtering of orders (c/ch=order) or positions (c/ch=pos)'},
		{value: 'noorder=', meta: '#, label | If NOTHING open/found AT ALL: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'nopos=', meta: '#, label | If NOTHING open/found AT ALL: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'noresult=', meta: '#, label | If NOTHING open/found AT ALL: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'onerror=', meta: '#, label | On error: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'order=', meta: 'newest, oldest; lowest, highest; smallest, biggest; random | Sort order for cancel/close/check'},
		{value: 'p=', meta: '#(-#), %(-%), @abs | Amount to over/undercut currentcut bid/ask price- either by percentage or absolute price!'},
		{value: 'pb=', meta: '#(-#), %(-%), @abs | OANDA only: Worst market price allowed for an order to be filled at'},
		{value: 'par=', meta: 'Use instead of a= to run ALL following commands in parallel on the given accounts'},
		{value: 'parallel=', meta: 'Use instead of a= to run ALL following commands in parallel on the given accounts'},
		{value: 'pauseall=', meta: 'Pause all currently running alerts'},
		{value: 'pc=', meta: '0, 1 | Use last cached price data (ticker) for a symbol if possible, otherwise new ticker data is queried!'},
		{value: 'plref=', meta: 'pos, lev, total | Reference for profit loss calculation, lev = position leveraged, total = account total'},
		{value: 'pnlref=', meta: 'pos, lev, total | Reference for profit loss calculation, lev = position leveraged, total = account total'},
		{value: 'price=', meta: '#(-#), %(-%), @abs | Amount to over/undercut currentcut bid/ask price- either by percentage or absolute price!'},
		{value: 'pr=', meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for price calc, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'priceref=', meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for price calc, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'pm=', meta: 'normal / oneway, hedge / hedged, auto | Binance Futures / Bybit / Phemex: Position mode for order or to filter by (default: auto)'},
		{value: 'posmode=', meta: 'normal / oneway, hedge / hedged, auto | Binance Futures / Bybit / Phemex: Position mode for order or to filter by (default: auto)'},
		{value: 'pp=', meta: '0, 1 | Binance Futures only: When the stop triggers the difference between mark and contract price cannot be > than symbols "trigger protect"'},
		{value: 'protect=', meta: '0, 1 | Binance Futures only: When the stop triggers the difference between mark and contract price cannot be > than symbols "trigger protect"'},
		{value: 'q=', meta: '#(-#), %(-%), customX, _name | Portion of your balance or position (!) you want to affect (add "e" for y=equity, "p" for y=pos, "$" for u=currency)'},
		{value: 'quantity=', meta: '#(-#), %(-%), customX, _name | Portion of your balance or position (!) you want to affect (add "e" for y=equity, "p" for y=pos, "$" for u=currency)'},
		{value: 'query=', meta: 'PNL | Query last 100 PNL records for current symbol, specify time window via from=, limit with maximum=/cm=, filter with minpl/maxpl/mins/maxs/gt/gte/lt/lte'},
		{value: 'quit=', meta: 'Shorthand for do=abort'},
		{value: 'qr=', meta: 'customX, _name | Specifies from which custom plot data to take an optional quantity multiplier'},
		{value: 'qref=', meta: 'customX, _name | Specifies from which custom plot data to take an optional quantity multiplier'},
		{value: 'r=', score: 8, meta: '0, 1 | BinanceFT, BitMEX, Bybit, Deribit, FTX, KrakenFT, KuCoinFT, OANDA, Phemex: Place as "reduce only" (default for close=pos)'},
		{value: 'rd=', score: 7, meta: 'seconds | Pauses execution between this and the next command IF a conditional BACK jump is made'},
		{value: 'reduce=', score: 6, meta: '0, 1 | BinanceFT, BitMEX, Bybit, Deribit, FTX, KrakenFT, KuCoinFT, OANDA, Phemex: Place as "reduce only" (default for close=pos)'},
		{value: 'repay=', score: 5, meta: 'BTC, BNB, ... | Binance: margin repay of specified assets (comma separated, use q=#/% for amount)'},
		{value: 'restart=', score: 4, meta: `Force ${pvName} to restart (USE WITH CAUTION!)`},
		{value: 'resumeall=', score: 3, meta: 'Resume all currently paused alerts'},
		{value: 'ret=', score: 2, meta: '# | Command expected to return at least # order(s) or none (0), otherwise treat as error (skip retries with negative #)'},
		{value: 'return=', score: 1, meta: '# | Command expected to return at least # order(s) or none (0), otherwise treat as error (skip retries with negative #)'},
		{value: 'retries=', score: 0, meta: '# | Number of times command is repeated in case of error (overriding Exchange settings)'},
		{value: 'retryall=', meta: '0, 1 | Ignore the built-in list of errors that are usually exempt from retry-on-error options (eg. no balance available, internal errors etc.)'},
		{value: 'retrydelay=', meta: 'seconds | Pauses execution between this and the next command IF a conditional BACK jump is made'},
		{value: 'reverse=', meta: '0, 1 | Inverse the filtering of orders (c/ch=order) or positions (c/ch=pos)'},
		{value: 'rf=', meta: '0, 1 | FTX only: Set retryUntilFilled flag (true by default)'},
		{value: 'run=', meta: `alert | Run another ${pvShortU} Alert using current ex/sym/acc and OHLCV data, waiting for it to complete (similar to "including" the other alert)`},
		{value: 'runen=', meta: `alert | Run another ${pvShortU} Alert (IF ENABLED!) using current ex/sym/acc and OHLCV data, waiting for it to complete (similar to "including" the other alert)`},
		{value: 's=', score: 10, meta: 'BTCUSD, ETHEUR, ... | Market on the receiving exchange to be used for this command (* = ALL, for check/cancel/close) (default: taken from TV alert!)'},
		{value: 'save=', score: 9, meta: '0, 1, prefix | Saves data from order/pos/ticker to vars: filled, left, lastqty, lastqtysum, lastprice, lastavg, laststop, lastid, lastside, lasttype, laststatus, lastpnl etc.'},
		{value: 'savename=', meta: 'prefix | Custom variable name prefix for the save= function'},
		{value: 'savebalance=', meta: 'Shorthand for updatebalance=1 save=1'},
		{value: 'saveorder=', meta: 'Shorthand for check=order save=1'},
		{value: 'savepos=', meta: 'Shorthand for check=pos save=1'},
		{value: 'saveprice=', meta: 'Shorthand for updateprice=1 save=1'},
		{value: 'sell=', score: 8, meta: 'Shorthand for side=sell quantity=... (default 100%)'},
		{value: 'short=', score: 7, meta: 'Shorthand for side=short quantity=... (default 100%) (margin trade!)'},
		{value: 'side=', score: 6, meta: 'buy, long; sell, short | Side of the market you want to place your order on or check for (long/short = margin trade!)'},
		{value: 'since=', score: 5, meta: 'fired, bar, candle | Time/count reference to use for max time/count check'},
		{value: 'size=', score: 4, meta: '#(-#), %(-%), customX, _name | Portion of your balance or position (!) you want to affect (add "e" for y=equity, "p" for y=pos, "$" for u=currency)'},
		{value: 'sizeref=', score: 3, meta: 'customX, _name | Specifies from which custom plot data to take an optional quantity multiplier'},
		{value: 'skip=', score: 2, meta: '#, label | Skip over given number of commands (-1 = all) or jump to <label>, unless error (see err=, ifn=, ifo= etc.)'},
		{value: 'sl=', score: 1, meta: '#(-#), %(-%), @abs, customX, _name | Bitfinex: 1 = stop | Spot: Stop loss with buy/sell | Margin: Place buy/sell when price hits stop price'},
		{value: 'slref=', score: 0, meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for stop loss, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'sleep=', meta: 'seconds, % | Pauses execution between previous and this command (line) for the specified time (supports time units eg. 2.5h = 150m)'},
		{value: 'so=', meta: '#(-#), %(-%), @abs, customX, _name | Stop order – place buy/sell when price hits stop price'},
		{value: 'soref=', meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for stop order, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'spa', meta: 'ask, bid, mid, last, pos, _name | Price reference for spread check "ask" (high) (see maxsp=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'spreadask', meta: 'ask, bid, mid, last, pos, _name | Price reference for spread check "ask" (high) (see maxsp=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'spb', meta: 'ask, bid, mid, last, pos, _name | Price reference for spread check "bid" (low) (see minsp=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'spreadbid', meta: 'ask, bid, mid, last, pos, _name | Price reference for spread check "bid" (low) (see minsp=), mid = ask-bid average, last = last trade price, pos = position avg (margin!)'},
		{value: 'sr=', meta: 'last, mark, index | BinanceFT, BitMEX, Bybit, Deribit, KrakenFT, KuCoinFT: Price reference for SL/TP stops (default: last)'},
		{value: 'stop=', meta: '#(-#), %(-%), @abs, customX, _name | Stop order – place buy/sell when price hits stop price'},
		{value: 'stoploss=', meta: '#(-#), %(-%), @abs, customX, _name | Bitfinex: 1 = stop | Spot: Stop loss with buy/sell | Margin: Place buy/sell when price hits stop price'},
		{value: 'stopref=', meta: 'last, mark, index | BinanceFT, BitMEX, Bybit, Deribit, KrakenFT, KuCoinFT: Price reference for SL/TP stops (default: last)'},
		{value: 'sv=', meta: '0, 1, prefix | Saves data from order/pos/ticker to vars: filled, left, lastqty, lastqtysum, lastprice, lastavg, laststop, lastid, lastside, lasttype, laststatus, lastpnl etc.'},
		{value: 'svn=', meta: 'prefix | Custom variable name prefix for the save= function'},
		{value: 'sym=', meta: 'BTCUSD, ETHEUR, ... | Market on the receiving exchange to be used for this command (* = ALL, for check/cancel/close) (default: taken from TV alert!)'},
		{value: 'symbol=', meta: 'BTCUSD, ETHEUR, ... | Market on the receiving exchange to be used for this command (* = ALL, for check/cancel/close) (default: taken from TV alert!)'},
		{value: 't=', score: 8, meta: 'limit, market, fok, ioc, post, close, open, settle | Order Type, see Setup: Commands for more info'},
		{value: 'takeprofit=', score: 7, meta: '#(-#), %(-%), @abs, customX, _name | Bitfinex: 1 = stop | Spot: Take profit with buy/sell | Margin: Place buy/sell when price hits stop price'},
		{value: 'tb=', score: 6, meta: 'BTC, BNB, ... | Binance, Deribit, FTX: transfer balances (comma separated) between spot/margin/sub (use y=spot/margin q=#/%, Deribit/FTX: y=sub)'},
		{value: 'tp=', score: 5, meta: '#(-#), %(-%), @abs, customX, _name | Bitfinex: 1 = stop | Spot: Take profit with buy/sell | Margin: Place buy/sell when price hits stop price'},
		{value: 'tpref=', score: 4, meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for take profit, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'ts=', score: 3, meta: '#(-#), %(-%), @abs, customX, _name | Bitfinex: 1 = trailing | Margin (where supported): Place buy/sell when price hits stop price trailing with offset'},
		{value: 'tsref=', score: 2, meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for trailing stop0, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'time=', score: 1, meta: '#(-#), %(-%) | Maximum time allowed since initial firing of alert in either seconds or as % of bar time'},
		{value: 'timeout=', score: 0, meta: 'seconds | Timeout in seconds between retries in case of an error (overriding Exchange settings)'},
		{value: 'toggle=', meta: `alert | Toggle given ${pvShortU} Alert or current running alert if none is specified between enabled/disabled, use "${pvShortU}" or "APP" to toggle ${pvShortU}`},
		{value: 'trailing=', meta: '#(-#), %(-%), @abs | Bitfinex: 1 = trailing | Margin (where supported): Place buy/sell when price hits stop price trailing with offset'},
		{value: 'trigger=', meta: `alert | Trigger another ${pvShortU} Alert using current ex/sym/acc and OHLCV data, immediately resuming running this alert IN PARALLEL (similar to external trigger)`},
		{value: 'triggeren=', meta: `alert | Trigger another ${pvShortU} Alert (IF ENABLED!) using current ex/sym/acc and OHLCV data, immediately resuming running this alert IN PARALLEL (similar to external trigger)`},
		{value: 'tr=', meta: 'customX, _name | Transpose given custom plot points (comma separated) from OHLC close price as base to the current commands ticker price'},
		{value: 'trref=', meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for transpose command, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'transfer=', meta: 'BTC, BNB, ... | Binance, Deribit, FTX: transfer balances (comma separated) between spot/margin/sub (use y=spot/margin q=#/%, Deribit/FTX: y=sub)'},
		{value: 'transpose=', meta: 'customX, _name | Transpose given custom plot points (comma separated) from OHLC close price as base to the current commands ticker price'},
		{value: 'transref=', meta: 'ask,bid,mid,last,pos,customX,_name... | Price reference for transpose command, mid = ask-bid average, last = last trade price, pos = avg entry (margin!)'},
		{value: 'type=', meta: 'limit, market, fok, ioc, post, day, settle, open, close | Order Type, see Setup: Commands for more info'},
		{value: 'u=', score: 6, meta: 'contracts, currency | Unit to be used for the provided quantity (q) parameter (if absolute value)'},
		{value: 'unit=', score: 5, meta: 'contracts, currency | Unit to be used for the provided quantity (q) parameter (if absolute value)'},
		{value: 'ub=', score: 4, meta: '0, 1 | Update balance info, use to query the current account balance (eg. for notifications) without executing a trade'},
		{value: 'up=', score: 3, meta: '0, 1 | Update and cache price data (ticker) for a symbol, use on its own or as a trade option'},
		{value: 'update=', score: 2, meta: 'Shorthand for updateprice=1 updatebalance=1'},
		{value: 'updatebalance=', score: 1, meta: '0, 1 | Update balance info, use to query the current account balance (eg. for notifications) without executing a trade'},
		{value: 'updateprice=', score: 0, meta: '0, 1 | Update balance info, use to query the current account balance (eg. for notifications) without executing a trade'},
		{value: 'wait=', meta: 'seconds, % | Pauses execution between previous and this command (line) for the specified time (supports time units eg. 2.5h = 150m)'},
		{value: 'wid=', meta: '#, label | If no open order matches given IDs (see cid=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'ws=', meta: '#, label | If pos/order found but its on the OPPOSITE side: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'wrongid=', meta: '#, label | If no open order matches given IDs (see cid=): cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'wrongside=', meta: '#, label | If pos/order found but its on the OPPOSITE side: cancel, cancelall, cancelside, closeall, closeside, abort or skip # cmnds / jump to <label>'},
		{value: 'y=', meta: 'free/balance, total/equity, pos/possize, spot, margin | Balance: Available excl. orders/positions, Equity: Total incl. profit/loss (for others see tb=)'},
		{value: 'yield=', meta: 'free/balance, total/equity, pos/possize, spot, margin | Balance: Available excl. orders/positions, Equity: Total incl. profit/loss (for others see tb=)'},
	]
	const syntaxCompleter = {
		getCompletions: function(editor, session, pos, prefix, callback) {
			if (!prefix.length) {
				callback(null, [])
				return
			}
			callback(null, syntaxCommands)//.filter(cmd => cmd.value.startsWith(prefix))
		}
	}
	langTools.setCompleters([syntaxCompleter])

	let startPage = ''
	if (window.location.hash.length > 3) {
		if (window.location.hash[1] != '/') {
			window.location.hash = '/'+window.location.hash.split('-')[0].substr(1)
		}
		startPage = window.location.hash.substr(2)
	}
	if (startPage === 'alerts') {
		initAlerts()
	} else {
		setTimeout(initAlerts, 100)
	}
	openPage(startPage, true)

	$(document.body).tooltip({selector: '[title]', placement: 'bottom', container: 'body', trigger: 'hover', html: true, delay: {show: 250, hide: 50}})
	$("[title][data-placement=top]").tooltip({placement: 'top', container: 'body', trigger: 'hover', html: true, delay: {show: 250, hide: 50}})
	window.addEventListener('keydown', onHotkey)
	onResize()

	console.log(`Ready after ${formatDur((Date.now() - timeStart) / 1000, 2)}`)

	setTimeout(run.bind(window, handleAutoExports), autoInterval - (Date.now() % autoInterval) + opt.fireOffset * 1000)
}

function onHotkey(ev) {
	if (ev.ctrlKey) {
		switch (ev.key) {
			case 'ArrowUp':
			case 'ArrowDown':
				if(currentPage === 'alerts') {
					ev.preventDefault(), ev.stopPropagation()
					const curr = q("[data-alert='"+currentAlert+"']")
					const currBtn = curr.querySelector("[data-action='minAlert']")
					if (ev.shiftKey) {
						let next = curr
						do {
							next = ev.key === 'ArrowUp' ? next.previousElementSibling : next.nextElementSibling
						} while(next && !next.classList.contains('visible'))

						if (next) {
							opt.onlyOneAlert && !curr.classList.contains('min') && run(minAlert, currBtn, ev)
							qa("#alertlist form.selected").forEach(el => el.classList.remove('selected'))
							next.classList.add('selected')
							currentAlert = next.dataset.alert

							const nextBtn = next.querySelector("[data-action='minAlert']")
							if (opt.onlyOneAlert && next.classList.contains('min')) {
								run(minAlert, nextBtn, ev)
							} else {
								const el = isAlertMin(currentAlert) ? next.querySelector("[name='names']") : 
									next.querySelector("[name='commands']").nextSibling.firstChild
								el && el.focus()
							}
						}
						break
					}
					(curr.classList.contains('min') ^ (ev.key === 'ArrowUp')) && run(minAlert, currBtn, ev)
				}
				break

/*			case 'c':
				if (currentPage.startsWith('exchange-')) {
					const exchange = broker.getByAlias(currentPage.substr(9).toUpperCase())
					run(copyAccount.bind(exchange, document.activeElement))
				}
				break
*/
			case 'd':
				if(currentPage === 'alerts') {
					ev.preventDefault(), ev.stopPropagation()
					run(createAlert)
				} else if (currentPage.startsWith('exchange-')) {
					ev.preventDefault(), ev.stopPropagation()
					const exchange = broker.getByAlias(currentPage.substr(9).toUpperCase())
					run(createAccount.bind(exchange))
				}
				break

			case 'e':
				ev.preventDefault(), ev.stopPropagation()
				run(exportConfig)
				break

			case 'i':
			case 'm':
				if(currentPage === 'alerts') {
					ev.preventDefault(), ev.stopPropagation()
					run(minAlert, ev.key === 'i' ? 'min' : 'max')
				}
				break

			case 'o':
				ev.preventDefault(), ev.stopPropagation()
				run(importConfig)
				break

			case 'l':
			case 'p':
				if(currentPage === 'alerts') {
					ev.preventDefault(), ev.stopPropagation()
					const btn = q(`[data-alert='${currentAlert}'][data-action='runAlert'][data-side='${ev.key === 'l' ? 'long' : 'short'}']`)
					btn && run(runAlert, btn, ev)
				}
				break

			case 'Q':
			case 'q':
				if(currentPage === 'alerts') {
					ev.preventDefault(), ev.stopPropagation()
					const btn = q(`[data-alert='${currentAlert}'][data-action='removeAlert']`)
					btn && run(removeAlert, btn, ev)
				} else if (currentPage.startsWith('exchange-')) {
					const exchange = broker.getByAlias(currentPage.substr(9).toUpperCase())
					run(removeAccount.bind(exchange, document.activeElement, ev))
				}
				break

			case 's':
				ev.preventDefault(), ev.stopPropagation()
				if(currentPage === 'alerts') {
					const el = document.activeElement
					el && el.blur()
					run(saveAlerts)
					el && el.focus()
				} else if (currentPage.startsWith('exchange-')) {
					const exchange = broker.getByAlias(currentPage.substr(9).toUpperCase())
					run(saveAccounts.bind(exchange))
				}
				break

			case 'u':
				ev.preventDefault(), ev.stopPropagation()
				if(currentPage === 'positions') {
					run(updatePos, q("#positions .panel:not(.hide) [data-action='updatePos']"))
				}
				break

			case 'v':
				if (currentPage.startsWith('exchange-')) {
					if(document.activeElement && document.activeElement.nodeName === 'INPUT') return
					const exchange = broker.getByAlias(currentPage.substr(9).toUpperCase())
					run(pasteAccount.bind(exchange, document.activeElement))
				}
				break

//			case 'y':
//				$("#spinner").modal('toggle')
//				break
		}
	}
	if (ev.altKey && ev.key === 's' && currentPage === 'alerts') {
		ev.preventDefault(), ev.stopPropagation()
		const btn = q(`[data-alert='${currentAlert}'][data-action='lookupSymbol']`)
		btn && run(lookupSymbol, btn)
	}
}

let lastHashChange = 0
let defaultPage = 'getting-started'
let currentPage = defaultPage
let currentAlert = 0
let livePageInterval = 0
const logViewLimit = 1200
const livePageRate = 1000 / 2

const livePages = {
	'log': function(page){
		function filter(level, msg) {
			//if (level < 1 && !opt.showDebug) return true
			let filter = 0, matches = 0
			if (opt.showOnlyEvents) {
				++filter
				if(msg.includes("Received Alert")) ++matches
			}
			if (opt.showOnlyOrders) {
				++filter
				if(msg.includes("placed")) ++matches
			}
			if (opt.showOnlyWarns) {
				++filter
				if(level === 2 && !msg.startsWith("Manually")) ++matches
			}
			if (opt.showOnlyErrors) {
				++filter
				if(level === 3) ++matches
			}
			if (opt.showOnlyCond) {
				++filter
				if(level === 5 && msg.includes("Conditional")) ++matches
			}
			if (opt.showOnlyBalance) {
				++filter
				if(msg.includes("available balance")) ++matches
			}
			if (opt.showOnlyNotify) {
				++filter
				if(msg.includes("notification sent")) ++matches
			}
			return filter && !matches
		}
		function getHTML(events) {
			const classes = ['active', '', 'warning', 'danger', 'danger', 'info', 'success']
			let html = ''
			try {
				for (let i = 0; i < events.length; ++i) {
					const ev = events[i]
					if (!ev.d || !ev.m || (ev.l < 1 && !opt.showDebug)) continue
					html += `<tr class="${classes[ev.l]} ${filter(ev.l, ev.m)?'hide':''}" data-l="${ev.l}"><th class="nowrap">${formatDateS(ev.d, true)}</th>`+
							`<td><code class="${classes[ev.l] ? 'label-'+classes[ev.l] : ''}">${log.levelName[ev.l]}</code></td>`+
							`<td>${ev.m.replace('{\n', '<pre>').replace('\n}', '</pre>').replace(/,\n/g, '\n')}</td></tr>`
				}
			} catch(ex){}
			return html
		}
		function scrollToEnd(anim) {
			const scroll = $('#log-container')
			scroll.stop(true).animate({
				scrollTop: scroll[0].scrollHeight - scroll[0].clientHeight
			}, anim ? 200 : 0)
		}

		const logStore = log.getStore()
		let last = 0
		$('[id^=show]').on('change.log', function(el){
			if (el.currentTarget.name === 'showDebug') {
				last = Number.MAX_VALUE
				return
			}
			const table = page.querySelector('tbody')
			table.querySelectorAll('tr').forEach(row => filter(Number(row.dataset.l), row.lastElementChild.innerHTML) ? row.classList.add('hide') : row.classList.remove('hide'))
		})

		let lastCB = Date.now()
		logStore.getEventsCB(function(events){
			page.querySelector('tbody').innerHTML = getHTML(events)
			last = events.length ? events[events.length-1].id+1 : last
			opt.scrollLog && setTimeout(scrollToEnd, 100)
			lastCB = 0
		}, logStore.getNext() - logViewLimit, logViewLimit, true)

		return function(page){
			const next = logStore.getNext()
			if (next != last && Date.now() - lastCB > 1000) {
				const table = page.querySelector('tbody')
				if (next < last) {
					table.innerHTML = '', last = 0
				}

				lastCB = Date.now()
				logStore.getEventsCB(function(events){
					table.insertAdjacentHTML('beforeend', getHTML(events))
					last = events.length ? events[events.length-1].id+1 : last

					while (table.childElementCount > logViewLimit) {
						table.removeChild(table.firstChild)
					}
					opt.scrollLog && scrollToEnd(true)
					lastCB = 0
				}, Math.max(last, next - logViewLimit), logViewLimit)
			}
		}
	},

	'alerts': function(page){
		function update(page) {
			if (Math.max(bg.tvLastEvent, bg.autoLastEvent) > state.initAlerts) {
				const last = state.initAlerts
				state.initAlerts = Math.max(bg.tvLastEvent, bg.autoLastEvent)
				const isAuto = bg.autoLastEvent > bg.tvLastEvent

				function checkDate(alert, what) {
					const date = new Date(alert[what])
					if (date > last) {
						const el = page.querySelector(`#alert-${alert.id}-${what}`)
						if (el) {
							el.classList.remove('updated')
							void el.offsetWidth
							el.classList.add('updated')
							const num = alert[what+'Num']
							el.innerHTML = formatDateS(date) + (num ? `<span class="numicon"></span>`+num : '')
						}
						return true
					}
					return false
				}
				function checkLock(alert) {
					const el = page.querySelector(`[data-alert='${alert.id}'][data-action='showLock']`)
					el && el.classList[alert.locks ? 'add' : 'remove']('alert-locked')
				}
				function checkStats(alert) {
					const el = page.querySelector(`#alert-${alert.id}-stats`)
					const stats = alertStats(alert)
					if (el && el.innerHTML != stats) {
						el.classList.remove('updated')
						void el.offsetWidth
						el.classList.add('updated')
						el.innerHTML = stats
						return true
					}
					return false
				}

				const alerts = getAlerts()
				let updated = 0
				for (const alert of alerts) {
					let changed = checkDate(alert, 'recv')
					changed = checkDate(alert, 'fired') || changed
					checkLock(alert)
					if (changed || isAuto) {
						changed = checkStats(alert) || changed
					}
					changed && ++updated
				}
				if (updated && !(["custom", "name", "created"].includes(opt.sort))) {
					filterChanged()
				}
			}
		}
		page.querySelectorAll('label').forEach(el => el.classList.remove('updated'))
		update(page)

		currentAlert && setTimeout(function(){
			try {
				const el = isAlertMin(currentAlert) ? page.querySelector(`#alert-${currentAlert}-names`) : 
					page.querySelector(`#alert-${currentAlert}-commands`).nextSibling.firstChild
				el && el.focus()
			} catch(ex){}
		}, 100)
		return update
	},

	'positions': function(page){
		function update(page) {
			if (opt.posAuto && opt.posRate != 0 && onInterval(opt.posRate * 1000, livePageRate, q("#posRateInd"))) {
				run(updatePos)
			} else if (onInterval(3000, livePageRate)) {
				qa("[name=positions] [data-since]").forEach(el => {
					const since = (Date.now() - Number(el.dataset.since)) / 1000
					el.innerHTML = since > 2 ? `(${formatDur(since)} ago)` : ''
				})
			}
		}
		filterPosChanged(), update()
		return update
	},

	'stats': function(page){
		function update(page) {
			let html = ''
			function start() {
				html = "<thead><tr><th>Metric</th><th>Current Session</th><th>Total / Previous</th></tr></thead>"
			}
			function add(name = ' ', session = '', total = '') {
				html += `<tr><td>${name}</td><td>${session}</td><td>${total}</td></tr>`
			}

			try {
				start()
				add("Startup Time", formatDate(ls.statBootup), formatDate(ls.statPrevBootup))
				add("⤷ Instances / First Startup", ls.statInstances, formatDate(ls.statBootupFirst))
				add("⤷ Updates / Startups", ls.statUpdates||0, ls.statBootups)
				add("⤷ Last Update", formatDate(ls.statLastUpdate) + (ls.statLastVersion ? " ("+ls.statLastVersion+")" : ""), formatDate(ls.statPrevUpdate) + (ls.statPrevVersion ? " ("+ls.statPrevVersion+")" : ""))
				add("⤷ Restarts / Auto-Restarts", ls.statRestarts||0, ls.statAutoRestarts||0)
				add("Current Uptime", formatDur(ls.statUptime, -2), formatDur(ls.statUptimeLast, -2))
				add("⤷ Average / Total Uptime", formatDur(ls.statUptimeAvg, -2, 0), formatDur(ls.statUptimeTotal, -2, 0))
				add("⤷ Shortest / Longest Uptime", formatDur(ls.statUptimeMin, -2, Number.MAX_VALUE), formatDur(ls.statUptimeMax, -2, 0))
				add("Last Downtime", formatDur(ls.statDowntime, -2), formatDur(ls.statDowntimeLast, -2))
				add("⤷ Average / Total Downtime", formatDur(ls.statDowntimeAvg, -2, 0), formatDur(ls.statDowntimeTotal, -2, 0))
				add("⤷ Shortest / Longest Downtime", formatDur(ls.statDowntimeMin, -2, Number.MAX_VALUE), formatDur(ls.statDowntimeMax, -2, 0))
				add("Remote Config Updates (Sync)", ls.statSessionSync||0, ls.statTotalSync||0)
				add("⤷ Last Remote Config Update", formatDate(ls.statLastSync), formatDate(ls.statPrevSync))
				add("Last Heartbeat", formatDate(ls.statHeartbeat), formatDate(ls.statPrevHeartbeat))
				add("Log Writing Errors", ls.statSessionLogErr||0, ls.statTotalLogErr||0)
				add("Ping Errors", ls.statSessionPingErr||0, ls.statTotalPingErr||0)
			} catch(ex){}
			page.querySelector("#general-stats").innerHTML = html
			try {
				start()
				add("Received Events (Listener)", ls.statSessionEvents||0, ls.statTotalEvents||0)
				add("Received Events (Stream)", ls.statSessionStreamEvents||0, ls.statTotalStreamEvents||0)
				add("Received Screener Alerts (Listener)", ls.statSessionScreenerEvents||0, ls.statTotalScreenerEvents||0)
				add("Received Screener Alerts (Stream)", ls.statSessionScreenerStreamEvents||0, ls.statTotalScreenerStreamEvents||0)
				add("Received Events (Auto Interval)", ls.statSessionAuto||0, ls.statTotalAuto||0)
				add("Received Events (TelegramBot)", ls.statSessionTelebot||0, ls.statTotalTelebot||0)
				add("Temp. Blocked (Auto Interval)", ls.statSessionAutoSkip||0, ls.statTotalAutoSkip||0)
				add("Scheduler Misses (Auto Interval)", ls.statSessionAutoMiss||0, ls.statTotalAutoMiss||0)
				add("⤷ Average Slippage", formatDur(ls.statSessionOffsetAvg, 2), formatDur(ls.statTotalOffsetAvg, 2))
				add("⤷ Shortest Slippage", formatDur(ls.statSessionOffsetMin, 2), formatDur(ls.statTotalOffsetMin, 2))
				add("⤷ Longest Slippage", formatDur(ls.statSessionOffsetMax, 2), formatDur(ls.statTotalOffsetMax, 2))
				add("Filtered - Alert Disabled", ls.statSessionFilterEn||0, ls.statTotalFilterEn||0)
				add("Filtered - Unscheduled", ls.statSessionFilterSch||0, ls.statTotalFilterSch||0)
				add("Filtered - Position", ls.statSessionFilter||0, ls.statTotalFilter||0)
				add("Filtered - Once / Solo", ls.statSessionFilterOnce||0, ls.statTotalFilterOnce||0)
				add("Not Filtered - Name Diff", ls.statSessionFilterRepName||0, ls.statTotalFilterRepName||0)
				add("Not Filtered - Rep Timeout", ls.statSessionFilterRepRes||0, ls.statTotalFilterRepRes||0)
				add("Filtered - Repeat", ls.statSessionFilterRep||0, ls.statTotalFilterRep||0)
				add("Filtered - Timeout", ls.statSessionFilterTime||0, ls.statTotalFilterTime||0)
				add("Filtered - Lock", ls.statSessionFilterLock||0, ls.statTotalFilterLock||0)
				add("Legacy Filtered - Position", ls.statSessionFilterLegacy||0, ls.statTotalFilterLegacy||0)
				add("Legacy Filtered - Once / Solo", ls.statSessionFilterLegacyOnce||0, ls.statTotalFilterLegacyOnce||0)
				add("Legacy Filtered - Repeat", ls.statSessionFilterLegacyRep||0, ls.statTotalFilterLegacyRep||0)
				add("Legacy Filtered - Timeout", ls.statSessionFilterLegacyTime||0, ls.statTotalFilterLegacyTime||0)
			} catch(ex){}
			page.querySelector("#event-stats").innerHTML = html
			try {
				start()
				add("TelegramBot Messages", ls.statSessionTelebotMsg||0, ls.statTotalTelebotMsg||0)
				add("⤷ Number of Errors", ls.statSessionTelebotErr||0, ls.statTotalTelebotErr||0)
				add("⤷ Last Message", formatDate(ls.statLastTelebotMsg), formatDate(ls.statPrevTelebotMsg))
				add("⤷ Last Error", formatDate(ls.statLastTelebotErr), formatDate(ls.statPrevTelebotErr))
				add("TelegramBot Starts", ls.statSessionTelebotStart||0, ls.statTotalTelebotStart||0)
				add("⤷ Last Start", formatDate(ls.statLastTelebotStart), formatDate(ls.statPrevTelebotStart))
				add("TelegramBot Stops", ls.statSessionTelebotStop||0, ls.statTotalTelebotStop||0)
				add("⤷ Last Stop", formatDate(ls.statLastTelebotStop), formatDate(ls.statPrevTelebotStop))
			} catch(ex){}
			page.querySelector("#bot-stats").innerHTML = html
			try {
				start()
				add("Running Alerts", bg.statRunning)
				add("Max Alerts in Parallel", ls.statSessionPar||0, ls.statTotalPar||0)
				add("Skipped - Too many Parallel", ls.statSessionMaxPar||0, ls.statTotalMaxPar||0)
				add("Skipped - Too much Slippage", ls.statSessionMaxTime||0, ls.statTotalMaxTime||0)
				add("Time of Last Command", formatDate(ls.statLastCmd), formatDate(ls.statPrevCmd))
				add("Total Number of Run Alerts", ls.statSessionAlerts||0, ls.statTotalAlerts||0)
				add("⤷ TradingView Alerts", (ls.statSessionAlerts||0) - (ls.statSessionChildren||0) - (ls.statSessionManual||0) - (ls.statSessionAuto||0) - (ls.statSessionTeleRun||0) - (ls.statSessionTrigger||0), 
					(ls.statTotalAlerts||0) - (ls.statTotalChildren||0) - (ls.statTotalManual||0) - (ls.statTotalAuto||0) - (ls.statTotalTeleRun||0) - (ls.statTotalTrigger||0))
				add("⤷ Child Alerts", ls.statSessionChildren||0, ls.statTotalChildren||0)
				add("⤷ Manual Runs", ls.statSessionManual||0, ls.statTotalManual||0)
				add("⤷ Interval Runs", ls.statSessionAuto||0, ls.statTotalAuto||0)
				add("⤷ TelegramBot", ls.statSessionTeleRun||0, ls.statTotalTeleRun||0)
				add("⤷ Triggered", ls.statSessionTrigger||0, ls.statTotalTrigger||0)
				add("⤷ Empty Alerts", ls.statSessionAlertsEmpty||0, ls.statTotalAlertsEmpty||0)
				add("Number of Forked Alerts", ls.statSessionForked||0, ls.statTotalForked||0)
				add("Total Command Runtime", formatDur(ls.statSessionTime, -2), formatDur(ls.statTotalTime, -2))
				add("⤷ Average Runtime", formatDur((ls.statSessionTime||0) / (Number(ls.statSessionAlerts) || 1), 2, 0), formatDur((ls.statTotalTime||0) / (Number(ls.statTotalAlerts) || 1), 2, 0))
				add("⤷ Shortest Runtime", formatDur(ls.statSessionTimeMin, 2), formatDur(ls.statTotalTimeMin, 2))
				add("⤷ Longest Runtime", formatDur(ls.statSessionTimeMax, 2), formatDur(ls.statTotalTimeMax, 2))
				add("Total Number of Run Commands", ls.statSessionCommands||0, ls.statTotalCommands||0)
				add("⤷ Average Commands per Alert", ((ls.statSessionCommands||0) / (ls.statSessionAlerts || 1)).toFixed(1), ((ls.statTotalCommands||0) / (ls.statTotalAlerts || 1)).toFixed(1))
				add("Number of Conditionals", ls.statSessionCond||0, ls.statTotalCond||0)
				add("Number of Errors", ls.statSessionErr||0, ls.statTotalErr||0)
				add("Number of Warnings", ls.statSessionWarn||0, ls.statTotalWarn||0)
				add("Number of Retries", ls.statSessionRetries||0, ls.statTotalRetries||0)
				add("Number of Maximum Retries", ls.statSessionRetriesMax||0, ls.statTotalRetriesMax||0)
			} catch(ex){}
			page.querySelector("#alert-stats").innerHTML = html
			try {
				start()
				add("Email Notifications sent", ls.statEmail||0, ls.statTotalEmail||0)
				add("⤷ Number of Errors", ls.statEmailErr||0, ls.statTotalEmailErr||0)
				add("⤷ Number of Auth Retries", ls.statEmailAuth||0, ls.statTotalEmailAuth||0)
				add("⤷ Last Message", formatDate(ls.statEmailLast), formatDate(ls.statPrevEmailLast))
				add("Discord Notifications sent", ls.statDiscord||0, ls.statTotalDiscord||0)
				add("⤷ Number of Errors", ls.statDiscordErr||0, ls.statTotalDiscordErr||0)
				add("⤷ Last Message", formatDate(ls.statDiscordLast), formatDate(ls.statPrevDiscordLast))
				add("Telegram Notifications sent", ls.statTelegram||0, ls.statTotalTelegram||0)
				add("⤷ Number of Errors", ls.statTelegramErr||0, ls.statTotalTelegramErr||0)
				add("⤷ Last Message", formatDate(ls.statTelegramLast), formatDate(ls.statPrevTelegramLast))
				add("Twilio Notifications sent", ls.statTwilio||0, ls.statTotalTwilio||0)
				add("⤷ Number of Errors", ls.statTwilioErr||0, ls.statTotalTwilioErr||0)
				add("⤷ Last Message", formatDate(ls.statTwilioLast), formatDate(ls.statPrevTwilioLast))
				add("IFTTT Notifications sent", ls.statIFTTT||0, ls.statTotalIFTTT||0)
				add("⤷ Number of Errors", ls.statIFTTTErr||0, ls.statTotalIFTTTErr||0)
				add("⤷ Last Message", formatDate(ls.statIFTTTLast), formatDate(ls.statPrevIFTTTLast))
			} catch(ex){}
			page.querySelector("#notification-stats").innerHTML = html
			try {
				start()
				add("Last TradingView Ping", formatDate(bg.tvLastPing), formatDate(ls.statPrevPing))
				add("Last TradingView Event", formatDate(bg.tvLastEvent), formatDate(ls.statPrevEvent))
				add("TradingView Tab Reloads", ls.statSessionTab||0, ls.statTotalTab||0)
				add("TradingView Timeouts ("+opt.timeoutTVmin+"min)", ls.statSessionTimeout||0, ls.statTotalTimeout||0)
				add("⤷ Last Timeout", formatDate(ls.statLastTimeout), formatDate(ls.statPrevTimeout))
				add("TradingView Listener Stalls", ls.statSessionStall||0, ls.statTotalStall||0)
				add("⤷ Last Listener Stall", formatDate(ls.statLastStall), formatDate(ls.statPrevStall))
				add("TradingView Listener Restores", ls.statSessionRest||0, ls.statTotalRest||0)
				add("⤷ Last Listener Restore", formatDate(ls.statLastRest), formatDate(ls.statPrevRest))
				add("⤷ Last Listener Downtime", formatDur(ls.statLastDown, -2, 0), formatDur(ls.statPrevDown, -2, 0))
				add("⤷ Shortest Listener Downtime", formatDur(ls.statSessionDownMin, -2, Number.MAX_VALUE), formatDur(ls.statTotalDownMin, -2, Number.MAX_VALUE))
				add("⤷ Longest Listener Downtime", formatDur(ls.statSessionDownMax, -2, 0), formatDur(ls.statTotalDownMax, -2, 0))
				add("TradingView Stream Token Loss", ls.statSessionStreamToken||0, ls.statTotalStreamToken||0)
				add("⤷ Last Stream Token Loss", formatDate(ls.statLastStreamToken), formatDate(ls.statPrevStreamToken))
				add("TradingView Stream Stalls", ls.statSessionStreamStall||0, ls.statTotalStreamStall||0)
				add("⤷ Last Stream Stall", formatDate(ls.statLastStreamStall), formatDate(ls.statPrevStreamStall))
				add("TradingView Stream Restores", ls.statSessionStreamRest||0, ls.statTotalStreamRest||0)
				add("⤷ Last Stream Restore", formatDate(ls.statLastStreamRest), formatDate(ls.statPrevStreamRest))
				add("⤷ Last Stream Downtime", formatDur(ls.statLastStreamDown, -2, 0), formatDur(ls.statPrevStreamDown, -2, 0))
				add("⤷ Shortest Stream Downtime", formatDur(ls.statSessionStreamDownMin, -2, Number.MAX_VALUE), formatDur(ls.statTotalStreamDownMin, -2, Number.MAX_VALUE))
				add("⤷ Longest Stream Downtime", formatDur(ls.statSessionStreamDownMax, -2, 0), formatDur(ls.statTotalStreamDownMax, -2, 0))
			} catch(ex){}
			page.querySelector("#tradingview-stats").innerHTML = html
		}
		update(page)
		return update
	}
}

function* importConfig() {
	if (yield yesno.bind(this,
		`<b>!!!    WARNING     !!!     WARNING     !!!     WARNING     !!!     WARNING     !!!     WARNING     !!!</b><br/>
		<br/>
		<u><b>Please note:</b></u> By importing a previously saved configuration all your current existing configuration data (API keys, Alerts etc.) will be <b>OVERWRITTEN</b> and this action <b>cannot be undone</b>!<br/>
		<br/>
		Are you sure you want to continue?<br/>
		<br/>
		<b>!!!    WARNING     !!!     WARNING     !!!     WARNING     !!!     WARNING     !!!     WARNING     !!!</b>`)) {

		let config = yield openFile.bind(this, ".json")
		if (config === null) {
			toastr.error("<b>Aborted</b> – no configuration data was imported!")
			return
		}
		try {
			if (config === "") {
				throw new Error("Empty or unreadable file")
			}
			config = JSON.parse(config)
			const check = simpleHash(config.description)
			if ((check !== 155265370 && check !== 264133116) || !config.version) {
				throw new Error("Required fields missing")
			}
		} catch(ex) {
			error(`<u><b>Error:</b></u> The selected file does not seem to be containing ${pvName} configuration data!<br/>
				<br/>
				(Reason: ${ex.message})`)
			return
		}

		if (config.exchanges) {
			let rem = new Permissions(0)
			let add = new Permissions(0)
			let removed = 0
			let added = 0
			const exes = broker.getAll()
			for (const ex of exes) {
				if (!config.exchanges.includes(ex.getAlias())) {
					ex.hasPermission() && rem.grant(ex.getPermissions()) && ++removed
				} else {
					!ex.hasPermission() && add.grant(ex.getPermissions()) && ++added
				}
			}
			rem.filter(perm => !perm.startsWith("webRequest"))

			if (rem.length() || add.length()) {
				if (yield yesno.bind(this,
					`<b><u>Note:</u></b> If additional Exchanges were activated in the saved configuration, you may have to confirm their access permissions following this dialog!<br/>
					<br/>Do you want to continue?`)) {

					if (rem.length() && (yield removePerms.bind(this, rem.get()))) {
						perms.revoke(rem).revoke(getLegacyPermissions(rem.get(), true))
						log.warn("Permission removed: "+rem.origins())
						bg.exNum -= removed
					}
					if (add.length() && (yield requestPerms.bind(this, add.get()))) {
						perms.grant(add)
						log.success("Permission granted: "+add.origins())
						bg.exNum += added
					}
					perms.save()
				} else {
					toastr.error("<b>Aborted</b> – no configuration data was imported!")
					return
				}
			}
		}
		syncStore.set(Object.filter(config, key => key.startsWith('options_')))
		if (config.options) {
			opt = Object.assign(opt, config.options)
			upgradeOptions()
			saveOptions()
		}
		if (config.accounts) {
			broker.setAccounts(config.accounts)
			bg.accountNum = 0
			Object.each(config.accounts, ex => Object.each(config.accounts[ex], acc => ++bg.accountNum))
		}
		if (config.balances) {
			localStore.get().balances = config.balances
			localStore.updated()
		}
		if (config.positions) {
			localStore.get().positions = config.positions
			localStore.updated()
		}
		if (config.alerts && config.alerts.length) {
			for (const alert of config.alerts) {
				delete alert.locks
				config["alert_"+alert.id] = alert
			}
			localStore.remove('lastAlert', 'lock')
		}
		let alerts = 0
		const store = opt.alertLocal ? localStore : syncStore
		const noStore = opt.alertLocal ? syncStore : localStore
		store.set(Object.filter(config, key => key.startsWith('alert_') && ++alerts))
		if (alerts) {
			store.set('hasAlerts', true)
			noStore.set('hasAlerts', false)
		}
		getAlerts().forEach(upgradeAlert)

		initPermissions()
		initExchanges()
		initAlerts()

		log.notice("Imported saved configuration!")
		toastr.success("Successfully imported the configuration!")
	}
}

function doConfigExport(autoInt) {
	let exchanges = []
	broker.getAll().forEach(ex => ex.hasPermission() && exchanges.push(ex.getAlias()))

	let config = Object.assign({
			description: pvName+' Configuration File',
			version: pvVersion,
			exported: new Date().toISOString(),
			options: Object.without(opt, ['crc', 'sum']),
			exchanges,
			accounts: broker.getAccounts(),
			balances: localStore.get().balances,
			positions: localStore.get().positions,
		},
		Object.filter(syncStore.get(), key => key.startsWith('options_')),
		Object.filter(localStore.get('hasAlerts') ? localStore.get() : syncStore.get(), key => key.startsWith('alert_')))

	downloadAs(stringifyPretty(config), `${pvName.toLowerCase()}${opt.instName ? '-'+opt.instName : ''}-%d-config.json`)

	log.notice((autoInt ? `[Auto Export => ${autoInt}] `:'') + "Exported current configuration!")
	!autoInt && toastr.success("Successfully exported the current configuration!")
}

function* exportConfig(el, ev) {
	if (ev && ev.ctrlKey || (yield yesno.bind(this,
		`<b><u>Please note:</u></b> Make sure to store the exported configuration <b>securely</b> as it contains all your current configured <b>private API keys</b>!<br/>
		<br/>
		Do you still want to export the configuration?`))) {
		doConfigExport()
	}
}

function* saveLog(doAll, autoInt, events) {
	if (doAll) {
		showSpinner('Formatting log...')
		yield* sleep(0.3)
	}

	const level = 0 //opt.showDebug ? 0 : 1
	const chunkSize = 10 * 1024 * 1024
	let chunkNum = 1
	let text = ""
	let lastId = 0

	try {
		for (let i = 0; i < events.length; ++i) {
			const ev = events[i]
			if (!ev.d || !ev.m || ev.l < level) {
				continue
			}
			lastId = ev.id
			text += formatDate(ev.d, true)+' '+log.levelName[ev.l]+' '+
				ev.m.replace('{\n','\n').replace('\n}','').replace(/,\n/g, '\n').replace(/\n/g, newLine)+newLine

			if (text.length >= chunkSize) {
				downloadAs(text, `${pvName.toLowerCase()}-%d-${chunkNum++}.log`)
				text = ""
				yield* sleep(0.3)
			}
		}
	} catch(ex){}

	downloadAs(text, `${pvName.toLowerCase()}-%d${chunkNum > 1 ? '-'+chunkNum : ''}.log`)
	hideSpinner()
	if (autoInt) {
		ls.plogAuto = lastId + 1
		log.notice(`[Auto Export => ${autoInt}] Exported app log! (${events.length} entries)`)
	}
}

function doLogExport(doAll, autoInt) {
	const logStore = log.getStore()
	const fromId = autoInt && opt.expLogInc ? ls.plogAuto || 0 : (doAll ? 0 : logStore.getNext() - logViewLimit)
	logStore.getEventsCB(run.bind(this, saveLog, doAll, autoInt), fromId, doAll ? 0 : logViewLimit, true)
}

function* exportLog(el, ev) {
	let doAll = ev && ev.ctrlKey ? 1 : (yield onetwo.bind(this, `Do you want to export the <b>whole log</b> or only the part currently loaded in the <b>log viewer</b>?<br/>
		<br/>
		<u>Note:</u> If you have a long log history this might take a bit, also the log will be split up in chunks of 10MB in size!`, "Whole Log", "In Viewer"))

	if (doAll) {
		doLogExport(doAll === 1)
	}
}

function* clearLog() {
	if (yield yesno.bind(this,
		`Do you really want to purge the log?<br/>
		<br/>
		<u>Note:</u> This action cannot be undone!`)) {

		log.getStore().clear()
		ls.plogAuto = 0
	}
}

function* restartApp(el, ev) {
	if (ev && ev.ctrlKey || (yield yesno.bind(this,
		`Are you sure you want to force a restart of the app?<br/>
		<br/>
		<u>Note:</u> Depending on your general options this can lead to reloading of all TradingView tabs, interruption of currently running commands and <b>loss of unsaved changes</b>!`))) {

		bg.restart(true)
	}
}

function onResize() {
	ls.zoomLevel = window.devicePixelRatio.toFixed(2)
	changeCSS("#exchange-links", `height:calc(100vh - ${jQuery("#exchange-links").offset().top + 20}px)`, "@media(min-width:768px)")
	changeCSS("#log-container", `height:calc(100vh - ${jQuery("#log-container").offset().top + 25}px)`)
	changeCSS("#alerts .scroll-container", `height:calc(100vh - ${jQuery("#alerts .scroll-container").offset().top + 50}px)`)
	changeCSS("#positions .scroll-container", `height:calc(100vh - ${jQuery("#positions .scroll-container").offset().top + 20}px)`)
}

function openPage(id, fromHash) {
	if (!id || id.length < 3) {
		id = defaultPage
	}
	id = id.split('?')[0]
	const page = q(`article[id=${id}]`)
	if (!page) return

	const pages = qa('article')
	if (pages.length < 1) {
		throw new ReferenceError("No pages found!")
	}
	pages.forEach(el => el.classList.add('hide'))
	clearInterval(livePageInterval)

	const livePage = livePages[id] && livePages[id](page)
	if (livePage) {
		livePageInterval = setInterval(livePage.bind(window, page), livePageRate)
	}
	page.classList.remove('hide')

	qa('ul.nav li').forEach(el => el.classList.remove('active'))
	const nav = q(`.nav [data-page=${id}]`)
	nav.parentNode.classList.add('active')

	if (!fromHash) {
		lastHashChange = Date.now()
		window.location.hash = (id != defaultPage) ? '/'+id : ''
	}
	currentPage = id
	onResize()
}

window.addEventListener('hashchange', function(){
	if (Date.now() - lastHashChange > 300) {
		openPage(window.location.hash.substr(2), true)
	}
})

function enableBtn(name, state = true) {
	qa(`input[name=${name}]`).forEach(el => el.disabled = !state)
	qa(`label[for=${name}]`).forEach(el => el.classList[state ? 'remove' : 'add']('disabled'))
}

function setButton(item, state) {
	if (typeof item === 'string') item = q('#'+item)
	const on = item.querySelector(state ? ".btn-on" : ".btn-off")
	const off = item.querySelector(state ? ".btn-off" : ".btn-on")
	if (on && off) {
		on.classList.add('active'), on.classList.add('pv-act'), on.firstElementChild.checked = true
		off.classList.remove('active'), off.classList.remove('pv-act'), off.firstElementChild.checked = false
	}
}

function setRadio(item, value) {
	if (typeof item === 'string') item = q('#'+item)
	const act = item.querySelector(`input[value="${value}"]`)
	if (act) {
		item.querySelectorAll('label.active').forEach(el => el.classList.remove('active'))
		act.checked = true
		act.parentElement.classList.add('active')
	}
}

function initPermissions() {
	const exPerms = qi("perm-exchanges")
	if (exPerms.childElementCount < 2 && broker) {
		const exes = broker.getAll()
		for (const ex of exes) {
			new Template("#perm-exchange", null, {
				"exchange.alias": ex.getAlias(),
				"exchange.id": ex.getAlias().toLowerCase(),
				"exchange.name": ex.getName(),
				"exchange.url": ex.getWebsite(),
				"exchange.desc": ex.getDescription()
			}).render()
		}
	}

	const items = exPerms.querySelectorAll("li.list-group-item")
	for (const item of items) {
		const ex = broker.getByAlias(item.dataset.exchange)
		const state = ex && ex.hasPermission()
		item.classList[state ? 'add' : 'remove']('success')
		setButton(item, state)
	}

	for (const addon in addons) {
		const state = perms.hasAny(getLegacyPermissions(addons[addon], true))
		const panel = q(`[data-addon='${addon}']`).closest(".panel")
		panel.classList[state ? 'remove' : 'add']('disabled')
		setButton(panel, state)
	}
	ls.authToken && updateAuthToken(false)
}

function* requestPermission(el) {
	if (el.className.includes("pv-act")) return
	const data = el.dataset

	if (data.addon && qa("[name=license-remove].hide").length > 1) {
		return error(`Sending ${data.addon} notifications is unfortunately a <b>${pvName} PRO only</b> feature at this time!`)
	}
	if (data.addon === 'Email') {
		return updateAuthToken(true)
	}
	const add = new Permissions(addons[data.addon] || this.getPermissions())
	let granted = null
	try {
		granted = yield requestPerms.bind(this, add.get())
	} catch(ex) {
		initPermissions()
		return error(ex.message)
	}

	if (granted) {
		perms.grant(add).save()
		log.success("Permission granted: "+add.origins())
		!data.addon && ++bg.exNum
	}
	initPermissions()
	initExchanges()
}

function* removePermission(el) {
	if (el.className.includes("pv-act")) return
	const data = el.dataset
	if (data.addon === 'Email') {
		yield ok.bind(this, `To remove the permission to send emails in your name, simply revoke
			the access for ${pvName} on the following Google Account permissions page!<br/>
			<br/>
			Afterwards you have to refresh/restart ${pvName} to reflect this change.`)
		initPermissions()
		return window.open("https://myaccount.google.com/permissions")
	}

	let msg = `Are you sure you want to <b>DISABLE</b> ${data.addon || this.getName()}?`
	if (!data.addon) {
		msg += `<br/>
			<br/>
			<u>Note:</u> All stored accounts will be <b>deleted</b>!`
	}

	if (yield yesno.bind(this, msg)) {
		const rem = new Permissions(
			(data.addon === 'Discord') ? discordAccess : (data.addon === 'Telegram') ? telegramAccess : 
			(data.addon === 'Twilio') ? twilioAccess : (data.addon === 'IFTTT') ? iftttAccess : 
			this.getPermissions()).filter(perm => !perm.startsWith("webRequest"))

		if (yield removePerms.bind(this, rem.get())) {
			perms.revoke(rem).revoke(getLegacyPermissions(rem.get(), true)).save()
			if (!data.addon) {
				this.removeAccounts()
				--bg.exNum
			}
			log.warn("Permission removed: "+rem.origins())
		}
		initPermissions()
		initExchanges()
	} else {
		initPermissions()
	}
}

function* cancelLicense(el) {
	const dialog = el.closest(".modal")
	$('#'+dialog.id).modal("hide")
}

function* startLicense(el) {
	const data = el.dataset
	if (!data || !data.originalTitle) return
	const product = data.originalTitle.split(' ').slice(1).join(' ')
	const monthly = product.includes("monthly")

	if (!bg.ID) {
		return yield error.bind(this, "To register you have to enable Google Sync in the browser options (Settings / Sync) "+
			"and make sure you are logged in to the correct Google account!")
	}

	const id = Math.floor(Date.now() / 1000)
	new Template("#exchange-license", null, {
		"product.id": id,
		"product.name": product,
		"product.account": bg.ID,
		"product.type": monthly ? "Monthly" : "Onetime",
		"product.url": _(`aHR0cHM6Ly9wcm9maXR2aWV3LmFwcC9idXk${monthly?"vbW9udGhseQ":''}`)
	}).render()
	$(`#license-${id}`).on('hidden.bs.modal', function(){
		this.parentNode.removeChild(this)
	}).modal({
		backdrop: "static"
	})

	$(`#license-${id} .glyphicon-copy`).click(function(ev){
		const data = ev.target.dataset
		data.clipboard && clipboardWrite("text/plain", data.clipboard)
	})
}

function initLicense() {
	if (q("#license-exchanges tbody").childElementCount < 2 && broker) {
		const exes = broker.getAll()
		for (const ex of exes) {
			const alias = ex.getAlias()
			new Template("#exchange-lic", null, {
				"exchange.alias": alias,
				"exchange.id": alias.toLowerCase(),
				"exchange.name": ex.getName(),
				"exchange.url": ex.getWebsite(),
				"exchange.desc": ex.getDescription(),
				"exchange.sub": ex.getSubscriptions().inactive.length
			}).render()
		}
	}

	const page = qi("license")
	let row = localStore.get(_('cGVybXM'), _('Y3Jj'))
	let col = 0
	let tmp = window.spice || bg.spice
	let mods = 0

	for (const mod of page.querySelectorAll("table thead .panel")) {
		if (!mod.dataset.package) continue
		++col
		if ((perms.hasAny(Packages.get(mod.dataset.package)) && !row) || (row && col > 2)) {
			mod.classList.add("panel-success")
			mod.classList.remove("panel-default")
			mod.querySelector("[name='license-start']").classList.add("hide")
			mod.querySelector("[name='licensed']").classList.remove("hide")
			if (col > 2) {
				mod.querySelector("[name='licensed']").classList.add("btn-success")
				mod.querySelector("[name='licensed']").disabled = false
				mod.querySelector("[name='licensed']").innerText = "Exp. "+(tmp ? formatDate(tmp).substr(0, 16) : "1 month")
				++mods
			}
			page.querySelectorAll(`.panel table tbody tr td:nth-of-type(${col})`).forEach(el => el.classList.add('success'))
			++mods
		} else if (col > 1) {
			mod.classList.remove("panel-success")
			mod.classList.add("panel-default")
			mod.querySelector("[name='license-start']").classList.remove("hide")
			mod.querySelector("[name='licensed']").classList.add("hide")
			if (mods && col > 2) {
				mod.querySelector("[name='license-start']").classList.add("hide")
				mod.querySelector("[name='licensed']").classList.remove("hide")
				mod.querySelector("[name='licensed']").disabled = true
				mod.querySelector("[name='licensed']").innerHTML = `<span class="glyphicon glyphicon-check"></span> Licensed`
			}
			page.querySelectorAll(`.panel table tbody tr td:nth-of-type(${col})`).forEach(el => el.classList.remove('success'))
		}
	}

	const p1 = _('ICZuYnNwOzxzcGFuIGNsYXNzPSJnbHlwaGljb24gZ2x5cGhpY29uLXJlbW92ZSB0ZXh0LWRhbmdlciI+PC9zcGFuPg')
	const p2 = p1.replace(_('cmVtb3Zl'), _('b2s')).replace(_('ZGFuZ2Vy'), _('c3VjY2Vzcw'))
	const p3 = perms.hasAny([_('YzJhMTdhN2FjNzlkNzU3YTZlZjA5MDkwOTJjMmExMDA')])
	qi(_('bGljZW5zZS11c2Vy')).innerHTML = bg.ID || _('bi9h')
	qi(_('bGljZW5zZS10eXBl')).innerHTML = mods ? _('UFYgMS54'+(!(window.p=p3)?'IFBSTw':'IEJVU0lORVNT')) + 
		(mods > 1 ? _('IChtb250aGx5IOKAkyA', true)+Math.max(0, (tmp - new Date()) / 86400000).toFixed(1)+_('IGRheXMgbGVmdCk') : _('IChMaWZldGltZSk')) + p2 : 
		_('VFJJQUwgKA') + (tmp && tmp.getTime() != 1586360040000 ? _('bm90IGxpY2Vuc2VkIOKAkyBtb250aGx5IGV4cGlyZWQg', true)+Math.max(0, (new Date() - tmp) / 86400000).toFixed(1)+_('IGRheXMgYWdvISk') : 
			_('bm90IGxpY2Vuc2VkISk')) + p1
	qi(_('bGljZW5zZS10dg')).innerHTML = bg.UUIDD || _('bi9h')
	if (!pvCode) {
		q(_('I2dldHRpbmctc3RhcnRlZCAucGFuZWwtbGFzdA')).style.display = q(_('W2RhdGEtcGFnZT1saWNlbnNlXQ')).style.display = "none"
		q(_('I3Blcm1pc3Npb25zIC5zY3JvbGwtY29udGFpbmVyIHA6bGFzdC1jaGlsZA')).innerHTML = q(_('I2xpY2Vuc2UgLnNjcm9sbC1jb250YWluZXIgcDpsYXN0LWNoaWxk')).innerHTML
	}
}

function alertStats(alert) {
	return `recv ${alert.recvNum||0}   fired ${alert.firedNum||0}   filt. ${Math.max((alert.recvNum||0)-(alert.firedNum||0)+(alert.autoNum||0)+(alert.intNum||0), 0)}`+
		`   auto ${alert.autoNum||0}   int. ${alert.intNum||0}   man. ${alert.manualNum||0}   err ${alert.errNum||0}   slip ${formatDur(alert.slip, -2, 0)}   /   `+
		`last ${formatDur(alert.time, -2, 0)}   avg ${formatDur(alert.timeAvg, -2, 0)}   min ${formatDur(alert.timeMin, -2, 0)}   max ${formatDur(alert.timeMax, -2, 0)}`
}

function* cloneAlert(el) {
	let alert = formData(el.closest("form"))
	alert.names = yield ask.bind(this, "Create a copy with the following name:<br/><br/>", `Clone alert "${alert.names}"`, `${alert.names} (copy)`)

	if (alert.names) {
		;["sort", ...resetProps].forEach(e => delete alert[e])
		alert.id = Date.now() - firstAlertId

		alert.names = alert.names.split(/[,;]/).map(s => s.trim()).filter(Boolean)
		alert.commands = alert.commands.split('\n')
		yield* createAlert(null, null, alert, true)
		q('[data-page=alerts]').classList.add('unsaved')
	}
}

function* createAlert(el, ev, alert, isNew) {
	isNew = isNew || alert === undefined
	if (isNew && opt.onlyOneAlert) {
		yield* minAlert("min")
	}
	if (isNew && (opt.searchName || opt.searchSyntax)) {
		opt.searchName = opt.searchSyntax = ''
		saveOptions()
		filterChanged("searchName")
	}
	alert = alert || {
		id: Date.now() - firstAlertId,
		names: [], enabled: true, parallel: false,
		barOnce: true, barSolo: false, noRepeat: false, repName: false, repReset: false, repResetMin: 180,
		barOpen: true, barStart: true, barMid1: true, barMid2: true, barEnd: true, barClose: true, barPos: 0, barPA: false, barAmount: 0,
		ignLock: false, setLock: false, clearLock: false, lockTF: true, lockOnly: false, lockExp: 0,
		auto: false, autoInt: "*", allow: false, detect: false, log: 15,
		commands: '### Commands ###\n\n'.split('\n')
	}
	alert.lockTF === undefined && (alert.lockTF = true)
	alert.lockExp === undefined && (alert.lockExp = 0)
	alert.autoInt === undefined && (alert.autoInt = "*")
	alert.log === undefined && (alert.log = alert.noLog ? 0 : 15)
	alert.names = Object.values(alert.names)
	alert.sort = alert.sort || state.alertSort + 1
	state.alertSort = Math.max(state.alertSort, alert.sort)
	const syntax = alert.commands.join('\n')

	const alertList = q("#alertlist")
	const form = new Template("alert", alertList, {
		"alert.#": alert.id,
		"alert.sort": alert.sort,
		"alert.created": formatDate(Number(alert.id) + firstAlertId),
		"alert.names": alert.names.join(', '),
		"alert.enabled": alert.enabled,
		"alert.parallel": alert.parallel,
		"alert.barOnce": alert.barOnce,
		"alert.barSolo": alert.barSolo,
		"alert.noRepeat": alert.noRepeat,
		"alert.repName": alert.repName,
		"alert.repReset": alert.repReset,
		"alert.repResetMin": alert.repResetMin,
		"alert.barOpen": alert.barOpen,
		"alert.barStart": alert.barStart,
		"alert.barMid1": alert.barMid1,
		"alert.barMid2": alert.barMid2,
		"alert.barEnd": alert.barEnd,
		"alert.barClose": alert.barClose,
		"alert.barPos": alert.barPos,
		"alert.barPA": alert.barPA,
		"alert.barAmount": alert.barAmount,
		"alert.ignLock": alert.ignLock,
		"alert.setLock": alert.setLock,
		"alert.clearLock": alert.clearLock,
		"alert.lockTF": alert.lockTF,
		"alert.lockOnly": alert.lockOnly,
		"alert.lockExp": alert.lockExp,
		"alert.auto": alert.auto,
		"alert.autoInt": alert.autoInt,
		"alert.sched": alert.sched,
		"alert.allow": alert.allow,
		"alert.detect": alert.detect,
		"alert.log": alert.log,
		"alert.locked": alert.locks,
		"alert.recv": formatDateS(alert.recv, false, '—') + (alert.recvNum ? `<span class="numicon"></span>`+alert.recvNum : ''),
		"alert.fired": formatDateS(alert.fired, false, '—') + (alert.firedNum ? `<span class="numicon"></span>`+alert.firedNum : ''),
		"alert.stats": alertStats(alert),
		"alert.commands": syntax
	}).render(isNew && !opt.sortRev && alertList.lastElementChild)

	$("button.dropdown-toggle", form).each(function(){
		if (this.dataset.value) {
			const item = this.nextElementSibling.querySelector(`[data-value='${this.dataset.value}' i]`)
			this.innerHTML = ((item && item.innerHTML) || this.dataset.value) + '<span class="caret"></span>'

			$(this.nextElementSibling).find('a').on('click.drop', function(){
				const btn = this.parentElement.parentElement.previousElementSibling
				btn.innerHTML = this.innerHTML + '<span class="caret"></span>'
				$(btn).dropdown("toggle")
				btn.dataset.value = this.dataset.value
				return false
			})
		}
	})

	const text = $('textarea', form).hide()
	const container = $('<div>', {
		class: text.attr('class'),
		id: "alert-"+alert.id+"-editor"
	}).insertAfter(text)
	const editor = ace.edit(container[0], {
		mode: "ace/mode/pv",
		theme: "ace/theme/ambiance",
		enableBasicAutocompletion: true,
		enableLiveAutocompletion: true,
		fontSize: '105%',
		showPrintMargin: false,
		minLines: 12,
		maxLines: 36,
		navigateWithinSoftTabs: true
	})
	editor.commands.bindKeys({"ctrl-shift-up": null, "ctrl-shift-down": null, "ctrl-l": null})
	editor.renderer.setScrollMargin(3, 8)
	const session = editor.getSession()
	session.setValue(syntax)
	session.on('change', function(e) {
		const syntax = session.getValue()
		text.val(syntax)

		const check = syntaxCheck(syntax, form.names.value)
		session.setAnnotations(check)
		if (check && check.length) {
			form.classList.add(check.errors ? 'err' : 'warn')
			form.classList.remove(check.errors ? 'warn' : 'err')
		} else {
			form.classList.remove('err')
			form.classList.remove('warn')
		}
		q('[data-page=alerts]').classList.add('unsaved')
	})
	editor.textInput.getElement().addEventListener('keydown', onHotkey)

	$("input", form).on('input.unsaved change.unsaved', function(ev){
		q('[data-page=alerts]').classList.add('unsaved')
	})
	$("input[name='names']", form).on('change.names', function(ev){
		const name = ev.target.value.split(/[,;]/).map(s => s.trim()).filter(Boolean).join(', ')
		ev.target.value = name === '' ? ev.target.defaultValue : name
	})
	$("input[name='enabled']", form).on('change.enabled', function(ev){
		ev.target.closest('form').classList.toggle('disabled')
	})
	$("input[name='autoInt']", form).on('change.autoInt', function(ev){
		const check = intervalMatch(ev.target.value)
		check && error(check+"<br/>"+
			"<br/>"+
			"<u>Note:</u> Please use the <code>Edit interval</code> button next to this field to safely edit the interval!")
	})
/*	$("input[name='repReset']", form).on('change.repReset', function(ev){
		this.checked && (ev.target.closest('form').querySelector("input[name='noRepeat']").checked = true)
	})
	$("input[name='noRepeat']", form).on('change.noRepeat', function(ev){
		!this.checked && (ev.target.closest('form').querySelector("input[name='repReset']").checked = false)
	})*/
	$("input[data-warn]", form).on('change.warn', function(ev){
		this.checked && ok(this.dataset.warn)
	})
	$(".panel-heading", form).on('dblclick.heading', function(ev){
		if (ev.target.classList.contains('nodrag')) {
			const selection = window.getSelection()
			const range = document.createRange()
			range.selectNodeContents(ev.target)
			selection.removeAllRanges()
			selection.addRange(range)
			return
		}
		if (ev.target.nodeName !== 'INPUT' && !ev.target.classList.contains('ace_content')) {
			run(minAlert, ev.target)
		}
	})
	$(form).on('click.alert', function(ev){
		qa("#alertlist form.selected").forEach(el => el.classList.remove('selected'))
		ev.currentTarget.classList.add('selected')
		currentAlert = ev.currentTarget.dataset.alert
	})
	$(".dropdown", form).on('show.bs.dropdown', function(ev){
		ev.target.closest('.row').style.overflow = 'visible'
	}).on('hidden.bs.dropdown', function(ev){
		ev.target.closest('.row').style.overflow = ''
	})

	if (isNew) {
		const scroll = $("#alerts .scroll-container")
		scroll.animate({
			scrollTop: opt.sortRev ? 0 : scroll[0].scrollHeight - scroll[0].clientHeight
		}, 200)

		qa("#alertlist form.selected").forEach(el => el.classList.remove('selected'))

		$("input[name='names']", form).focus()
		currentAlert = alert.id
		filterChanged("onlyActAlert")

		const current = q(`[data-alert='${currentAlert}']`)
		current && current.classList.add('selected')
	} else {
		if (isAlertMin(alert.id)) {
			form.classList.add('min')
		} else {
			currentAlert = alert.id
		}

		const check = syntaxCheck(syntax, form.names.value)
		session.setAnnotations(check)
		if (check && check.length) {
			form.classList.add(check.errors ? 'err' : 'warn')
		}
	}
	if (!alert.enabled) {
		form.classList.add('disabled')
	}
}

function initAlerts() {
	const alertList = q('#alertlist')
//	alertList.style.display = 'none'
	alertList.querySelectorAll('form').forEach(alert => alertList.removeChild(alert))

	const alerts = getAlerts().sort(alertSort)
	state.alertSort = 0
	for (const alert of alerts) {
		if (alert && alert.names) {
			run(createAlert, null, null, alert)
		}
	}
	if (!state.alertSort) {
		run(createAlert)
	} else if (currentAlert) {
		q("[data-alert='"+currentAlert+"']").classList.add('selected')
	}
	state.initAlerts = Date.now()
	hideMinimized()

	filterChanged("onlyActAlert")
	filterChanged()
	showFilterChanged()
//	alertList.style.display = ''

	dragula([alertList], {
		revertOnSpill: true,
		moves: function (el, source, handle, sibling) {
			return !["INPUT", "BUTTON", "SMALL", "A"].includes(handle.nodeName) && !handle.className.includes("ace") && 
				!handle.classList.contains("btn") && !handle.classList.contains("glyphicon") && !handle.classList.contains("nodrag")
		}
	}).on('drag', function(){
		changeCSS(".tooltip", "display:none !important")
	}).on('dragend', function(){
		$(".tooltip").tooltip("destroy")
		changeCSS(".tooltip", "")
	}).on('drop', function(){
		const forms = qa("#alertlist form")
		let sort = opt.sortRev ? forms.length+1 : 0
		forms.forEach(form => form.sort.value = opt.sortRev ? --sort : ++sort)
		opt.sort = "custom"
		saveOptions()
	})
}

function* saveAlerts() {
	const forms = qa("#alertlist form")
	let saved = []
	let total = 0

	const source = localStore.get('hasAlerts') ? localStore.get() : syncStore.get()
	const store = opt.alertLocal ? localStore.get() : syncStore.get()

	try {
		for (const form of forms) {
			let alert = formData(form)
			alert.names = alert.names.split(/[,;]/).map(s => s.trim()).filter(Boolean)

			if (alert.names.length < 1) {
				throw new SyntaxError("Each alert has to be given at least one name!")
			}
			alert.commands = alert.commands.split('\n')

			const current = source['alert_'+alert.id]
			current && resetProps.forEach(e => current[e] && (alert[e] = current[e]))

			if (!opt.alertLocal) {
				const size = JSON.stringify(alert).length+64
				if (size >= 8 * 1024) {
					throw new SyntaxError(`Alert "${alert.names.join(', ')}" exceeds the Google Sync Storage item size quota of 8kb!<br/>
						Please enable local storage for alerts under "General Options"!<br/>
						<br/>
						(nothing saved)`)
				}
				total += size
				if (total >= 75 * 1024) {
					throw new SyntaxError(`The total size of alert definitions exceed the Google Sync Storage quota of around 100kb!<br/>
						Please enable local storage for alerts under "General Options"!<br/>
						<br/>
						(nothing saved)`)
				}
			}
			store[`alert_${alert.id}`] = alert
			saved.push(alert.names.join(', '))
		}
	} catch(ex) {
		return error(ex.message)
	}

	store.hasAlerts = true
	if (source != store) {
		source.hasAlerts = false
		yield* bg.removeKey(source == localStore.get() ? 'local' : 'sync', Object.keys(source).filter(key => key.startsWith('alert_')))
	}
	opt.alertLocal ? localStore.updated() : syncStore.updated()

	toastr.success(`Saved ${forms.length} alerts successfully!`)
	log.info(`Saved ${forms.length} alerts: [${saved.join('] – [')}]`)
	opt.expConfSave && doConfigExport("Alerts changed")
	q('[data-page=alerts]').classList.remove('unsaved')
}

function* removeAlert(el) {
	const form = el.closest('form')
	const data = el.dataset
	const prompt = form.names.value ? `the alert "${form.names.value}"` : "this alert"

	if (yield yesno.bind(this,
		`Do you really want to remove ${prompt}?<br/>
		<br/>
		<u>Note:</u> This action cannot be undone!`)) {

		form.remove()
		yield* bg.removeKey("sync", `alert_${data.alert}`)
		yield* bg.removeKey("local", `alert_${data.alert}`)

		Object.each((localStore.get().lastAlert || {}).lock, (key, entry) => {
			entry.id == data.alert && localStore.remove('lastAlert', 'lock', key)
		})
		let mins = (ls.minAlerts || '').split(',')
		ls.minAlerts = mins.filter(id => id !== data.alert)
		filterChanged("onlyActAlert")
	}
}

function getEditor(id) {
	const el = q(`#alert-${id}-editor`)
	return el && ace.edit(el).getSession()
}

function* runAlert(el, ev) {
	const data = el.dataset
	const form = el.closest('form')
	const syntax = form.commands.value
	const name = form.names.value || "MANUAL"
	const prompt = form.names.value ? `the alert "${form.names.value}"` : "this alert"
	const side = form.detect.checked ? "auto" : ev.altKey ? ({long: "buy", short: "sell"})[data.side] : data.side

	if (form.classList.contains('err')) {
		const check = syntaxCheck(syntax, form.names.value)
		return error(`Syntax error found in alert on line ${check[0].row+1}:<br/>
			<br/>
			${check[0].text}`)
	}

	if (opt.runNoConfirm || (yield noyes.bind(this,
		`Do you really want to <b>${data.disabled ? "TEST" : "RUN"}</b> ${prompt} right now?<br/>
		<br/>
		<u>Warning:</u> Defaults set under 'General Options' will be used instead of TV alert data!<br/>
		<br/>
		<u>Current defaults:</u><b>${(opt.defAccount ? " account="+opt.defAccount : "")+(opt.defExchange ? " exchange="+opt.defExchange : "")+(opt.defSymbol ? " symbol="+opt.defSymbol : "")}</b>
		${side ? "<br/><u>Side placeholders:</u> <b>"+side.toUpperCase()+"</b>" : ""}`))) {

		let delay = 100
		if(opt.runShowLog ^ ev.shiftKey) {
			openPage('log')
			delay = opt.showDebug ? 500 : 250
		}
		--state.initAlerts
		setTimeout(run.bind(bg, bg.runCommandsCheck, name, {isManual: true, name: name, desc: syntax, side: side}, data.disabled, data.alert), delay)
	}
}

function isAlertMin(alert) {
	return alert && (ls.minAlerts || '').split(',').includes(alert.toString())
}

let hideMinimizedTask = 0
function hideMinimized() {
	q('#alertlist').querySelectorAll('.min .help-block, .min .filter-options, .min .panel-body').forEach(e => e.style.display = 'none')
}

function* minAlert(el) {
	clearTimeout(hideMinimizedTask)

	if (el === 'max' || (el.dataset && el.dataset.alert === 'max')) {
		q('#alertlist').querySelectorAll('.help-block, .filter-options, .panel-body').forEach(e => e.style.display = 'block')
		yield* sleep(0.05)
		qa('#alertlist form.min label.updated').forEach(e => e.classList.remove('updated'))
		qa('#alertlist form.min').forEach(e => e.classList.remove('min'))
		ls.minAlerts = ''
		return
	} else if (el === 'min' || (el.dataset && el.dataset.alert === 'min')) {
		let mins = []
		qa("#alertlist form").forEach(e => mins.push(e.dataset.alert) && e.classList.add('min'))
		hideMinimizedTask = setTimeout(hideMinimized, 1000)
		ls.minAlerts = mins
		return
	}

	const form = el.closest('form')
	const data = form.dataset
	let mins = (ls.minAlerts || '').split(',').filter(id => id.length)

	if (form.classList.contains('min')) {
		form.querySelectorAll('.help-block, .filter-options, .panel-body').forEach(e => e.style.display = 'block')
		yield* sleep(0.05)
		form.querySelectorAll('label.updated').forEach(e => e.classList.remove('updated'))
	}

	if (form.classList.toggle('min')) {
		if (getAlerts(data.alert)) {
			mins.push(data.alert)
		}
		q(`#alert-${data.alert}-names`).focus()
		hideMinimizedTask = setTimeout(hideMinimized, 1000)
	} else {
		if (opt.onlyOneAlert) {
			mins = []
			qa("form[id^='alert-']").forEach(e => e.dataset.alert !== data.alert && mins.push(e.dataset.alert) && e.classList.add('min'))
			hideMinimizedTask = setTimeout(hideMinimized, 1000)
		} else {
			mins = mins.filter(id => id !== data.alert)
		}
		q(`#alert-${data.alert}-commands`).nextSibling.firstChild.focus()
		currentAlert = data.alert
		// $$$ if MAXED and fits in view => make bottom visible / scroll
		//q(`#alert-${data.alert} .panel-body .help-block`).scrollIntoView(true)
	}
	ls.minAlerts = mins
}

function* resetRepeat(el) {
	const data = el.dataset

	if (!data.alert) {
		if (yield yesno.bind(this, "Do you really want to clear the repeat filter info for legacy alerts?")) {
			const lastAlert = localStore.get().lastAlert || {}
			lastAlert.legacy = {}
			lastAlert.legacyFired = {}
			lastAlert.legacyLast = {}
			localStore.updated()
			log.warn("Manually reset legacy repeat filter info!")
		}
	} else {
		const form = el.closest('form')
		const prompt = form.names.value ? `"${form.names.value}"` : "this alert"
		if (yield yesno.bind(this, `Do you really want to clear the repeat filter info for ${prompt}?`)) {	// $$$ add note about diff names vs repeat
			const lastAlert = localStore.get().lastAlert || {}
			Object.each(lastAlert.local, el => {
				if (lastAlert.local[el] == data.alert) {
					delete lastAlert.local[el]
					delete lastAlert.localLast[el]
				}
			})
			Object.each(lastAlert.legacy, el => {
				if (lastAlert.legacy[el] == data.alert) {
					delete lastAlert.legacy[el]
					delete lastAlert.legacyFired[data.alert]
					delete lastAlert.legacyLast[el]
				}
			})
			localStore.updated()
			log.warn(`Manually reset repeat filter info for: ${(getAlerts(data.alert) || {names:["Unknown"]}).names.join(', ')}`)
		}
	}
}

function* getChatID(el) {
	if (!q("[data-addon='Telegram'].btn-on").classList.contains("pv-act")) {
		return error(`Please allow ${pvName} to connect to Telegram first!`)
	}
	if (!opt.telegramToken) {
		return error("Please fill in the correct Bot Token first!")
	}

	if (!opt.telegramChat || (yield yesno.bind(this, "Do you really want to replace the current Chat ID with an auto detected one?"))) {
		fetch(`https://api.telegram.org/bot${opt.telegramToken}/getUpdates`).then(resp => {
			if (!resp.ok) {
				throw new Error(telegramErr(resp))
			}
			return resp.json()
		}).then(function(data){
			const chatID = (data.result[0] && data.result[0].message && data.result[0].message.chat && data.result[0].message.chat.id) || bg.telebotLastSender
			if (!chatID) {
				throw new Error('No message received yet!')
			}
			q("#telegramChat").value = opt.telegramChat = chatID
			saveOptions()
			toastr.success("Chat ID successfully detected!")
			// $$$$$ check telebot whitelist and ask to add
		}).catch(ex => {
			error(`Unable to retrieve the last active Chat ID, make sure you sent at least one message to your bot before!<br/>
				<br/>
				(Error: ${ex.message})`)
		})
	}
}

function telebotWLChanged() {
	if (opt.telebotWL) {
		opt.telebotWL = opt.telebotWL.replace(/@/g, '').split(',').numbers().filter(Boolean).join(", ")
		saveOptions()
	}
	// $$$$$ else ... check if bot enabled and warn about empty whitelist!
}

function telebotChWLChanged() {
	if (opt.telebotChWL) {
		opt.telebotChWL = opt.telebotChWL.replace(/@/g, '').split(',').numbers().filter(Boolean).join(", ")
		saveOptions()
	}
	// $$$$$ else ... check if bot enabled and warn about empty whitelist!
}

function* testNotify(el) {
	const data = el.dataset
	if (qa("[name=license-remove].hide").length > 1) {
		return error(`Sending ${data.addon} notifications is unfortunately a <b>${pvName} PRO only</b> feature at this time!`)
	}
	if (!q("[data-addon='"+data.addon+"'].btn-on").classList.contains("pv-act")) {
		return error(`Please allow ${pvName} to connect to ${data.addon} first!`)
	}

	if (yield yesno.bind(this, `Do you really want to send a test ${data.addon} notification?`)) {
		try {
			if (window[data.addon.toLowerCase()+"Message"]("test", data.addon !== "IFTTT" ? "** This is just a test! **" : null, null,
				`You should be able to see the test message now or in the next few minutes (Email / SMS)!<br/>
				<br/>
				If it doesn't show up please check the configuration again carefully and have a look at the ${pvName} log.`) === undefined) {
				throw new Error(`An error occured while trying to test ${data.addon} notifications – check "Setup: Permissions" and the license status under "Setup: Unlock PRO"!`)
			}
		} catch(ex) {
			error(ex.message)
		}
	}
}

function* resetBalances(el, ev) {
	if (ev && ev.ctrlKey || (yield yesno.bind(this, "Do you really want to purge all currently saved balance info?"))) {
		broker.purgeBalances()
		const msg = "All balance info purged!"
		log.notice(msg), toastr.success(msg)
	}
}

function doBalanceExport(format = "csv", autoInt, email) {
	const filename = `${pvName.toLowerCase()}${opt.instName ? '-'+opt.instName : ''}-${formatDateFN()}-balances`
	const cols = ["Exchange","Alias","Account","Asset","Available","Total","Updated"]
	if (format === "copy" || format === "email") {
		let output = cols.join() + newLine
		broker.getBalances().forEach(e => output += `${e.name},${e.alias},${e.acc},${e.ccy},${e.avail},${e.total},${formatDate(e.update)}${newLine}`)
		if (format === "copy") {
			clipboardWrite("text/plain", output)
		} else {
			emailMessage("balances", "** All balance info exported as CSV **", email, null, null, output, filename+'.csv', 'application/csv')
			const msg = (autoInt ? `[Auto Export => ${autoInt}] `:'') + `All balance info sent via email to ${email || opt.emailAddress}!`
			log.notice(msg), !autoInt && toastr.success(msg)
		}
		return
	}

	$(document.body).append(`<table id="export" style="display:none"></table>`)
	$('#export').DataTable({
		searching: false,
		ordering: false,
		paging: false,
		info: false,
		dom: 'Bt',
		buttons: [
			{extend: format, filename: filename, title: ''}
		],
		columns: cols.map(e => {return {title: e}}),
		data: broker.getBalances(true),
		initComplete: function() {
			$('#export').DataTable().button(0).trigger()
			$('#export_wrapper').remove()
			const msg = (autoInt ? `[Auto Export => ${autoInt}] `:'') + `All balance info exported as ${format.toUpperCase()}!`
			log.notice(msg), !autoInt && toastr.success(msg)
		}
	})
}

function* exportBalances(el, ev) {
	if (ev && ev.shiftKey) {
		return doBalanceExport("copy")
	}
	const format = el.dataset.format
	if (ev && ev.ctrlKey || (yield yesno.bind(this, `Do you really want to export <b>all</b> saved balance info in ${format.toUpperCase()} format?`))) {
		doBalanceExport(format)
	}
}

function getLockInfo(alert) {
	let count = 0
	let info = '<table class="table table-striped table-condensed"><thead><tr><th>Exchange / Symbol</th><th>Timeframe</th><th>Lock Time</th><th>Exp.</th></tr></thead>'

	if (localStore.get().lastAlert) {
		Object.each(localStore.get().lastAlert.lock, (key, entry) => {
			if (!alert || entry.id == alert) {
				const expired = entry.exp ? entry.exp+'m' + (timeAdd(entry.time, entry.exp * 60) < new Date() ? ' – <b>EXPIRED!</b>' : '') : '–'
				info += `<tr><td>${key}</td><td>${formatTF(entry.res)}</td><td>${formatDateS(entry.time)}</td><td>${expired}</td></tr>`
				++count
			}
		})
	}
	if (!count) {
		return "<b>– No locks found! –</b><br/><br/>"
	}

	info += '</table>'
	return info
}

function* showLock(el) {
	const data = el.dataset
	const form = el.closest('form')
	const prompt = form.names.value ? `"${form.names.value}"` : "this alert"
	ok(`The following symbols have been locked by ${prompt}:<br/>
		<br/>
		${getLockInfo(data.alert)}`)
}

function* setLock(el) {
	const data = el.dataset
	const form = el.closest('form')
	const prompt = form && form.names.value ? `"${form.names.value}"` : "this alert"

	if (data.lock > 0) {
		let symbol = yield ask.bind(this, `Please enter the full exchange and symbol you want to lock, optionally followed 
			by the timeframe in minutes (eg. BYBIT:BTCUSDT ${opt.defaultRes}) – default timeframe is ${opt.defaultRes}<br/>
			<br/>`,
			`Lock Symbol via ${prompt}`, opt.defExchange.toUpperCase() + ':' + opt.defSymbol.toUpperCase())

		if (symbol) {
			symbol = symbol.trim().replace(/,/g, ' ').replace(/\s\s+/g, ' ').toUpperCase().split(':')
			let exchange = symbol[0]
			symbol = symbol[1].split(' ')
			let res = Number(symbol[1]) || opt.defaultRes
			symbol = symbol[0]

			if (!symbol) {
				error("Please enter data in the form of &lt;exchange&gt;:&lt;symbol&gt; – eg. BYBIT:BTCUSDT !")
			} else if (!broker.getByAlias(exchange)) {
				error(`The entered exchange does not seem to be valid!<br/>
					<br/>
					Your input: ${exchange}`)
			} else if (yield yesno.bind(this, `Are you sure you want ${prompt} to lock the following exchange/symbol combination?<br/>
					<br/>
					Exchange: ${exchange}<br/>
					Symbol: ${symbol}<br/>
					Timeframe: ${formatTF(res)}`)) {

				bg.lockSymbol(getAlerts(data.alert), exchange+':'+symbol, res)
				q("[data-alert='"+data.alert+"'][data-action='showLock']").classList.add('alert-locked')
			}
		}
	} else if (data.lock < 0) {
		if (yield yesno.bind(this, `Do you really want to remove <b>ALL</b> locking information from <b>ALL</b> symbols?<br/>
				<br/>
				${getLockInfo()}
				<u>Note:</u> This might cause flipping or early exit of existing trades depending on your alerts and strategy!`)) {

			bg.unlockSymbol()
			qa("[data-action='showLock']").forEach(el => el.classList.remove('alert-locked'))
		}
	} else {
		if (yield yesno.bind(this, `Do you really want to remove <b>ALL</b> of the following locks placed by ${prompt}?<br/>
				<br/>
				${getLockInfo(data.alert)}
				<u>Note:</u> This might cause flipping or early exit of existing trades depending on your alerts and strategy!`)) {

			bg.unlockSymbol(getAlerts(data.alert, {}))
			q(`[data-alert='${data.alert}'][data-action='showLock']`).classList.remove('alert-locked')
		}
	}
}

function* resetCount(el) {
	if (yield yesno.bind(this, "Do you really want to <b>reset</b> the icon badge counter?")) {
		//ls.badgeEvt && (ls.badgeEvtLast = ls.badgeEvt)
		;['badgeRcv', 'badgeRun', 'badgeErr'/*, 'badgeEvt'*/].forEach(key => delete ls[key])
		const msg = "Icon badge counter reset!"
		log.info(msg), toastr.success(msg)
	}
}

function* resetTotals() {
	if (yield yesno.bind(this, "Do you really want to <b>reset</b> the total & previous metrics?")) {
		for (const key in ls) {
			if (key.startsWith("statTotal")) {
				delete ls[key]
			}
		}
		;['statBootups', 'statRestarts', 'statAutoRestarts', 'statUptimeTotal', 'statUptimeMin', 'statUptimeMax', 'statUptimeAvg', 
		'statDowntimeTotal', 'statDowntimeMin', 'statDowntimeMax', 'statDowntimeAvg'].forEach(key => delete ls[key])
	}
}

function* confInterval(el) {
	const isOpt = el.dataset.conf
	const alert = el.dataset.alert
	const form = !isOpt && el.closest('form')
	const prompt = form && form.names.value ? `"${form.names.value}"` : isOpt ? (el.dataset.desc || "this option") : "this alert"

	function getInterval(asArray) {
		let i = 0, exp = []
		;["minute", "hour", "day", "month", "weekday"].forEach(which => {
			const what = q('input[type=radio][name='+which+']:checked').value

			if (what == 0) {
				exp.push("*")
			} else if (what == 1) {
				exp.push("*/" + q('#' + which + 'EveryVal').value)
			} else {
				const el = q('#' + which + 'List')
				let range = -1, from, to, list = []

				Array.prototype.forEach.call(el.options, opt => {
					if (opt.selected) {
						list.push(opt.value)
						if (range == -1 || range == 1) {
							++range
							from = opt.value
						}
						to = opt.value
					} else if (!range) ++range
				})
				if (!range || range == 1) {
					exp.push(from + (to != from ? "-"+to : ""))
				} else {
					exp.push(list.join(","))
				}
			}
			++i
		})
		while (exp.length > 1 && exp.slice(-1)[0] === "*") exp.pop()
		return asArray ? exp : exp.join(' ')
	}
	function updateText() {
		q(".bootbox-body h3").innerHTML = interval2Text(getInterval(true))
	}

	const type = !isOpt && q(`#alert-${alert}-auto`).checked ? 'Interval' : !isOpt && q(`#alert-${alert}-sched`).checked ? 'Schedule' : 'Interval / Schedule'
	bootbox.dialog({
		title: `<span class="glyphicon glyphicon-time"></span>  Customize ${type} for ${prompt}`,
		message: q("#cronbox").innerHTML,
		size: 'large',
		onEscape: true,
		backdrop: true,
		buttons: {
			apply: {
				label: '<span class="glyphicon glyphicon-ok"></span> Apply',
				className: 'btn-success',
				callback: function(){
					if (isOpt) {
						opt[isOpt] = getInterval()
						saveOptions()
					} else {
						q(`#alert-${alert}-autoInt`).value = getInterval()
						q('[data-page=alerts]').classList.add('unsaved')
					}
				}
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel',
				className: 'btn-danger'
			},
		}
	})

	const exp = isOpt ? (opt[isOpt] || "") : q(`#alert-${alert}-autoInt`).value
	const parts = exp.split(' ').filter(Boolean)
	let i = 0

	for (const part of parts) {
		const which = ["minute", "hour", "day", "month", "weekday"][i]
		if (++i > 5) break
		if (part === "*") continue
		const isEvery = part.startsWith("*/") || ((i < 3 || i > 4) && part.startsWith("0/")) || ((i >= 3 && i <= 4) && part.startsWith("1/"))
		const el = q('#' + which + (isEvery ? 'EveryVal' : 'List'))
		qa('[name='+which+']')[el.tagName === "SELECT" ? 2 : 1].checked = true

		if (isEvery) {
			const every = Number(part.slice(2)) || 2
			el.value = every
		} else if (part.includes("-")) {
			const range = part.split("-").map(e => Number(e) || 0)
			const from = Math.min(range[0], range[1])
			const to = Math.max(range[0], range[1])
			Array.prototype.forEach.call(el.options, opt => opt.value >= from && opt.value <= to && (opt.selected = true))

		} else if (part.includes(",")) {
			const list = part.split(",")
			Array.prototype.forEach.call(el.options, opt => list.includes(opt.value) && (opt.selected = true))

		} else {
			const val = parseInt(part)
			Array.prototype.forEach.call(el.options, opt => opt.value == val && (opt.selected = true))
		}
	}

	qa(".cronbox input[type=number], .cronbox select").forEach(el => el.addEventListener('focus', function(){
		q('#'+this.name.replace("List", "Multi").replace("EveryVal", "EveryX")).checked = true
	}))
	qa(".cronbox input, .cronbox select").forEach(el => el.addEventListener('change', updateText))
	updateText()
}

function* resetAlertStats(el, ev) {
	const timeOnly = ev.shiftKey
	const countOnly = ev.ctrlKey && !timeOnly
	const form = el.closest('form')
	const prompt = form.names.value ? `"${form.names.value}"` : "this alert"

	if (yield yesno.bind(this, `Do you really want to <b>reset</b> the ${timeOnly?'time ':countOnly?'count ':''}stats of ${prompt}?<br/>
		<br/>
		<u>Note:</u> This action cannot be undone!`)) {

		const fields = [...(countOnly ? [] : ["time", "timeAvg", "timeMin", "timeMax", "slip"]), ...(timeOnly ? [] : ["recvNum", "firedNum", "intNum", "autoNum", "manualNum", "errNum"])]
		let alert = getAlerts(el.dataset.alert)
		fields.forEach(e => delete alert[e])

		bg.autoLastEvent = Date.now()
	}
}

function* clearInput(el) {
	el = el.previousElementSibling
	opt[el.dataset.opt] = el.value = el.defaultValue
	saveOptions()

	const changed = (el.dataset.change || el.dataset.opt) + 'Changed'
	window[changed] && window[changed](el.name)
}

function* handbrake(el, ev) {
	const isStop = el.id.startsWith("stop")
	if (!bg.statRunning) {
		ev.shiftKey && disableAll(true)
		return ev.ctrlKey || ok("No alert is currently running!")
	}
	if (bg.exitAll) return ok("Please wait for all alerts to terminate!")

	if (isStop) {
		if (ev.ctrlKey || (yield yesno.bind(this, `Do you really want to <b>terminate all</b> currently running alerts? (${bg.statRunning})<br/>
			<br/>
			<u>Note:</u> Depending on your alert commands this may leave newly opened positions unprotected!`))) {

			if (bg.statRunning) {
				bg.exitAll = true; bg.pauseAll = false
				ev.shiftKey && disableAll(true)
			}
		}
	} else {
		bg.pauseAll = !bg.pauseAll
		ev.shiftKey && disableAll(bg.pauseAll)
	}
	q(bg.pauseAll ? '#resumeBtn' : '#pauseBtn').classList.remove('hidden')
	q(bg.pauseAll ? '#pauseBtn' : '#resumeBtn').classList.add('hidden')
}

let browserEx = null
let browserSym = null
let browserTitle = null
let browserData = null

function* lookupSymbol(editor, focusEl) {
	// $$$ check if already open - then ignore!
	if (editor.dataset && editor.dataset.alert) {
		editor = q("#alert-"+editor.dataset.alert+"-editor")
		focusEl = editor.firstChild
		editor = ace.edit(editor).getSession()
	}

	let alias = editor.dataset && opt[editor.dataset.ex] || opt.defExchange
	if (editor.getValue) {
		const synEx = findSyntax("e|ex|exchange", editor.getValue(), editor.selection.getCursor().row)
		const synAcc = findSyntax("a|acc|account|ap|par|parallel", editor.getValue(), editor.selection.getCursor().row)
		alias = synAcc && synAcc.contains(':') && synAcc.split(':', 1)[0] || synEx || alias
	} else {
		focusEl = editor
	}
	alias = broker.getByAlias(alias && alias.toUpperCase())
	alias && (alias = alias.getAlias(-1))

	let preSelect = 0
	let exData = []
	const exes = broker.getAll()
	for (const ex of exes) {
		exData.push([ex.hasSubscription() && ex.hasPermission() ? 
			'<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>', 
			ex.getName(), ex.getAlias(-1), ex.getDescription()])
		if (ex.getAlias(-1) === alias) {
			preSelect = exData.length
		}
	}

	bootbox.dialog({
		title: "Select Exchange for Symbol Browser",
		size: 'large',
		className: 'exchange-browser',
		message: '<table id="exBrowser" class="table-striped" width="100%""></table>',
		buttons: {
			confirm: {
				label: '<span class="glyphicon glyphicon-chevron-right"></span> Continue',
				className: 'btn-success disabled',
				callback: function() {
					setTimeout(run.bind(window, browseSymbols.bind(broker.getByAlias(browserEx)), null, null, editor, focusEl), 200)
//					run(browseSymbols.bind(broker.getByAlias(browserEx)), null, null, editor, focusEl)
				},
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel',
				className: 'btn-danger',
			}
		},
		onHidden: function(){
			focusEl && focusEl.focus()
		},
		onShow: function(){
			const table = $('#exBrowser').DataTable({
				data: exData,
				columns: [
					{title: "Act", width: "3%"},
					{title: "Name", width: "22.5%"},
					{title: "Alias", width: "22.5%"},
					{title: "Description", width: "52%"},
				],
				buttons: [
					{extend: 'colvis', text: '<span class="glyphicon glyphicon-list"></span> Columns', titleAttr: "Show/Hide columns"}, 
					{extend: 'copy', text: '<span class="glyphicon glyphicon-copy"></span> Copy', titleAttr: "Copy list to clipboard", title: browserTitle, exportOptions: {modifier: {selected: null}}}, 
					{extend: 'csv', text: '<span class="glyphicon glyphicon-download-alt"></span> CSV', titleAttr: "Export list in CSV format", title: browserTitle, exportOptions: {modifier: {selected: null}}}, 
					{extend: 'excel', text: '<span class="glyphicon glyphicon-download-alt"></span> Excel', titleAttr: "Export list in Excel format", title: browserTitle, exportOptions: {modifier: {selected: null}}}, 
				],
				autoWidth: false,
				lengthChange: false,
				select: {
					style: 'single',
					items: 'row',
					toggleable: false,
				},
				language: {
					info: "Showing _START_-_END_ of _TOTAL_",
					infoFiltered: "(_MAX_ total)",
					paginate: {
						previous: '<span class="glyphicon glyphicon-chevron-left"></span>',
						next: '<span class="glyphicon glyphicon-chevron-right"></span>',
					},
					select: {
						rows: {_: ""},
					},
				},
				pageLength: 25,
				scrollY: 25 * 20,
				paging: false,
				order: [[2, 'asc']]
			})
			table.on('select', function(e, dt, type, indexes){
				if (type === 'row') {
					browserEx = table.rows(indexes).data()[0][2]
					q(".exchange-browser .bootbox-accept").classList.remove('disabled')
				}
			})
			table.buttons().container().appendTo('#exBrowser_wrapper .col-sm-6:eq(0)')
			preSelect && table.row(preSelect-1).select().node().scrollIntoView(true)
		}
	})
}

function* browseSymbols(el, ev, editor, focusEl) {
	// $$$ if (binance bitfinex deribit ftx kraken kucoin okex)
	if (this.getSymbolCache()._refresh) {
		showSpinner('')
		yield* sleep(0.1)
	}
	try {
		yield* this.updateSymbols()
	} catch(ex) {
		hideSpinner()
		const err = getError(ex)
		error(`The following error occured when trying to update the list of available symbols for this exchange:<br/>
			<br/>
			<b>${err}</b><br/>
			<br/>
			<u>Note:</u> Some exchanges require at least one valid API key for this action!`)
		return
	}
	const symbols = this.getSymbolCache()

	browserData = []
	for (const name in symbols) {
		const sym = symbols[name]
		if (typeof sym !== 'object') continue

		const qtyPrec = sym.quantityPrecision || sym.quantity_precision || sym.tradeUnitsPrecision || sym.lotSize || sym.lot_decimals || sym.step || sym.baseQtyPrecision || sym.basePrecision || 0
		browserData.push([
			name, sym.contractType || sym.instType || sym.type || sym.typ || sym.kind || sym.product_type || /*sym.aclass_quote ||*/ "–", 
			sym.pricePrecision || sym.price_precision || sym.displayPrecision || sym.precision || sym.pair_decimals || sym.digits || sym.quotePrecision || 0, 
			qtyPrec < 0 ? "÷ "+Math.abs(qtyPrec) : qtyPrec, 
			(sym.minimumTradeSize || sym.MinTradeSize || sym.minimum_order_size || sym.base_min_size || sym.baseMinSize || sym.initMargin || sym.minTradeQuantity || 0) + (sym.minNotional || sym.quote_min_size ? ` (${sym.minNotional || sym.quote_min_size})`:'')
		])
	}

	browserEx = this.getAlias(-1)
	browserSym = null
	browserTitle = "Available Symbols on "+this.getName()

	const data = (editor && editor.dataset) || (el && el.dataset)
	const isEditor = editor && editor.getValue
	const isOpt = !isEditor && editor
	const isButton = !editor && data && data.btn

	bootbox.dialog({
		title: browserTitle,
		size: 'large',
		className: 'symbol-browser',
		message: '<table id="symbolBrowser" class="table-striped" width="100%""></table>',
		buttons: {
			confirm: {
				label: `<span class="glyphicon glyphicon-${isEditor ? 'chevron-right' : 'ok'}"></span> ` +
					((data && data.btn) || (isEditor ? 'Insert Syntax' : isOpt ? 'Set Default' : 'Copy Syntax')),
				className: 'btn-success disabled',
				callback: function() {
					if (isEditor) {
						const row = editor.selection.getCursor().row
						let line = editor.getLine(row)
						// $$$$$ acc=ex:sym:acc
						if (line.match(/(^|\s|#)(e|ex|exchange)=/i)) {
							line = line.replace(/(^|\s|#)(e|ex|exchange)=((?:\".*?\"|[^=\s]+?)*)/i, " ex="+browserEx)
						} else {
							line += " ex="+browserEx
						}
						if (line.match(/(^|\s|#)(s|sym|symbol)=/i)) {
							line = line.replace(/(^|\s|#)(s|sym|symbol)=((?:\".*?\"|[^=\s]+?)*)/i, " sym="+(browserSym.includes(' ')?`"${browserSym}"`:browserSym))
						} else {
							line += " sym="+(browserSym.includes(' ')?`"${browserSym}"`:browserSym)
						}
						editor.replace(new ace.Range(row, 0, row, Number.MAX_VALUE), line.trim())
					} else if (isOpt) {
						opt[data.ex] = browserEx
						opt[data.sym] = browserSym
						initOptions(true)
						saveOptions()
					} else if (isButton) {
						qa(`input[name=${data.sym}], span[for=${data.sym}]`).forEach(el => {
							switch (el.nodeName) {
								case 'INPUT': el.value = browserSym; el.dispatchEvent(new Event('change')); break
								case 'SPAN': el.innerHTML = browserSym; break
							}
						})
					} else {
						clipboardWrite("text/plain", "ex=" + browserEx + (browserSym ? " sym="+(browserSym.includes(' ')?`"${browserSym}"`:browserSym):''))
					}
				},
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel',
				className: 'btn-danger',
			}
		},
		onHidden: function(){
			focusEl && focusEl.focus()
		},
		onShow: function(){
			// $$$$ limit rows depending on screen size
			symbolPagingChanged()
			let expires = ((symbols._updated + opt.infoExpire * 60 * 60 * 1000) - Date.now()) / 1000
			$(`<div class="input-group" style="width:5.5em;position:absolute;left:47%;bottom:49px">
				<button class="btn btn-default dropdown-toggle" type="button" style="height:38px" data-toggle="dropdown" data-opt="symbolPaging" title="Number of entries per page">
					25 <span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li><a href="#" data-value="10">10</a></li>
					<li><a href="#" data-value="25">25</a></li>
					<li><a href="#" data-value="50">50</a></li>
					<li><a href="#" data-value="75">75</a></li>
					<li><a href="#" data-value="100">100</a></li>
					<li><a href="#" data-value="0">scroll</a></li>
				</ul>
			</div>
			<div class="row">
				<div class="col-sm-12">Last updated: ${formatDateS(symbols._updated)} (${expires < 0 ? 'expired!' : 'expires in '+formatDur(expires)})</div>
			</div>`).insertAfter('#symbolBrowser_wrapper')
			initOptions()
		},
		onShown: function(){
			hideSpinner()
		}
	})
}

function symbolPagingChanged() {
	$('#symbolBrowser_wrapper').html('<table id="symbolBrowser" class="table-striped" width="100%""></table>')
	const paging = Number(opt.symbolPaging || 25)
	const table = $('#symbolBrowser').DataTable({
		data: browserData,
		columns: [
			{title: "Symbol", width: "25%"},
			{title: "Type"},
			{title: "Price Precision"},
			{title: "Quantity Precision"},
			{title: "Minimum Quantity"},
		],
		columnDefs: [{targets: '_all', width: "18%"}],
		buttons: [
			{extend: 'colvis', text: '<span class="glyphicon glyphicon-list"></span> Columns', titleAttr: "Show/Hide columns"}, 
			{extend: 'copy', text: '<span class="glyphicon glyphicon-copy"></span> Copy', titleAttr: "Copy list to clipboard", title: browserTitle, exportOptions: {modifier: {selected: null}}}, 
			{extend: 'csv', text: '<span class="glyphicon glyphicon-download-alt"></span> CSV', titleAttr: "Export list in CSV format", title: browserTitle, exportOptions: {modifier: {selected: null}}},
			{extend: 'excel', text: '<span class="glyphicon glyphicon-download-alt"></span> Excel', titleAttr: "Export list in Excel format", title: browserTitle, exportOptions: {modifier: {selected: null}}},
		],
		autoWidth: false,
		lengthChange: false,
		select: {
			style: 'single',
			items: 'row',
			toggleable: false,
		},
		language: {
			info: "Showing _START_-_END_ of _TOTAL_",
			infoFiltered: "(_MAX_ total)",
			paginate: {
				previous: '<span class="glyphicon glyphicon-chevron-left"></span>',
				next: '<span class="glyphicon glyphicon-chevron-right"></span>',
			},
			select: {
				rows: {_: ""},
			},
		},
		pageLength: paging || 25,
		scrollY: (paging || 25) * 20,
		paging: paging > 0,
	})
	table.on('select', function(e, dt, type, indexes){
		if (type === 'row') {
			browserSym = table.rows(indexes).data()[0][0]
			q(".symbol-browser .bootbox-accept").classList.remove('disabled')
		}
	})
	table.buttons().container().appendTo('#symbolBrowser_wrapper .col-sm-6:eq(0)')
}

function* copyAlert(form) {
	let alert = formData(form)
	alert.names = alert.names.split(',').map(s => s.trim()).filter(Boolean)
	alert.commands = alert.commands.split('\n')

	const source = localStore.get('hasAlerts') ? localStore.get() : syncStore.get()
	const current = source['alert_'+alert.id]
	current && resetProps.forEach(e => current[e] && (alert[e] = current[e]))

	clipboardWrite("text/plain", stringifyPretty(alert))
}

function* pasteAlert(el) {
	navigator.clipboard.readText().then(text => run(doAlertPaste, text)).catch(err => {})
}

function* doAlertPaste(text) {
	let alert = null
	try {
		alert = JSON.parse(text)
	} catch(ex) {}

	if (!alert || !alert.commands || !Array.isArray(alert.commands)) {
		return yield error.bind(this, `Clipboard data is not a valid ${pvName} alert!`)
	}

	if ((alert.names = yield ask.bind(this, "Create a new alert with the following name:<br/><br/>", 
			`Paste alert "${alert.names}"`, `${alert.names} (new)`))) {
		;["sort", "locks"].forEach(e => delete alert[e])
		alert.id = Date.now() - firstAlertId

		alert.names = alert.names.split(',').map(s => s.trim()).filter(Boolean)
		yield* createAlert(null, null, alert, true)
	}
}

function* clearIdCache() {
	if (yield yesno.bind(this, `Do you really want to purge <b>all</b> currently cached custom order IDs?<br/>
		<br/>
		<u>Warning:</u> This will cause running alerts/upcoming exits to fail on all exchanges without native custom ID support when using IDs to target orders!`)) {
		broker.purgeIds()
		const msg = "All custom order IDs removed from cache!"
		log.notice(msg), toastr.success(msg)
	}
}

function* clearSymbols(el, ev) {
	if (ev && ev.ctrlKey || (yield yesno.bind(this, `Do you really want to clear <b>all</b> currently cached market info?<br/>
		It will automatically be refreshed by the next order command that requires it.`))) {
		broker.purgeSymbolCache()
		const msg = "All current market info removed from cache!"
		log.notice(msg), toastr.success(msg)
	}
}

let idData = null

function* showIdCache() {
	idData = []
	Object.each(window.STORE.local.idcache, (name, ex) => {
		Object.each(ex, (id, e) => idData.push([name, id, e[0], formatDate(e[1])]))
	})
	bootbox.dialog({
		title: "Cached Order IDs",
		size: 'large',
		message: '<table id="idBrowser" class="table-striped" width="100%""></table>',
		buttons: {
			ok: {
				label: '<span class="glyphicon glyphicon-ok"></span> OK',
				className: 'btn-success'
			}
		},
		onShow: function(){
			// $$$$ limit rows depending on screen size
			idPagingChanged()
			$(`<div class="input-group" style="width:5.5em;position:absolute;left:47%;bottom:30px">
				<button class="btn btn-default dropdown-toggle" type="button" style="height:38px" data-toggle="dropdown" data-opt="idPaging" title="Number of entries per page">
					25 <span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li><a href="#" data-value="10">10</a></li>
					<li><a href="#" data-value="25">25</a></li>
					<li><a href="#" data-value="50">50</a></li>
					<li><a href="#" data-value="75">75</a></li>
					<li><a href="#" data-value="100">100</a></li>
					<li><a href="#" data-value="0">scroll</a></li>
				</ul>
			</div>`).insertAfter('#idBrowser_wrapper')
			initOptions()
		}
	})
}

function* timeSync(el, ev) {
	if (ev && ev.ctrlKey || (yield yesno.bind(this, `Do you want to synchronize the internal clock correction with supported exchanges?<br/><br/>Last time sync: ${formatDate(bg.lastTimeSync)}`))) {
		openPage('log')
		yield* bg.timeSync(true)
	}
}

function idPagingChanged() {
	$('#idBrowser_wrapper').html('<table id="idBrowser" class="table-striped" width="100%""></table>')
	const paging = Number(opt.idPaging || 25)
	const table = $('#idBrowser').DataTable({
		data: idData,
		columns: [
			{title: "Exchange", width: "20%"},
			{title: "Order #", width: "33%"},
			{title: "ID", width: "29%"},
			{title: "Date", width: "18%"},
		],
		buttons: [
			{extend: 'colvis', text: '<span class="glyphicon glyphicon-list"></span> Columns', titleAttr: "Show/Hide columns"}, 
			{extend: 'copy', text: '<span class="glyphicon glyphicon-copy"></span> Copy', titleAttr: "Copy list to clipboard", title: browserTitle}, 
			{extend: 'csv', text: '<span class="glyphicon glyphicon-download-alt"></span> CSV', titleAttr: "Export list in CSV format", title: browserTitle},
			{extend: 'excel', text: '<span class="glyphicon glyphicon-download-alt"></span> Excel', titleAttr: "Export list in Excel format", title: browserTitle},
		],
		autoWidth: false,
		lengthChange: false,
		select: {
			//style: 'single',
			items: 'row',
			toggleable: false,
		},
		language: {
			info: "Showing _START_-_END_ of _TOTAL_",
			infoFiltered: "(_MAX_ total)",
			paginate: {
				previous: '<span class="glyphicon glyphicon-chevron-left"></span>',
				next: '<span class="glyphicon glyphicon-chevron-right"></span>',
			},
			select: {
				rows: {_: ""},
			},
		},
		pageLength: paging || 25,
		scrollY: (paging || 25) * 20,
		paging: paging > 0,
	})
	table.buttons().container().appendTo('#idBrowser_wrapper .col-sm-6:eq(0)')
}

function *posInfo(el) {
	const data = el.dataset
	if (!data.ex || !data.acc) return
	const allPos = bg.positions[data.ex][data.acc].list.reduce((obj, pos) => ({...obj, [pos._symbol+'.'+pos._side]: pos}), {})
	const pos = allPos[data.sym+'.'+data.side]
	if (!pos) return

	let info = `<div style="max-height:calc(50vh);overflow-y:auto"><table class="table table-striped table-condensed"><thead><tr><th>Field</th><th>Value</th></tr></thead></div>`
	Object.each(pos, (key, val) => info += `<tr><td>${key}</td><td>${val}</td></tr>`)
//	Object.each(Object.sort(p), (key, val) => info += `<tr><td>${key}</td><td>${val}</td></tr>`)

	confirm(`Details of ${data.ex}:${data.sym} ${data.side} ${data.acc !== '*'?'@ '+data.acc:''}`, info)
}

function doPosExport(format = "csv", id, autoInt, email) {
	const filename = `${pvName.toLowerCase()}${opt.instName ? '-'+opt.instName : ''}-${formatDateFN()}-positions`
	const exes = broker.getAll()
	const cols = ["Exchange","Alias","Account","Symbol","Side","Hedge","Entry","Mark","Liq","Size","SizeUSD","SizeCoin","RelSize","Currency","Lev","Cross","Profit","PnL","PnLLev","PnLTotal","Opened","Age"]
	const data = []
	for (const ex of exes) {
		if (!ex.canListPos()) continue
		const alias = ex.getAlias(-1)
		if (id && alias != id) continue

		const exPos = bg.positions[alias]
		Object.each(exPos, (acc, pos) => {
			if (pos.error || !pos.list) return
			pos.list.forEach(p => data.push([
				ex.getName(), 
				alias, 
				acc, 
				p._symbol, 
				p._side, 
				p._hedge === "both" ? 0 : p._hedge ? 1 : '', 
				p._entry, 
				toDecimal(Number(p._mark)) || '', 
				toDecimal(Number(p._liq)) || '', 
				toDecimal(p._size), 
				p._sizeUSD ? Number(p._sizeUSD).toFixed(2) : '', 
				p._sizeCoin ? Number(p._sizeCoin).toFixed(4) : '', 
				(p.relSize * 100).toFixed(2) + '%', 
				p.ccy || p.currency || '', 
				Number(p.leverage || 1).toFixed(2).replace('.00', ''), 
				p.cross === true ? 1 : p.isolated === true ? 0 : '',
				toDecimal(p._profit), 
				(p.pnl * 100).toFixed(2) + '%', 
				(p.pnlLev * 100).toFixed(2) + '%', 
				(p.pnlTotal * 100).toFixed(2) + '%', 
				p._date ? formatDate(p._date) : '', 
				p._date ? formatDur(tillNow(p._date)) : '', 
			]))
		})
	}

	if (format === "copy" || format === "email") {
		const output = cols.join() + newLine + data.map(e => e.join()).join(newLine) + newLine
		if (format === "copy") {
			clipboardWrite("text/plain", output)
		} else {
			emailMessage("positions", "** All position info exported as CSV **", email, null, null, output, filename+'.csv', 'application/csv')
			const msg = (autoInt ? `[Auto Export => ${autoInt}] `:'') + `${id || 'All'} position info sent via email to ${email || opt.emailAddress}!`
			log.notice(msg), !autoInt && toastr.success(msg)
		}
		return
	}
	$(document.body).append(`<table id="export" style="display:none"></table>`)
	$('#export').DataTable({
		searching: false,
		ordering: false,
		paging: false,
		info: false,
		dom: 'Bt',
		buttons: [
			{extend: format, filename: filename, title: ''}
		],
		columns: cols.map(e => {return {title: e}}),
		data: data,
		initComplete: function() {
			$('#export').DataTable().button(0).trigger()
			$('#export_wrapper').remove()
			const msg = (autoInt ? `[Auto Export => ${autoInt}] `:'') + `${id || 'All'} position info exported as ${format.toUpperCase()}!`
			log.notice(msg), !autoInt && toastr.success(msg)
		}
	})
}

function* exportPos(el, ev) {
	const info = el && el.closest("[name=positions]")
	const id = info && info.dataset.id
	const name = info && info.dataset.name || "all"
	if (ev && ev.shiftKey) {
		return doPosExport("copy", id)
	}
	const format = ev && ev.altKey ? "excel" : "csv"
	if (ev && ev.ctrlKey || (yield yesno.bind(this, `Do you really want to export <b>${name}</b> saved position info in ${format.toUpperCase()} format?`))) {
		doPosExport(format, id)
	}
}
