package jforex.strategies;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import java.util.*;
import java.lang.Math;
import java.text.DecimalFormat;
import com.dukascopy.api.*;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import java.text.DecimalFormat;
import java.util.Currency;
import java.util.HashSet;
import java.util.Set;

public class GoldenEye4 implements IStrategy {
    private IEngine engine;
    private IHistory history;
    private IIndicators indicators;
    private int counter = 1;
    
    
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period selectedPeriod = Period.FOUR_HOURS;
    
    private double amount ;
    
    @Configurable("Coefficient Risk ")
    public double CoefficientRisk = 2;
    @Configurable("SL pips")
    public int stopLossPips = 30;
    @Configurable("TP pips")
    public int takeProfitPips = 50;
    @Configurable("Volatility percent")
    public double VP = 0.26;
    
    
 
    
 public Filter indicatorFilter = Filter.NO_FILTER;
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
          Set subscribedInstruments = new HashSet();
            subscribedInstruments.add(instrument);
            context.setSubscribedInstruments(subscribedInstruments);
    }


    public void onAccount(IAccount account) throws JFException {
    // Equity
    double equity = account.getEquity();}



    public void onMessage(IMessage message) throws JFException {
          if (message.getType().equals(IMessage.Type.ORDER_CLOSE_OK)) {
         IOrder order = message.getOrder();
       
         if (order.getProfitLossInUSD()<0)
         {
         amount = 0.5;
         }
         
         }
    }
    
    
    
    
    

    public void onStop() throws JFException {
        //close all orders
        for (IOrder order : engine.getOrders()) {
            engine.getOrder(order.getLabel()).close();
        }
    }

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

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

        if (!instrument.equals(instrument) || !period.equals(selectedPeriod))
            return;

        IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);

      
        double alfa=bidBar.getOpen()-bidBar.getClose();
        double  beta=bidBar.getClose()-bidBar.getOpen();
     
      

        // 
       if (alfa>VP) {
            submitOrder(OrderCommand.SELL);
        }
        // 
        if  (beta>VP) {
            submitOrder(OrderCommand.BUY);
        }
    }

    private IOrder submitOrder(OrderCommand orderCmd) throws JFException {

        double stopLossPrice, takeProfitPrice;
        
        // Calculating Lot size 
        
        
       // The ERROR IS HERE --->---->    double amount = equity * CoefficientRisk/100000 ;
       //Details of the error
       
   //    2012-07-24 11:15:46    ----------
//2012-07-24 11:15:46    equity cannot be resolved
//2012-07-24 11:15:46    ^^^^^^
//2012-07-24 11:15:46    amount = equity * CoefficientRisk/100000 ;
//2012-07-24 11:15:46    1. ERROR in C:\DOCUME~1\genio\IMPOST~1\Temp\jfxide\tmp\compile\GoldenEye3.java 
// 2012-07-24 11:15:46    ----------
        
        
        
        // Calculating stop loss and take profit prices
        if (orderCmd == OrderCommand.BUY) {
            stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
        } else {
            stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
        }

        // Submitting an order for the specified instrument at the current market price
        return engine.submitOrder(getLabel(instrument), this.instrument, orderCmd, this.amount, 0, 20, stopLossPrice, takeProfitPrice);
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label + (counter++);
        label = label.toUpperCase();
        return label;
    }

    private double getPipPrice(int pips) {
        return pips * this.instrument.getPipValue();
    }

}
