package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class BarLoading 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();
        
        GetCandles(Instrument.EURUSD, Period.WEEKLY, 0, 200);
        GetCandles(Instrument.EURUSD, Period.DAILY_SUNDAY_IN_MONDAY, 0, 200);
        GetCandles(Instrument.EURUSD, Period.FOUR_HOURS, 0, 200);
        GetCandles(Instrument.EURUSD, Period.FIFTEEN_MINS, 0, 200);
        GetCandles(Instrument.EURUSD, Period.FIVE_MINS, 0, 200);

    }

    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
    {
        IBar bar = history.getBar(Instrument.EURUSD, Period.FIVE_MINS, OfferSide.BID, 0);            if(bar == null) console.getOut().println(" --> onTick() -> could not load M5-Bar!");
        bar = history.getBar(Instrument.EURUSD, Period.FIFTEEN_MINS, OfferSide.BID, 0);              if(bar == null) console.getOut().println(" --> onTick() -> could not load M15-Bar!");
        bar = history.getBar(Instrument.EURUSD, Period.FOUR_HOURS, OfferSide.BID, 0);                if(bar == null) console.getOut().println(" --> onTick() -> could not load H4-Bar!");
        bar = history.getBar(Instrument.EURUSD, Period.DAILY_SUNDAY_IN_MONDAY, OfferSide.BID, 0);    if(bar == null) console.getOut().println(" --> onTick() -> could not load D1-Bar!");
        bar = history.getBar(Instrument.EURUSD, Period.WEEKLY, OfferSide.BID, 0);                    if(bar == null) console.getOut().println(" --> onTick() -> could not load W1-Bar!");
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    
    /****************************************************************************************/
    /*    loads (num)Bars before Index(start)                                               */
    /****************************************************************************************/
    private void GetCandles(Instrument instrument, Period tf, int start, int num)
    {
        long now = 0;
        List<IBar> bars = new ArrayList<IBar>();

        try {
            now = history.getBar(instrument, tf, OfferSide.BID, start).getTime();
        } catch (JFException e)
        {
            console.getOut().println(" -->  JFCandleHistory::GetCandles() -> Exception @ history.getBar().getTime();    --> could not determine Start-Time of Bar!");
            e.printStackTrace();
        }

        try {
            bars = history.getBars(instrument, tf, OfferSide.BID, Filter.WEEKENDS, num, now, 0);
        } catch (JFException e)
        {
            console.getOut().println(" -->  JFCandleHistory::GetCandles() -> Exception @ history.getBars();    --> could not load Bars!");
            e.printStackTrace();
        }

        console.getOut().println(" --> JFCandleHistory::GetCandles(); -> " + bars.size() + " " + tf.toString() +"-Bars loaded");
    }
}