DoQuant
English
Back to handbook

Harbor SDK

Example: trend

This example is one perpetual. Each bar loads the latest 1h closes and compares the previous close with the latest. If there is no position and price rose, it buys at market.

  • markets is swap. symbolMode is single.
  • It calls getKlines, then getPositions. Flat means the position array length is 0.
  • clientOrderId is trend- plus the bar time.
  • Each bar writes an info log so you can check the close on the instance.

It buys only when flat and rising. It does not sell. Add an exit before live, and read the log on a paper account first. Full example:

const manifest = {
  name: "趋势",
  version: "1.0.0",
  ccxtIds: ["binance"],
  markets: ["swap"],
  symbolMode: "single",
  params: {},
  permissions: ["market", "order", "account"],
};
async function onBar(rt, bar) {
  const bars = await rt.market.getKlines(bar.symbol, "1h", 3);
  const prev = bars.length > 1 ? Number(bars[bars.length - 2].close) : Number(bar.close);
  const last = Number(bar.close);
  const pos = await rt.account.getPositions();
  if (last > prev && pos.length === 0) {
    await rt.order.create({
      clientOrderId: "trend-" + bar.time,
      symbol: bar.symbol,
      side: "buy",
      type: "market",
      amount: "0.01",
    });
  }
  rt.log("info", "trend " + bar.close);
}
This chapter is for signed-in readers

Create a free account to read the full chapter, including sample code and step-by-step instructions.

Sign in to continue