Dukascopy
 
 
Wiki JStore Search Login

Help with merge order profit and loss calculation
 Post subject: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Thu 19 Jan, 2012, 04:08 
User avatar

User rating: 0
Joined: Tue 29 Nov, 2011, 02:17
Posts: 25
Location: Australia,
The very simple strategy below buys 1M GBP, then buys another 1M GBP, then sells 1M GBP and then sells 1M GBP again.

So the total position is flat. Then the strategy merges all four orders. Shortly afterwards all orders are closed if they are not already close and finally a summary of the orders is output.


The output from the strategy run between 04/10/2011 10:00:00 and 04/10/2011 10:00:40 is:

Quote:
2012-01-19 15:44:23.844 INFO Main - Executing onStop
Label: BUY1M_1
Amount: 1.0
Open: 1.53932
Close: 1.53932
Commission: 18.47184
PandL: 0.0

Label: BUY1M_2
Amount: 1.0
Open: 1.53927
Close: 1.53927
Commission: 18.471239999999998
PandL: 0.0

Label: SELL1M_1
Amount: 1.0
Open: 1.53905
Close: 1.53905
Commission: 18.468600000000002
PandL: 0.0

Label: SELL1M_2
Amount: 1.0
Open: 1.53909
Close: 1.53909
Commission: 18.46908
PandL: 0.0

Label: MERGE
Amount: 0.0
Open: 0.0
Close: 0.0
Commission: 0.0
PandL: 0.0

context.getAccount().getEquity() = 999466.12
profit = 533.8800000000047
commission = 73.88076
profit before commission = 459.99924000003375
2012-01-19 15:44:23.857 INFO Main - Strategy stopped: 1
leaving onstop


Can somebody please explain how the number $999,466.12 which is output by getEquity() was calculated? I get $999,476.12 whatever way I try to calculate it.

package Strategies;

import java.util.ArrayList;
import java.util.List;

import com.dukascopy.api.*;

public class ExampleMerge implements IStrategy {

   IContext context;
   int callcount;
   List<IOrder> orders = new ArrayList<IOrder>();

   public void onStart(IContext context) throws JFException {
      this.context = context;
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {

      boolean useMerge = true;

      if (callcount == 10)
         orders.add(context.getEngine().submitOrder("BUY1M_1", instrument,
               IEngine.OrderCommand.BUY, 1));

      if (callcount == 20)
         orders.add(context.getEngine().submitOrder("BUY1M_2", instrument,
               IEngine.OrderCommand.BUY, 1));

      if (callcount == 30)
         orders.add(context.getEngine().submitOrder("SELL1M_1", instrument,
               IEngine.OrderCommand.SELL, 1));

      if (callcount == 40)
         orders.add(context.getEngine().submitOrder("SELL1M_2", instrument,
               IEngine.OrderCommand.SELL, 1));

      if (useMerge) {
         if (callcount == 50)
            orders.add(context.getEngine().mergeOrders(
                  "MERGE", orders.toArray(new IOrder[0])));
      }

      if (callcount == 60)
         for (IOrder order : orders) {
            if (IOrder.State.FILLED.equals(order.getState()))
               order.close();
         }

      callcount = callcount + 1;
   }

   public void onMessage(IMessage message) throws JFException {
   }


   public void onAccount(IAccount account) throws JFException {
   }

   public void onStop() throws JFException {
      double commission = 0;
      for (IOrder i : orders) {
         System.out.println("       Label: " + i.getLabel());
         System.out.println("      Amount: " + i.getAmount());
         System.out.println("        Open: " + i.getOpenPrice());
         System.out.println("       Close: " + i.getClosePrice());
         System.out.println("  Commission: " + i.getCommission()*i.getOpenPrice());
         System.out.println("       PandL: " + i.getProfitLossInUSD());
         System.out.println("");
         commission += i.getCommission()*i.getOpenPrice();
      }

                System.out.println("context.getAccount().getEquity() = " +  context.getAccount().getEquity());

      System.out.println("profit = " + (1000000 - context.getAccount().getEquity()));

      System.out.println("commission = "
            + commission);
      
      System.out.println("profit before commission = "
            + (1000000 - (context.getAccount().getEquity() + commission)));
      
   }

   public void onBar(Instrument instrument, Period period, IBar askBar,
         IBar bidBar) throws JFException {
   }

}


 
 Post subject: Re: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Thu 19 Jan, 2012, 08:57 
User avatar

User rating: 2
Joined: Thu 19 May, 2011, 09:37
Posts: 86
Location: India, Chennai
your answer is in line 75
System.out.println("profit = " + (1000000 - context.getAccount().getEquity()));

1000,000-533.88


 
 Post subject: Re: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Thu 19 Jan, 2012, 20:05 
User avatar

User rating: 0
Joined: Tue 29 Nov, 2011, 02:17
Posts: 25
Location: Australia,
Thanks Victor, but my question is how does getEquity() get to that figure from the IOrder data.

In other words, how can we replicate the getEquity() for merge orders by processing the messages which get sent to onMessage() and how can we keep an up-to-date Net Asset Value figure inside onTick() based on the current ITick values?

e.g. if we calculate the weighted average BUY price and the weighted average SELL price, then the P/L is before commissions is $450, which is different to the value which getEquity() outputs by $10.


 
 Post subject: Re: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Mon 23 Jan, 2012, 07:14 
User avatar

User rating: 0
Joined: Tue 29 Nov, 2011, 02:17
Posts: 25
Location: Australia,
Does anybody know what the calculation is to take the fields in the IOrder objects (as given above) which replicates the same number that is in getEquity() in the above example? PS there is no need to replicate the experiment, just to explain the calculation.


 
 Post subject: Re: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Wed 25 Jan, 2012, 01:40 
User avatar

User rating: 0
Joined: Tue 29 Nov, 2011, 02:17
Posts: 25
Location: Australia,
Can Dukascopy please respond and show how to calculate the same value reported by getEquity() (i.e. 999466.12) in the example above?

Where did that extra $10 come from?

If the question is not clear enough then please say so and I will attempt to increase clarity but I have posted the strategy file, the conditions of the experiment and the results in as clear a format as I could make.

thanks.


 
 Post subject: Re: Help with merge order profit and loss calculation Post rating: 0   New post Posted: Sun 19 Feb, 2012, 02:21 

User rating: -
Any advances?


 

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