package jforex.bugtests.customInd;

import java.io.File;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.indicators.IIndicator;

public class MyIndStrategy implements IStrategy {

	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IIndicators indicators;
	private Instrument selectedInstrument = Instrument.EURUSD;
	private Period fixedPeriod = Period.TEN_SECS;

	@Override
	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.console = context.getConsole();
		this.history = context.getHistory();
		this.indicators = context.getIndicators();
		
		//register custom indicator located in ...\JForex\Strategies\files folder
		indicators.registerCustomIndicator(new File(context.getFilesDir() + System.getProperty("file.separator") + "MyInd2.jfx"));
		IBar prevBar = history.getBar(selectedInstrument, fixedPeriod, OfferSide.BID, 1); 
		
		//Support: print some meta data
		printIndicatorInfos(indicators.getIndicator("Trend2"));
		
		//Support: added an optional input
		Object[] CustomIndicator = this.indicators.calculateIndicator(selectedInstrument, fixedPeriod, new OfferSide[] {OfferSide.BID}, 
				"Trend2", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{20, 2}, 1);
		
		//Support: as you see the first output is just an object, so we needed to add another output to strategy which outputs the double values
		console.getOut().println("1st indicator value: " + ((Object[])CustomIndicator)[0]);
		console.getOut().println("2nd indicator value: " + ((Object[])CustomIndicator)[1]);
		

	}
	
	
	//Support: helper functions
	private void printIndicatorInfos(IIndicator ind){
	    for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++){
	        print(ind.getIndicatorInfo().getName() +" Input " + ind.getInputParameterInfo(i).getName() +" " + ind.getInputParameterInfo(i).getType());
	    }
	    for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++){
	        print(ind.getIndicatorInfo().getName() +" Opt Input " + ind.getOptInputParameterInfo(i).getName() +" " + ind.getOptInputParameterInfo(i).getType());
	    }
	    for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++){
	        print(ind.getIndicatorInfo().getName() +" Output " + ind.getOutputParameterInfo(i).getName() +" " + ind.getOutputParameterInfo(i).getType());
	    }
	}
	 
	private void print(Object o){
	    console.getOut().println(o);
	}

	@Override
	public void onTick(Instrument instrument, ITick tick) throws JFException {}

	@Override
	public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

	@Override
	public void onMessage(IMessage message) throws JFException {}

	@Override
	public void onAccount(IAccount account) throws JFException {}

	@Override
	public void onStop() throws JFException {}

}
