Dukascopy
 
 
Wiki JStore Search Login

How to better control the number of positions (rule 6.2)
 Post subject: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 21 Jun, 2010, 08:39 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
As some participants keep asking this questions, I will post one example how you can control the amount of opened positions.

1) Declare a new boolean variable

Quote:
private boolean tradingAllowed;


2) Update the boolean variable in onAccount method
Quote:
if (account.getUseOfLeverage() == 0) { tradingAllowed = true;
} else tradingAllowed = false;


3) Before opening a new position in onBar or onTick methods, check if you are allowed to open a new positions

Quote:
if (tradingAllowed = true) { // open a new position }



Other examples will be posted in this thread below.


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 15:11 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
Dear Support,

First of all thank you for your great work.

I think your solution still allows small positions; if the intent is to have only one position (no matter how small it may be) then I think it is possible to break the rule, passing your test, provided that the overall leverage remains at zero.

Probably you represent 'leverage' (in your code) with an integer or with few digits, I do not know; but what I do know is that if the leverage is almost zero, then
the getUseOfLeverage() method would have a zero output, allowing trading, even if some position is opened; then trading would still be allowed for further positions (breaking rule 6.2).

I have not experimented it thoroughly, but I am assuming that leverage up to 0.5% would be allowed.

I have tried to have one position of a micro lot opened on a demo account of $100k: leverage stays at zero, even after the position has been opened. I have implemented your code and, in fact, my tradingAllowed was set to true.

A possible workaround would be using the following method (for example):

private static boolean isTradingAllowed(IAccount account) throws JFException
{
boolean tradAllowed = false;

double maxPossibleLeverage = account.getLeverage();
if ((account.getEquity() * maxPossibleLeverage- account.getCreditLine()) ==0)
tradAllowed = true;
return tradAllowed;

}


of course the method's signature may be different.

E.g.:
I chose to declare an IAccount field inside my strategy class, so my method's signature would be:
private boolean isTradingAllowed() throws JFException.

Also, for anyone interested in LIVE implementation of his/her own strategies: please be aware that, as is, the method gives a wrong response if some position is opened (or closed) manually, while the strategy is running.

To avoid this, my solution has been to add an extra line:

account = context.getAccount();

at the beginning of my isTradingAllowed() method.

Please let me know if you concur and/or if you can suggest a better solution.

Hope this helps.

Have a nice week.


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 17:22 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
dedalus wrote:

then
the getUseOfLeverage() method would have a zero output, allowing trading, even if some position is opened; then trading would still be allowed for further positions (breaking rule 6.2).



getUseOfLeverage() returns 0 in the 2 following cases:
- there is no any position on the account, the total exposure is equal to 0
- the exposure is equal to 0 but there are still some hedging positions on the same instruments but in the opposite directions

However, as you know, the contest platform does not allow you to have 2 positions on the platform. It makes hedging mode unavailable on the contest account. Method getUseOfLeverage() will always return 0 when you don't have any positions.


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 18:03 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
Thank you for your prompt reply.

Maybe I was not clear:

getUseOfLeverage() returns 0
also if there are open positions and the leverage is approximated to zero in finite precision.

If you are not convinced;

please try a test with a DEMO account, open a 0.1% leverage position and check the output of your solution: you will get that trading is allowed, albeit the open position (no hedging).

At least that is what is giving to me; I wonder whether there is some problem in my code, though.

Please let me know if this happens also to you.

Thanks again.


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 22:02 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
it's a typo, the test should be:

account.getUseOfLeverage() == 0d


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 22:21 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
quantisan,

Thanks a lot for pointing this out.

Unfortunately I still encounter the same problem if I use the procedure described above. Please let me know if this is also true in your case.

Thanks


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 22:51 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
I update this.account in onAccount() then check

   private boolean isTradingAllowed() {
      return (this.account.getUseOfLeverage() == 0d);
   }


before making any trade. I used to run into your problem too but it seems ok now with this for a few months. Perhaps I haven't tested it thoroughly?


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Mon 08 Nov, 2010, 23:20 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
quantisan,

I am reporting you the messages from my last test.

Equity 111k circa, a 25k USD open position on EURUSD. Use of Leverage on the screen is 0% (in fact it is <.5%).

I run online the following code:

public void onAccount(IAccount account) throws JFException {

// check whether there open positions
if (isTradingAllowed())
{
print("my isTradingAllowed() is true");}
else

{
print("my isTradingAllowed() is false");}
if (account.getUseOfLeverage() == 0d )
{
print("true .getUseOfLeverage()");}
else
{
print("false .getUseOfLeverage()");}
}

Messages received:


2010-11-08 22:19:43 true .getUseOfLeverage()
2010-11-08 22:19:43 my isTradingAllowed() is false

isTradingAllowed() is the method posted above. I agree with you that we should get the right response, but at least it is not working for me.

Thanks again for your help.

P.S. this is the definition of the print method:


public void print(String string) {
console.getOut().println(string);
}


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Tue 09 Nov, 2010, 01:02 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
You're right. It doesn't work for leverage ~ 0%. Until Dukascopy fix this, I guess we can use getOrders() and check for non-closed orders.

package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class GetLeverageTester implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
    private IAccount account;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
   }

   public void onAccount(IAccount account) throws JFException {
        this.account = account;
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (isTradingAllowed())
            this.console.getOut().println("Allowed");
        else
            this.console.getOut().println("Not Allowed");
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    private boolean isTradingAllowed() {
        return (this.account.getUseOfLeverage() == 0d);
    }
}


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Tue 09 Nov, 2010, 01:25 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
quantisan,

thanks for confirming me this.

Yes, I will use some workarounds to be sure I do not have any open position.


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Tue 09 Nov, 2010, 09:29 
User avatar

User rating: 8
Joined: Wed 21 Apr, 2010, 10:42
Posts: 1167
That's right. If the position is very very small, the use of leverage is rounded to 0. :x

In this case, we would suggest to have a look at another example how to make the same validation:

package jforex;

import java.util.List;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;

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

   public void onAccount(IAccount account) throws JFException {
    
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (hasOpenedOrder())
            this.console.getOut().println("Not Allowed");
        else
            this.console.getOut().println("Allowed");
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
   
    public boolean hasOpenedOrder() throws JFException {
       
       List<IOrder> orders = engine.getOrders();
       
       if (orders.size()>0) return true;
       else return false;
       
    }
   
   
}


 
 Post subject: Re: How to better control the number of positions (rule 6.2) Post rating: 0   New post Posted: Tue 09 Nov, 2010, 15:50 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
Thanks for your kind reply, "Support" ;)

Have a nice day.


 

Jump to:  

cron
  © 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