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.

strategy does not run in Historical Tester
 Post subject: strategy does not run in Historical Tester Post rating: 0   New post Posted: Wed 22 Jun, 2011, 18:44 

User rating: -
package jforex;

import java.util.*;

import com.dukascopy.api.*;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import java.util.concurrent.ExecutionException;
import java.lang.InterruptedException;

public class HistoryTester implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
    private Future<Object> future;
    private boolean isOrdered = false;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
       
       
                           
       
   }

   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 {
        if ( ! isOrdered )
        {
            if (instrument == Instrument.EURUSD)
            {
                String label = String.format("order%d", System.currentTimeMillis());
              MarketOrder marketBuy  = new MarketOrder("BUY" + label, instrument, IEngine.OrderCommand.BUYSTOP, 1000, tick.getAsk() + 5 * instrument.getPipValue(), 1000.0);
               console.getOut().println("> a");
                try {
                    future = context.executeTask(marketBuy);
                    console.getOut().println("> b");
                    IOrder myOrder = (IOrder)future.get();
                    console.getOut().println("> c");
                } catch (InterruptedException e)
                {
                    console.getErr().println("InterruptedException");
                    console.getErr().println(e.getMessage());
                } catch ( ExecutionException e)
                {
                    console.getErr().println("ExecutionException");
                    console.getErr().println(e.getMessage());
                }
                isOrdered = true;
           }
        }
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    private class MarketOrder implements Callable<Object>{
        private String label;
        private Instrument instrument;
        private IEngine.OrderCommand orderCommand;
        private double amount;
        private double price;
        private double slippage;
        private double stopLossPrice;
        private double takeProfitPrice;
       
        public MarketOrder( String label_,
                            Instrument instrument_,
                            IEngine.OrderCommand orderCommand_,
                            double amount_,
                            double price_,
                            double slippage_)
        {
            this.label           = label_;
            this.instrument      = instrument_;
            this.orderCommand    = orderCommand_;
            this.amount          = amount_;
            this.price           = price_;
            this.slippage        = slippage_;
            this.stopLossPrice   = 0.0;
            this.takeProfitPrice = 0.0;
           
            console.getOut().println("MarketOrder() " + this.label);
        }
                       
        public IOrder call() throws Exception {
            console.getOut().println("MarketOrder.call()");
           
            try {
                String label = String.format("order%d", System.currentTimeMillis());
                return engine.submitOrder(  this.label,
                                            this.instrument,
                                            this.orderCommand,
                                            this.amount/1000000.0,
                                            this.price,
                                            this.slippage,
                                            this.stopLossPrice,
                                            this.takeProfitPrice);
            } catch (Exception e) {
                console.getErr().println("exception");
                console.getErr().println(e.getMessage());
                return null;
            }
        }
    }
   
}


 
 Post subject: Re: strategy does not run in Historical Tester Post rating: 0   New post Posted: Thu 23 Jun, 2011, 12:23 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
(IOrder)future.get() is redundant and caused the startegy to wait indefinately. Consider the following onTick method:
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (!isOrdered) {
         if (instrument == Instrument.EURUSD) {
            String label = String.format("order%d", System.currentTimeMillis());
            MarketOrder marketBuy = new MarketOrder("BUY" + label, instrument, IEngine.OrderCommand.BUYSTOP, 1000, tick.getAsk() + 5
                  * instrument.getPipValue(), 1000.0);
            console.getOut().println("> a");
            future = context.executeTask(marketBuy);
            console.getOut().println("> b");
            isOrdered = true;
         }
      }
   }
For spawning new threads in IStrategy see:
https://www.dukascopy.com/wiki/index.php?title=Threading


 
 Post subject: Re: strategy does not run in Historical Tester Post rating: 0   New post Posted: Fri 24 Jun, 2011, 07:03 

User rating: -
The code was an example to show the blocking and is not the actual code.

Does this mean I have to check first if the current thread is the strategy thread and if so call it directly instead of using future?


 
 Post subject: Re: strategy does not run in Historical Tester Post rating: 0   New post Posted: Mon 27 Jun, 2011, 09:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
trd wrote:
Does this mean I have to check first if the current thread is the strategy thread and if so call it directly instead of using future?
No, this is a check you would make in a method (outside IStrategy) if you would want to find out if it is being called from IStrategy or some other thread.


 

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