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.

optimizing strategy in JForex SDK
 Post subject: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Mon 07 Jan, 2013, 22:51 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

What is the best/easiest way to optimize a strategy using the JForex SDK? I have modified the TesterMain.java file to call my strategy, which is working fine. If I want to run the same strategy over and over again, with different parameters, what would be the best way to do this?

Use https://www.dukascopy.com/wiki/#JForex_S ... strategies? Maybe together with a loop in the main() function of TesterMain class and implement the parameter modification in that loop?

The JForex client`s Historical Tester has an optimization feature, where we can set some parameter intervals to be passed to a strategy, and then the strategy is being executed with the different parameters. By any chance, is there such thing already implemented for the API itself? So I don`t have to bother to implement/code this.


Thanks and regards.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Tue 08 Jan, 2013, 18:10 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tcsabina wrote:
Maybe together with a loop in the main() function of TesterMain class and implement the parameter modification in that loop?
The approach is up to you.
tcsabina wrote:
By any chance, is there such thing already implemented for the API itself?
There is not, otherwise it would be described here https://www.dukascopy.com/wiki/#JForex_SDK.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Fri 11 Jan, 2013, 00:31 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

How can I find out when will a strategy stop? It is started by
client.startStrategy()

I am trying to create something like this, using TesterMain.java from the SDK:
...
public static void main(String[] args) throws Exception {
...
LOGGER.info("Starting strategy");
mySuperStrategy strategy1 = new mySuperStrategy();
mySuperStrategy strategy2 = new mySuperStrategy();

strategy1.stopLoss = 20;
strategy1.takeProfit = 20;
strategy2.stopLoss = 40;
strategy2.takeProfit = 40;
       
client.startStrategy(strategy1, new LoadingProgressListener() {
    @Override
    public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
        LOGGER.info(information);
    }

    @Override
    public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
    }

    @Override
    public boolean stopJob() {
        return false;
    }
});

// Have to wait here till the first instance is stopped. How to do this?
client.startStrategy(strategy2, new LoadingProgressListener() {
    @Override
    public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
        LOGGER.info(information);
    }

    @Override
    public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
    }

    @Override
    public boolean stopJob() {
        return false;
    }
});
}
...


Edit:
I quickly came up with something like this:
myStrategy.java
public class myStrategy implements IStrategy
{
    ...
    private boolean isRunning;
    ...
    public void onStart(IContext context) throws JFException
    {
        ...
        isRunning = true;
        ...
    }

    public void onStop() throws JFException
    {
        ...
        isRunning = false;
        ...
    }

    public boolean getRunning()
    {
        return this.isRunning;
    }
}

TesterMain.java:
...
mySuperStrategy strategy1 = new mySuperStrategy();
mySuperStrategy strategy2 = new mySuperStrategy();
...
// Have to wait here till the first instance is stopped. How to do this?
while (strategy1.getRunning())
{}

client.startStrategy(strategy2, new LoadingProgressListener() {
    ...
}

I was not able to test it yet, but before I implement, would be nice to know if there is a better way.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Fri 11 Jan, 2013, 08:35 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tcsabina wrote:
How can I find out when will a strategy stop?
In ISystemListener.onStop.
tcsabina wrote:
I am trying to create something like this, using TesterMain.java from the SDK:

...
public static void main(String[] args) throws Exception {
...
LOGGER.info("Starting strategy");
mySuperStrategy strategy1 = new mySuperStrategy();
mySuperStrategy strategy2 = new mySuperStrategy();
 
strategy1.stopLoss = 20;
strategy1.takeProfit = 20;
strategy2.stopLoss = 40;
strategy2.takeProfit = 40;
         
client.startStrategy(strategy1, new LoadingProgressListener() {
    @Override
    public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
        LOGGER.info(information);
    }
 
    @Override
    public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
    }
 
    @Override
    public boolean stopJob() {
        return false;
    }
});
 
// Have to wait here till the first instance is stopped. How to do this?
client.startStrategy(strategy2, new LoadingProgressListener() {
    @Override
    public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
        LOGGER.info(information);
    }
 
    @Override
    public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
    }
 
    @Override
    public boolean stopJob() {
        return false;
    }
});
}
...

For conciseness and modifiability purposes consider adding both empty constructor and constructor to strategy that accepts all configurable parameters and instantiate strategies the following way:
mySuperStrategy[] strategies = new mySuperStrategy[]{
    new mySuperStrategy(20, 20),
    new mySuperStrategy(40, 40),
};

and then run them the following way:
for(IStrategy strategy : strategies){
    client.startStrategy(strategy);
}


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Thu 17 Jan, 2013, 00:58 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

Could you help me on the subject to put the pieces together?

API Support wrote:
and then run them the following way:
for(IStrategy strategy : strategies){
    client.startStrategy(strategy);
}

If I use this, the strategies will be executed right after each other. I want to avoid run multiple strategy instances as my hardware resources are limited. I rather wait for the 1st strategy instance stop, and the fire the next one.

I also would like to implement something like the platform has. To have a kind of array of the parameters, which will be used for a strategy instance.
This is what I came up with (TesterMain.java):
....
static boolean isRunning = false;
public static void main(String[] args) throws Exception {
...
    client.setSystemListener(new ISystemListener() {
        @Override
        public void onStart(long processId) {
            LOGGER.info("Strategy started: " + processId);
            isRunning = true;
        }

        @Override
        public void onStop(long processId) {
            LOGGER.info("Strategy stopped: " + processId);
            isRunning = false;
            File reportFile = new File("E:\\report.html");
            try {
                  client.createReport(processId, reportFile);
            } catch (Exception e) {
                  LOGGER.error(e.getMessage(), e);
            }
            // I have to disable this, otherwise after the 1st strategy instance is stops, the program terminates
//            if (client.getStartedStrategies().size() == 0) {
//                   System.exit(0);
//            }
        }

        @Override
        public void onConnect() {
            LOGGER.info("Connected");
        }

        @Override
        public void onDisconnect() {
            //tester doesn't disconnect
        }
    });
...
    //start the strategy
    LOGGER.info("Starting strategy");
    double[] takeProfit =  new double[]{20, 40, 60, 80};
    double[] stopLoss =  new double[]{20, 40, 60, 80};

    for (int i = 0; i < takeProfit.length; i++)
        for (int j = 0; j < stopLoss.length; j++)
        {
            myStrategy strategy = new myStrategy(takeProfit[i], stopLoss[j]);
            client.startStrategy(strategy);
            while (isRunning == true) // is this safe to use?
            {
            }              
        }

        if (client.getStartedStrategies().size() == 0) {
               System.exit(0);
        }
}


I am not sure if this is good at all. I have just started to gather experience with Eclipse debugging, so it is a bit difficult for me to test and verify the code.
I am also not sure about the safety of the infinite loop (while (isRunning == true){}). Will the strategy run fine (and create log files, update the console, etc.) while the main thread is in a loop?


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Thu 17 Jan, 2013, 17:16 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tcsabina wrote:
Will the strategy run fine (and create log files, update the console, etc.) while the main thread is in a loop?
Each strategy runs in its own thread, meaning that an infinite loop in a non-strategy thread won't affect it.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Fri 01 Feb, 2013, 14:11 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

I have the following code snippet:
....
static boolean isRunning = false;
public static void main(String[] args) throws Exception {
...
    client.setSystemListener(new ISystemListener() {
        @Override
        public void onStart(long processId) {
            LOGGER.info("Strategy started: " + processId);
            isRunning = true;
        }
 
        @Override
        public void onStop(long processId) {
            LOGGER.info("Strategy stopped: " + processId);
            isRunning = false;
            File reportFile = new File("E:\\report.html");
            try {
                  client.createReport(processId, reportFile);
            } catch (Exception e) {
                  LOGGER.error(e.getMessage(), e);
            }
            // I have to disable this, otherwise after the 1st strategy instance is stops, the program terminates
//            if (client.getStartedStrategies().size() == 0) {
//                   System.exit(0);
//            }
        }
 
        @Override
        public void onConnect() {
            LOGGER.info("Connected");
        }
 
        @Override
        public void onDisconnect() {
            //tester doesn't disconnect
        }
    });
...
    //start the strategy
    LOGGER.info("Starting strategy");
    double[] takeProfit =  new double[]{20, 40};
    double[] stopLoss =  new double[]{20, 40};
 
    for (int i = 0; i < takeProfit.length; i++)
        for (int j = 0; j < stopLoss.length; j++)
        {
            myStrategy strategy = new myStrategy(takeProfit[i], stopLoss[j]);
            client.startStrategy(strategy);
            while (isRunning == true) // this is not working. only 1 strategy instance is being executed
            {
            }             
        }
 
        if (client.getStartedStrategies().size() == 0) {
               System.exit(0);
        }
}


This is not working as designed, as only 1 strategy instance will run.
The embedded for() loop generates 4 instances, so it should be 4 instances, called after each other whenever the previous is finished.
If I remove the "while (isRunning == true)" part, all 4 instance are created. But as I stated above, I don`t want multiple strategies run at the same time.
Could you help me find out what is wrong with the code above? And how to achieve what I want: run strategy instances after each other, several time (10, 100, 1000 times), but always only once at a time.

Thanks.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Tue 05 Feb, 2013, 09:05 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You should take into account that the system listener runs in another thread (likewise strategy), thus, ISystemListener.onStart will set isRunning = true, but a while after the execution of line 49. Therefore consider adding some additional variable for stage, when you are waiting on strategy start. Also for thread-safety consider using AtomicBoolean instead of a primitive variable.


 
 Post subject: Re: optimizing strategy in JForex SDK Post rating: 0   New post Posted: Fri 08 Feb, 2013, 12:53 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

I replaced the while loop with:
while (client.getStartedStrategies().size() != 0) {
}

and this is doing what I want.

Thanks for guiding me in the good direction.


 

Jump to:  

  © 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