Skip to content

Instantly share code, notes, and snippets.

@rrag
Last active August 19, 2019 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rrag/1741fe3e882f0eb144af to your computer and use it in GitHub Desktop.
Save rrag/1741fe3e882f0eb144af to your computer and use it in GitHub Desktop.
Force index with react-stockcharts
license: MIT
height: 570
"use strict";
var { ChartCanvas, Chart, series, scale, coordinates, tooltip, axes, indicator, helper } = ReStock;
var { CandlestickSeries, BarSeries, LineSeries, AreaSeries, RSISeries, StraightLine } = series;
var { discontinuousTimeScaleProvider } = scale;
var { CrossHairCursor, MouseCoordinateX, MouseCoordinateY, CurrentCoordinate } = coordinates;
var { EdgeIndicator } = coordinates;
var { OHLCTooltip, MovingAverageTooltip, SingleValueTooltip, RSITooltip } = tooltip;
var { XAxis, YAxis } = axes;
//console.log(indicator);
var { forceIndex, ema } = indicator;
var { fitWidth, TypeChooser } = helper;
class CandleStickChartWithForceIndexIndicator extends React.Component {
render() {
var { data, type, width, ratio } = this.props;
var fi = forceIndex()
.merge((d, c) => {d.fi = c})
.accessor(d => d.fi);
var fiEMA13 = ema()
.id(1)
.windowSize(13)
.sourcePath("fi")
.merge((d, c) => {d.fiEMA13 = c})
.accessor(d => d.fiEMA13);
return (
<ChartCanvas ratio={ratio} width={width} height={550}
margin={{left: 70, right: 70, top:20, bottom: 30}} type={type}
seriesName="MSFT"
data={data} calculator={[fi, fiEMA13]}
xAccessor={d => d.date} xScaleProvider={discontinuousTimeScaleProvider}
xExtents={[new Date(2012, 0, 1), new Date(2012, 6, 2)]}>
<Chart id={1} height={300}
yExtents={d => [d.high, d.low]}
padding={{ top: 10, right: 0, bottom: 20, left: 0 }}>
<YAxis axisAt="right" orient="right" ticks={5} />
<XAxis axisAt="bottom" orient="bottom" showTicks={false} outerTickSize={0} />
<MouseCoordinateY
at="right"
orient="right"
displayFormat={d3.format(".2f")} />
<CandlestickSeries />
<EdgeIndicator itemType="last" orient="right" edgeAt="right"
yAccessor={d => d.close} fill={d => d.close > d.open ? "#6BA583" : "#FF0000"}/>
<OHLCTooltip origin={[-40, -10]}/>
</Chart>
<Chart id={2} height={150}
yExtents={d => d.volume}
origin={(w, h) => [0, h - 350]} >
<YAxis axisAt="left" orient="left" ticks={5} tickFormat={d3.format(".0s")}/>
<MouseCoordinateY
at="left"
orient="left"
displayFormat={d3.format(".4s")} />
<BarSeries
yAccessor={d => d.volume}
fill={(d) => d.close > d.open ? "#6BA583" : "#FF0000"}
opacity={0.5} />
</Chart>
<Chart id={3} height={100}
yExtents={fi.accessor()}
origin={(w, h) => [0, h - 200]}
padding={{ top: 10, right: 0, bottom: 10, left: 0 }} >
<XAxis axisAt="bottom" orient="bottom" showTicks={false} outerTickSize={0} />
<YAxis axisAt="right" orient="right" ticks={4} tickFormat={d3.format(".0s")}/>
<MouseCoordinateY
at="right"
orient="right"
displayFormat={d3.format(".4s")} />
<AreaSeries baseAt={scale => scale(0)} yAccessor={fi.accessor()} />
<StraightLine yValue={0} />
<SingleValueTooltip
yAccessor={fi.accessor()}
yLabel="ForceIndex (1)"
yDisplayFormat={d3.format(".4s")}
origin={[-40, 15]}/>
</Chart>
<Chart id={4} height={100}
yExtents={fiEMA13.accessor()}
origin={(w, h) => [0, h - 100]}
padding={{ top: 10, right: 0, bottom: 10, left: 0 }} >
<XAxis axisAt="bottom" orient="bottom" />
<YAxis axisAt="right" orient="right" ticks={4} tickFormat={d3.format(".0s")}/>
<MouseCoordinateX
at="bottom"
orient="bottom"
displayFormat={d3.timeFormat("%Y-%m-%d")} />
<MouseCoordinateY
at="right"
orient="right"
displayFormat={d3.format(".4s")} />
<AreaSeries baseAt={scale => scale(0)} yAccessor={fiEMA13.accessor()} />
<StraightLine yValue={0} />
<SingleValueTooltip
yAccessor={fiEMA13.accessor()}
yLabel={`ForceIndex (${fiEMA13.windowSize()})`}
yDisplayFormat={d3.format(".4s")}
origin={[-40, 15]}/>
</Chart>
<CrossHairCursor />
</ChartCanvas>
);
}
};
CandleStickChartWithForceIndexIndicator.propTypes = {
data: React.PropTypes.array.isRequired,
width: React.PropTypes.number.isRequired,
ratio: React.PropTypes.number.isRequired,
type: React.PropTypes.oneOf(["svg", "hybrid"]).isRequired,
};
CandleStickChartWithForceIndexIndicator.defaultProps = {
type: "svg",
};
CandleStickChartWithForceIndexIndicator = fitWidth(CandleStickChartWithForceIndexIndicator);
var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S");
d3["tsv"]("//rrag.github.io/react-stockcharts/data/MSFT.tsv", (err, data) => {
/* change MSFT.tsv to MSFT_full.tsv above to see how this works with lots of data points */
data.forEach((d, i) => {
d.date = new Date(d3.timeParse("%Y-%m-%d")(d.date).getTime());
d.open = +d.open;
d.high = +d.high;
d.low = +d.low;
d.close = +d.close;
d.volume = +d.volume;
// console.log(d);
});
/* change the type from hybrid to svg to compare the performance between svg and canvas */
ReactDOM.render(<TypeChooser type="hybrid">{type => <CandleStickChartWithForceIndexIndicator data={data} type={type} />}</TypeChooser>, document.getElementById("chart"));
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Stockcharts - CandleStickChartWithForceIndexIndicator Example</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.7/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.17.0/babel.min.js"></script>
<!-- <script type="text/javascript" src="//unpkg.com/react-stockcharts@latest/dist/react-stockcharts.min.js"></script> -->
<script type="text/javascript" src="//rrag.github.io/react-stockcharts/dist/react-stockcharts.min.js"></script>
</head>
<body>
<span id="iconPreload" class="glyphicon glyphicon-arrow-down"></span>
<div id="chart">
</div>
<script>
// Use babel transform so the examples work on the browser
d3.request("./CandleStickChartWithForceIndexIndicator.jsx")
.get(function(err, data) {
var outputEl = document.getElementById('chart');
try {
var output = Babel.transform(data.responseText, { presets: ["es2015", "react", "stage-3"] }).code;
eval(output);
} catch (ex) {
outputEl.innerHTML = 'ERROR: ' + ex.message;
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment