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 RangeBarsMinMaxIndicator2 implements IStrategy {

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

    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();

        PriceRange priceRange = PriceRange.valueOf(priceRangePips);

        context.subscribeToRangeBarFeed(Instrument.EURUSD, OfferSide.BID, PriceRange.valueOf(2),
                new IRangeBarFeedListener() {
                    public void onBar(Instrument instrument, OfferSide offerSide, PriceRange priceRange, IRangeBar bar) {
                        console.getOut().println("Range Bar: " + instrument + ", " + offerSide + ", " + priceRange + ", " + bar);
                    }
                }
        );

        // pause to allow the subscription to complete
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // log bar values - to check that the indicator value is correct
        IRangeBar rangeBar = history.getRangeBar(instrument, offerSide, priceRange, 0);
        List<IRangeBar> rangeBars = history.getRangeBars(instrument, offerSide, priceRange, timePeriod, rangeBar.getTime(), 0);
        for (IRangeBar rb : rangeBars) {
            console.getOut().println(rb);
        }

        // range bar feed
        IFeedDescriptor feedDescriptor = new FeedDescriptor();
        feedDescriptor.setDataType(DataType.PRICE_RANGE_AGGREGATION);
        feedDescriptor.setOfferSide(OfferSide.BID);
        feedDescriptor.setInstrument(Instrument.EURUSD);
        feedDescriptor.setPriceRange(PriceRange.valueOf(priceRangePips));

        // calculate indicator
        Object[] result = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MINMAX",
                new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { timePeriod }, 0);
        double min = (Double) (result[0]);
        double max = (Double) (result[1]);

        console.getOut().println(String.format("Over last %s %s-pip range bars max=%s, min=%s", timePeriod, priceRangePips, min, max));
    }

    @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 {
    }

}
