package jforex;

import java.util.*;
import java.text.*;
import java.lang.*;
import java.io.*;

import com.dukascopy.api.*;


public class acRemoteTests implements IStrategy {
    public IEngine engine;
    public IConsole console;
    public IHistory history;
    public IContext context;
    public IIndicators indicators;
    
    public Instrument instrumentG = Instrument.EURUSD;
    private SimpleDateFormat dateSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        context = context;
        indicators = context.getIndicators();
                
        print( " Hello !");

		Set<Instrument> instruments = new HashSet<Instrument>();
  		instruments.add(instrumentG);                     
  		context.setSubscribedInstruments(instruments);
        
		int kk = 10;
		while (!context.getSubscribedInstruments().containsAll(instruments) && kk >=0) {
			try {
				console.getOut().println("Instruments not subscribed yet " + kk);
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				console.getOut().println(e.getMessage());
			}
			kk--;
		}		

		print(" 10s- and 15min- Bar time: "+
			dateSDF.format(history.getStartTimeOfCurrentBar(instrumentG, Period.TEN_SECS))+", "+
			dateSDF.format(history.getStartTimeOfCurrentBar(instrumentG, Period.FIFTEEN_MINS)));
		
		int nPos = 0;
		for (IOrder order : engine.getOrders(instrumentG)) {
			print(" Position : "+order.getLabel());
			nPos++;
		}
		print( "nPos: "+nPos);	
        
    }

    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(instrumentG)) return;
		
		if (period.equals(Period.ONE_MIN)) {
        					
			print(" 10s- and 15min- Bar time: "+
				dateSDF.format(history.getStartTimeOfCurrentBar(instrumentG, Period.TEN_SECS))+", "+
				dateSDF.format(history.getStartTimeOfCurrentBar(instrumentG, Period.FIFTEEN_MINS)));
				
			int nPos = 0;
			for (IOrder order : engine.getOrders(instrumentG)) {
				print(" Position : "+order.getLabel());
				nPos++;
			}
			print( "nPos: "+nPos);	
			
		}
		
    }


    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        print(" onStop: Stopping.");                                                    
    }

    // --------------------
    public void print (String message) {
        console.getOut( ).println(message);
    }
    

}