Write variables in file

A simple strategy example demonstrates how to store different variables on a local machine.

package jforex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import com.dukascopy.api.*;

public class MyStrategy implements IStrategy {

    private static String directory = System.getProperty("user.home");            //define directory where our cache file will be placed
    private static String fileName = "JForexCache.properties";                    //define name of our cache file
    private static String path = directory+"/"+fileName;                        //just placing all together and in one path name  

    // some variables which will use in our strategy
    private IOrder myOrder = null;
    private IEngine engine = null;
    private IHistory history = null;
    private IConsole console = null;

    public void onStart(IContext context) throws JFException {
        // initialize our variables
        this.engine = context.getEngine();
        this.history = context.getHistory();
        this.console = context.getConsole();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {

        //Just printing message on console to demonstrate, how to get values from our cache
        //Check if this is our order and check if it is filled 
        if(message.getOrder() == myOrder && message.getOrder().getState()==IOrder.State.FILLED){            
            try {
                this.console.getOut().println(
                        "Order with Label: "+myOrder.getLabel()+" - slippage ask: "+getProperty(myOrder.getLabel()+"_ask")+"\n"+     
                        "Order with Label: "+myOrder.getLabel()+" - slippage bid: " + getProperty(myOrder.getLabel()+"_bid"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void onStop() throws JFException {

        //close the order and clean cache 
        if (myOrder != null){
            myOrder.close();
            cleanCache();
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {

        //On first tick place our order 
        if (myOrder == null) {
            myOrder = engine.submitOrder("myOrder", instrument, IEngine.OrderCommand.BUY, 0.1);            

            //taking last tick
            ITick lastTick = history.getLastTick(instrument);

            try {
                // here we get last tick slippage ask/bid and setting them into our cache file 
                setProperty(myOrder.getLabel()+"_ask", String.valueOf(lastTick.getAsk()));
                setProperty(myOrder.getLabel()+"_bid", String.valueOf(lastTick.getBid()));

            } catch (Exception e) {    
                e.printStackTrace();
            }
        }

    }

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

    /**
     * Creates or replaces new record in local cache file, which are automatically created in user.home path if its not exists.
     * 
     * @param key - the key to be placed into this property file. 
     * @param value - the value corresponding to key.
     *       
     * */
    public static synchronized void setProperty(String key, String value)throws Exception{

        //Define new file and place it in our specified path
        File resourceFile = new File(path);

        try{
            //create new Properties instance variable
            Properties properties = new Properties();

            //check if our file really exists on local machine
            if (resourceFile.exists()){
                try {
                    //if it does exists, we should load those content in to earlier created properties
                    properties.load(new FileInputStream(path));
                } catch (IOException e) {
                    System.err.println("Error during loading properties file: " + e.getMessage());                    
                }
            }

            //just create/replace new record using shown key and value pair
            properties.setProperty(key, value);

            // Write properties file.
            try {
                properties.store(new FileOutputStream(path), null);
            } catch (IOException e) {
                System.err.println("Error during writing properties file: " + e.getMessage());                                
            }

        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    /**
     * Creates or replaces new record in local cache file, which are automatically created in user.home 
     * path if its not exists.
     * 
     * @param key - the value key, which using value are stored in property file.
     * 
     * @return the value in property file with specified key.
     *       
     * */       
    public static synchronized String getProperty(String key) throws Exception{

        //define file our resource file
        File resourceFile = new File(path);

        //check if our file really exists on local machine
        if (resourceFile.exists()){

            //create new Properties instance variable
            Properties properties = new Properties();            
            try {
                //Load our cache file content in to created properties variable
                properties.load(new FileInputStream(path));
            } catch (IOException e) {
                System.err.println("Error during loading properties file: " + e.getMessage());                    
            }
            return properties.getProperty(key);
        }    
        else throw new FileNotFoundException(); //if file dosen't exists we should just let to know about it :)     
    }

    /**
     * Deletes created cache file.  
     * */

    public static void cleanCache(){        
        File resourceFile = new File(path);
        resourceFile.delete();
    }
}

MyStrategy.java

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