package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class Strategy implements IStrategy {
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;
	
	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();
        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.EURJPY);
        context.setSubscribedInstruments(instruments);
	}

	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 {
	}
    
    private void printValues(Instrument instrument, double[] values) {
        StringBuilder str = new StringBuilder(instrument + " " + values.length + " ");
        for (double elem : values) {
            if (!Double.isNaN(elem)) {
                str.append(elem).append(",");
            }
        }
        console.getOut().println(str.toString());
    }
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (period == Period.TEN_SECS) {
//            long time = history.getPreviousBarStart(period, history.getBarStart(period, tick.getTime()));
            long time = askBar.getTime();
            double[] notFiltered = indicators.zigzag(instrument, Period.TEN_SECS, OfferSide.ASK, 12, 5, 3, Filter.NO_FILTER, 100, time, 0);
            double[] filtered = indicators.zigzag(instrument, Period.TEN_SECS, OfferSide.ASK, 12, 5, 3, Filter.ALL_FLATS, 100, time, 0);
            printValues(instrument, notFiltered);
            printValues(instrument, filtered);
            console.getOut().println("---------");
        }
    }
}