Write bars in File

package jforex;

import com.dukascopy.api.*;
import java.io.*;
import java.util.*;

import javax.swing.filechooser.*;

public class TestBarsSaving implements IStrategy {
   private IContext context;
   private IConsole console;
   private IHistory history;

   public void onStart(IContext context) throws JFException {
      this.context = context;
      console = context.getConsole();
      history = context.getHistory();
   }

   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 {
      long prevBarTime = history.getPreviousBarStart(Period.TEN_SECS, tick.getTime());
      List<IBar> bars = history.getBars(instrument, Period.TEN_SECS, OfferSide.BID, prevBarTime - 1000L * 10L * 9L, prevBarTime);
      File dirFile = new File(getMyStrategiesDir(), "files");
      if (!dirFile.exists()) {
         console.getErr().println("Please create files directory in My Strategies");
         context.stop();
      }
      File file = new File(dirFile, "last10bars.txt");
      console.getOut().println("Writing file " + file);
      try {
         PrintWriter pw = new PrintWriter(file);
         for (IBar bar : bars) {
            pw.println(bar.getTime() + ","
                  + bar.getOpen() + "," + bar.getClose() + "," + bar.getHigh() + "," + bar.getLow() + "," + bar.getVolume());
         }
         pw.close();
      } catch (IOException e) {
         e.printStackTrace(console.getErr());
      }
      console.getOut().println("File saved, exiting");
      context.stop();
   }

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

   public static File getMyStrategiesDir() {
      File myDocs = FileSystemView.getFileSystemView().getDefaultDirectory();
      File dir = new File(myDocs.getPath() + File.separator + "My Strategies");
      return dir;
   }
}

TestBarsSaving.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.