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.

how to get leverage of such kind orders
 Post subject: how to get leverage of such kind orders Post rating: 0   New post Posted: Thu 17 Feb, 2011, 03:27 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
suppose ,i have 20 filled long orders,and 12 of them with their SL 3 pips above open price(reset under some condition to avoid profit turn into loss).

and the other 8 orders with their original SL 10 pips below their open price,

how to get the leverage of the 8 orders


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Thu 17 Feb, 2011, 15:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
Could you please elaborate what does leverage of the 8 orders mean?


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Thu 17 Feb, 2011, 15:32 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
Support wrote:
Hi,
Could you please elaborate what does leverage of the 8 orders mean?

ok let me describe my strategy in detail:

ie. i long 20 orders of eu ,and 12 of them have open price of 1.35900,with SL at 1.35800 ;the other 8 orders opens at 136000 and original SL is 1.35900 ,now eu has got to 1.36050 and my strategy change the first 12 orders'SL to 1.35930 ,so i define these 12 orders to be "safe positions ", but the second group orders still have their SL @1.35900,so this group will be defined to be "unsafe positions",i want to calculate the total amount of unsafe positions and the leverage they take.

thanks for your patience.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Thu 17 Feb, 2011, 15:38 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
if (instrument.equals(selectedInstrument))
     {
        for (IOrder order : engine.getOrders(instrument))
      {currentSLL= order.getStopLossPrice();
                  open=order.getOpenPrice();
        if ((order.getState() == IOrder.State.FILLED)&&((currentSLL-open)<stopvalue))
      
       {
        UsfAmount=UsfAmount+order.getAmount();
        UsfLev=UsfAmount/account.getEquity();
      
       }
      }
     }


this is what i wrote for long positions ,UsfAmount is supposed to be the total of amount of unsafe orders ,but in fact UsfAmount is just total amount of both safe and unsafe positions


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Thu 17 Feb, 2011, 22:06 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
Hi liyinan,

I guess you problem lies here:
(currentSLL-open)<stopvalue))

In your code snippet you are not showing how 'stopvalue' is set, but if it is a price (rather than a price-distance), you are comparing a price difference with a price, which will not work.

How about replacing the above condition by:
(currentSLL-open > 0.0)

This will return true (for LONGs!), if the current SL is above the entry price, which would define a position as 'safe'.

Best, RR.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 11:53 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
hi roadrunner , stopvalue is already defined and initiated :
stopvalue =instrument. getpipvalue ()*2.5,
in my source, i add a console.getout.println(stopvalue); to print stopvalue in message box ,and is perfectly right :2.5^10-4, so i think it's not the stopvalue that cause the problem .


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 12:03 

User rating: 0
Joined: Tue 27 Jul, 2010, 20:57
Posts: 49
There is a flaw in your logic..

(currentSLL-open) will always evaluate to <stopvalue.

As I understand it, you just need to check if currentSSL is > open.

Or go with the solution proposed by RoadRunner.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 12:37 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
astro orbitor wrote:
There is a flaw in your logic..

(currentSLL-open) will always evaluate to <stopvalue.

As I understand it, you just need to check if currentSSL is > open.

Or go with the solution proposed by RoadRunner.

thanks astro , but in fact the algorithm i use in this code is to calculate unsafe long orders , and if sl>(open+stopvalue ),safe orders are calculated , so i should use sl <(open +stopvalue ) or equal algorithm to add up unsafe orders .
is my logic and algorithm right ?


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 12:52 

User rating: 0
Joined: Tue 27 Jul, 2010, 20:57
Posts: 49
Oh, I see. Well, I was wrong, your logic is/was not flawn. Sorry for that.

Yes, sl<(open+stopvalue) "should" do the trick.

Now, I suppose that the problem lies in the fact that addition or subtraction of floating point numbers in some cases create artefacts, thus values have to be rounded, as some numbers cannot be exactly represented.. E.g. although a term like 1.3600 + 0.00025 should evaluate to 1.36025, in practice it may evaluate to something like 1.36024999999999... So it would need to be rounded to the 5th decimal. To actually check/see this you need to print out the term currentSSL-open.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 13:25 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
astro orbitor wrote:
Oh, I see. Well, I was wrong, your logic is/was not flawn. Sorry for that.

Yes, sl<(open+stopvalue) "should" do the trick.

Now, I suppose that the problem lies in the fact that addition or subtraction of floating point numbers in some cases create artefacts, thus values have to be rounded and so on. E.g. a term like 1.3600 + 0.00025 would have to evaluate to 1.36025, but in practice it may evaluate to something like 1.36024999999999... So it would need to be rounded to the 5th decimal. To actually check/see this you need to print out the term currentSSL-open.

thanks for your advice,astro
can you improve the comparation,open ,sl,stopvalue are all double values.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 13:44 

User rating: 0
Joined: Tue 27 Jul, 2010, 20:57
Posts: 49
Well, first, if your SL is calcualted elsewhere and only changed if price > (open + stop) then your if-clause would simply be if (currentSL < open).

In general, when I want to round a fp number to say 5 decimals, I do Math.round(<number> * 100000.0) / 100000.0; to my knowledge this is only necessary in the case that <number> is a result of an addition or subtraction. For the purpose you also can use the BigDecimal class, although it might be slower in calculation.
In that case your original if-clause would look like if ((order.getState() == IOrder.State.FILLED)&&(Math.round((currentSLL-open)*100000.0)/100000.0<stopvalue))


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 15:21 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
astro orbitor wrote:
Well, first, if your SL is calcualted elsewhere and only changed if price > (open + stop) then your if-clause would simply be if (currentSL < open).

In general, when I want to round a fp number to say 5 decimals, I do Math.round(<number> * 100000.0) / 100000.0; to my knowledge this is only necessary in the case that <number> is a result of an addition or subtraction. For the purpose you also can use the BigDecimal class, although it might be slower in calculation.
In that case your original if-clause would look like if ((order.getState() == IOrder.State.FILLED)&&(Math.round((currentSLL-open)*100000.0)/100000.0<stopvalue))


i am afraid not astro,
these messages are copied from message box,and 6 short orders are filled
and 4 of them have reset SLs(and now they are safe orders) and 2 new orders still have original SLs.

13:48:44 stopvalue :0.025,open-currentSLS :-9.999999999998899E-4
13:48:44 stopvalue :0.025,open-currentSLS :-0.0011699999999998933
13:48:44 stopvalue :0.025,open-currentSLS :-0.0020799999999998597
13:48:44 stopvalue :0.025,open-currentSLS :-0.0020799999999998597
13:48:44 stopvalue :0.025,open-currentSLS :-0.0021099999999998342
13:48:44 stopvalue :0.025,open-currentSLS :-0.0021099999999998342

the weired thing is even though 4 orders are now safe positions ,but open-currentSLS are negative numbers (in fact should be positive ones ,because safe short order must have its SL below its open),but as i can see from chart SL lines are below open price .......
currentSLS means current SL for short orders and stopvalue in message box =0.025,because i multiple stopvalue with 100 in println function
its weired ,really weired


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 17:56 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
Hi liyinan,

if you are calling your function directly after submitting the stoploss-order, the stoploss you retrieve by order.getStopLossPrice() could still hold the old price. It takes a bit until the value is updated.

Try to include order.waitForUpdate(...) after submitting the new stoploss prices and check, if prices have been changed.

Best, RR.


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Fri 18 Feb, 2011, 18:35 

User rating: 0
Joined: Tue 27 Jul, 2010, 20:57
Posts: 49
Oh, yes, the short positions!

And good point from RR.

So, after your SL is set and confirmed, you would simply check:

if (order.getState() == IOrder.State.FILLED)
   if (order.isLong() && (currentSL < open)) {..do your calculation here..}
   else if (currentSL > open) {..do your calculation here..}


 
 Post subject: Re: how to get leverage of such kind orders Post rating: 0   New post Posted: Sat 19 Feb, 2011, 13:29 

User rating: 0
Joined: Thu 24 Jun, 2010, 04:04
Posts: 27
thanks for patience and kindness ,RR and astro
i looked into message box carefully and found something interesting ,still the same 6 orders and there two

things we might have to notice.
1. ever since SL are changed to new value,(open-SL) is calculated right for first time that is 2.5 pips ,but only first time ;in next loops (open-SL) equals to older value ,that is -10 pips

2.there are artefacts as astro pointed out .

you can see that from the file i upload,messages copied from message box

and i make some comments after message lines ,starting with //


Attachments:
message.txt [190.53 KiB]
Downloaded 353 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: how to get leverage of such kind orders Post rating: 0   New post Posted: Sat 19 Feb, 2011, 17:16 

User rating: 0
Joined: Tue 27 Jul, 2010, 20:57
Posts: 49
I do not know your code, so I have coded a solution to your problem according to what I understand from your description.
Maybe you can use the code or get some clues out of it.
I assume that the entry point is onTick().
In onTick() a new task is created to not clog the main thread.
The task first collects all orders.
Then it checks if an order is long; if so it checks if the order's SL is still below its open + stopvalue, thus the order is potentially unsafe;
if so it checks if the current bid just has moved above open + stopvalue, if so, SL must be adjusted (put your code there), otherwise the order is unsafe and can be counted as such (put your code there).
The else-clause does the same for shorts.
I think the code should work :)

public void onTick(Instrument instrument, ITick tick) throws JFException {
   //context has to be an instance of IContext
   context.executeTask(new DoAll()); // new task to avoid clogging the main thread
}

private class DoAll implements Callable<Object>{
   public Object call() throws Exception {
      //engine has to be an instance of IEngine
      for(IOrder order : engine.getOrders())
         if (order.getState() == IOrder.State.FILLED)
            if(order.isLong())
               if (order.getStopLossPrice() < order.getOpenPrice())
                  //history has to be an instance of IHistory
                  if(history.getLastTick(order.getInstrument()).getBid() > order.getOpenPrice() + stopValue){
                     //set new SL here
                  }else{
                     //accumulate to unsafe
                  }
               }
            else if (order.getStopLossPrice() > order.getOpenPrice())
                  //history has to be an instance of IHistory
                  if(history.getLastTick(order.getInstrument()).getAsk() < order.getOpenPrice() - stopValue){
                     //set new SL here
                  }else{
                     //accumulate to unsafe
                  }
      return null;
   }
}


 

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