package jforex;

import java.util.*;
import java.text.SimpleDateFormat;

import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.JFUtils;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;

public class ADR_Test implements IStrategy, IFeedListener {
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;

     private Period dailyPeriod = Period.createCustomPeriod(Unit.Day, 1, JFTimeZone.EET);
     private IFeedDescriptor dailyFeed;



	public Instrument instrument = Instrument.GBPUSD;

	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.console = context.getConsole();
		this.history = context.getHistory();
		this.context = context;
		this.indicators = context.getIndicators();
		this.userInterface = context.getUserInterface();

          dailyFeed = new TimePeriodAggregationFeedDescriptor(instrument, dailyPeriod, OfferSide.BID, Filter.WEEKENDS);
          context.subscribeToFeed(dailyFeed, this);
	}

	public void onAccount(IAccount account) throws JFException {
	}

	public void onMessage(IMessage message) throws JFException {
	}

	public void onStop() throws JFException {
	}

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

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (period != Period.FIFTEEN_MINS) {
            return;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
        sdf.setTimeZone(TimeZone.getTimeZone("EET"));
        console.getOut().println("bidBar Timestamp: " + sdf.format(bidBar.getTime()));  // LOW


        // CALC ADR
        double[][] adr = ADRCalc(dailyFeed, bidBar.getTime(), 5);
        //console.getOut().println("ADR length: " + adr[0].length);
        console.getOut().println("ADR acthigh: " + adr[0][adr[0].length-1]); // HIGH
        console.getOut().println("ADR actlow: " + adr[1][adr[0].length-1]);  // LOW
        //console.getOut().println("ADR prevlow: " + adr[0][adr[0].length-2]); // LOW
        //console.getOut().println("ADR prevhigh: " + adr[1][adr[0].length-2]);  // HIGH


    }

    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
    }

    public double[][] ADRCalc(IFeedDescriptor feedDescriptor, long timeFrom, int length) throws JFException {
        Object[] adrFeed = indicators.calculateIndicator(
                feedDescriptor,
                new OfferSide[] {OfferSide.BID},
                "ADR",
                new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
                new Object[] { length },
                length,
                timeFrom,
                0);
                double[][] ADR = { (double[]) adrFeed[0], (double[]) adrFeed[1] };
            return ADR;
    }
}
