Add Indicators on Chart

The IChartPanel.add methods get used to plot an indicator on chart. One can retrieve parameters and appearance information of already plotted indicators by calling the IChartPanel.getIndicatorApperanceInfos method. One can also plot another indicator or chart objects on the same indicator panel.
IChartPanel.add
法用于在图表上绘制一个指示器。您可以通过调用 IChartPanel.getIndicatorApperanceInfos 来检索已经绘制的指示器的参数和外观信息。你也可以在同一个指示器面板上绘制另一个指示器或图表对象。

Use optional parameters

The following methods may be used to add indicators on chart. The latter method lets you specify values for indicator optional parameters.
下列方法可用于在图表上添加指标。后一种方法允许您为指示器可选参数指定值。

chart.add(indicators.getIndicator("ZIGZAG"));
chart.add(indicators.getIndicator("ET_Nico"), new Object[]{15});

Specify output style

Use the following code to specify the applied price, drawing style, colors, etc for the indicator you add:
使用以下代码来指定您所添加的指示器的应用价格、绘图样式、颜色等:

IChart chart = context.getChart(Instrument.EURUSD);
IIndicator indCOG = indicators.getIndicator("COG");
for (int i = 0; i < indCOG.getIndicatorInfo().getNumberOfInputs(); i++) {
  InputParameterInfo inputParameterInfo = indCOG.getInputParameterInfo(i);
  inputParameterInfo.setAppliedPrice(AppliedPrice.LOW);
}                                                   

chart.add(indCOG, 
  new Object[]{5, 6, MaType.SMA.ordinal()}, 
  new Color[]{Color.RED, Color.GREEN},
  new DrawingStyle[]{DrawingStyle.DASHDOT_LINE, DrawingStyle.LINE}, 
  new int[]{1, 2});        

Object array optParams, the second parameter of chart.addIndicator, stands for indicator-specific parameters whose info can be retrieved from indicator metadata.
chart.add 的第二个参数,对象数组 optParams。代表特定指标的参数,其信息可以从指示器 metadata 中检索。

Use on-chart indicator parameters

As mentioned above, one can retrieve parameters and appearance information of already plotted indicators by calling the IChartPanel.getIndicatorApperanceInfos method. Consider a strategy which calculates indicators with the same parameters as they appear on the last selected chart:
如上所述,可以通过调用 IChartPanel.getIndicatorApperanceInfos 来检索已经绘制的指示器的参数和外观信息。考虑一种策略,该策略使用与最后一个选定图表上相同的参数来计算指标:

private IIndicators indicators;
private IHistory history;
private IConsole console;
private IChart chart;

private int dataCount = 3;

@Override
public void onStart(IContext context) throws JFException {
    indicators = context.getIndicators();
    history = context.getHistory();
    console = context.getConsole();
    chart = context.getLastActiveChart();
    if (chart == null) {
        console.getErr().println("No chart opened!");
        return;
    }
    IFeedDescriptor feedDescriptor = chart.getFeedDescriptor();
    if(feedDescriptor.getDataType() == DataType.TICKS){
        console.getWarn().println("Tick charts need to get calculate with from-to method");
        return;
    }

    for (IIndicatorAppearanceInfo info : chart.getIndicatorApperanceInfos()) {
        AppliedPrice[] appliedPrices = new AppliedPrice[info.getDrawingStyles().length];
        Arrays.fill(appliedPrices, AppliedPrice.CLOSE);
        OfferSide[] offerSides = new OfferSide[info.getDrawingStyles().length];
        Arrays.fill(offerSides, chart.getSelectedOfferSide());
        IIndicator indicator = indicators.getIndicator(info.getName());
        ITimedData feedData = history.getFeedData(feedDescriptor, 0);
        Object[] result = indicators.calculateIndicator(
                feedDescriptor, offerSides, info.getName(), appliedPrices, info.getOptParams(),
                dataCount, feedData.getTime(), 0);
        for (int i = 0; i < indicator.getIndicatorInfo().getNumberOfOutputs(); i++) {
            OutputParameterInfo.Type outputType = indicator.getOutputParameterInfo(i).getType();
            String resultStr = 
                    outputType == OutputParameterInfo.Type.DOUBLE ? Arrays.toString((double[]) result[i])
                    : outputType == OutputParameterInfo.Type.INT ? Arrays.toString((int[]) result[i])
                    : "object outputs need special processing";
            console.getOut().format(
                "%s %s last %s values: %s", 
                info.getName(), indicator.getOutputParameterInfo(i).getName(), dataCount, resultStr).println();
        }

    }
    context.stop();
}

CalculateIndicatorsFromChart.java

Include in OHLC

Consider a strategy which plots EMA and MACD indicators on chart and adds an OHLC informer with indicator showing option. The strategy also calculates the indicators on every bar, such that one can verify that the values match:

ChartAndStrategy.jpg

The function that adds the indicators and the OHLC informer:

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.indicators = context.getIndicators();

        IChart chart = null;
        for(IChart c : context.getCharts(instrument)){
            if(c.getSelectedOfferSide() == this.side
                    && c.getSelectedPeriod() == this.period
                    && c.getFilter() == this.filter){
                chart = c;
                break;
            }
            if(c.getFilter() != this.filter){
                console.getErr().println(
                    "Filter dismatch! Change in platform settings the filter " +
                            "to the same one that the strategy is using!");
                context.stop();
            }
        }
        if(chart == null){
            chart = context.openChart(new TimePeriodAggregationFeedDescriptor(instrument, period, side, filter));
        }

        chart.add(indicators.getIndicator("EMA"), 
                  new Object[] { emaTimePeriod });
        chart.add(indicators.getIndicator("MACD"), 
                  new Object[] { macdFastPeriod, macdSlowPeriod, macdSignalPeriod });

        IOhlcChartObject ohlc = null;
        for (IChartObject obj : chart.getAll()) {
            if (obj instanceof IOhlcChartObject) {
                ohlc = (IOhlcChartObject) obj;
            }
        }
        if (ohlc == null) {
            ohlc = chart.getChartObjectFactory().createOhlcInformer();
            chart.add(ohlc);
        }
        ohlc.setShowIndicatorInfo(true);

        //calculate on the previous bar over candle interval such that the filters get used
        long time = context.getHistory().getBar(instrument, period, side, 1).getTime();
        double[][] macd = indicators.macd(instrument, period, side, AppliedPrice.CLOSE, 
            macdFastPeriod, macdSlowPeriod, macdSignalPeriod, filter, 1, time, 0);

        double[] ema = indicators.ema(instrument, period, side, AppliedPrice.CLOSE, 
            emaTimePeriod, filter, 1, time, 0);

        console.getOut().format("%s - ema=%s, macd=%s (by candle interval)", 
            DateUtils.format(time), arrayToString(ema), arrayToString(macd)).println();
    }

PlotEmaMacdWithOhlc.java

Also consider a generalized version that works with any feed:

PlotEmaMacdWithOhlcFeed.java

Plot on newly opened chart

Consider creating a wrapper class which describes both the indicator data and the feed it is supposed to be calculated on:

    class IndDataAndFeed{

        private IFeedDescriptor feedDescriptor;
        private String indicatorName;
        private Object[] optionalInputs;
        private int outputIndex;
        private IIndicator indicator;
        private IChart chart;

        public IndDataAndFeed(
                String indicatorName, Object[] optionalInputs, int outputIndex, IFeedDescriptor feedDescriptor) {
            this.feedDescriptor = feedDescriptor;
            this.indicatorName = indicatorName;
            this.optionalInputs = optionalInputs;
            this.outputIndex = outputIndex;
        }

        public void openChartAddIndicator(){
            for(IChart openedChart : context.getCharts(feedDescriptor.getInstrument())){
                IFeedDescriptor chartFeed = openedChart.getFeedDescriptor();
                if(chartFeed.getPeriod() == feedDescriptor.getPeriod() 
                        && chartFeed.getOfferSide() == feedDescriptor.getOfferSide()){
                    chart = openedChart;
                }
            }
            if(chart == null){
                chart = context.openChart(feedDescriptor);
            }
            if(chart.getFeedDescriptor().getFilter() != feedDescriptor.getFilter()){
                console.getErr().println("Chart filter " + chart.getFeedDescriptor().getFilter() + 
                    " does not match indicator feed filter " + feedDescriptor.getFilter() +
                    " please adjust the platform settings");
                context.stop();
            }
            indicator = indicators.getIndicator(indicatorName);

            int outputCount = indicator.getIndicatorInfo().getNumberOfOutputs();
            Color[] colors = new Color[outputCount];
            DrawingStyle[] styles = new DrawingStyle[outputCount];
            int[] widths = new int[outputCount];
            for(int outIdx = 0; outIdx< outputCount; outIdx++){
                OutputParameterInfo outInfo = indicator.getOutputParameterInfo(outIdx);
                if(outInfo == null){
                    console.getErr().println(indicatorName + " "  + outIdx + "is null");
                    continue;
                }
                //make colors darker
                colors[outIdx] = new Color(new Random().nextInt(256 * 256 * 256));
                //make solid-line inputs dashed
                styles[outIdx] = outInfo.getDrawingStyle() == DrawingStyle.LINE ?
                        DrawingStyle.DASH_LINE : 
                        outInfo.getDrawingStyle();
                //thicken the 1-width lines
                widths[outIdx] = 2;
            }

            chart.add(indicator, optionalInputs, colors, styles, widths);

            //show indicator values in ohlc
            IOhlcChartObject ohlc = null;
            for (IChartObject obj : chart.getAll()) {
                if (obj instanceof IOhlcChartObject) {
                    ohlc = (IOhlcChartObject) obj;
                }
            }
            if (ohlc == null) {
                ohlc = chart.getChartObjectFactory().createOhlcInformer();
                chart.add(ohlc);
            }
            ohlc.setShowIndicatorInfo(true);
        }

        public double getCurrentValue() throws JFException{
            Object[] outputs = indicators.calculateIndicator(
                    feedDescriptor, 
                    new OfferSide[] { feedDescriptor.getOfferSide() }, 
                    indicatorName,
                    new AppliedPrice[] { AppliedPrice.CLOSE }, 
                    optionalInputs, 
                    0);
            double value = (Double) outputs[outputIndex];
            return value;
        }

        public void removeFromChart(){
            if(chart != null && indicator != null){
                chart.removeIndicator(indicator);
            }
        }

        @Override 
        public String toString(){
            return String.format("%s %s on %s %s feed",
                indicatorName, 
                Arrays.toString(optionalInputs), 
                feedDescriptor.getOfferSide(),
                feedDescriptor.getPeriod());
        }

    }

Then add a list of indicators which you wish to calculate and plot on respective chart:

    private List<IndDataAndFeed> calculatableIndicators = new ArrayList<>(Arrays.asList(new IndDataAndFeed[]{
            new IndDataAndFeed("MACD", new Object[] {12,26,9}, 0,
                               new TimePeriodAggregationFeedDescriptor(
                                       instrument, Period.FIVE_MINS, OfferSide.BID, Filter.WEEKENDS)),
            new IndDataAndFeed("RSI", new Object[] {50}, 0,
                               new TimePeriodAggregationFeedDescriptor(
                                       instrument, Period.FIVE_MINS, OfferSide.BID, Filter.WEEKENDS)),
            new IndDataAndFeed("RSI", new Object[] {50}, 0,
                               new TimePeriodAggregationFeedDescriptor(
                                       instrument, Period.ONE_HOUR, OfferSide.BID, Filter.WEEKENDS)),
            new IndDataAndFeed("CCI", new Object[] {14}, 0,
                               new TimePeriodAggregationFeedDescriptor(
                                       instrument, Period.FIFTEEN_MINS, OfferSide.BID, Filter.WEEKENDS)),
            new IndDataAndFeed("CCI", new Object[] {14}, 0,
                               new TimePeriodAggregationFeedDescriptor(
                                       instrument, Period.ONE_HOUR, OfferSide.BID, Filter.WEEKENDS))
    }));

And finally make calls from the strategy:

    @Override
    public void onStart(IContext context) throws JFException {

        if(!context.getSubscribedInstruments().contains(instrument)){
            context.setSubscribedInstruments(
                    new HashSet<Instrument>(
                            Arrays.asList(new Instrument [] {instrument})), true);
        }

        this.context = context;
        console = context.getConsole();
        indicators = context.getIndicators();

        for(IndDataAndFeed indDataAndFeed : calculatableIndicators){
            indDataAndFeed.openChartAddIndicator();
        }
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {
            return;
        }
        for (IndDataAndFeed indDataAndFeed : calculatableIndicators) {
            double value = indDataAndFeed.getCurrentValue();
            print("%s current value=%.5f", indDataAndFeed, value);
        }
    }

    @Override
    public void onStop() throws JFException {
        for(IndDataAndFeed indDataAndFeed : calculatableIndicators){
            indDataAndFeed.removeFromChart();
        }
    }

FeedMultiIndOpenChartsOhlc.java

Randomize output style and include in OHLC

Consider modifying the previous example, by modifying the indicator ouput styles and including the indicator values in the OHLC information object:

public void openChartAddIndicator(){
    for(IChart openedChart : context.getCharts(feedDescriptor.getInstrument())){
        IFeedDescriptor chartFeed = openedChart.getFeedDescriptor();
        if(chartFeed.getPeriod() == feedDescriptor.getPeriod() && 
                        chartFeed.getOfferSide() == feedDescriptor.getOfferSide()){
            chart = openedChart;
        }
    }
    if(chart == null){
        chart = context.openChart(feedDescriptor);
    }
    if(chart.getFeedDescriptor().getFilter() != feedDescriptor.getFilter()){
        console.getErr().println(
                "Chart filter " + chart.getFeedDescriptor().getFilter() + 
                                " does not match indicator feed filter " +
                                feedDescriptor.getFilter() + " please adjust the platform settings");
        context.stop();
    }
    indicator = indicators.getIndicator(indicatorName);

    int outputCount = indicator.getIndicatorInfo().getNumberOfOutputs();
    Color[] colors = new Color[outputCount];
    DrawingStyle[] styles = new DrawingStyle[outputCount];
    int[] widths = new int[outputCount];
    for(int outIdx = 0; outIdx< outputCount; outIdx++){
        OutputParameterInfo outInfo = indicator.getOutputParameterInfo(outIdx);
        if(outInfo == null){
            console.getErr().println(indicatorName + " "  + outIdx + "is null");
            continue;
        }
        //make colors darker
        colors[outIdx] = new Color(new Random().nextInt(256 * 256 * 256));
        //make solid-line inputs dashed
        styles[outIdx] = outInfo.getDrawingStyle() == DrawingStyle.LINE ? 
                    DrawingStyle.DASH_LINE : 
                        outInfo.getDrawingStyle();
        //thicken the 1-width lines
        widths[outIdx] = 2;
    }

    chart.add(indicator, optionalInputs, colors, styles, widths);

    //show indicator values in ohlc
    IOhlcChartObject ohlc = null;
    for (IChartObject obj : chart.getAll()) {
        if (obj instanceof IOhlcChartObject) {
            ohlc = (IOhlcChartObject) obj;
        }
    }
    if (ohlc == null) {
        ohlc = chart.getChartObjectFactory().createOhlcInformer();
        chart.add(ohlc);
    }
    ohlc.setShowIndicatorInfo(true);
}

FeedMultiIndOpenChartsOhlc.java

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,176评论 5 469
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,190评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,232评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,953评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,879评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,177评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,626评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,295评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,436评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,365评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,414评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,096评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,685评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,771评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,987评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,438评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,032评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,269评论 0 10
  • 《金文诚〈论语〉学习笔记463尧曰第二十1》 【尧曰:"咨,尔舜,天之历数在尔躬,允执其中。四海穷困,天禄永终。"...
    金吾生阅读 424评论 1 1
  • 我出生在一个破旧落后的小城镇,很小的时候爸爸还用粮票买食物,零花钱经常是零或者几毛钱,上小学爸爸给我两块钱那次是我...
    夕屿Nicole阅读 464评论 0 2
  • 郭芳艳 焦点网络初级五期 坚持原创分享第157天 感觉很惭愧,大家可能都已经报过讲课的名字了,可是我却...
    冰山蓝鹰阅读 169评论 0 0
  • 我们从小在她的臂弯里初生,跌倒,受伤,长大,当我们能用肩膀拥抱她时,她已悄然走远,岁月带走了她曾经如花的笑颜和被我...
    我有一把刀阅读 294评论 0 0