Post
Topic
Board Trading Discussion
Topic OP
Any bot developers? I'm working on a JavaScript bot library and want feedback
by
its-a-me-mario
on 05/07/2021, 23:19:32 UTC
I've been working on a JavaScript library for writing crypto trading bots. Think if it as sort of like TradingView's PineScript, but with the ability to read your wallet balances, subscribe to real-time full order books on multiple exchanges at once, submit orders, anything a bot may need. I'm trying to make it as simple and easy to understand/read as possible, while still being powerful enough to handle any bot strategy's needs. I'd love to get feedback from anyone who's written bots in the past or has been thinking about it. I'm specifically asking what you think about the available methods, etc and if they seem like they'd be make sense to you as a developer. All of the underlying tech is already working, I'm just looking for the best way to let people define their strategies in JavaScript.

My goals for the library are:

1. You should be able to just describe what the bot does on each interval inside a `run()` function. The bot internally takes care of getting up-to-date wallet balances, handling real-time order book data, avoiding rate limits, etc.
2. Basic things a bot needs are available through functions like `getBalances()`, `submitOrder()`
3. Bots are written in plain JavaScript with an API that someone who doesn't code but understands trading could still comprehend.
4. Bots can be easily shared so others can try running it and modifying it themselves.

I've written an example swing trading bot strategy that buys when the price is <=  a pre-configured midpoint by X %, and then submits a sell at Y % above the midepoint. Please let me know what you think!

Code:

/**
 * Basic swing trading strategy example. Buys when price is a desired percentage below a configured midpoint, and sells when price is a desired percentage above it. Once a buy has been submitted, no more buys will be submitted until the accompanying sell has filled.
 */

// ----------------------------------------------------------------------------
//                Step 1 - define the config inputs
// ----------------------------------------------------------------------------
const market = input({
  type: input.market,
  label: 'The market (i.e. trade pair on an exchange) that this bot monitors and submits orders to',
  required: true
});
const midpoint = input({
  type: input.number,
  label: 'The midpoint price that buy and sell triggers will be centered around',
  required: true
});
const percentBelowToBuy = input({
  type: input.number,
  label: 'Buy trigger - When the price goes <= the midpoint by this percent, a buy is triggered',
  required: true,
  default: 3,
  min: 0.1,
  max: 50
});
const percentAboveToSell = input({
  type: input.number,
  label: 'Sell price - When a buy has been submitted, an accompanying sell will be submitted at this percent above the midpoint',
  required: true,
  default: 3,
  min: 0.1,
  max: 99
});
const orderAmount = input({
  type: input.number,
  label: 'Amount that will be used for each buy and sell order',
  required: true
});
// ----------------------------------------------------------------------------
//                Step 2 - define any variables the bot will use
// ----------------------------------------------------------------------------
const buyTriggerPrice = midpoint * (100 - percentBelowToBuy) / 100;
const sellTriggerPrice = midpoint * (100 + percentAboveToSell) / 100;
let hasEnteredPosition = false;// if a buy has been submitted and we've entered a position. if true, we are now waiting for the accompanying sell trigger
let submittedBuyOrders = series(); // store all submitted buy orders in a series
let submittedSellOrders = series(); // store all submitted sell orders in a series

// ----------------------------------------------------------------------------
//                Step 3 - define the execution logic
// ----------------------------------------------------------------------------
// finally, define the run execution logic that is run on every iteration
run(async () => {
  // get the last price on this market
  const last = market.last;
  if (hasEnteredPosition === false) {
    // if we haven't entered a position yet, check if we should buy
    const shouldBuy = last <= buyTriggerPrice;
    if (shouldBuy) {
      // record that we've entered a position
      hasEnteredPosition = true;
      // submit the buy order as a market order
      const thisBuyOrder = await submitOrder({
        type: ORDER_TYPES.MARKET_BUY,
        amount: orderAmount
      });
      // store this order in a series so we have access to all submitted orders
      submittedBuyOrders.add(thisBuyOrder);
      // immediately submit the accompanying sell at the configured price trigger for our exit
      const thisSellOrder = await submitOrder({
        type: ORDER_TYPES.LIMIT_SELL,
        rate: sellTriggerPrice,
        amount: orderAmount
      });
      // again store this order in a series so we have access to all submitted orders
      submittedSellOrders.add(thisSellOrder);
    }
  } else if (hasEnteredPosition === true) {
    // if we've entered a position then we've already submitted the limit sell when we bought
    // so need to check if it's filled or not. if it's filled, we are no longer in a position
    // note: submittedSellOrders.get() defaults to returning the most recently added order. we can get previous orders with submittedSellOrders.get(index)
    const sellOrderForThisPosition = submittedSellOrders.get();
    if (sellOrderForThisPosition.status === ORDER_STATUSES.closed) {
      // the limit sell order that accompanies our buy has filled! we've successfully exited our position
      hasEnteredPosition = false;
    }
  }
});