/*
 * My first strategy. Entry is based on crossing moving averages. SL & TP are based on 
 * adx indicator. There are a bunch of unused variables left in from the research
 * phase.
 */

package jForex;

import com.dukascopy.api.*;

public class MAcross01 implements IStrategy{
	
	private final double version = 1.00;
	private final Instrument myInstrument = Instrument.EURUSD;
	
	private	IEngine engine;
	private	IIndicators indicators;
	private	IHistory history;
	private	IConsole console;
	private	IContext context;
	
	double 
			//Moving Averages
			fast 		= 	0.0, 
			slow		= 	0.0, 
			fast2		= 	0.0, 
			slow2		= 	0.0, 
			fast1 		= 	0.0, 
			slow1		= 	0.0,
			//adx indicator
			adx = 0.0,
			entry = 0.0,
			//Used for finding high and low price in onBar				
			min 		= 	99999, 
			max 		= 	0;
	String dir = null;	

	int orderNumber = 1; 
	/***************************
			Parameters
	***************************/	
	double					
			TP = .0050,
			SL = .01,
			lot = 5;
	final IIndicators.MaType		maType = IIndicators.MaType.EMA;
	final OfferSide 				offerSide = OfferSide.BID;
	final IIndicators.AppliedPrice	appliedPrice = IIndicators.AppliedPrice.CLOSE;
	final Period 					period = Period.FIVE_MINS;
	final int 						fastPeriod = 180;
	final int 						slowPeriod = 360;
	final int						adxPeriod = 280;
	
	@Override
	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.indicators = context.getIndicators();
		this.history = context.getHistory();
		this.console = context.getConsole();
		this.context = context;
		this.console.getOut().println("MAcross01 version: "+version+" has started...");
		
	}
	
	@Override
	public void onBar(Instrument instrument, Period period, IBar askBar,
			IBar bidBar) throws JFException {
		
		if (instrument != myInstrument){
			return;
		}
		if (period != this.period){
			return;
		}
		if (fast1 == 0.0 && slow1 == 0.0){
			fast1 = indicators.ma(instrument, period, offerSide, appliedPrice, 
					fastPeriod, maType, 1);
			slow1 = indicators.ma(instrument, period, offerSide, appliedPrice, 
					slowPeriod, IIndicators.MaType.SMA, 1);
		}
		
		min = Math.min(min, bidBar.getLow());
		max = Math.max(max, bidBar.getHigh());
		fast = indicators.ma(instrument, period, offerSide, appliedPrice, fastPeriod, 
				maType, Filter.ALL_FLATS,1,bidBar.getTime(),0)[0];
		
		slow = indicators.ma(instrument, period, offerSide, appliedPrice, slowPeriod, 
				IIndicators.MaType.SMA, Filter.ALL_FLATS,1,bidBar.getTime(),0)[0];
		adx = indicators.adx(instrument, period, offerSide, adxPeriod, 0);
		
		double profit = 0.0;
		
		//************Buy*******************
		if (getSentiment() == "bullcross" ){
			for (IOrder order : engine.getOrders()) {
				order.close();
	            profit = order.getProfitLossInPips();
	            entry = order.getOpenPrice();
	        }
						
			if(adx>12){
				TP = .0200;
				SL = .0050;
			}else if(adx>10){
				TP = .0175;
				SL = .0050;
			}else if(adx > 8){
				TP = .0095;
				SL = .0050;
			} else if(adx > 6){
				TP = .0045;
				SL = .0050;
			} else if (adx > 4){
				TP = .0045;
				SL = .0025;
			} else {
				TP = .0015;
				SL = .0015;
			}
			
			engine.submitOrder("Order" + orderNumber, instrument, IEngine.OrderCommand.BUY, 
					lot, 0, 1,bidBar.getClose() - SL, bidBar.getClose() + TP);
			dir = "buy";
			//reset min/max
			min = 99999; max = 0;
			orderNumber++;
		}
		
		//**********Sell*****************
		if (getSentiment() == "bearcross"){
			for (IOrder order : engine.getOrders()) {
	            order.close();
	            profit = order.getProfitLossInPips();
	            entry = order.getOpenPrice();
	        }
			
			if(adx>12){
				TP = .0200;
				SL = .0050;
			} else if (adx>10){
				TP = .0175;
				SL = .0050;
			} else if (adx > 8){
				TP = .0095;
				SL = .0050;
			} else if(adx > 6){
				TP = .0045;
				SL = .0050;
			} else if (adx > 4){
				TP = .0045;
				SL = .0025;
			} else {
				TP = .0015;
				SL = .0015;
			}
			
			engine.submitOrder("Order" + orderNumber, instrument,IEngine.OrderCommand.SELL,
					lot, 0, 1, bidBar.getClose() + SL, bidBar.getClose() - TP);
			dir = "sell";
			//reset min/max
			min = 99999; max = 0;
			orderNumber++;
			
		}
		
		fast2 = fast1;
		slow2 = slow1;
		fast1 = fast;
		slow1 = slow;
		
	}

	@Override
	public void onTick(Instrument instrument, ITick tick) throws JFException {
		
		
	}
	
	@Override
	public void onStop() throws JFException {
		this.console.getOut().print("---Stopped---");
		
	}
	
	
	/**
	 * @return "bullish" or "bearish" based on whether fast MA is greater than 
	 * slow MA or "bullcross/bearcross" if they just crossed.
	 */
	public String getSentiment() throws JFException{
		String sentiment = null;
		
		if (fast > slow){
			sentiment = "bullish";
		}
		if (fast < slow){
			sentiment = "bearish";
		}
		if (fast > slow && fast1 < slow1){
			sentiment = "bullcross";
		}
		if (fast < slow && fast1 > slow1){
			sentiment = "bearcross";
		}
		return sentiment;
	}
	
	@Override
	public void onAccount(IAccount account) throws JFException {
		
		
	}

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

	

}
