create tradingview screen in typescript

To create a TradingView screen in TypeScript, you'll need to follow these steps:

  1. Install the tradingview/charting_library package and its dependencies.
    index.ts
    npm install tradingview/charting_library
    
    41 chars
    2 lines
  2. Create a new React component that will hold the TradingView chart.
    index.ts
    import * as React from 'react';
    import * as TradingView from 'tradingview/charting_library';
    
    interface TradingViewProps {
      ticker: string;
      interval: string;
      timeframe: string;
    }
    
    class TradingViewChart extends React.Component<TradingViewProps, {}> {
      private tvWidget: any;
    
      public componentDidMount() {
        const widgetOptions: TradingView.WidgetOptions = {
          symbol: this.props.ticker,
          interval: this.props.interval,
          timeframe: this.props.timeframe,
          fullscreen: true,
          autosize: true,
          container_id: 'tv_chart_container',
          // Add any custom indicators, studies etc. here
          // studies_overrides: {},
        };
    
        const tvWidget = new TradingView.widget(widgetOptions);
        this.tvWidget = tvWidget;
      }
    
      public componentWillUnmount() {
        if (this.tvWidget !== null) {
          this.tvWidget.remove();
          this.tvWidget = null;
        }
      }
    
      public render() {
        return (
          <div id="tv_chart_container" />
        );
      }
    }
    
    export default TradingViewChart;
    
    1009 chars
    44 lines
  3. Add the TradingViewChart component to your app and pass in the required props.
    index.ts
    <TradingViewChart
      ticker="AAPL"
      interval="D"
      timeframe="1D"
    />
    
    69 chars
    6 lines
  4. Make sure to add the necessary CSS and make the required API calls according to your project requirements.

Note: this is just a starting point and there may be further customization and configuration needed to suit your particular use case.

gistlibby LogSnag