package jforex.bugtests;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.MaType;

public class StochTest3 implements IStrategy {

	public Instrument selectedInstrument = Instrument.EURUSD;
	public Period fixedPeriod = Period.TEN_SECS;
	public OfferSide side = OfferSide.BID;
	
	private IHistory history;
	private IConsole console;
	private IIndicators indicators;


	
	private int fastKPeriod = 4;
	private int slowKPeriod = 10;
	private MaType slowKMaType = MaType.SMA;
	private int slowDPeriod = 15;
	private MaType slowDMaType = MaType.SMA;
	private int shift = 1;

	@SuppressWarnings("serial")
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
		{
			setTimeZone(TimeZone.getTimeZone("GMT"));
		}
	};
	public static DecimalFormat df = new DecimalFormat("0.00000");
	
	@Override
	public void onStart(IContext context) throws JFException {
		System.out.println(1111);
		this.history = context.getHistory();
		this.console = context.getConsole();
		this.indicators = context.getIndicators();

		print("strategy start");

	}

	@Override
	public void onTick(Instrument instrument, ITick tick) throws JFException {
	}

	@Override
	public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
		if (!selectedInstrument.equals(instrument) || !period.equals(fixedPeriod)) {
			return;
		}
		double[] stochastic = indicators.stoch(instrument, period, side, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType,
				shift);
		double sma = indicators.sma(instrument, period, side, IIndicators.AppliedPrice.MEDIAN_PRICE, 10, 4);
		createOrderOnStochastic(instrument, bidBar, stochastic);
		
		print(sdf.format(bidBar.getTime()) + " stoch: " + arrayToString(stochastic) + " sma: " + df.format(sma));
		System.out.println(sdf.format(bidBar.getTime()) + " stoch: " + arrayToString(stochastic) + " sma: " + df.format(sma));
	}
	
	
	private void print(Object o){
		//console.getOut().println(o);
		System.out.println(o);
	}

	public static String arrayToString(double[] arr) {
		String str = "";
		for (int r = 0; r < arr.length; r++) {
			str += "[" + r + "] " + df.format(arr[r]) + "; ";
		}
		return str;
	}

	private void createOrderOnStochastic(Instrument instrument, IBar bidBar, double[] stochastic) {}

	@Override
	public void onMessage(IMessage message) throws JFException {}

	@Override
	public void onAccount(IAccount account) throws JFException {}

	@Override
	public void onStop() throws JFException {}

}
