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 {
}
}