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.

Closed Account Balance
 Post subject: Closed Account Balance Post rating: 0   New post Posted: Thu 28 Jul, 2011, 02:59 

User rating: 0
Joined: Wed 15 Jun, 2011, 00:55
Posts: 21
Location: AustraliaAustralia
Hey Everyone,

I'm looking for something that will show me the current account balance, NOT including any profit/loss from open positions. ie. Equity minus open profit/loss. I have no idea how to code this, but would greatly appreciate any assistance!

Thanks.


 
 Post subject: Re: Closed Account Balance Post rating: 0   New post Posted: Thu 28 Jul, 2011, 14:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi
In method onAccount(IAccount account) equity can be obtained by account.getEquity().
Profit/loss can be found by inspecting all orders.

Here is a code sample that prints current equity, total profit/loss and equity minus profit/loss:
   public void onAccount(IAccount account) throws JFException {
        double profitLoss = 0;
        for (IOrder order : engine.getOrders()) {
            profitLoss = profitLoss + order.getProfitLossInUSD();           
        }
        double equity = account.getEquity();
        console.getOut().println( new Formatter().format(" equity = % f profit/loss = % f equity - profit/loss = % f ", equity, profitLoss, equity - profitLoss ) );
   }


 
 Post subject: Re: Closed Account Balance Post rating: 0   New post Posted: Fri 29 Jul, 2011, 05:06 

User rating: 0
Joined: Wed 15 Jun, 2011, 00:55
Posts: 21
Location: AustraliaAustralia
Hi,

Thanks for your fast response and for the code. Like I said, I'm completely new to all of this, so forgive me for asking a dumb question, but how do I go about getting this to display in the platform? I don't care how or where it is displayed, I just want to know the current balance at all times.

Many thanks,
fifty_fifty


 
 Post subject: Re: Closed Account Balance Post rating: 0   New post Posted: Fri 29 Jul, 2011, 10:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See: https://www.dukascopy.com/wiki/index.php ... _in_JForex
Then, before saving the newly created strategy, replace the strategy source code with the following:
package jforex.strategies;

import java.util.Formatter;

import com.dukascopy.api.*;

public class FirstStrategy implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IContext context;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.context = context;;
   }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
       for (IOrder order : engine.getOrders()) {
           engine.getOrder(order.getLabel()).close();
       }
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (instrument != Instrument.EURUSD)
         return;
      
        double profitLoss = 0;
        for (IOrder order : engine.getOrders()) {
            profitLoss = profitLoss + order.getProfitLossInUSD();           
        }
        double equity = context.getAccount().getEquity();
        console.getOut().println( new Formatter().format(" equity = % f profit/loss = % f equity - profit/loss = % f ", equity, profitLoss, equity - profitLoss ) );
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


 

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