Write in File

Method IContext.getFilesDir() returns path to the directory where read/write operations are allowed. Use @RequiresFullAccess annotation whenever you need to write/read files outside this directory. Method isFullAccessGranted returns true when a full access is granted to a strategy.

The following strategy submits the initial buy limit order with amount 10000 and every next order with amount 20000. Every time ORDER_FILL_OK or ORDER_CLOSE_OK message is received, strategy writes order details to file, this way the strategy logs order information.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;

@RequiresFullAccess
public class LoggingStrategy implements IStrategy {
        private IEngine engine;
        private IConsole console;
        private static final String FILE_NAME = "C:/orders.txt";
        FileRead fileReader = new FileRead();
        FileWrite fileWriter = new FileWrite();

        @Configurable("Instrument")
        public Instrument selectedInstrument = Instrument.EURUSD;
        @Configurable("Take profit")
        public int takeProfit   = 10;
        @Configurable("Stop Loss")
        public int stopLoss     = 5;
        @Configurable("Price")
        public double price     = 1.2716;

First method onStart determines the amount of the order each time the strategy starts and creates the buy limit order with the calculated amount

  public void onStart(IContext context) throws JFException {
                this.engine = context.getEngine();
                this.console = context.getConsole();
                boolean orderExist = fileReader.read();
                double amout = calculateAmount(orderExist);
                createOrder(amout);
        }
        private void createOrder(double amout) throws JFException {
                double stopLossPrice = price - (selectedInstrument.getPipValue() * stopLoss);
                double takeProfitPrice = price + (selectedInstrument.getPipValue() * takeProfit);
                engine.submitOrder("Order1", selectedInstrument, OrderCommand.BUYLIMIT, amout, price, 0, stopLossPrice, takeProfitPrice);
        }

        private double calculateAmount(boolean ordersExist) {
                double amount = 0.01;
                return ordersExist ? amount * 2 : amount;
        }

        public void onAccount(IAccount account) throws JFException {
        }

After order is filled or closed, the onMessage write order details including order execution price to the file using the FileWrite class method writeToFile. public void onMessage(IMessage message) throws JFException { if (message.getType().equals(IMessage.Type.ORDER_FILL_OK) || message.getType().equals(IMessage.Type.ORDER_CLOSE_OK)) { fileWriter.writeToFile(message.toString()); double orderPrice = message.getOrder().getOpenPrice(); fileWriter.writeToFile("Order fill(execution) price: " + orderPrice); } }

The method onStop closes all orders.

        public void onStop() throws JFException {
                for (IOrder order : engine.getOrders()) {
                        engine.getOrder(order.getLabel()).close();
                }
        }
        public void onTick(Instrument instrument, ITick tick) throws JFException {
        }

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

The writeToFile method of FileWrite inner class creates a file with a specified name if the file does not exist otherwise it opens the file for appending. Then the method creates BufferedWriter object and writes data to the file.

        public class FileWrite {
                public void writeToFile(String message){
                        try {
                                FileWriter fstream = new FileWriter(FILE_NAME, true);
                                BufferedWriter out = new BufferedWriter(fstream);
                                out.append( message + "\n");
                                out.close();
                        } catch (Exception e) {
                                console.getOut().print("File access error: " + e.getMessage());
                        }
                }
        }      

The read method of FileRead inner class creates an input stream object from specified file and reads data from it. The created file contains entry about the filled/closed orders and if any entry exist, method return true.

       public class FileRead {
                public boolean read() {
                        boolean orderExist = false;
                        try {
                                FileInputStream fstream = new FileInputStream(FILE_NAME);
                                // Get the object of DataInputStream
                                DataInputStream in = new DataInputStream(fstream);
                                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                                while (((br.readLine()) != null) ) {
                                        orderExist = true;
                                        return orderExist;
                                }
                                in.close();
                        } catch (Exception e) { // Catch exception if any
                                console.getOut().print("File access error: " + e.getMessage());
                        } 
                        return orderExist;
                }
        }  
}

LoggingStrategy.java

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