package jforex;

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

import com.dukascopy.api.*;


public class remoteModeOrderLabels implements IStrategy {
    public IEngine engine;
    public IConsole console;
    public IHistory history;
    public IContext context;
    public IIndicators indicators;
    
    public Instrument instrumentG = Instrument.EURUSD;
            
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        context = context;
        indicators = context.getIndicators();
                
        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--;
        }
              
        int counter = 0;
        String msg = "";
        for (IOrder order : engine.getOrders(instrumentG)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
                msg += String.format(" Opened position label : %s%n", order.getLabel());
            }
        }
        msg += String.format(" N opened positions: %d%n", counter);
        
		try {
			engine.broadcast("Info", msg);
			print(msg);
		} catch (Exception ex) {
			ex.printStackTrace();
			console.getErr().println("Error while broadcasting : " + ex);
		}

        context.stop();                
    }

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


    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        print(" onStop: ok.");
    }
    // --------------------
    public void print (String message) {
        console.getOut( ).println(message);
    }
}