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.

Position Summary available in strategie?
 Post subject: Position Summary available in strategie? Post rating: 0   New post Posted: Tue 02 Aug, 2011, 10:29 

User rating: -
Hi, i would like to get position summary infos for my strategie. Does API provide me with a function, or do I need to calculate ip by myself?

Greetings ayk


 
 Post subject: Re: Position Summary available in strategie? Post rating: 0   New post Posted: Tue 02 Aug, 2011, 13:14 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Currently it is not included in the API. Consider the following strategy which prints out the position summary:
package jforex.info;

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

import com.dukascopy.api.*;

/**
 * The strategy prints position summary over an instrument: order count, order amount and weighed price
 * The data reflects those shown in "Position Summary" tab
 *
 */
public class PositionSummaryOnTick implements IStrategy {

   private IEngine engine;
   private IConsole console;
   
   private Instrument instrument = Instrument.GBPUSD;
   
    @SuppressWarnings("serial")
   private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};
   private DecimalFormat df = new DecimalFormat("0.00000");
   
   @Override
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
   }

   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if(instrument != this.instrument)
         return;
      printPositionSummary(tick.getTime());
   }
   
   private void printPositionSummary(long time) throws JFException {

      int orderCount = 0;
      double totalAmount = 0;
      double weightedPrice = 0;
      for (IOrder order : engine.getOrders(instrument)) {
         if (order.getState() != IOrder.State.FILLED)
            continue;

         orderCount++;
         //Negate SHORT orders
         double amount = order.getAmount() * (order.isLong() ? 1 : -1);
         totalAmount += amount;
         weightedPrice += amount * order.getOpenPrice();
      }
      //normalize weighted price - as if amount sum were 1
      weightedPrice /= totalAmount;
      
      print(sdf.format(time) + " Order Count=" + orderCount + " Total Amount="+totalAmount + " Weighted Price=" + df.format(weightedPrice));
   }
   
   private void print(Object o){
      console.getOut().println(o);
   }
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
   public void onMessage(IMessage message) throws JFException {}
   public void onAccount(IAccount account) throws JFException {}
   public void onStop() throws JFException {}
}


 

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