Buying Hive Automatically

in Programming & Dev3 years ago

Automated trading is something I've wanted to do for a while. I never got around to it but recently I've been thinking more and more about it, especially with the price of bitcoin going up higher every day. I have no good strategy ideas nor do I plan on trying to make a living off it, but I wanted to dip my feet in the well that is trading. I had an idea of arbitrage program, but got lazy halfway though and so I modified a tiny part of it to buy Hive automatically for me. I plan on finishing the arbitrage bot in the future, but with all the other stuff I have to do, its going to be a while.

My plan here was simple, I want to buy Hive at the lowest possible price on ionomy. Normally I just do a simple market trade to avoid the wait. But thanks to the magic of code, I can have the bot automatically cancel and replace orders when needed. This bot will ensure that as long as I have enough of a balance on Ionomy, it will keep a buy order for Hive on Ionomy at the top of the bids.

Lets start with our imports

const Ionomy = require('ionomy-node')

const MIN_ORDER_SIZE_BTC = 0.00001000

let marketToUse = "btc-hive"

let client = new Ionomy({
  apiKey: 'NO',
  apiSecret: 'NO',
})

The ionomy-node module was created by @reazuliqbal which is nice. I took a quick look through it so see if it was sending the api keys anywhere and I didn't see anything so I decided to use it. The value MIN_ORDER_SIZE_BTC is the size of orders to place. The amount I've set, 1000 sats is the minimum size that your orders can be.


async function getHighestBid(market){
  let orders = await client.orderBook({ market: market, type: 'both' })//just bid not working for some reason
  let bids = orders.bids
  if (bids.size === 0){
    return 0
  }
  let largestBid = bids[0].price
  return parseFloat(largestBid)
}

We have a function that gets the highest bid. We want that so we know what price to place our order at. We should have been able to get just bids, but that wasn't working so I get both orders and take a look at both sides. Since it comes ordered with largest to smallest, we can take the 1st item in the array and thats the highest bid thats out there. We can just return that (as a float so we can add numbers to it).


async function getOpenBids(market){
  let orders = await client.openOrders(market)
  let valid = []
  for(let i in orders){
    if (orders[i].type === "LIMIT_BUY" ){
      valid.push(orders[i])
    }
  }
  return valid
}

We also need to get our open bids. We want to get all of our open bids on that market so we can cancel them.


async function act(market){
  let bids = await getOpenBids(market)
  for (let i in bids){
    await client.cancelOrder(bids[i].orderId);
  }
  
  let balance = await client.balance('btc')
  let balanceUsable = balance.available
  if (balanceUsable >= MIN_ORDER_SIZE_BTC){
    let highestBid = await getHighestBid(market)
    await client.limitBuy({ market: marketToUse, amount: (MIN_ORDER_SIZE_BTC / (highestBid + 0.00000001)).toFixed(3), price: highestBid + 0.00000001});
  }
}

Now we have the big boy function that does a lot of work. First off we cancel all our orders. Then we get our balance. If our balance is larger than the minimum size we will place our orders at, we then get the highest bid and place an order to buy MIN_ORDER_SIZE_BTC BTC of hive.



setInterval(() => {
  act(marketToUse)
}, 1000 * 30)

Now Ionomy's market doesn't move too often, so we can modify our orders once every 30 seconds, but if this market moves more often, we can easily change it so that it does this more often.


This should help me get some more Hive in my wallet at a lower price than what I pay now because I just buy at market price.

Sort:  

Ionomy Bot... say what?

Can you sell this bot to @blocktrades so everyone else can use it? XD

This is all the code there's to it :), its a simple one meant for me to accumulate Hive. Theres lots of risk with it(anyone smart can manipulate it in order to get more for their sell orders easily) so I don't think anyone will really run it. I'll probably keep it up for a while then take it down once I have my real arbitrage bot ready.

Well okay.
I just want an automatic way of buying Hive at any price, like use it as a dollar-cost average thingy :)

Yay! 🤗
Your post has been boosted with Ecency Points.
Continue earning Points just by using https://ecency.com, every action is rewarded (being online, posting, commenting, reblog, vote and more).

Support Ecency, check our proposal:
Ecency: https://ecency.com/proposals/141
Hivesigner: Vote for Proposal