Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How to modify this strategy in order to be compiled sucessfully?
 Post subject: How to modify this strategy in order to be compiled sucessfully? Post rating: 0   New post Posted: Wed 18 Apr, 2012, 11:45 

User rating: 0
Joined: Sat 24 Mar, 2012, 09:54
Posts: 13
I use the following code to download historical data of three weeks into file fold "D:/JforexTemp" with each one specific named .csv file for each one week. However error encounted when compiling. Could you please help to modify the code:
package jforex;
import java.util.*;
import java.io.*;
import java.text.*;
import com.dukascopy.api.*;

@RequiresFullAccess
public class DownloadtoCSV34 implements IStrategy {
    @Configurable("Period")
    public Period period=Period.ONE_HOUR;
    @Configurable("Instrument")
    public Instrument instrument=Instrument.EURUSD;
    @Configurable("OfferSide")
    public OfferSide offerSide=OfferSide.BID;
     
    //private DecimalFormat priceFormat;
    private SimpleDateFormat gmtsdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat gmtsdf2=new SimpleDateFormat("yyyyMMddHHmm");
    private SimpleDateFormat gmtsdf3=new SimpleDateFormat("yyyyMMdd");
   
    private String startDateStr="2012-04-07 00:00:00";
    private int numOfWeeks=3;
   
    private IEngine engine;
    private IConsole console; // use
    private IHistory history; // use
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    //@Override
    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();
       
        gmtsdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        gmtsdf2.setTimeZone(TimeZone.getTimeZone("GMT"));// the default is GMT+8
        gmtsdf3.setTimeZone(TimeZone.getTimeZone("GMT"));// the default is GMT+8
        //priceFormat=new DecimalFormat("0.#####");
     
        long startDate=0;
        try{
            startDate=gmtsdf.parse(startDateStr).getTime();
        }catch(ParseException el){
            //stop strategy
            console.getErr().println(el);
            context.stop();
            return;
        }
       
        long startBarTime=history.getBarStart(period, startDate);// minus one to exclude the crrunt time.
        long startBarTime0;
        for (int i=0; i<numOfWeeks; i++){
           startBarTime0=startBarTime;
           startBarTime=startBarTime-24*7*3600*1000;
           singleInstrWeekprint(instrument,startBarTime,startBarTime0);
        }
       
    }
    @Override
    public void onAccount(IAccount account) throws JFException {}
    @Override
    public void onMessage(IMessage message) throws JFException {}
    @Override
    public void onStop() throws JFException {}
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    private void singleInstrWeekprint(Instrument instrument,long startBarTime,long startBarTime0){
        int k;
        int r=0;
        private String FILE_NAME;
        public String path="D:\\JforexTemp\\";
        FileWriter fstream;
        BufferedWriter out;
        Calendar c = Calendar.getInstance();//
        c.setTimeZone(TimeZone.getTimeZone("GMT"));
        try{
            //List<IBar> bars=history.getBars(instrument,period,OfferSide.BID,endBarTime,startBarTime);
            List<IBar> bars=history.getBars(instrument,Period.ONE_HOUR,OfferSide.BID,Filter.WEEKENDS,startBarTime,startBarTime0);
            print(String.format("start to %s,  end to %s, retrieved bar count: %s",  gmtsdf.format(startBarTime),gmtsdf.format(startBarTime0), bars.size()));
            for(IBar bar:bars){
                c.setTimeInMillis(bar.getTime());
                k=c.get(Calendar.DAY_OF_WEEK);// The day-of-week is an integer value where 1 is Sunday, 2 is Monday, ..., and 7 is Saturday
                if (r==0){ // set the filename at the first bar.
                    FILE_NAME=path+"WriteTickToFile_"+instrument.toString().replace("/","")+"_"+gmtsdf3.format(c)+".csv";
                    try{
                          //fstream=new FileWriter(FILE_NAME,true); // append is allowed
                          fstream=new FileWriter(FILE_NAME,false);
                          out=new BufferedWriter(fstream);
                    }catch (IOException e){
                          writeMessage("Can not initialize output stream; stopping the strategy");
                          context.stop();
                    }
                    r=1;
                }
                try{
                      //out.append("1"+","+"2.3"+"\n");
                      out.write(gmtsdf2.format(bar.getTime())+","+k+","+bar.getOpen()+","+bar.getHigh()+","+bar.getLow()+","+
                          bar.getClose()+","+bar.getVolume()+"\r\n");
                }catch (IOException e){
                      writeMessage("File write error");
                      e.printStackTrace();
                }
               
            }
            try{
                  if(out!=null){
                      out.flush();
                      out.close();
                  }
            }catch(IOException e){
                  writeMessage("can not close output stream");
                  e.printStackTrace();
            }
            //print(String.format("end of %s,  retrieved bar count: %s",  gmtsdf.format(startBarTime), bars.size()));
       }catch(Exception e){
          console.getErr().println(e.getMessage());
          e.printStackTrace(console.getErr());
          context.stop();
       }
    }

    private void print(Object o){
        console.getOut().println(o);
    }
}


 
 Post subject: Re: How to modify this strategy in order to be compiled sucessfully? Post rating: 0   New post Posted: Wed 18 Apr, 2012, 14:28 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
package jforex;
import java.util.*;
import java.io.*;
import java.text.*;
import com.dukascopy.api.*;
 
@RequiresFullAccess
public class DownloadtoCSV34 implements IStrategy {
    @Configurable("Period")
    public Period period=Period.ONE_HOUR;
    @Configurable("Instrument")
    public Instrument instrument=Instrument.EURUSD;
    @Configurable("OfferSide")
    public OfferSide offerSide=OfferSide.BID;
     
    //private DecimalFormat priceFormat;
    private SimpleDateFormat gmtsdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat gmtsdf2=new SimpleDateFormat("yyyyMMddHHmm");
    private SimpleDateFormat gmtsdf3=new SimpleDateFormat("yyyyMMdd");
     
    private String startDateStr="2012-04-07 00:00:00";
    private int numOfWeeks=3;
     
    private IEngine engine;
    private IConsole console; // use
    private IHistory history; // use
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
     
    //@Override
    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();
         
        gmtsdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        gmtsdf2.setTimeZone(TimeZone.getTimeZone("GMT"));// the default is GMT+8
        gmtsdf3.setTimeZone(TimeZone.getTimeZone("GMT"));// the default is GMT+8
        //priceFormat=new DecimalFormat("0.#####");
     
        long startDate=0;
        try{
            startDate=gmtsdf.parse(startDateStr).getTime();
        }catch(ParseException el){
            //stop strategy
            console.getErr().println(el);
            context.stop();
            return;
        }
         
        long startBarTime=history.getBarStart(period, startDate);// minus one to exclude the crrunt time.
        long startBarTime0;
        for (int i=0; i<numOfWeeks; i++){
           startBarTime0=startBarTime;
           startBarTime=startBarTime-24*7*3600*1000;
           singleInstrWeekprint(instrument,startBarTime,startBarTime0);
        }
       
    }
    @Override
    public void onAccount(IAccount account) throws JFException {}
    @Override
    public void onMessage(IMessage message) throws JFException {}
    @Override
    public void onStop() throws JFException {}
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
 
    private void singleInstrWeekprint(Instrument instrument,long startBarTime,long startBarTime0){
        int k;
        int r=0;
        String FILE_NAME;
        String path="D:\\JforexTemp\\";
        FileWriter fstream;
        BufferedWriter out = null;
        Calendar c = Calendar.getInstance();//
        c.setTimeZone(TimeZone.getTimeZone("GMT"));
        try{
            //List<IBar> bars=history.getBars(instrument,period,OfferSide.BID,endBarTime,startBarTime);
            List<IBar> bars=history.getBars(instrument,Period.ONE_HOUR,OfferSide.BID,Filter.WEEKENDS,startBarTime,startBarTime0);
            print(String.format("start to %s,  end to %s, retrieved bar count: %s",  gmtsdf.format(startBarTime),gmtsdf.format(startBarTime0), bars.size()));
            for(IBar bar:bars){
                c.setTimeInMillis(bar.getTime());
                k=c.get(Calendar.DAY_OF_WEEK);// The day-of-week is an integer value where 1 is Sunday, 2 is Monday, ..., and 7 is Saturday
                if (r==0){ // set the filename at the first bar.
                    FILE_NAME=path+"WriteTickToFile_"+instrument.toString().replace("/","")+"_"+gmtsdf3.format(c)+".csv";
                    try{
                          //fstream=new FileWriter(FILE_NAME,true); // append is allowed
                          fstream=new FileWriter(FILE_NAME,false);
                          out=new BufferedWriter(fstream);
                    }catch (IOException e){
                          print("Can not initialize output stream; stopping the strategy");
                          context.stop();
                    }
                    r=1;
                }
                try{
                      //out.append("1"+","+"2.3"+"\n");
                      out.write(gmtsdf2.format(bar.getTime())+","+k+","+bar.getOpen()+","+bar.getHigh()+","+bar.getLow()+","+
                          bar.getClose()+","+bar.getVolume()+"\r\n");
                }catch (IOException e){
                      print("File write error");
                      e.printStackTrace();
                }
                 
            }
            try{
                  if(out!=null){
                      out.flush();
                      out.close();
                  }
            }catch(IOException e){
                  print("can not close output stream");
                  e.printStackTrace();
            }
            //print(String.format("end of %s,  retrieved bar count: %s",  gmtsdf.format(startBarTime), bars.size()));
       }catch(Exception e){
          console.getErr().println(e.getMessage());
          e.printStackTrace(console.getErr());
          context.stop();
       }
    }
 
    private void print(Object o){
        console.getOut().println(o);
    }
}


Attachments:
DownloadtoCSV34.java [5.34 KiB]
Downloaded 253 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: How to modify this strategy in order to be compiled sucessfully? Post rating: 0   New post Posted: Wed 18 Apr, 2012, 15:51 

User rating: 0
Joined: Sat 24 Mar, 2012, 09:54
Posts: 13
Thanks for your quick reply.
Now this strategy can be compiled successfully, but error accoured when local running.
The error is "14:53:06 Cannot format given Object as a Date". What's wrong with it?


 
 Post subject: Re: How to modify this strategy in order to be compiled sucessfully? Post rating: 0   New post Posted: Thu 19 Apr, 2012, 04:49 

User rating: 0
Joined: Sat 24 Mar, 2012, 09:54
Posts: 13
The problem is solved. Error comes from the parameter c. Thanks.


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com