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.

How to get spread realtime
 Post subject: How to get spread realtime Post rating: 0   New post Posted: Wed 03 Aug, 2011, 19:39 

User rating: 0
Joined: Wed 03 Aug, 2011, 19:31
Posts: 3
Location: TR
Hi to all,

Trying to code a strategy with ability to open positions for many pairs, at some point I have to check is the spread is below the acceptable value, but could not find any way.
Since spread is very usable info there should be some variables holding this data in real time for all pairs !!? ( like MT4 marketinfo(pair,spread) function)
Have an idea of coding an array in "onTick" which subtracts Ask and Bid prices and holds it in the array, but the idea of continuously taking cpu time and my level of java dont let me do it :)

Can you share your ideas or better code examples for spread ?
Thanks alot.


 
 Post subject: Re: How to get spread realtime Post rating: 0   New post Posted: Thu 04 Aug, 2011, 09:09 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
caga wrote:
Since spread is very usable info there should be some variables holding this data in real time for all pairs !!?
The data is held in IHistory for all subsribed currency pairs. Consider the following approach to working with spread:
package jforex.test;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

import com.dukascopy.api.*;

/**
 * The strategy on every bar prints out  the spread of the last tick and the biggest spread
 * over the bar's period
 *
 */
public class SpreadTest implements IStrategy {

   @Configurable("Period")
   public Period period = Period.TEN_SECS;
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   
   private IConsole console;
   private IHistory history;
   
   private SimpleDateFormat sdf;
   private DecimalFormat df = new DecimalFormat("0.00000");
   
   @Override
   public void onStart(IContext context) throws JFException {
      console = context.getConsole();
      history = context.getHistory();

      sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
   }

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if(period != this.period || instrument != this.instrument)
         return;
      print ("last tick spread: " + df.format(getSpread()) + " max spread over last " + period + ": "+ df.format(getMaxSpread(period)) );

   }
   
   private double getSpread() throws JFException{
      ITick tick = history.getLastTick(instrument);
      return tick.getAsk() - tick.getBid();
   }

   private double getMaxSpread(Period period) throws JFException{
      long lastTickTime = history.getLastTick(instrument).getTime();
      List<ITick> ticks = history.getTicks(instrument, lastTickTime - period.getInterval(), lastTickTime);
      print("tick count over the interval: " + ticks.size());
      double maxSpread = 0.0;
      for (ITick tick : ticks){
         if(maxSpread < tick.getAsk() - tick.getBid())
            maxSpread = tick.getAsk() - tick.getBid();
      }
      return maxSpread;
   }
   
   private void print(Object o){
      console.getOut().println(o);
   }
   
   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {   }
   @Override
   public void onMessage(IMessage message) throws JFException {}
   @Override
   public void onAccount(IAccount account) throws JFException {}
   @Override
   public void onStop() throws JFException {}
   
}



 
 Post subject: Re: How to get spread realtime Post rating: 0   New post Posted: Fri 05 Aug, 2011, 09:27 

User rating: 0
Joined: Wed 03 Aug, 2011, 19:31
Posts: 3
Location: TR
Thanks for the reply, will test this.


 

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