/*     
 * This is a test programm only, do not use for trading! 
 * It checks calculateIndicator function with a IFeedDescriptor
 * At current version 2.15 this throws an exception in forward test
 *
 * (c) Stash GmbH Muenchen / Germany 
 *     www.stash.de
 *     Author: Bernhard Schicht
 *     Email: exp@stash.de
 */

package jforex.rangebars;

import java.util.List;

import com.dukascopy.api.*;
import com.dukascopy.api.feed.FeedDescriptor;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IRangeBar;
import com.dukascopy.api.feed.IRangeBarFeedListener;
import com.dukascopy.api.IIndicators.AppliedPrice;

public class RangeBarsIndicatorBug implements IStrategy {

    @Configurable("")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("")
    public OfferSide offerSide = OfferSide.BID;
    @Configurable("")
    public int priceRangePips = 2;

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

    @Override
    public void onStart(IContext context) throws JFException {
        indicators = context.getIndicators();
        console = context.getConsole();
        history = context.getHistory();

        // range bar feed
        final IFeedDescriptor feedDescriptor = new FeedDescriptor();
        feedDescriptor.setDataType(DataType.PRICE_RANGE_AGGREGATION);
        feedDescriptor.setOfferSide(offerSide);
        feedDescriptor.setInstrument(instrument);
        feedDescriptor.setPriceRange(PriceRange.valueOf(priceRangePips));
        
        context.subscribeToRangeBarFeed(instrument, offerSide, PriceRange.valueOf(priceRangePips),
            new IRangeBarFeedListener() {       

                public void onBar(Instrument instrument, OfferSide offerSide, PriceRange priceRange, IRangeBar bar) {
                    // calculate HT_TRENDLINE indicator
                    try{           
                        Object[] result = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "HT_TRENDLINE",
                                new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] {  }, 0);
                        double value = (Double) (result[0]);
                        console.getOut().println("rbar pips = "+priceRangePips+": "+ bar+"; HT_TRENDLINE = "+value);
                    }
                    catch (Exception e){
                        e.printStackTrace(console.getErr());
                    }
                }
        });
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onStop() throws JFException {
    }

}
