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.

Issue with .getProfitLossInAccountCurrency() or "+=" operator problem.
 Post subject: Issue with .getProfitLossInAccountCurrency() or "+=" operator problem. Post rating: 0   New post Posted: Fri 03 Jan, 2014, 11:55 
User avatar

User rating: 0
Joined: Sun 10 Mar, 2013, 16:32
Posts: 6
Location: United Kingdom, slough
Happy New Year for all at Dukascopy Support Team and our community!

I tried to solve this problem for past one week but finally i gave up.

Lets say I have an ArrayList or Map of orders that interest me. I want to get ProfitLoss in total value for those orders combined.
Once printing this I get the right value:
public void onAccount(IAccount account) throws JFException { 
for (IOrder o : MainOrders.values()) {
                if(o.getState() == IOrder.State.FILLED){
                    profitLoss += o.getProfitLossInAccountCurrency();
                    totalAmount += o.getAmount();
               } 
            console.getOut().format("last server equity=%.2f calculated equity=%.2f profit/loss=%.2f balance=%.2f total amount=%.3f",
                account.getEquity(), history.getEquity(), profitLoss, account.getCreditLine(), totalAmount).println();
        }


But once I add this statement :
I get wrong value of ProfitLoss for those orders in the Map/Array.
Therefore this "if" statement is not or wrongly executed.
 if(profitLoss >= minprofitrounded){                            
                             console.getOut().println(String.format("ORDERS CLOSED " + minprofitrounded + "and profitloss= " + profitLoss + " || Actual LotSize now =" + lotsizerounded + " || pipvalue= " + pipvalue2 ));                                       
                    }                                                               




Messages : where u see properly displayed value under profit/loss=-160.43 and same variable wrongly via profitloss=34.31.
profitloss represents last order which was iterated.
I know that i use wrong way to string.format this value but testing shows that this is actually the problem. That profitloss variable is showing only the last order and not total orders profitloss summarised.
Quote:
2014.01.03 10:42:24 ORDERS CLOSED 25.8and profitloss= 34.31 || Actual LotSize now 0.04 || pipvalue= 2.58
2014.01.03 10:42:24 last server equity=9833.08 calculated equity=9833.08 profit/loss=-160.43 balance=949039.14 total amount=0.240


I looked closely and seems like under o.getProfitLossInAccountCurrency(); it stores each order value separately and via += it summarise them.
 profitLoss += o.getProfitLossInAccountCurrency();

But later I cant use profitLoss variable for anything than printing its values.

I want to compare total profit of orders to close them after reaching some static profit value. Like TakeProfit.


What do I do wrongly, am I missing something?
Please help.


 
 Post subject: Re: Issue with .getProfitLossInAccountCurrency() or "+=" operator problem. Post rating: 0   New post Posted: Fri 03 Jan, 2014, 12:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please provide a full example strategy which replicates the case? Also please provide information about what opened orders/positions you have.


 
 Post subject: Re: Issue with .getProfitLossInAccountCurrency() or "+=" operator problem. Post rating: 0   New post Posted: Sat 04 Jan, 2014, 15:17 
User avatar

User rating: 0
Joined: Sun 10 Mar, 2013, 16:32
Posts: 6
Location: United Kingdom, slough
Dear Support,

Please find attached file with Startegy1 which replicates this problem.

Please note this is only simple example strategy i made for problem solving purpose.

I hope hearing from you soon.


Attachments:
Strategy1.java [13.5 KiB]
Downloaded 95 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Issue with .getProfitLossInAccountCurrency() or "+=" operator problem. Post rating: 0   New post Posted: Mon 06 Jan, 2014, 10:28 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We could not replicate this. In fact the strategy never entered the PL calculation block that you referred to. Consider trying to resolve the issue on a smaller scale, without additional strategy logic and data structures, in a similar way that it is done in the wiki examples. If you have suspicion that some values during your calculation are wrong, consider adding additional logging statementsto track exactly where the problem lies. Alternatively consider running the strategy from JForex-SDK and debugging it.
On another note - on your strategy:
  1. Consider moving the P/L calculation logic from onAccount to onTick.
  2. For your purpose map usage is an overkill, consider using a simple IOrder reference for both H1 and long.
  3. Why do you need to round prices before comparison? In general you only need rounding where it is required by the API for value setting. For displaying rounded values when printing consider using String.format as it is shown in multiple wiki examples.


 

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