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.

Calling ITesterClient from within strategy
 Post subject: Calling ITesterClient from within strategy Post rating: 0   New post Posted: Fri 18 Nov, 2011, 17:53 
User avatar

User rating: 0
Joined: Fri 18 Nov, 2011, 16:19
Posts: 8
Location: NetherlandsNetherlands
I'm trying to launch the ITesterClient interface from within a running strategy and am not having much luck. The strategy I'm trying to test is declared with public class MAF implements IStrategy { .. in it's own MAF.java file with package selfopt; as the first line.

I'm getting these error messages:
16:10:01 ----------
16:10:01 MAF cannot be resolved to a type
16:10:01 ^^^
16:10:01 client.startStrategy(new MAF(), new LoadingProgressListener() { //
16:10:01 2. ERROR in C:\Users\Chris\AppData\Local\Temp\jfxide\tmp\mafMain2.java (at line 118)
16:10:01 ----------
16:10:01 DataLoadingMethod cannot be resolved
16:10:01 ^^^^^^^^^^^^^^^^^
16:10:01 client.setDataInterval(DataLoadingMethod.ALL_TICKS, tmFrom , tmTo);
16:10:01 1. ERROR in C:\Users\Chris\AppData\Local\Temp\jfxide\tmp\mafMain2.java (at line 117)
16:10:01 ----------

when compiling this:
package selfopt;

import java.io.File;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import selfopt.*;

public class mafMain2 implements IStrategy {
   
public double ordAmount = 0.001;
@Configurable ("intPer") public int intPer = 15;
@Configurable ("selPer") public Period selectedPeriod = Period.ONE_HOUR ;
@Configurable ("selIns") public Instrument selIns = Instrument.EURGBP;

public Period upperPeriod = Period.WEEKLY;
Set<Instrument> instSet = new HashSet<Instrument>();
private String strParam="";
private int anPeriod = 0;
private long lstWeekTm = 0;

    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IIndicators indicators;
    //private IAccount account;

   
    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 {
    }
   

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (!selIns.equals(instrument)) return;
        if ((anPeriod == 0 && period == Period.ONE_HOUR) || period == Period.WEEKLY)
        {
            long timeTo = history.getBar(instrument, Period.WEEKLY, OfferSide.BID, 0).getTime();
            long timeFrom = timeTo - Period.WEEKLY.getInterval()*2;
            try {
                anHist( timeFrom , timeTo );
            } catch (IllegalAccessException e) {
                console.getErr().println(e.getMessage());
            } catch (InstantiationException e) {
                console.getErr().println(e.getMessage());
            } catch (Exception e) {
                console.getErr().println(e.getMessage());
            }
        }
     
    }
   
    @Override
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
       
        instSet.add(selIns);
        context.setSubscribedInstruments(instSet);

    }
   
    public enum OrderStat {
        BUYFILLED, BUYUNFILLED, SELLFILLED, SELLUNFILLED, NONE
    }
   
    public void anHist(long tmFrom, long tmTo) throws JFException, Exception, IllegalAccessException, InstantiationException {
         final ITesterClient client = TesterFactory.getDefaultInstance();
            client.setSystemListener(new ISystemListener() {
                @Override
                public void onStart(long processId) {
                    console.getOut().println("Backtest started: " + processId);
                }
   
                @Override
                public void onStop(long processId) {
                    console.getOut().println("Backtest stopped: " + processId);
                    File reportFile = new File("C:\\Temp\\report.html");
                    try {
                        client.createReport(processId, reportFile);
   
                    } catch (Exception e) {
                        console.getErr().println(e.getMessage());
                    }
                    if (client.getStartedStrategies().size() == 0) {
                        System.exit(0);
                    }
                }
   
                @Override
                public void onConnect() {
                    console.getOut().println("Connected");
                }
   
                @Override
                public void onDisconnect() {
                    //tester doesn't disconnect
                }
            });
         client.setSubscribedInstruments(instSet);
         client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 5000);
         client.setDataInterval(ITesterClient.DataLoadingMethod.ALL_TICKS, tmFrom , tmTo);
         client.startStrategy(new MAF(), new LoadingProgressListener() { //
                @Override
                public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
                    double num = currentTime - startTime;
                    double tot = endTime - startTime;
                    double res = (int) (num/tot*100);
                    console.getOut().println( Double.toString(res)) ;
                }

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

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


 
 Post subject: Re: Calling ITesterClient from within strategy Post rating: 0   New post Posted: Sat 19 Nov, 2011, 20:38 
User avatar

User rating: 0
Joined: Fri 18 Nov, 2011, 16:19
Posts: 8
Location: NetherlandsNetherlands
Ok, so I kept at it and shoved the whole MAF class into the same file removing all modifiers. I then used this:
        client.setDataInterval(Period.TICK, OfferSide.BID, InterpolationMethod.CLOSE_TICK, tmFrom, tmTo);
instead of the other setDataInterval method.
With these changes it compiled for a change, bringing a small but cautious smile to my face. Upon executing though I got a "Not Connected" message. Thinking I would still have the last laugh I proceeded to shove in:
       
        String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
        String userName = "DEMOacct"; // my demo username
        String password = "password"; // my demo password
        client.connect(jnlpUrl, userName, password);

before client.startStrategy. To my dissapointment, this time the console spit out 19:22:21 access denied (java.util.PropertyPermission auth.ssl.ignore.errors read)

And here I am now, without more ideas how to get it to "RESPECT MA AUTHORITA" - Cartman


 
 Post subject: Re: Calling ITesterClient from within strategy Post rating: 0   New post Posted: Sat 19 Nov, 2011, 22:07 
User avatar

User rating: 0
Joined: Fri 18 Nov, 2011, 16:19
Posts: 8
Location: NetherlandsNetherlands
So some digging around on the forums provided me with some answers. It appears I need to @RequiresFullAccess at the top to give me the authority which I require to allow the ITesterClient to connect. Now I'm struggling with passing arguments to the strategy that is to be ran in the tester.

2011-11-19 21:06:52 ----------
2011-11-19 21:06:52 The constructor MAF(int, Period, Instrument) is undefined
2011-11-19 21:06:52 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011-11-19 21:06:52 IStrategy toRun = new MAF(15, Period.ONE_HOUR, Instrument.EURGBP);

class MAF implements IStrategy {
   
    public double ordAmount = 0.001;
    @Configurable ("intPer") public int intPer = 15;
    @Configurable ("selPer") public Period selectedPeriod = Period.ONE_HOUR ;
    @Configurable ("selIns") public Instrument selIns = Instrument.EURGBP;


 
 Post subject: Re: Calling ITesterClient from within strategy Post rating: 0   New post Posted: Sun 20 Nov, 2011, 00:01 
User avatar

User rating: 0
Joined: Fri 18 Nov, 2011, 16:19
Posts: 8
Location: NetherlandsNetherlands
So with some more coffee and digging I found I have to pass parameters in this format:
IStrategy toRun = new Maf() {{ this.tstintPer = 5; this.tstselIns = selIns;}};


 

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