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

How to detect previous candles if they where ranging #78

Open
albizeka opened this issue Dec 14, 2021 · 1 comment
Open

How to detect previous candles if they where ranging #78

albizeka opened this issue Dec 14, 2021 · 1 comment

Comments

@albizeka
Copy link

Maybe out of library scope as a issue but more a question.

I developed a strategy that is working well in trending markets but when it's ranging there is a loss of money. I know that we can't predict market movements but at least how can i check if previous candles were ranging?

@PabloMz85
Copy link

Hi @albizeka, you need to create a routine, where you connect with the exchanger to get the historical candles.

Just as an example:
First perform an interval

createRequests() {
    const max = this.interval * 60;   // can be (1, 3, 5, 15,....)
    const requestSize = 60 * 200;
    let starts = parseInt(this.start);  // Starting timestamp 
    let ends = parseInt(this.end);     // Ending timestamp

    // Diff in seconds
    const diff = ends - starts;
    const numberIntervals = (diff / 60) / 200;
    const numberRequests = Math.ceil(numberIntervals / this.interval);

    const intervals = Array(numberRequests).fill().map((_, reqNum) => {
      const start = starts + (reqNum * requestSize);
      const end = (reqNum + 1 === numberRequests) ? ends : start + requestSize;
      return { start, end };
    });

    return intervals;
  }

with that interval, now you can recursively do:

async performInterval({ intervals }) {
    if (intervals.length == 0)
      return [];
    
    const interval = intervals[0];
    logger.info("Getting: " + interval.start + " - " + interval.end);
    const result = await this.performRequest({ start: interval.start, end: interval.end });
    this.results.push(result.result);
    logger.info("Result: " + result.result.length);
    this.storeResults();   // Here we store the results

    await timeout(400);   // We need to wait, because the exchange can block us
    await this.performInterval({ intervals: intervals.slice(1) });
  }

and the request is:

  async performRequest({ start, end }) {
    const results = await this.client.getKline({
                                                symbol: this.pair, 
                                                interval: this.interval,
                                                from: start, 
                                                end: end
                                              });
    return results;
  }

Hope this helps you!

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