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.

Scala und JForex
 Post subject: Scala und JForex Post rating: 0   New post Posted: Wed 17 Nov, 2010, 01:03 

User rating: 0
Joined: Wed 17 Nov, 2010, 00:31
Posts: 2
I use Scala with Simple Build Tool (SBT). Have created a project and copied the JForex libs in the lib folder. I have after that writte in the src/main/scala folder a simple file called SForex.scala:
import com.dukascopy.api.Instrument
import com.dukascopy.api.system.IClient
import com.dukascopy.api.system.ClientFactory
import com.dukascopy.api.system.ISystemListener

import java.util.HashSet

import org.slf4j.LoggerFactory

object SForex {

  def main(args: Array[String]) {

    // Arguments
    var username = "XXX"
    var password = "XXX"

    // Initialize the logger.
    val log = LoggerFactory.getLogger(getClass)
    org.apache.log4j.BasicConfigurator.configure

    // Get default instance of Dukascopy IClient
    val client = ClientFactory.getDefaultInstance

    // Create system listener
    val systemListener = new ISystemListener {

      var maxReconnects = 3

      override def onConnect {

        log.info("[network] connected on dukascopy server")
        println("[network] connected on dukascopy server")
        maxReconnects = 3

      }

      override def onDisconnect {

        log.warn("[network] disconnected from dukascopy server")
        println("[network] disconnected from dukascopy server")
        if (maxReconnects > 0) {

            client.reconnect
            maxReconnects = maxReconnects - 1

        } else {

            try {
              Thread.sleep(10000)
            } catch {
              case e:InterruptedException => //ignore
            }

            try {
              client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", username, password)
            } catch {
              case e:Exception => {
                log.error(e.getMessage(), e)
                println(e.getMessage(), e)
              }
            }

        }

      }

      override def onStart(processId: Long) {

        log.info("[strategy] started process id: " + processId)
        println("[strategy] started process id: " + processId)

      }

      override def onStop(processId: Long) {

        log.info("[strategy] stopped process id: " + processId)
        println("[strategy] stopped process id: " + processId)
        if (client.getStartedStrategies.size == 0)
          exit(0)

      }

    }

    // Set system listener
    client.setSystemListener(systemListener)

    // Connect to Dukascopy
    log.info("[network] connecting to dukascopy server...")
    println("[network] connecting to dukascopy server...")

    client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", username, password)

    // Wait of Dukascopy response
    var maxSeconds = 10
    while (maxSeconds > 0 && !client.isConnected) {

        Thread.sleep(1000)
        maxSeconds = maxSeconds - 1

    }

    // If not connected, exit
    if (!client.isConnected()) {
        log.error("[network] failed to connect dukascopy servers")
        println("[network] failed to connect dukascopy servers")
        exit(1)
    }

    //now it's running
    exit(0)

  }

}

If the code passed the last row resp. comment "now it's running", the exit(0) function don't stop the whole application (hang up). I have read in this topic viewtopic.php?f=5&t=2672&view=previous that ISystemListener use own thread. It's possible kill this thread? And if yes, can the console application after that shutdown normal or should I kill additional another thread?
In another words, I need an advice how a console Application based on JForex API can shutdown normal without Ctrl+C ;)


 
 Post subject: Re: Scala und JForex Post rating: 0   New post Posted: Fri 19 Nov, 2010, 15:30 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
    make a new thread to wait for user input
    read an external file for command
    make a client/server socket

I've been thinking of trying Scala. Is it that much better to use it for trading system? What resources can you recommend about using Scala for financial analysis?


 
 Post subject: Re: Scala und JForex Post rating: 0   New post Posted: Fri 19 Nov, 2010, 19:05 

User rating: 0
Joined: Wed 17 Nov, 2010, 00:31
Posts: 2
Did you mean write the whole JForex IClient code inside a new thread and destroy this thread for normal exit? Mhh, I have tried but the application still hang. As far as I know is the Socket connection between IClient and Dukascopy server the problem, I can't disconnect them and any thread blocked :(

Regarding Scala, I have never coded with Java but rather with C and C++. And anytime I have seen the new Scala language and experimented with them. So I have continued this with JForex now. Whether it would be better, is a question of needs. If you want speed use C++ or C (or even Assembler :P). A great benefit in Scala is the very compact code, simple Thread coding with Actors and you have access of all Java packages. If you write Scala and know Java, you missing nothing :) And in combination with the LiftWeb Framework, you have easy interface to third party databases like PostgreSQL or others. And for communication with another software components you can use simple JMS about ActiveMQ. One of the negative side of Scala is the often changes in the API and language too, because Scala is young. But I can't judge whether Scala is ideal for financial system, except that for me it's the right language.


 

Jump to:  

  © 1998-2024 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