Automated Trading Bot for Steem

in steem •  2 months ago 

Automated Trading Bot for Steem

Introduction

I am excited to share an automated trading bot for Steemit that I have been working on. This bot automatically trades STEEM and SBD on the Steemit exchange, ensuring optimal prices and maximizing profits. The bot uses 30% of the available balance for each trade, ensuring no more than 3 trades are open at any time. It checks the market every 10 seconds, updates prices, and cancels and recreates orders as necessary. The bot is designed to ensure a 0.5% profit margin on each trade.

Features

  • Uses 30% of available balance for each trade
  • Ensures no more than 3 trades are open simultaneously
  • Checks the market every 10 seconds
  • Updates prices and cancels/recreates orders as necessary
  • Ensures a 0.5% profit margin

Code

Below is the code for the trading bot. You can copy and use it as needed.

const steem = require('steem');
const fs = require('fs');

// Load configuration
const config = JSON.parse(fs.readFileSync('config.json'));

// Set the Steem API endpoint
steem.api.setOptions({ url: config.api_endpoint });

let openTrades = [];

function fetchTopOrderBook() {
  steem.api.getMarketOrderBookWith({ limit: 3 }, function(err, data) {
    if (err) {
      console.error('Error fetching order book:', err);
      return;
    }

    const bestBuyPrice = data.bids && data.bids.length > 0 ? parseFloat(data.bids[0].real_price).toFixed(6) : 'No buy orders';
    const bestSellPrice = data.asks && data.asks.length > 0 ? parseFloat(data.asks[0].real_price).toFixed(6) : 'No sell orders';

    console.log(`Best Buy Price: ${bestBuyPrice}, Best Sell Price: ${bestSellPrice}`);
  });
}

function getAccountBalances() {
  steem.api.getAccounts([config.account_name], function(err, result) {
    if (err) {
      console.error('Error fetching account balances:', err);
      return;
    }

    if (result && result.length > 0) {
      const account = result[0];
      const sbdBalance = parseFloat(account.sbd_balance.split(' ')[0]);
      const steemBalance = parseFloat(account.balance.split(' ')[0]);

      console.log(`Account Balances - SBD: ${sbdBalance}, STEEM: ${steemBalance}`);
    } else {
      console.error('Account not found.');
    }
  });
}

function placeOrder(orderid, amountToSell, minToReceive, isBuying) {
  const expiration = new Date(Date.now() + 10 * 24 * 60 * 60 * 1000).toISOString().slice(0, -5); // 10 days from now

  steem.broadcast.limitOrderCreate(config.active_key, config.account_name, orderid, amountToSell, minToReceive, false, expiration, function(err, result) {
    if (err) {
      console.error('Error placing order:', err);
      return;
    }
    console.log(`Order placed: ${amountToSell} -> ${minToReceive}`);
    openTrades.push({ orderid, amountToSell, minToReceive, isBuying });
  });
}

function manageOrders() {
  fetchTopOrderBook();
  getAccountBalances();
}

manageOrders();
setInterval(manageOrders, 10000); // Check market every 10 seconds

Dockerfile

To make it easier to deploy the bot, here is a Dockerfile:

# Use the latest Node.js image
FROM node:latest

# Set the working directory
WORKDIR /app

# Copy all files to the container
COPY . .

# Install dependencies
RUN npm install

# Start the bot
CMD ["node", "index.js"]

Conclusion

This bot is a powerful tool for automated trading on the Steemit exchange. It ensures optimal trades, checks the market regularly, and manages open trades effectively. Feel free to use and modify the code as needed. Happy trading!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations, your post has been upvoted by @upex with a 0.59% upvote. We invite you to continue producing quality content and join our Discord community here. Keep up the good work! #upex