package jforex;

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

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;

public class ExampleStrategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private ArrayList<Period> periods;
    
    private DateFormat dfm = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    
    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();
        periods = new ArrayList<Period>();
        //periods.add(Period.ONE_MIN);
        periods.add(Period.FOUR_HOURS);
        //periods.add(Period.DAILY);
    }

    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 {
        for (Period period : periods) {
            test(instrument,period,tick.getTime());
        }
    }
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    
    private void test(Instrument instrument, Period period, long time) throws JFException {
        time = history.getBarStart(period, time);
        List<IBar> bars = history.getBars(instrument, period, OfferSide.BID, Filter.WEEKENDS, 2, time, 0);
        if (time-bars.get(0).getTime()!=period.getInterval()) {
            console.getOut().println(instrument+": "+time+"-"+bars.get(0).getTime()+"=="+(time-bars.get(0).getTime())+"!="+period.getInterval());
            for (IBar bar : bars) {
                console.getOut().println("found bars: "+instrument+" "+bar);
            }
        }
        
    }
}