TradingView-API icon indicating copy to clipboard operation
TradingView-API copied to clipboard

[V3] Same output multiple times

Open jadamcraig opened this issue 3 years ago • 3 comments

Describe the bug

I know this was previously reported as affecting v2 in Issue #23 , but I appear to be encountering it in v3.2.0 also. If I'm doing something incorrectly, please let me know!

To Reproduce

Version Info

$ npm list
[email protected] /Users/jacraig/Documents/node/strategy_optimiser2
├── @mathieuc/[email protected]
└── @mathieuc/[email protected] (git+ssh://[email protected]/Mathieu2301/TradingView-API.git#5e769af35d4e3de1281044a620de354650bb94a2)

./IndicatorTest.js

/* eslint-disable no-await-in-loop */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-restricted-syntax */
/* eslint-disable global-require */

const fs = require('fs');
require('@mathieuc/console')(
  '§', // Character you want to use (defaut: '§')
  true, // Active timestamp (defaut: false)
);

const TESTS = {};

function err(name) {
  return (...msg) => {
    TESTS[name].errors += 1;
    console.error('§30§101 > §0', ...msg);
  };
}

function warn(name) {
  return (...msg) => {
    TESTS[name].warnings += 1;
    console.warn('§30§103 > §0', ...msg);
  };
}

function log() {
  return (...msg) => console.log('§30§107 > §0', ...msg);
}

function success() {
  return (...msg) => console.info('§30§102 > §0', ...msg);
}

(async () => {
  console.info('§90§30§104 ==== Starting tests ==== §0');

  for (const file of fs.readdirSync('./tests').filter((f) => f.endsWith('.js'))) {
    const test = require(`./tests/${file}`);
    const name = file.replace(/\.js/g, '');
    TESTS[name] = { errors: 0, warnings: 0 };
    console.log(`§90§30§107 ${name}`);
    await new Promise((cb) => test(log(), success(), warn(name), err(name), cb));
  }

  console.info('§90§30§104 ==== ALL TESTS DONE ==== §0\n');
  Object.keys(TESTS).forEach((t) => {
    let color = '2';
    if (TESTS[t].warnings) color = '3';
    if (TESTS[t].errors) color = '1';

    console.info(`§90§30§10${color} ${t} §0 §91E§0: ${TESTS[t].errors}§0 §93W§0: ${TESTS[t].warnings}`);
    // console.info(` - §91 Errors:§0 ${TESTS[t].errors}`);
    // console.info(` - §93 Warnings:§0 ${TESTS[t].warnings}`);
  });

  process.exit(0);
})();

setTimeout(() => {
  console.log('§30§101 TIMEOUT §0');
  throw new Error('Timeout');
}, 60000);

./tests/4. Indicators.js

const TradingView = require('@mathieuc/tradingview');

module.exports = async (log, success, warn, err, cb) => {
  const client = new TradingView.Client();

  client.onError((...error) => {
    err('Client error', error);
    throw new Error('Client error');
  });

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

  chart.onError((...error) => {
    err('Chart error', error);
    throw new Error('Chart error');
  });

  TradingView.getIndicator('STD;Supertrend%Strategy').then((indicator) => {
    indicator.setOption('commission_type', 'percent');
    indicator.setOption('commission_value', 0);
    indicator.setOption('initial_capital', 25000);
    indicator.setOption('default_qty_value', 20);
    indicator.setOption('default_qty_type', 'percent_of_equity');
    indicator.setOption('currency', 'EUR');
    indicator.setOption('pyramiding', 10);

    const SuperTrend = new chart.Study(indicator);

    SuperTrend.onError((...error) => {
      err('SuperTrend error', error[0]);
      throw new Error('SuperTrend error');
    });

    let QTY = 10;

    SuperTrend.onUpdate(async () => {
      // MarketCipher B is a strategy so it sends a strategy report
      const perfReport = SuperTrend.strategyReport.performance;

      success('Performances:', {
        total: {
          trades: perfReport.all.totalTrades,
          perf: `${Math.round(perfReport.all.netProfitPercent * 10000) / 100} %`,
        },
        buy: {
          trades: perfReport.long.totalTrades,
          perf: `${Math.round(perfReport.long.netProfitPercent * 10000) / 100} %`,
        },
        sell: {
          trades: perfReport.short.totalTrades,
          perf: `${Math.round(perfReport.short.netProfitPercent * 10000) / 100} %`,
        },
      });

      if (QTY >= 50) {
        SuperTrend.remove();
        chart.delete();
        await client.end();
        cb();
        return;
      }

      QTY += 10;
      log('TRY WITH', QTY, '%');
      indicator.setOption('default_qty_value', QTY);

      SuperTrend.setIndicator(indicator);
    });
  });

};

Output:

$ node IndicatorTest.js
[06/03 16:16:09]  ==== Starting tests ====
[06/03 16:16:09]  4. Indicators
[06/03 16:16:10]  >  Performances: {
  total: { trades: 229, perf: '-2.73 %' },
  buy: { trades: 115, perf: '5.95 %' },
  sell: { trades: 114, perf: '-8.67 %' }
}
[06/03 16:16:10]  >  TRY WITH 20 %
[06/03 16:16:10]  >  Performances: {
  total: { trades: 229, perf: '-2.73 %' },
  buy: { trades: 115, perf: '5.95 %' },
  sell: { trades: 114, perf: '-8.67 %' }
}
[06/03 16:16:10]  >  TRY WITH 30 %
[06/03 16:16:11]  >  Performances: {
  total: { trades: 229, perf: '-4.92 %' },
  buy: { trades: 115, perf: '8.35 %' },
  sell: { trades: 114, perf: '-13.27 %' }
}
[06/03 16:16:11]  >  TRY WITH 40 %
[06/03 16:16:11]  >  Performances: {
  total: { trades: 229, perf: '-4.92 %' },
  buy: { trades: 115, perf: '8.35 %' },
  sell: { trades: 114, perf: '-13.27 %' }
}
[06/03 16:16:11]  >  TRY WITH 50 %
[06/03 16:16:11]  >  Performances: {
  total: { trades: 229, perf: '-10.76 %' },
  buy: { trades: 115, perf: '12.08 %' },
  sell: { trades: 114, perf: '-22.84 %' }
}
[06/03 16:16:11]  ==== ALL TESTS DONE ====

[06/03 16:16:11]  4. Indicators  E: 0 W: 0

Expected behavior

$ node IndicatorTest.js
[06/03 16:16:58]  ==== Starting tests ====
[06/03 16:16:58]  4. Indicators
[06/03 16:16:02]  >  Performances: {
  total: { trades: 229, perf: '-2.73 %' },
  buy: { trades: 115, perf: '5.95 %' },
  sell: { trades: 114, perf: '-8.67 %' }
}
[06/03 16:16:02]  >  TRY WITH 20 %
[06/03 16:16:02]  >  Performances: {
  total: { trades: 229, perf: '-2.73 %' },
  buy: { trades: 115, perf: '5.95 %' },
  sell: { trades: 114, perf: '-8.67 %' }
}
[06/03 16:16:02]  >  TRY WITH 30 %
[06/03 16:16:03]  >  Performances: {
  total: { trades: 229, perf: '-4.92 %' },
  buy: { trades: 115, perf: '8.35 %' },
  sell: { trades: 114, perf: '-13.27 %' }
}
[06/03 16:16:03]  >  TRY WITH 40 %
[06/03 16:16:04]  >  Performances: {
  total: { trades: 229, perf: '-7.62 %' },
  buy: { trades: 115, perf: '10.39 %' },
  sell: { trades: 114, perf: '-18.01 %' }
}
[06/03 16:16:04]  >  TRY WITH 50 %
[06/03 16:16:04]  >  Performances: {
  total: { trades: 229, perf: '-10.76 %' },
  buy: { trades: 115, perf: '12.08 %' },
  sell: { trades: 114, perf: '-22.84 %' }
}
[06/03 16:16:04]  ==== ALL TESTS DONE ====

[06/03 16:16:04]  4. Indicators  E: 0 W: 0

Environment:

  • OS: macOS Monterey v12.2.1
  • Node version: v8.5.1

jadamcraig avatar Mar 06 '22 21:03 jadamcraig

Hey @jadamcraig , Sorry to be speaking out in the middle of an issue. Is it possible i can hire you to make a custom script to retrieve data from this API? Can we talk on email if possible?

mtahreemalam avatar Mar 13 '22 15:03 mtahreemalam

 indicator.setOption('commission_type', 'percent');
    indicator.setOption('commission_value', 0);
    indicator.setOption('initial_capital', 25000);

how can i know how many options there are?

luca-fullstack avatar Aug 03 '22 03:08 luca-fullstack

 indicator.setOption('commission_type', 'percent');
    indicator.setOption('commission_value', 0);
    indicator.setOption('initial_capital', 25000);

how can i know how many options there are?

    STD.onReady(() => {
      console.log(STD.instance.inputs)
    })

mykz avatar Aug 06 '22 16:08 mykz