function newRandomOrder(side, price, distance) { var size = getRandomInt(2, 12) var buyStep = mangler.fixed(getRandomInt(1, 4) / 10) var delta = mangler.fixed((side === 'buy' ? -1 : 1) * (SPREAD + distance)) var orderPrice = mangler.fixed(price + delta) return newOrder(side, orderPrice, size) }
bot.random = function (price) { var buys = {}, sells = {} var bid = mangler.fixed(price - SPREAD), ask = mangler.fixed(price + SPREAD) for (var i = 0; i < DEPTH;) { var buyOrder = newRandomOrder('buy', price, Math.floor(i / 10)) buys[buyOrder.price] = buyOrder var sellOrder = newRandomOrder('sell', price, Math.floor(i / 10)) sells[sellOrder.price] = sellOrder i += buyOrder.quantity + sellOrder.quantity } return { buys: buys, sells: sells } }
function* init() { bot.strategies = { 'collar': bot.collar, 'random': bot.random } if (CROSS) { STP = instrument(SYMBOL).crossMarginInitialStop } var spread = getSpread() var tick = mangler.fixed(1 / instrument(SYMBOL).ticksperpoint) affirm(spread >= tick, 'SPREAD ' + spread + ' is less than tick ' + tick) affirm(STEP >= tick, 'STEP ' + STEP + ' is less than tick ' + tick) debug('botParams', JSON.stringify({ 'baseurl' : baseurl, 'SYMBOL' : SYMBOL, 'MARGINPERCENT': marginPercent, 'DEPTH' : DEPTH, 'SPREAD' : spread, 'STEP' : STEP, 'STP' : STP, 'TGT' : TGT, 'STRAT' : STRAT, 'QTY' : QTY, CROSS : CROSS, PREMIUM : PREMIUM }, null, 2)) var info = yield account.loginless.rest.get('/all/info') // debug('current price', info) var price = info[SYMBOL].indexPrice currentBand = { price: price } yield moveAndMerge() }
function newOrder(side, price, quantity) { var inst = instrument(SYMBOL) return { clientid : account.newUUID(), userid : account.userid, side : side, quantity : quantity || 1, price : mangler.fixed(price), orderType : 'LMT', stopPrice : STP, targetPrice: 'NONE',//mangler.fixed(SPREAD * 2 - 0.1, inst.ticksize), crossMargin: CROSS, instrument : SYMBOL } }
bot.collar = function (price) { affirm(price && typeof price === 'number', 'Numeric price required for collar') var buys = {}, sells = {} var availableMargin = calculateAvailableMargin() debug('availableMargin', availableMargin) if (availableMargin <= 0) return { buys: buys, sells: sells } var inst = instrument(SYMBOL) var satoshiPerQuantity = getSatoshiPerQuantity[inst.type]() var max = Math.floor(availableMargin / (satoshiPerQuantity * QTY * 2)) var depth = Math.min(DEPTH, max * STEP) var spread = getSpread() var buySpread = spread, sellSpread = spread var position = account.getPositions()[inst.symbol] if (position) { var adjustedSpread = mangler.fixed(Math.floor(Math.abs(position.quantity) / 100) / 5) buySpread = position.quantity > 0 ? mangler.fixed(buySpread + adjustedSpread) : buySpread sellSpread = position.quantity < 0 ? mangler.fixed(sellSpread + adjustedSpread) : sellSpread } var maxBuyCount = bot.getMaxOrderCounts(position, 'buy') var maxSellCount = bot.getMaxOrderCounts(position, 'sell') var premium = bot.getPremium(inst.expiry - Date.now()) var buyCount = 0 for (var i = mangler.fixed(price - buySpread); i > mangler.fixed(price - buySpread - depth); i = mangler.fixed(i - STEP)) { if (buyCount >= maxBuyCount) break; var buy = newOrder('buy', bot.getPremiumPrice(i, premium, inst.ticksize), QTY) buys[buy.price] = buy buyCount += QTY } var sellCount = 0 for (var j = mangler.fixed(price + sellSpread); j < mangler.fixed(price + sellSpread + depth); j = mangler.fixed(j + STEP)) { if (sellCount >= maxSellCount) break; var sell = newOrder('sell', bot.getPremiumPrice(j, premium, inst.ticksize), QTY) sells[sell.price] = sell sellCount += QTY } return { buys: buys, sells: sells } }
bot.getPremiumPrice = function (price, premium, ticksize) { var multiplier = Math.pow(10, ticksize) return mangler.fixed(Math.round(multiplier * price * (1 + premium)) / multiplier) }