package JForex;

import com.dukascopy.api.*;

public class ChaosBase implements IStrategy {
	private IContext context = null;
    private IConsole console;
    private IEngine engine = null;
    private IChart chart = null;
    private IOrder order;
    private IIndicators indicators = null;
    private IUserInterface userInterface = null;
    private IAccount account = null;
    private IHistory history = null;
    
  //double Variables
    private double AmountToUse;
    private double profitsLoss = 0;
    private double totalProfitLoss =0;
    private double currentStopLossPrice = 0;
    
    IOrder orderSell; 
    IOrder orderBuy;
    
    //boolean
    private boolean goLong = false;
    private boolean goShort = false;
    private boolean hasPositionBuy = false;
    private boolean hasPositionSell = false;
    
//    @Configurable("Instrument") 
    public Instrument instrument 			= Instrument.EURUSD; 
//    @Configurable("Time") 
    public Period period					= Period.ONE_HOUR;
//    @Configurable("stop Loss Distance") 
    public double stopLossDistance 			= 0.0200;
//    @Configurable("Stop loss")
    public int ParamStopLoss          		= 50 ;
//    @Configurable("trailingStep") 
    public double trailingStep 				= 20.0;
//    @Configurable("Take profit") 
    public int ParamTakeProfit        		= 250 ;
//    @Configurable("Risiko") 
    public double  Risiko             		= 30  ; 
    
    //int Variables
    private int tagCounter = 0;
    
    
    public void onStart(IContext context) throws JFException {
        this.context = context;
        engine = context.getEngine();
        this.history = context.getHistory();    
        this.console = context.getConsole ();
        indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        console.getOut().println("Started");

        
    }
    
    public void onAccount(IAccount account) throws JFException {
    	this.account = context.getAccount();
        AmountToUse = (Math.floor(account.getEquity()/1000000 * Risiko));
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    	IBar CurrHourBar = history.getBar(instrument, period, OfferSide.BID, 0);
    	IBar prev1HourBar = history.getBar(instrument, period, OfferSide.BID, 1);
    	
    	//Current
    	double currHourOpen = CurrHourBar.getOpen();
        double currHourClose = CurrHourBar.getClose();
        double currHourHigh = CurrHourBar.getHigh();
        double currHourLow = CurrHourBar.getLow();
		long   currHourTime = CurrHourBar.getTime();
		
        //One Hour Ago
        double prevHourOpen = prev1HourBar.getOpen();
        double prevHourClose = prev1HourBar.getClose();
        double prevHourHigh = prev1HourBar.getHigh();
        double prevHourLow = prev1HourBar.getLow();
        long   prevHourTime = prev1HourBar.getTime();
        
      //Indicators
        
      //AWESOME(5,SMA,34,SMA)
        double[][] Awesome5 = indicators.awesome(
        		instrument, 								//Instrument
        		period, 									//Period 					One Hour
        		OfferSide.BID, 								//Offerside					BID
        		IIndicators.AppliedPrice.MEDIAN_PRICE, 		//Applied Price				MEDIAN PRICE
        		5, 											//FasterMA Time Period		5
        		IIndicators.MaType.SMA, 					//IIndicators.MaType		SMA
        		34, 										//SlowerMA Time Period		34
        		IIndicators.MaType.SMA, 					//IIndicators.MaType		SMA
        		prevHourTime, 								//From						1 Hour Ago
        		currHourTime								//To						Current Time
        		);
        
        double[][] ac5 = indicators.ac(
        		instrument, 								//Instrument
        		period, 									//Period 					One Hour
        		OfferSide.BID, 								//Offerside					BID
        		IIndicators.AppliedPrice.MEDIAN_PRICE, 		//Applied Price				MEDIAN PRICE
        		5, 											//fastPeriod				5
        		34,											//slowPeriod				34
        		prevHourTime, 								//From						1 Hour Ago
        		currHourTime								//To						Current Time
        		);
        
        //Set Long Position
        if (ac5[0][1] !=0 ) {
        	console.getOut().println("AC Up: " + ac5[0][1]);
        	if (!Double.isNaN(Awesome5[1][1])) {
        		console.getOut().println("Awe Up: " + Awesome5[1][1]);
        				goLong = true;


        	}
        }
        //Set Short Position
        if (ac5[1][1] !=0 ) {
        	console.getOut().println("AC Down: " + ac5[1][1]);
        	if (!Double.isNaN(Awesome5[2][1])) {
        		console.getOut().println("Awe Down: " + Awesome5[2][1]);
        				goShort = true;
        	}
        }
    	
    }
    
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    	if (goLong) {
    		orderBuy = engine.submitOrder(
					getLabel(instrument), 													//Label
					instrument, 															//Instrument
					IEngine.OrderCommand.BUY, 												//Order Command
					AmountToUse, 															//Amount
					0, 																		//Price
					1, 																		//Slippage
					tick.getAsk() - instrument.getPipValue() * ParamStopLoss, 				//Stop Loss
					tick.getAsk() + instrument.getPipValue() * ParamTakeProfit 				//Take Profit
					);
    		 hasPositionBuy = true;
			 goLong = false;
    	}
    	
    	if (goShort) {
    		orderSell = engine.submitOrder(
					getLabel(instrument), 													//Label
					instrument, 															//Instrument
					IEngine.OrderCommand.SELL, 												//Order Command
					AmountToUse, 															//Amount
					0, 																		//Price
					0, 																		//Slippage
					tick.getAsk() + instrument.getPipValue() * ParamStopLoss, 				//Stop Loss
					tick.getAsk() - instrument.getPipValue() * ParamTakeProfit 				//Take Profit
					);
    		 hasPositionSell = true;
			 goShort = false;
    	}
    	
    }

public void onMessage(IMessage message) throws JFException {
    	
    	if (message.getType().equals(IMessage.Type.ORDER_FILL_OK)) {
            IOrder order = message.getOrder();
    
            console.getOut().println(
                     "Order state = " + order.getState() +
                     ", Open Price = " + order.getOpenPrice()
                     );
         }
         if (message.getType().equals(IMessage.Type.ORDER_CLOSE_OK)) {
            IOrder order = message.getOrder();
            profitsLoss = order.getProfitLossInPips();
            console.getOut().println("Order : "+order.getLabel()+ " Order state = " + order.getState() +
                    ", Close Price = " + order.getClosePrice() + " Pips: "+profitsLoss
                    );
            totalProfitLoss = totalProfitLoss + profitsLoss;
            console.getOut().println("Totat Profit Loss: " + totalProfitLoss);
         }
    }

    public void onStop() throws JFException {
        console.getOut().println("Stopped");

    }
    
   
    public String getLabel(Instrument instrument) {
        String label = instrument.name();
        label =  label.substring(0, 2) + label.substring(3, 5);
        label =  label + (tagCounter++) ;
        label =  label.toLowerCase();
        return label;    
    }
}
