package jforex.requests;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
import com.dukascopy.api.*;
 
public class TestWeekend implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
     
    private int CandleCount = 0;
    
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };
     
    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();
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    public void onStop() throws JFException {
        print("Candles counted: "+CandleCount);
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
         
        if (instrument != Instrument.EURUSD)
        {
            return;
        }
    }
     
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (instrument != Instrument.EURUSD) || (period != Period.FIVE_MINS) )
        {
            return;
        }
         
        if (isMarketClosed())
        {
            return;
        }
         
        print(askBar.toString());
        CandleCount++;
    }
     
    private boolean isMarketClosed() throws JFException
    {
        long lastTickTime = history.getLastTick(Instrument.EURUSD).getTime();
        Calendar calendar = Calendar.getInstance();
        int day;
        long time;
         
        calendar.setTimeInMillis(lastTickTime);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        day = calendar.get(Calendar.DAY_OF_WEEK);
       
         
        if (day == Calendar.SATURDAY) // no trading on Saturday
        {
             print("SATURDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            return true;
        }
         
        if (day == Calendar.FRIDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            print("FRIDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            if (lastTickTime > time) // No trading on Friday past 9pm
            {
                return true;
            }
        }
         
        if (day == Calendar.SUNDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            print("SUNDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            if (lastTickTime < time) // No trading on Sunday before 9pm
            {
                return true;
            }
        }
         
        // if not Friday, Saturday or Sunday, market is always open
        return false;
    }
     
    private void print(String message) 
    {
        console.getOut().println(message);
    }
}