package jforex;

import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IShortLineChartObject;
import java.awt.Color;


public class BBandsDiscrepancy implements IStrategy {
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period period = Period.THIRTY_MINS;
    @Configurable("MA Type")
    public IIndicators.MaType maType = IIndicators.MaType.SMA;
    @Configurable("Filter")
    public Filter filter = Filter.WEEKENDS;
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;
    
    IChart chart;
    double BBandHigh;
    double BBandLow;
    double BBandMiddle;
    double EMA;
    long len;
	
	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();
        
        console.getOut().println("MA type: " + maType + ", Filter: " + filter);
        chart = findChart(instrument, period);
        len = period.getInterval();
        int shift = 0;
        do
        {
            shift++;
            CalculateIndicators(shift);
            long barTime = history.getBar(instrument, period, OfferSide.BID, shift).getTime();

            drawShortLine(BBandHigh, barTime, BBandHigh, barTime + len, Color.blue);
            drawShortLine(BBandLow, barTime, BBandLow, barTime + len, Color.red);
            drawShortLine(BBandMiddle, barTime, BBandMiddle, barTime + len, Color.orange);
            drawShortLine(EMA, barTime, EMA, barTime + len, Color.black);

        }
        while (shift <= 50);
        
	}

	public void onAccount(IAccount account) throws JFException {
	}

	public void onMessage(IMessage message) throws JFException {
	}

	public void onStop() throws JFException {
        chart.removeAll();
	}

	public void onTick(Instrument instrument, ITick tick) throws JFException {
	}
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
            CalculateIndicators(1);
            long barTime = history.getBar(instrument, period, OfferSide.BID, 1).getTime();

            drawShortLine(BBandHigh, barTime, BBandHigh, barTime + len, Color.blue);
            drawShortLine(BBandLow, barTime, BBandLow, barTime + len, Color.red);
            drawShortLine(BBandMiddle, barTime, BBandMiddle, barTime + len, Color.orange);
            drawShortLine(EMA, barTime, EMA, barTime + len, Color.black);
    }
    
    public void drawShortLine(double price0, long time0, double price1, long time1, Color color)
    {
        if (chart != null)
        {
            IShortLineChartObject shortLine = chart.getChartObjectFactory().createShortLine();
            shortLine.setPrice(0, price0);
            shortLine.setPrice(1, price1);
            shortLine.setTime(0, time0);
            shortLine.setTime(1, time1);
            shortLine.setColor(color);
            shortLine.setOpacity(1.0f);
            chart.addToMainChart(shortLine);
        }
    }
    
    public IChart findChart(Instrument instrument, Period period)
    {
        Set<IChart> charts = context.getCharts(instrument);
        if (charts != null)
        {
            for (IChart tmpChart : charts)
            {
                if (tmpChart.getSelectedPeriod().equals(period))
                {
                    return tmpChart;
                }
            }
        }
        return null;
    }

    public void CalculateIndicators(int shift) throws JFException
    {
        long barTime = history.getBar(instrument, period, OfferSide.BID, shift).getTime();
//       double[] BBands = indicators.bbands(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 20, 1, 1,
//                                                        maType, shift);
        double[][] BBands = indicators.bbands(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE,
                                                          20, 1, 1, maType, filter, 1, barTime, 0);
//        EMA = indicators.ema(instrument, UsedPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, parent.EMAPeriod, shift);
        double[] EMAs = indicators.ema(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.OPEN, 200,
                                                 filter, 1, barTime, 0);

        BBandHigh = BBands[0][0];
        BBandMiddle = BBands[1][0];
        BBandLow = BBands[2][0];
//        BBandHigh = BBands[0];
//        BBandMiddle = BBands[1];
//        BBandLow = BBands[2];
        EMA = EMAs[0];
    }
}