Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.buy() with tpPrice issue #3

Open
tiddle opened this issue Sep 14, 2024 · 2 comments
Open

this.buy() with tpPrice issue #3

tiddle opened this issue Sep 14, 2024 · 2 comments

Comments

@tiddle
Copy link

tiddle commented Sep 14, 2024

When applying tpPrice to a this.buy it seems to hang for infinity. It just never resolves. No errors or messages.

I tried it with the example SMA cross strategy by replacing this line

this.buy({ size: 1 }); with this.buy({ size: 1, tpPrice: 300 });

@chunkai1312
Copy link
Collaborator

@tiddle I apologize for the delayed response. I'm not entirely sure what the issue might be; it could be that the historical data isn't being triggered. If you could provide more detailed code, we might be able to assist you further.

@tiddle
Copy link
Author

tiddle commented Sep 25, 2024

Sure, i replicated it with this one page bit of code

npm i ccxt @fugle/backtest date-fns technicalindicators

import { btcmarkets } from "ccxt";
import { Backtest, Strategy } from "@fugle/backtest";
import { formatISO } from "date-fns";
import { SMA, CrossUp, CrossDown } from "technicalindicators";

class SmaCross extends Strategy {
  params = { n1: 20, n2: 60 };

  init() {
    const lineA = SMA.calculate({
      period: this.params.n1,
      values: this.data["close"].values,
    });
    this.addIndicator("lineA", lineA);

    const lineB = SMA.calculate({
      period: this.params.n2,
      values: this.data["close"].values,
    });
    this.addIndicator("lineB", lineB);

    const crossUp = CrossUp.calculate({
      lineA: this.getIndicator("lineA"),
      lineB: this.getIndicator("lineB"),
    });
    this.addSignal("crossUp", crossUp);

    const crossDown = CrossDown.calculate({
      lineA: this.getIndicator("lineA"),
      lineB: this.getIndicator("lineB"),
    });
    this.addSignal("crossDown", crossDown);
  }

  next(ctx) {
    const { index, signals, prev } = ctx;
    if (index < this.params.n1 || index < this.params.n2) return;

    const price = prev.data["close"];
    if (signals.get("crossUp"))
      this.buy({ size: 1, tpPrice: price * 1.15, slPrice: price * 0.9 }); // If you remove tpPrice and slPrice code runs fine
    if (signals.get("crossDown")) this.sell({ size: 1 });
  }
}

let btcmarketsExchange = new btcmarkets();

const historyCandles = await btcmarketsExchange
  .fetchOHLCV("BTC/AUD", "1d", 1640995200000)
  .then(convertToObject);

console.log(historyCandles.length);

const backtest = new Backtest(historyCandles, SmaCross, {
  cash: 1000000,
  tradeOnClose: true,
});

backtest.run().then((result) => {
  result.print();
});

function convertToObject(data) {
  return data.map((curr) => {
    return {
      date: formatISO(curr[0]),
      open: curr[1],
      high: curr[2],
      low: curr[3],
      close: curr[4],
      volume: curr[5],
    };
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants