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.

Renko Asynchronous Threading
 Post subject: Renko Asynchronous Threading Post rating: 0   New post Posted: Mon 04 Aug, 2014, 22:06 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Hello,
I've recently been trying to run a few simultaneous Renko simulations but keep getting thread overloads (it never happened with just one sim working - each simulation is a different currency pair as I find it easier than chasing around multiple pairs within one single strategy).

After reading through the wiki and support boards I implemented asynchronously "read"-ing the necessary historical data onStart (which didn't solve the issue, but I've decided to leave it in anyway), further reading lead me onto threading which (fingers crossed) will solve the problem. I took a look at:

https://www.dukascopy.com/wiki/#Threading

and tried to implement an asynchronous version as best I could just to test that it was working but couldn't quite manage it. I tried implementing it within onFeedData (which is necessary when using a Renko feed) but I just can't debug the last compile error (asynchronous threading is new to me!).

If anyone could shed some light onto this it would be much appreciated. I have included the complete code below:

Many thanks

J
package jforex;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
//Dukascopy imports=========================
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.util.RenkoFeedDescriptor;
import com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.util.DateUtils;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.IOrder;
//============================================

public class RenkoThreadingTest implements IStrategy, IFeedListener {
   
    //================================
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    //=================================
   
    //------FEED DESCRIPTORS---------------------
    //---This is the renko data feed descriptor (2 pip bricks)
    public IFeedDescriptor feedDescriptor = new RenkoFeedDescriptor(Instrument.EURUSD,PriceRange.TWO_PIPS,OfferSide.BID);
    //---------------------------------------------
    //-------------SET DATE AND TIME FORMATS---------
    public static SimpleDateFormat MyDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
       {
       setTimeZone(TimeZone.getTimeZone("GMT"));
       }
    };
    private Instrument selectedInstrument = Instrument.EURUSD;
   //constant & variable declarations
    private double latestRenkoClosePrice;
    private IBar latestRenkoBrick;

   
    private int dataCount=0;
   //-------------------------------
    public void onStart(final 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();
       
        //--subscribe to renko feed
        context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
        context.subscribeToFeed(feedDescriptor, this);

       //------------------------------last renko brick----------------------------------------------------
       latestRenkoBrick = history.getRenkoBar(selectedInstrument, OfferSide.BID, PriceRange.TWO_PIPS, 0);
       
      final long fromTime = latestRenkoBrick.getTime()-TimeUnit.DAYS.toMillis(3);
      final long toTime = latestRenkoBrick.getTime();
     
      history.readFeedData(feedDescriptor, fromTime, toTime, new IFeedListener(){
                public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
                    dataCount++;
                }
            }, new LoadingProgressListener(){
               
                public void dataLoaded(long start, long end, long currentPosition, String information) {
                }

               
                public void loadingFinished(boolean allDataLoaded, long start, long end, long currentPosition) {
                    if(allDataLoaded){
                        console.getOut().format("%s - %s %s %s feed elements loaded",
                                DateUtils.format(fromTime), DateUtils.format(fromTime), dataCount, feedDescriptor.getDataType()).println();
                        //context.stop();
                    }
                }

               
                public boolean stopJob() {
                    return false;
                }
            }
        );
     
    }//--------------------------------end of onStart----------------------------------------------------------------
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData){
        Instrument myInstrument = feedDescriptor.getInstrument();
        OfferSide myOfferSide = feedDescriptor.getOfferSide();
     
        try{     
        IRenkoBar latestRenkoBar = history.getRenkoBar(selectedInstrument, OfferSide.BID, PriceRange.TWO_PIPS, 0);
        latestRenkoClosePrice = latestRenkoBar.getClose();   
       

            final AsynchThrdTest ThrdTest = new AsynchThrdTest(latestRenkoClosePrice);
   
            final IContext finalContext = context;
            Thread thread = new Thread(new Runnable(){
                public void run(){
                    try {
                        finalContext.executeTask(ThrdTest);
                    } catch (Exception e) {
                        console.getErr().println(Thread.currentThread().getName() + " " + e);
                    }           
                }
                });
            thread.start();           
                             
            }catch (JFException e) {
            console.getErr().println(e);
            e.printStackTrace();
        } //end of try   
    } // -----------------------------------end of onFeedData-----------------------------------------------------
   
    private class AsynchThrdTest{
        private final double currentBrickPrice;
        public AsynchThrdTest(double currentBrickPrice){
            this.currentBrickPrice = currentBrickPrice;
            if(currentBrickPrice>0){
                console.getOut().format("This asynchronous threading seems to be working: \n");
            }
        }
    }
       
             
    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 {
    }
}


 
 Post subject: Re: Renko Asynchronous Threading Post rating: 0   New post Posted: Tue 05 Aug, 2014, 14:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you precisely describe what do you want your strategy to do?


 
 Post subject: Re: Renko Asynchronous Threading Post rating: 0   New post Posted: Tue 05 Aug, 2014, 14:26 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
It is for a variety of things depending on the outcome of various technical indicators, be it adjusting a stop, entering/exiting a trade, displaying a message etc. Hence why I made the thread so basic and generic (i.e. just an if statement) so that it can then be adjusted as needs be.


 
The Best Answer  Post subject: Re: Renko Asynchronous Threading Post rating: 1   New post Posted: Tue 05 Aug, 2014, 19:24 
User avatar

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

About the compile error:
Modify your AsynchThrdTest class:
    private class AsynchThrdTest implements Callable<Object>{
        private final double currentBrickPrice;
        public AsynchThrdTest(double currentBrickPrice){
            this.currentBrickPrice = currentBrickPrice;
            if(currentBrickPrice>0){
                console.getOut().format("This asynchronous threading seems to be working: \n");
            }
        }
        public Object call() throws Exception {
            return null;
        }
    }


I don't know if this will do what you want, it is just a plain simple compile error fix.

To catch and solve these kind of errors, I suggest to use an IDE like Eclipse. There is a wiki page about setting up the JForex-SDK: https://www.dukascopy.com/wiki/#Use_in_Eclipse


 
 Post subject: Re: Renko Asynchronous Threading Post rating: 0   New post Posted: Wed 06 Aug, 2014, 21:17 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Hi tcsabina,

Thanks for your reply, your fix worked, I'm still trying to get to grips with Java (it's not a language I've ever worked with up until a few months ago) so I really appreciate your response.
I'll try my best to apply it as I need going forward.
Many thanks


 

Jump to:  

cron
  © 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