package com.pixelka.forex;

import com.dukascopy.api.*;
import java.util.*;

public class strategy5 implements IStrategy {
    
  
    private IContext context = null;
    private IEngine engine = null;
    private IChart chart = null;
    private IHistory history = null;
    private IIndicators indicators = null;
    private IConsole console = null;    
    private int profitLimit;
    private int lossLimit;
    private double bidPrice;
    private double askPrice;
    private double accountEquity;
    private double volume = 4;
    //private double[] vol = new double[50000];
    //private int i=0;
    //private int trend=100;
    
    private Instrument instrument = Instrument.EURUSD;

  
    public void onStart(IContext context) throws JFException {
        this.context = context;  
        engine = context.getEngine();
        indicators = context.getIndicators();
        history = context.getHistory();
        console = context.getConsole();
        this.indicators = context.getIndicators();
    }

   protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        
        long time = new java.util.Date().getTime();

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time;
        label = label.toLowerCase();
        return label;
    }
    protected int islong(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.isLong()) {
                counter++;
            }
        }
        return counter;
    }
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
    public void onStop() throws JFException {
    }
    
    public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException {
      
       if(instrument != this.instrument) return;
       if(period == Period.ONE_HOUR) { 
      
       
       
        profitLimit = 25;
        lossLimit = 90;
       
                      
      if (askbar.getVolume() == 0) return;
      

             double openPrice = bidbar.getOpen();
             
             askPrice = askbar.getClose();
             bidPrice = bidbar.getClose();          
          
        
           double sma = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 160, 0); 
           double smma = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 28, 0); 
           double willr = this.indicators.willr(instrument,period,OfferSide.BID,16,0);; 
            double [] bands = this.indicators.bbands(instrument,
                period,
                OfferSide.BID,
                IIndicators.AppliedPrice.CLOSE,
                50,
                1,
                1,
                IIndicators.MaType.DEMA,
                0);
                
           
          // vol[i] = this.indicators.natr(instrument, period, OfferSide.BID,  300, 0);  
              
           
        
       if (positionsTotal(instrument) == 0 ) {
                this.console.getOut().println("sma  :"+ sma);
                this.console.getOut().println("smma :" + smma);
                this.console.getOut().println("bidPrice :"+bidPrice);
            
                if (  bidPrice>sma && bidPrice > smma && bidPrice < smma+0.01  && willr>-25 && Math.abs(smma-sma)<0.01) {
                     
                 buy(instrument, engine, profitLimit, lossLimit, volume);
                  
                 }
                
                else  if (bidPrice<sma && bidPrice < smma && bidPrice > smma -0.01 && willr< -75  && Math.abs(sma-smma)<0.01 ) { 
                    
                   sell(instrument, engine, profitLimit, lossLimit, volume);
                    
                  }   
                
        }
    }
     if(period == Period.ONE_HOUR) {
       if (positionsTotal(instrument) != 0 ) {
        
        double smma = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 28, 0); 
        double sma = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 160, 0); 
        double willr = this.indicators.willr(instrument,period,OfferSide.BID,16,0);; 
        //this.console.getOut().println("smma :" + smma);
        this.console.getOut().println("bidPrice :"+bidPrice);
        if   (islong(instrument)!=0) {
            if (bidPrice < smma    ) {
                     
                 for (IOrder order : engine.getOrders(instrument)) {
                                    order.close();
                                }
                  
                 }
        }
        else{
            if (bidPrice > smma  ) {
                     
                 for (IOrder order : engine.getOrders(instrument)) {
                                    order.close();
                                }
                  
                 }
        }
        }
        
    }
    //i=i+1;
    
}
  public void onMessage(IMessage message) throws JFException {
        
        if (message != null && message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
            
            IOrder lastOne = message.getOrder();
            
            double profitsLoss = lastOne.getProfitLossInPips();
            
            console.getOut().println("Order : "+lastOne.getLabel()+ " "+ lastOne.getOrderCommand()+ " Pips: "+profitsLoss);
            
           
        }
}
     

 
   public void onAccount(IAccount account) throws JFException {
       
   }
    public void sell(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
        
        engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, volumeParam, 0, 3, bidPrice
                        + instrument.getPipValue() *endLossPipLevel, bidPrice - instrument.getPipValue() * takeProfitPipLevel);
     } 
     
      public void buy(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
        
         engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, volumeParam, 0, 3, askPrice
                        - instrument.getPipValue() * endLossPipLevel, askPrice + instrument.getPipValue() * takeProfitPipLevel);
     } 
}