package jforex;

import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.util.RangeBarFeedDescriptor;

@CustomIndicators("Track2.jfx")
public class TestCrossing implements IStrategy, IFeedListener {
    @Configurable("Feed")
    public IFeedDescriptor feedDescriptor =
            new RangeBarFeedDescriptor(
                    Instrument.GBPUSD, 
                    PriceRange.valueOf(13), 
                    OfferSide.BID
            );
    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();
        
        context.subscribeToFeed(feedDescriptor, this);
        context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
        
    }

    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 {
    }
    @Override
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
        try {
            Object[] resultByShiftS =  indicators.calculateIndicator(
                feedDescriptor, new OfferSide[] {OfferSide.BID}, "TRACK2", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, 
                new Object[]{ 3.5 }, 1);

                double test1 = (double) resultByShiftS[0];
                console.getOut().format("test1 Track2 3.5 = %.5f", test1).println();

            Object[] resultByShiftB =  indicators.calculateIndicator(
                feedDescriptor, new OfferSide[] {OfferSide.BID}, "TRACK2", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, 
                new Object[]{ 6.5 }, 1);

                double test2 = (double) resultByShiftB[0];
                console.getOut().format("test2 Track2 6.5 = %.5f", test2).println();

            Object[] result1 =  indicators.calculateIndicator(
                feedDescriptor, new OfferSide[] {OfferSide.BID}, "EMA", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, 
                new Object[]{ 50 }, 1);
    
                double test3 = (double) result1[0];
                console.getOut().format("test3 EMA50 = %.5f", test3).println();
    
            Object[] result2 =  indicators.calculateIndicator(
                feedDescriptor, new OfferSide[] {OfferSide.BID}, "EMA", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, 
                new Object[]{ 14 }, 1);
    
                double test4 = (double) result2[0];
                console.getOut().format("test4 EMA14 = %.5f", test4).println();
                
        } catch (JFException ex) {
            console.getErr().println(ex);
            ex.printStackTrace();
        }
    
    }
}