package jforex;

import java.util.*;
import java.text.*;  // DateFormat etc.
import java.awt.*;
import java.awt.Color;


import com.dukascopy.api.*;

public class Watcher implements IStrategy {
    @Configurable("Instrument")
    public Instrument _currentInstrument = Instrument.EURUSD;
    
    @Configurable("Max position amount")
    public double _max_position_amount=0.001;

    @Configurable("Position amount")
    public double _position_amount=0.001;
    
    @Configurable("Force update")
    public boolean _force_update=true;        
    
  
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;
    private IChart userChart;
    
    private int _tag_counter=0;
	
	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.console = context.getConsole();
		this.history = context.getHistory();
		this.context = context;
		this.indicators = context.getIndicators();
		this.userInterface = context.getUserInterface();        
        this.userChart = context.getChart(_currentInstrument);
        
	}

	public void onAccount(IAccount account) throws JFException {
	}

	public void onMessage(IMessage message) throws JFException {
	}

	public void onStop() throws JFException {
	}

	public void onTick(Instrument instrument, ITick tick) throws JFException {
       
       if(instrument!=_currentInstrument) return;
       userChart.comment("");
       for (IOrder order : engine.getOrders(_currentInstrument)) {
           
           // CHECK LIMIT ORDERS FOR CORRECT AMMOUNT
           if((order.getOrderCommand()!=IEngine.OrderCommand.BUY && order.getOrderCommand()!=IEngine.OrderCommand.SELL) && (order.getState()==IOrder.State.OPENED || order.getState()==IOrder.State.CREATED)) {
               if(order.getAmount()>_max_position_amount) {
                   if(_force_update==true) {
                       IOrder order_backup=order;
                       order.close();
                       openOrderByOldInfo(order_backup);
                   } else {
                       userChart.setCommentFont(new Font("Arial", Font.PLAIN, 18 ));
                       userChart.setCommentColor(Color.RED);
                       userChart.comment("ORDER LOT IS TOO BIG !!!");
                   }
               }               
           }

      } 
	}
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    
    public void openOrderByOldInfo(IOrder order) {
        try {
        engine.submitOrder(getLabel(),
        _currentInstrument, 
        order.getOrderCommand(),
        _position_amount,
        order.getOpenPrice(),
        0,
        order.getStopLossPrice(),order.getTakeProfitPrice());
        } catch(Exception e) {
            console.getOut().println("Unable to open order, exception: "+e.toString());
        }
        
    }

    protected String getLabel() {
        StringBuffer buff = new StringBuffer(_currentInstrument.name().toLowerCase());
        buff.append("Watcher");
        buff.append(System.currentTimeMillis());
        buff.append(_tag_counter++);
        return buff.toString();
    }
}
