package jforex;

import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.*;
import com.dukascopy.api.feed.FeedDescriptor;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators.AppliedPrice;

public class SARIssue 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();
    }

    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 {
        if ( instrument.equals(Instrument.EURUSD) ) {
            // That one works
            print("SAR : "+ indicators.sar(Instrument.EURUSD, Period.ONE_MIN, OfferSide.BID, 0.02, 0.2, 0));
            //
            FeedDescriptor feed = new FeedDescriptor();
            feed.setPeriod(Period.ONE_MIN);
            feed.setInstrument(Instrument.EURUSD);
            feed.setDataType(DataType.TIME_PERIOD_AGGREGATION);
            feed.setOfferSide(OfferSide.BID);
            feed.setFilter(Filter.NO_FILTER);
            
            Object[] output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { null }, new Object[] { 0.02, 0.2 }, 0);
            print("Sar feed : [0] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", null, new Object[] { 0.02, 0.2 }, 0);
            print("Sar feed : [1] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 0.02, 0.2 }, 0);
            print("Sar feed : [2] " + output[0]);
            
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { null }, new Object[] { 0.02, 0.2 }, 1);
            print("Sar feed : [3] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", null, new Object[] { 0.02, 0.2 }, 1);
            print("Sar feed : [4] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 0.02, 0.2 }, 1);
            print("Sar feed : [5] " + output[0]); 
            
            long time = history.getBar(Instrument.EURUSD, feed.getPeriod(), feed.getOfferSide(), 0).getTime();
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { null }, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [6] " + ((double[])output[0])[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", null, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [7] " + ((double[])output[0])[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [8] " + ((double[])output[0])[0]); 
            
             time = history.getBar(Instrument.EURUSD, feed.getPeriod(), feed.getOfferSide(), 1).getTime();
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { null }, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [9] " + ((double[])output[0])[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", null, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [10] " + ((double[])output[0])[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 0.02, 0.2 }, 1, time,0);
            print("Sar feed : [11] " + ((double[])output[0])[0]);   
            
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { null }, new Object[] { 0.2, 0.02 }, 0);
            print("Sar feed : [12] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", null, new Object[] { 0.2, 0.02 }, 0);
            print("Sar feed : [13] " + output[0]);
            output = indicators.calculateIndicator(feed,new OfferSide[]{feed.getOfferSide()}, "SAR", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 0.2, 0.02 }, 0);
            print("Sar feed : [14] " + output[0]);                                                                             
        }
    }
    
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    
    private void print(String message) {
        console.getOut().println(message);
    }    
}