package strategies;

import java.io.File;

import com.dukascopy.api.*;
import com.dukascopy.api.feed.FeedDescriptor;
import com.dukascopy.api.feed.IRenkoBar;
import com.dukascopy.api.feed.IRenkoBarFeedListener;


import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class renkoTestStrategy2 implements IStrategy 
{

	// used for logging
	private static final Logger LOGGER = LoggerFactory.getLogger(renkoTestStrategy2.class);

    private IIndicators indicators = null;
    private FeedDescriptor renkoFeedDescriptor = null;

	//Configurable strategy parameters
    // general
    @Configurable("Instrument") public Instrument instrument = Instrument.EURUSD;    
    @Configurable("Renko Block Size") public PriceRange renkoBarPriceRange = PriceRange.valueOf(15);
    
        
// *****  JFOREX API EVENT HANDLERS ****************************************************************

    class wtlRenkoBarFeedListener implements IRenkoBarFeedListener
    {
		@Override
		public void onBar(Instrument instrument, OfferSide offerSide, PriceRange priceRange, IRenkoBar bar) 
		{
	       	LOGGER.info("wtlRenkoBarFeedListener.onBar() [" + instrument.toString() + "]: " + priceRange.toString() + " " + bar.toString());

	       	try 
	       	{
				Object[] testValues = indicators.calculateIndicator
					(renkoFeedDescriptor, 
					 new OfferSide[] {OfferSide.BID}, 
					 "test indicator", 
					 new IIndicators.AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, // AppliedPrice
					 new Object[] {34, 5}, //Optional Inputs
					 1, // how many bars before time stamp (including bar that includes timestamp)
					 bar.getEndTime(), // time stamp
					 0);
				
				LOGGER.info("testIndicator value: " + ((double[])testValues[0])[0] + ", " + ((double[])testValues[1])[0]);
			} 
	       	catch (JFException e) 
	       	{
				LOGGER.error("JFException: " + e.toString());
				e.printStackTrace();
			}
			catch (Exception e) 
			{
				LOGGER.error("Exception: " + e.toString());
				e.printStackTrace();
			}
	       	
		}
    }
    
    public void onStart(IContext context) throws JFException 
    {
    	this.indicators = context.getIndicators();
    	
		try
		{
			// register custom indicators
			LOGGER.info("Loading custom indicators from: " + context.getFilesDir() + System.getProperty("file.separator"));
			indicators.registerCustomIndicator(new File(context.getFilesDir() + System.getProperty("file.separator") + "testIndicator.jfx"));
		}
		catch (JFException jfe)
		{
			LOGGER.info("Exception registering custom indicator: " + jfe.getMessage() + ", cause: " + jfe.getCause());
		}

		// create feed descriptor to use when getting indicator values
		renkoFeedDescriptor = new FeedDescriptor();
		renkoFeedDescriptor.setDataType(DataType.RENKO);
		renkoFeedDescriptor.setInstrument(instrument);
		renkoFeedDescriptor.setOfferSide(OfferSide.BID);
		renkoFeedDescriptor.setPriceRange(renkoBarPriceRange);

		// subscribe to Renko Bar Feed
		context.subscribeToRenkoBarFeed(instrument, OfferSide.BID, renkoBarPriceRange, new wtlRenkoBarFeedListener());
        
	}

    public void onMessage(IMessage message) throws JFException 
    {        

    }
    
    public void onStop() throws JFException 
    {

    }
    
    public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException 
    {
    	//LOGGER.info("renkoTestStrategy.onBar() [" + instrument.toString() + "]: " + bidbar.toString());

    }

    public void onTick(Instrument instrument, ITick tick) throws JFException 
    {
    
    }
    
    public void onAccount(IAccount account) throws JFException 
    {
        
    }

}