/*
 * Copyright (c) 2009 Dukascopy (Suisse) SA. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 * 
 * Neither the name of Dukascopy (Suisse) SA or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. DUKASCOPY (SUISSE) SA ("DUKASCOPY")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL DUKASCOPY OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF DUKASCOPY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */
package backtest;

import java.awt.Color;
import java.util.HashMap;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.drawings.IShortLineChartObject;
import com.dukascopy.api.indicators.IIndicator;

import data.Settings;

public class strangePlot implements IStrategy {
    private IEngine engine = null;
    private IIndicators indicators = null;
    private int tagCounter = 0;
    private IConsole console;
    private IContext context= null;
    
    private Instrument instrument= Instrument.EURUSD;
    public MaType                  maType         = MaType.SMA;
    public int                     timePeriod     = 20;
    public Period period = Period.FIFTEEN_MINS;

    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        indicators = context.getIndicators();
        this.console = context.getConsole();
        console.getOut().println("Started");
        this.context = context;
        
        if(context.getChart(instrument) != null) {
        	 
     
            IIndicator ma = indicators.getIndicator("MA");
            context.getChart(instrument).addIndicator(ma, new Object[] { timePeriod, maType.ordinal() });
        }
	
    }

    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            order.close();
        }
        console.getOut().println("Stopped");
    }

    int tickCount = 0; 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
			
			
			
			if(tickCount++ == 500){
			
				OfferSide side = OfferSide.ASK;
				AppliedPrice appliedPrice = AppliedPrice.CLOSE;
			double ma = context.getIndicators().ma(instrument, period, side, appliedPrice, timePeriod, maType, 0);
			double dev = context.getIndicators().stdDev(instrument, period, side, appliedPrice, timePeriod, 2, 0);
					
			addPointToLine("ma", Color.GREEN, tick.getTime(), ma);
			addPointToLine("bbUp", Color.RED, tick.getTime(), ma+dev);
			addPointToLine("bbDown", Color.RED, tick.getTime(), ma-dev);
			tickCount = 0;
			
			
			if (dev<0.0005){
				System.out.println(dev);
			}
			}
			
			
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {

    }


    public void onMessage(IMessage message) throws JFException {
    }

    public void onAccount(IAccount account) throws JFException {
    }
    
    
	//////////////////////
	// PLOT STUFF
	/////////////////////

	HashMap<String, Long> previousTimes = new HashMap<String, Long>();
	HashMap<String, Double> previousValues = new HashMap<String, Double>();
	int lineIndex = 0;
    public void addPointToLine(String lineName, Color color, long time, double value)
	{
		if (previousTimes.containsKey(lineName) && previousValues.containsKey(lineName))
		{
			IChart chart = context.getChart(Settings.instrument);
			if (chart != null)
			{
				IShortLineChartObject shortLine = chart.getChartObjectFactory().createShortLine(lineName+lineIndex++, previousTimes.get(lineName), previousValues.get(lineName), time, value);
				shortLine.setColor(color);
				chart.addToMainChart(shortLine);
			}
		}
		
		previousTimes.put(lineName, time);
		previousValues.put(lineName, value);
	}

}