Skip to content
Permalink
Browse files

Done Basic Logic For Market Maker

  • Loading branch information...
piavgh committed Feb 12, 2019
1 parent c4470df commit 18f918a539d521b32393dc77630de9b182f762e7
Showing with 54 additions and 5 deletions.
  1. +54 −5 cron/index.js
@@ -1,25 +1,35 @@
const CronJob = require('cron').CronJob

import { fetchOrderBook } from '../services/fetchOrderBook'
import { getOrderBook } from '../services/getOrderBook'
import { prepareOrderParams, createOrder } from '../services/createOrder'
import { calculateBetterBid, calculateBetterAsk } from '../utils/price'
import { getMarketQuotes } from "../services/coinmarketcap/getMarketQuotes"
import { calculateBetterBid, calculateBetterAsk, calculateBigNumberAmount, calculateCoinmarketcapPrice } from '../utils/price'
import { defaultOrderParams } from '../config'

const runMarketMaker = async () => {
try {
const orderBookData = await fetchOrderBook()
const orderBookData = await getOrderBook()

if (orderBookData.bids.length === 0) {
return await handleEmptyOrderbook('BUY')
}

if (orderBookData.asks.length === 0) {
return await handleEmptyOrderbook('SELL')
}

if (orderBookData.bids.length <= orderBookData.asks.length) {
const bestBid = orderBookData.bids[0]
let newBidOrder = calculateBetterBid(bestBid)
newBidOrder = await prepareOrderParams(newBidOrder.amount, newBidOrder.price, 'BUY')
console.log(newBidOrder)
// console.log(newBidOrder)

await createOrder(newBidOrder)
} else {
const bestAsk = orderBookData.asks[0]
let newAskOrder = calculateBetterAsk(bestAsk)
newAskOrder = await prepareOrderParams(newAskOrder.amount, newAskOrder.price, 'SELL')
console.log(newAskOrder)
// console.log(newAskOrder)

await createOrder(newAskOrder)
}
@@ -29,7 +39,46 @@ const runMarketMaker = async () => {
}
}

const handleEmptyOrderbook = async (side) => {
try {
const marketQuote = await getMarketQuotes()
const latestPrice = marketQuote['ETH'].quote['TOMO'].price
const newOrder = await prepareOrderParams(
calculateBigNumberAmount(defaultOrderParams.amount).toString(),
calculateCoinmarketcapPrice(side === 'BUY' ? latestPrice - 0.25 : latestPrice + 0.25),
side
)
console.log(newOrder)

await createOrder(newOrder)
} catch (err) {
console.log(err)
}
}

const applyLivePrice = async () => {
try {
const marketQuote = await getMarketQuotes()
const latestPrice = marketQuote['ETH'].quote['TOMO'].price
const newBidOrder = await prepareOrderParams(
calculateBigNumberAmount(defaultOrderParams.amount).toString(),
calculateCoinmarketcapPrice(latestPrice),
'BUY'
)
console.log(newBidOrder)

await createOrder(newBidOrder)
} catch (err) {
console.log(err)
}
}

/**
* Fetch order book
*/
new CronJob(process.env.CRON_VALUE, runMarketMaker, null, true)

/**
* Periodically (10 minutes) get real time price from coinmarketcap.com and create order
*/
new CronJob(process.env.CRON_FETCH_LIVE_PRICE, applyLivePrice, null, true)

0 comments on commit 18f918a

Please sign in to comment.
You can’t perform that action at this time.