package jforex;

import java.util.*;

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;
    
    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 {
        context.getConsole().getInfo().println("onTick "+instrument);
        for (Period period : periods) {
            test(instrument,period,tick.getTime());
        }
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (periods.contains(period))
            test(instrument,period,askBar.getTime());
    }
    
    private void test(Instrument instrument, Period period, long time) throws JFException {
        int candlesBefore=2;
        int candlesAfter=0;
        Object[] res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "MA", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{3, IIndicators.MaType.SMA.ordinal()},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "MA", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{23, IIndicators.MaType.SMA.ordinal()},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "MA", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{88, IIndicators.MaType.SMA.ordinal()},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "MACD", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{12, 26, 9},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "AROON", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{14},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        res = indicators.calculateIndicator(instrument, period, new OfferSide[] {OfferSide.BID}, "STOCH", new AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Integer[]{5, 3, IIndicators.MaType.SMA.ordinal(),3, IIndicators.MaType.SMA.ordinal()},Filter.WEEKENDS,candlesBefore,time,candlesAfter);
        context.getConsole().getOut().println("testing "+time);
    }
}