Skip to content

Commit

Permalink
Add FromTo fetching + Better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu2301 committed Dec 26, 2021
1 parent a4b9f66 commit e17b61d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
29 changes: 29 additions & 0 deletions examples/FromToData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const TradingView = require('../main');

/*
This example tests fetching chart
data of a number of candles before
or after a timestamp
*/

const client = new TradingView.Client();

const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
timeframe: '240',
range: 2, // Can be positive to get before or negative to get after
to: 1600000000,
});

// This works with indicators

TradingView.getIndicator('STD;Supertrend').then(async (indic) => {
console.log(`Loading '${indic.description}' study...`);
const SUPERTREND = new chart.Study(indic);

SUPERTREND.onUpdate(() => {
console.log('Prices periods:', chart.periods);
console.log('Study periods:', SUPERTREND.periods);
client.end();
});
});
12 changes: 7 additions & 5 deletions examples/GraphicIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ const client = new TradingView.Client();

const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
timeframe: 'W',
timeframe: '5',
range: 10000,
});

// TradingView.getIndicator('USER;01efac32df544348810bc843a7515f36').then((indic) => {
TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
// TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
TradingView.getIndicator('STD;Zig_Zag').then((indic) => {
const STD = new chart.Study(indic);

STD.onError((...err) => {
console.log('Chart error:', ...err);
console.log('Study error:', ...err);
});

STD.onReady(() => {
console.log('STD Loaded !');
console.log(`STD '${STD.instance.description}' Loaded !`);
});

STD.onUpdate(() => {
console.log(STD.graphic);
console.log('Graphic data:', STD.graphic);
// console.log('Tables:', changes, STD.graphic.tables);
// console.log('Cells', STD.graphic.tables[0].cells());
client.end();
Expand Down
16 changes: 10 additions & 6 deletions src/chart/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ChartTypes = {
* @prop {number} close Period close value
* @prop {number} max Period max value
* @prop {number} min Period min value
* @prop {number} change Period change absolute value
* @prop {number} volume Period volume value
*/

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ const ChartTypes = {
* @prop {number} minmove2 Minimum move value 2
* @prop {string} timezone Used timezone
* @prop {boolean} is_replayable If the replay mode is available
* @prop {boolean} has_adjustment If the adjustment mode is enabled ????
* @prop {boolean} has_adjustment If the adjustment mode is enabled
* @prop {boolean} has_extended_hours Has extended hours
* @prop {string} bar_source Bar source
* @prop {string} bar_transform Bar transform
Expand Down Expand Up @@ -200,7 +200,7 @@ module.exports = (client) => class ChartSession {
close: p.v[4],
max: p.v[2],
min: p.v[3],
change: Math.round(p.v[5] * 100) / 100,
volume: Math.round(p.v[5] * 100) / 100,
};
});

Expand Down Expand Up @@ -241,13 +241,16 @@ module.exports = (client) => class ChartSession {
/**
* @param {import('../types').TimeFrame} timeframe Chart period timeframe
* @param {number} [range] Number of loaded periods/candles (Default: 100)
* @param {number} [reference] Reference candle timestamp (Default is now)
*/
setSeries(timeframe = '240', range = 100) {
setSeries(timeframe = '240', range = 100, reference = null) {
if (!this.#currentSeries) {
this.#handleError('Please set the market before setting series');
return;
}

const calcRange = !reference ? range : ['bar_count', reference, range];

this.#periods = {};

this.#client.send(`${this.#seriesCreated ? 'modify' : 'create'}_series`, [
Expand All @@ -256,7 +259,7 @@ module.exports = (client) => class ChartSession {
's1',
`ser_${this.#currentSeries}`,
timeframe,
this.#seriesCreated ? '' : range,
this.#seriesCreated ? '' : calcRange,
]);

this.#seriesCreated = true;
Expand All @@ -268,6 +271,7 @@ module.exports = (client) => class ChartSession {
* @param {Object} [options] Chart options
* @param {import('../types').TimeFrame} [options.timeframe] Chart period timeframe
* @param {number} [options.range] Number of loaded periods/candles (Default: 100)
* @param {number} [options.to] Last candle timestamp (Default is now)
* @param {'splits' | 'dividends'} [options.adjustment] Market adjustment
* @param {'regular' | 'extended'} [options.session] Chart session
* @param {'EUR' | 'USD' | string} [options.currency] Chart currency
Expand Down Expand Up @@ -302,7 +306,7 @@ module.exports = (client) => class ChartSession {
`=${JSON.stringify(chartInit)}`,
]);

this.setSeries(options.timeframe, options.range);
this.setSeries(options.timeframe, options.range, options.to);
}

/**
Expand Down

0 comments on commit e17b61d

Please sign in to comment.