package jforex;
import com.dukascopy.api.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.sound.sampled.*;
import java.lang.Math.*;
import java.net.URL;
import java.util.concurrent.*;
import java.text.*;

import java.util.*;
 
import com.dukascopy.api.*;
@RequiresFullAccess
public class TradesCollData implements IStrategy {
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;
	
    public Period myperiod=Period.FIVE_MINS;
    public String LogFileName = "C:/Users/Karim/Documents/Trades_";
    public String LogFileNameCollectData = "C:/Users/Karim/Documents/CollectData_";
    public double amount = 0.1;
    private int tagCounter = 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();
        LogFileName += (new Date().getTime())+".txt";
        LogFileNameCollectData += (new Date().getTime())+".txt";
	}
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (period != myperiod) || ((instrument!=Instrument.EURUSD) &&(instrument!=Instrument.GBPUSD) && (instrument!=Instrument.USDCHF) && (instrument!=Instrument.USDJPY) 
         && (instrument!=Instrument.CHFJPY) && (instrument!=Instrument.EURCHF) && (instrument!=Instrument.EURGBP) && (instrument!=Instrument.GBPJPY) && (instrument!=Instrument.NZDJPY) 
         && (instrument!=Instrument.NZDUSD) && (instrument!=Instrument.GBPCHF))  ) return;
        
        IBar bar1=history.getBar(instrument,myperiod, OfferSide.BID, 1);
        IBar bar2=history.getBar(instrument,myperiod, OfferSide.BID, 2);
        IBar bar3=history.getBar(instrument,myperiod, OfferSide.BID, 3);
        IBar bar4=history.getBar(instrument,myperiod, OfferSide.BID, 4);
        print(LogFileNameCollectData,instrument+ "\t" +getDate(bidBar.getTime())+"\t"+bar1.getOpen()+"\t"+bar1.getHigh()+"\t"+bar1.getLow()+"\t"+bar1.getClose()+"\n",true);
        if (   (bar2.getClose()>3*instrument.getPipValue()+bar1.getClose()) 
            && (bar3.getClose()>3*instrument.getPipValue()+bar2.getClose())
            && (bar4.getClose()>3*instrument.getPipValue()+bar3.getClose()) ) {
                
                long date=bar1.getTime();
                double prix = askBar.getClose();
                //engine.submitOrder(getLabel(instrument,date),instrument, IEngine.OrderCommand.BUY, amount, prix,3,prix-10*instrument.getPipValue(),prix+10*instrument.getPipValue());
                print(LogFileName,instrument+ "\t" +getDate(askBar.getTime()+myperiod.interval)+"\t"+"BUY\t"+prix+"\n",true);
                print(instrument+ "\t" +getDate(askBar.getTime()+myperiod.interval)+"\t"+"BUY\t"+prix);
        }        
         if (   (bar1.getClose()>3*instrument.getPipValue()+bar2.getClose()) 
            && (bar2.getClose()>3*instrument.getPipValue()+bar3.getClose())
            && (bar3.getClose()>3*instrument.getPipValue()+bar4.getClose()) ) {

                long date=bar1.getTime();
                double prix = bidBar.getClose();
               //engine.submitOrder(getLabel(instrument,date),instrument, IEngine.OrderCommand.SELL, amount, prix,3,prix+10*instrument.getPipValue(),prix-10*instrument.getPipValue());
                print(LogFileName,instrument+ "\t" +getDate(bidBar.getTime()+myperiod.interval)+"\t"+"SELL\t"+prix+"\n",true);
                print(instrument+ "\t" +getDate(bidBar.getTime()+myperiod.interval)+"\t"+"SELL\t"+prix);
        }       
    }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  

   public void print(String filename, String s, boolean append) {
        try {
                BufferedWriter out;
                out = (append?new BufferedWriter(new FileWriter(filename,true)):new BufferedWriter(new FileWriter(filename,false)));
                   out.write(s);
                   out.close();
            } catch (IOException e) {
                console.getOut().println("Problème d'écriture dans le fichier : " + e.toString());
            }        
    } 
    public void print( String s) {
            console.getOut().println(s.replace('\t',' '));    
        } 
    public String getDate(Long tim) {
             Date dat =  new Date();
            dat.setTime(tim);
             SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss,S");
             dateformat.setTimeZone(TimeZone.getTimeZone("GMT"));
             StringBuilder date = new StringBuilder( dateformat.format( dat ) );
             return date.toString();
        } 
     public String getLabel(Instrument instrument, Long date) {
         String label = "StratFdD_"+instrument.name();
         label = label.replaceAll(Instrument.getPairsSeparator(),"");
         label = label + "_" + date.toString()+ (tagCounter++);
         label = label.toLowerCase();
         return label;
     }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	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 {
	}
	
    
}