Skip to content

Commit

Permalink
add EMA strat
Browse files Browse the repository at this point in the history
  • Loading branch information
bharathrao committed Jul 7, 2020
1 parent 63fece7 commit 1c70eba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/bot/config/default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"strats": { "COLLAR": true, "RANDOM": true, "EMA": true},
"spot": {
"strategy": "COLLAR",
"symbol": "LEVETH",
Expand Down
3 changes: 2 additions & 1 deletion packages/bot/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = (function () {
const app = config.app
affirm(app === 'spot' || app === 'futures', `BOT_APP must be spot or futures. found ${app}`)
const _config = config[app]
affirm(_config.strategy === "COLLAR" || _config.strategy === "RANDOM", "INVALID strategy: " + _config.strategy)
console.log('strats', config['strats'])
affirm(config['strats'][_config.strategy], "INVALID strategy: " + _config.strategy)
affirm(_config.startSide === "buy" || _config.startSide === "sell", "INVALID start side: " + _config.startSide)
return {
app: app,
Expand Down
34 changes: 33 additions & 1 deletion packages/bot/src/mm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const logger = require("@leverj/logger")
const BigNumber = require('bignumber.js')
const adaptor = require('@leverj/adapter/src/OrderAdapter')
const orderAdaptor = require('@leverj/adapter/src/DerivativesOrderAdaptor')
const OrderAdapter = require("@leverj/adapter/src/OrderAdapter")
let i = 1

module.exports = (async function () {
Expand All @@ -16,11 +17,14 @@ module.exports = (async function () {
let leverjConfig = {}
let collarWorking, lastPrice, lastSide, indexPrice
let orders = {}
let ema

const emaMultiplier = 2 / ( 10 + 1)
const isSpot = config.app === 'spot'
const strategy = {
COLLAR: doCollarStrategy,
RANDOM: doRandomStrategy
RANDOM: doRandomStrategy,
EMA: doEMAStrategy,
}

const toMakerSide = side => side === "buy" ? "sell" : "buy"
Expand All @@ -43,6 +47,7 @@ module.exports = (async function () {
}

function newOrder(side, price, quantity) {
console.log('Order:', side, quantity, price)
return isSpot ? spotOrder(side, price, quantity) : futuresOrder(side, price, quantity)
}

Expand Down Expand Up @@ -112,6 +117,13 @@ module.exports = (async function () {
logger.log('last price and side', price, side)
}

// EMA strategy
async function doEMAStrategy() {
const price = isSpot ? lastPrice : indexPrice
ema = ema ? (price * ema * emaMultiplier + ema * ( 1 - emaMultiplier)) : price
console.log('ema', ema)
}

// collar strategy ##########################################################################################


Expand Down Expand Up @@ -169,6 +181,13 @@ module.exports = (async function () {
collarWorking = false
}

function sendOrders(patch) {
if (patch && patch.length) {
logger.log("sending patch", patch)
zka.socket.send({method: "PATCH", uri: "/order", body: patch})
}
}

function getOrdersToBeAddedAndDeleted() {
if (instrument().topic) {
if (indexPrice) return collarStrategy.getOrdersToBeAddedAndDeleted(Object.values(orders), indexPrice)
Expand Down Expand Up @@ -198,13 +217,26 @@ module.exports = (async function () {
order_execution: onExecution,
trade: onTrade,
index: onIndex,
difforderbook: onDiffOrderBook,
}
Object.keys(eventMap).forEach(function (event) {
zka.socket.removeAllListeners(event)
zka.socket.on(event, eventMap[event])
})
}

function onDiffOrderBook(difforderbook) {
const qty = config.quantity
console.log('ema', ema, 'bid', difforderbook.bid, 'ask', difforderbook.ask, 'qty', qty)
if(!ema || config.strategy != 'EMA') return console.log('Returning ema:', ema, 'strategy', config.strategy)
deltaFrac = (lastPrice - ema) / ema
// const qty = deltaFrac * config.quantity
let patch = []
if(difforderbook.bid > ema) patch.push({op: 'add', value: [newOrder('sell', difforderbook.bid, qty)]})
if(difforderbook.ask < ema) patch.push({op: 'add', value: [newOrder('buy', difforderbook.ask, qty)]})
sendOrders(patch)
}

function onOrderAdd(response) {
const resultOrders = response.result
for (let i = 0; i < resultOrders.length; i++) {
Expand Down

0 comments on commit 1c70eba

Please sign in to comment.