package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class ErrorDemo implements IStrategy {
    // Declaring Initial Variables
    private IAccount account;
    private IConsole console;
    private IContext context;
    private IDataService dataService;
    private IEngine engine;
    private IHistory history;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private JFUtils utils;
    
    // Declaring Configurable Variables
    @Configurable("Trading Period")
    public Period tradingPeriod = Period.FOUR_HOURS;
    
    // Declaring Other Variables
    private double primarySentimentChange;
    private double secondarySentimentChange;
    private IBar prevAskBar;
    private IBar prevBidBar;
    
    public void onStart(IContext context) throws JFException {
        this.account = context.getAccount();
        this.console = context.getConsole();
        this.context = context;
        this.dataService = context.getDataService();
        this.engine = context.getEngine();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        this.utils = context.getUtils();
    }

    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 {
        // Filtering Non-Trading Periods
        if (!period.equals(tradingPeriod)) {
            return;
        }
        
        // Collecting Previous Bar Data
        prevAskBar = history.getBar(instrument, period, OfferSide.ASK, 2);
        prevBidBar = history.getBar(instrument, period, OfferSide.BID, 2);
        
        // Calculating Sentiment
        primarySentimentChange = dataService.getFXSentimentIndex(instrument.getPrimaryCurrency(), askBar.getTime()).getIndexValue() - dataService.getFXSentimentIndex(instrument.getPrimaryCurrency(), prevAskBar.getTime()).getIndexValue();
        console.getOut().println(instrument.name()+" Primary Sentiment Change: "+primarySentimentChange);
        secondarySentimentChange = dataService.getFXSentimentIndex(instrument.getSecondaryCurrency(), askBar.getTime()).getIndexValue() - dataService.getFXSentimentIndex(instrument.getSecondaryCurrency(), prevAskBar.getTime()).getIndexValue();
        console.getOut().println(instrument.name()+" Secondary Sentiment Change: "+secondarySentimentChange);
    }
}