package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.indicators.IIndicator;
import java.text.DecimalFormat;
import java.util.*;

public class indicatorTest implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    public static DecimalFormat df = new DecimalFormat("0.00000");
    
    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();
        
        atrIndicatorTest();
        context.stop();
    }
    
    void atrIndicatorTest()throws JFException{
        IIndicator atrIndicator = indicators.getIndicator("ATR");
        atrIndicator.setOptInputParameter(0, 14);

        int barsToLoad = 20;
        Instrument instrument = Instrument.EURUSD;
        Period period = Period.ONE_HOUR;
        long currentBarTime = history.getBarStart(period,history.getLastTick(instrument).getTime());
        List<IBar> bars = history.getBars(instrument,period,OfferSide.BID,Filter.WEEKENDS,barsToLoad,currentBarTime,0);
        //console.getOut().println(bars.toString());
        double [][] arr = new double[5][barsToLoad];
        for (int i = 0; i < barsToLoad; i++) {
            //open=0, close=1, high=2, low=3, volume=4
            IBar bar = bars.get(i);
            arr[0][i] = bar.getOpen();
            arr[1][i] = bar.getClose();
            arr[2][i] = bar.getHigh();
            arr[3][i] = bar.getLow();
            arr[4][i] = bar.getVolume();
        }       
        atrIndicator.setInputParameter(0, arr);
        
        //set outputs
        double [] resultArr = new double [barsToLoad-atrIndicator.getLookback()];
        atrIndicator.setOutputParameter(0, resultArr);
        
        console.getOut().format("look back: %d, look forward: %d", atrIndicator.getLookback(), atrIndicator.getLookforward()).println();

        //calculate indicator values on custon data
        atrIndicator.calculate(atrIndicator.getLookback(), barsToLoad - 1);
        
        //print results
        console.getOut().println("resultArr CustomData: " + arrayToString(resultArr));
        
        //print indicator values calculate on jforex feed
        console.getOut().println("resultArr JforexFeed: " + arrayToString(indicators.atr(instrument, period, OfferSide.BID, 14, Filter.WEEKENDS, barsToLoad-atrIndicator.getLookback(), currentBarTime, 0)));
    }


    public static String arrayToString(double[] arr) {
        String str = "";
        for (int r = 0; r < arr.length; r++) {
            str += "[" + r + "] " + df.format(arr[r]) + "; ";
        }
        return str;
    }
    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 {
    }
}