package jforex;

import java.awt.Color;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.MaType;

public class TestStrategy implements IStrategy {
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;

    @Configurable("Period")
    public Period strategyPeriod = Period.TEN_MINS;

    @Configurable("Instrument")
    public Instrument strategyInstrument = Instrument.GBPJPY;

    @Configurable("AC indicator? (else Awesome")
    public boolean isAC=true;
	
	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();
		
		print("Started test of the "+(isAC ? "AC" : "Awesome")+" indicator "+strategyInstrument+" "+strategyPeriod);
	}

	public void onAccount(IAccount account) throws JFException {
	}

	public void onMessage(IMessage message) throws JFException {
	}

	public void onStop() throws JFException {
        print("Stopped!");
	}

	public void onTick(Instrument instrument, ITick tick) throws JFException {
	}
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

    	if (instrument.equals(strategyInstrument) && period.equals(strategyPeriod)) {

            IChart chart = context.getChart(instrument);
    		double pipValue = instrument.getPipValue();
            long ts = bidBar.getTime();

            if (isAC){
            	testAC(instrument, bidBar, chart, pipValue, ts);
            }else{
            	testAwesome(instrument, bidBar, chart, pipValue, ts);
            }
    	}
    }

	/**
	 * test the AC indicator (5,34)
	 * @param instrument
	 * @param bidBar
	 * @param chart
	 * @param pipValue
	 * @param ts
	 * @throws JFException
	 */
	private void testAC(Instrument instrument, IBar bidBar, IChart chart,
			double pipValue, long ts) throws JFException {
				
            double[] ac1= indicators.ac(instrument, strategyPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 5, 34 ,1);
//            double[][] ac= indicators.ac(instrument, strategyPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 5, 34,
//            		Filter.WEEKENDS, 1, ts, 0);

		double x0 = ac1[0]; // green
		double x1 = ac1[1]; // red

		print(ts+" - "+((x0 != 0) ? ("green "+x0) : ("  red "+x1))+"  (x0: "+x0+"  x1: "+ x1+")");

		// show the colour on the chart using signal arrows under the bar
		if (x0 != 0){
			if (chart != null) chart.draw("x0"+ts, IChart.Type.SIGNAL_UP, ts, bidBar.getLow()-pipValue*30);
		}
		if (x1 != 0){
			if (chart != null) chart.draw("x1"+ts, IChart.Type.SIGNAL_DOWN, ts, bidBar.getLow()-pipValue*30);
		}

	}

	/**
	 * test the awesome indicator (Close,5,34)
	 * 
	 * @param instrument
	 * @param bidBar
	 * @param chart
	 * @param pipValue
	 * @param ts
	 * @throws JFException
	 */
	private void testAwesome(Instrument instrument, IBar bidBar, IChart chart,
			double pipValue, long ts) throws JFException {

		double[] ao1= indicators.awesome(instrument, strategyPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 
				5, //fasterMaTimePeriod, 
				MaType.SMA, //fasterMaType, 
				34, //slowerMaTimePeriod, 
				MaType.SMA, //slowerMaType, 
				1); //shift)

		double x0 = ao1[0]; // zero
		double x1 = ao1[1]; // green
		double x2 = ao1[2]; // red

		print(ts+" - "+(!Double.isNaN(x1) ? ("green "+x1) : ("  red "+x2))+"  (x0: "+x0+"  x1: "+ x1+"  x2: "+x2+")");
		
		// show the colour on the chart using signal arrows under the bar
		if (!Double.isNaN(x1)){
			if (chart != null) chart.draw("x0"+ts, IChart.Type.SIGNAL_UP, ts, bidBar.getLow()-pipValue*30);
		}
		if (!Double.isNaN(x2)){
			if (chart != null) chart.draw("x1"+ts, IChart.Type.SIGNAL_DOWN, ts, bidBar.getLow()-pipValue*30);
		}

	}

	public void print(Object o) {
        this.console.getOut().println(o.toString());
    }
}
