|
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.
Attempt to change stop loss of the order |
tcsabina
|
Post subject: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Thu 22 Nov, 2012, 22:41
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
I am trying to implement a kind of trailing stop loss for my orders, based on a wiki example of breakEven. I am using the onTick() function to check all (filled) orders, and if the profitLoss has reached a positive amount (in pips), I will `move` the SL of the order `closer` to the Take Profit price. This is working fine, however sometimes I got flooded with the message: "Attempt to change stop loss of the order". The wiki example ( https://www.dukascopy.com/wiki/#Set_Stop ... Break_even) is using a boolean variable (breakEvenReached), but for me it cannot be used, as I (maybe) want to modify the SL for every filled order. The only thing I can think of is that whenever a SL is modified, we call order.waitForUpdate(2000), and in this 2 second period, new ticks come in, so the onTick() is being called, and we try to modify the SL for an order which already has been modified, but sitting int the order.waitForUpdate(2000) cycle. But I am just guessing. Please help me understand why I still get the messages, and how to avoid them. Example message flood: 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36 2012.11.22 20:28:36,Attempt to change stop loss of the order [EURUSD1] with the same price. Old price [1.3638], new price [1.3638]2012.11.22 20:28:36
Here is the code: public void onTick(Instrument instrument, ITick tick) throws JFException { double originalSL;
//we can't update SL or TP more frequently than once per second if( (instrument != this.selectedInstrument) || ((tick.getTime() - lastTickTime) < 1000) ) return; lastTickTime = tick.getTime(); for (IOrder order : engine.getOrders()) { if (order.getState() != IOrder.State.FILLED) return;
if ( (order.getProfitLossInPips() > breakEvenPips) && (breakEvenReached == false) ) { originalSL = order.getStopLossPrice(); if (engine.getOrder(order.getLabel()).isLong()) { if (originalSL == (order.getOpenPrice() + breakEvenPips*tickSize)) // I tried this trick here to avoid the messages, but this is not perfect as I still get the messages return; order.setStopLossPrice(order.getOpenPrice() + breakEvenPips*tickSize); } else { if (originalSL == (order.getOpenPrice() - breakEvenPips*tickSize)) // check if we already modified the SL return; order.setStopLossPrice(order.getOpenPrice() - breakEvenPips*tickSize); } order.waitForUpdate(2000); // breakEvenReached = true; // I don`t need this flag, as I want to do it for multiple orders. } } }
|
|
|
|
 |
bradhallett
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 2
|
Posted: Fri 23 Nov, 2012, 02:59
|
|
User rating: 3
Joined: Thu 17 Nov, 2011, 04:06 Posts: 39 Location: Canada, Ottawa
|
I think the issue is that you are not rounding your prices before sending them to the server. Try the following code for price rounding: private double roundPrice(Instrument instrument, double price) { price = Math.round(price * Math.pow(10, instrument.getPipScale() + 1)); price = price / Math.pow(10, instrument.getPipScale() + 1); return price; } The rounding issue is likely at order entry.
|
|
|
|
 |
tcsabina
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Mon 26 Nov, 2012, 12:43
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Thanks for the tip. I implemented it, however the issue still exists. Here is the code, just in case I missed something: public void onTick(Instrument instrument, ITick tick) throws JFException { double originalSL;
//we can't update SL or TP more frequently than once per second if( (instrument != this.selectedInstrument) || ((tick.getTime() - lastTickTime) < 1000) ) return; lastTickTime = tick.getTime(); for (IOrder order : engine.getOrders()) { if (order.getState() != IOrder.State.FILLED) return;
if ( (order.getProfitLossInPips() > breakEvenPips) && (breakEvenReached == false) ) { originalSL = roundPrice(order.getStopLossPrice()); if (engine.getOrder(order.getLabel()).isLong()) { if (originalSL == (roundPrice(order.getOpenPrice()) + breakEvenPips*tickSize)) // check if we already modified the SL return; order.setStopLossPrice(roundPrice(order.getOpenPrice()) + breakEvenPips*tickSize); } else { if (originalSL == (roundPrice(order.getOpenPrice()) - breakEvenPips*tickSize)) // check if we already modified the SL return; order.setStopLossPrice(roundPrice(order.getOpenPrice() - breakEvenPips*tickSize)); } order.waitForUpdate(2000); // breakEvenReached = true; } } }
@Support: Any idea what is going on?
|
|
|
|
 |
API Support
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Mon 26 Nov, 2012, 14:53
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Please provide full example strategy. Please describe what you expect the strategy to do. And describe what it does instead.
|
|
|
|
 |
tcsabina
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Tue 27 Nov, 2012, 09:39
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
API Support wrote: Please provide full example strategy. The strategy where I have this issue is not public. I could create a little test strategy including to mentioned onTick() function, but I think that would be useless, as the problem is pretty obvious. Or at least the error message (which is coming from the platform) is. API Support wrote: Please describe what you expect the strategy to do. And describe what it does instead. I beg your pardon, but I think I fully described in the first post what is my problem. It is not about what does the strategy do (or does not do), it is about a message I got back from the platform. Thanks in advance.
|
|
|
|
 |
API Support
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Tue 27 Nov, 2012, 12:48
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
tcsabina wrote: I beg your pardon, but I think I fully described in the first post what is my problem. It is not about what does the strategy do (or does not do), it is about a message I got back from the platform. The message is rather self-explanatory - you have to check the open price before changing it, in order to avoid receiving the warning message. If you wish to set the break even for multiple orders, consider the following approach: https://www.dukascopy.com/wiki/#Set_Stop_Loss_price/Break_even_for_multiple_orders
|
|
|
|
 |
tcsabina
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Tue 27 Nov, 2012, 13:16
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Dear Support,
Thank you for your reply, and the updated Wiki page! I also did my homework, and created a strategy that can be used replicated the issue. You can find it attached.
However it is useless now I suppose, as I see now how to tackle the problem (to update multiple order in the onTick()) function.
I am still not sure why my code is not good. What I can imagine to be the problem is that during a for loop within the onTick() function another tick `arrives`, starting a new for loop, although the previous haven`t been finished yet. I do compare the SL price of an order and the target SL price, but even with this extra check, there are attempts to update the same order`s SL more then once.
To fully understand the problem, could you explain what will happen if a loop is not finished within onTick() and a new tick arrives, firing again the onTick() function?
Thanks and regards.
Attachments: |
File comment: test strategy with problem
Strategy2.java [7.27 KiB]
Downloaded 963 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.
|
|
|
|
|
 |
API Support
|
 |
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Tue 27 Nov, 2012, 13:29
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
tcsabina wrote: I am still not sure why my code is not good. Presumably this is due to double comparison (line 77), which does not work as you expect - the result of the expression will hardly ever be true. For double value comparison one uses the Double.compare method. tcsabina wrote: What I can imagine to be the problem is that during a for loop within the onTick() function another tick `arrives`, starting a new for loop, although the previous haven`t been finished yet. I do compare the SL price of an order and the target SL price, but even with this extra check, there are attempts to update the same order`s SL more then once. The strategy works in its own, single thread, meaning that there is no parallel execution of onTick methods. The ticks that arrive during the execution of onTick of another method, get enqueued and executed afterwards, see more here: https://www.dukascopy.com/wiki/#onTick_execution_policy
|
|
|
|
 |
tcsabina
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Wed 28 Nov, 2012, 22:07
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Dear Support, Thank you again for your replies, and for guiding me in the proper direction. And thanks for the explanation about how onTick() works. I was able to manage this, and fix the problem in my code. Here is a snipplet, in case someone else has similar issues. Part from onTick(): . . . if (order.getProfitLossInPips() > breakEvenPips) { originalSL = roundDecimals(order.getStopLossPrice()); if (engine.getOrder(order.getLabel()).isLong()) { increment = roundDecimals(order.getOpenPrice() + breakEvenPips); print("Trailing SL check for LONG %s: originalSL=%f; newSL=%f; compared=%d", order.getLabel(), originalSL, increment, Double.compare(originalSL, increment)); if (Double.compare(originalSL, increment) == 0) // check if we already modified the SL { print("Trailing SL already activated for LONG %s (SL=%f)", order.getLabel(), order.getStopLossPrice()); continue; } else order.setStopLossPrice(order.getOpenPrice() + breakEvenPips); } else { increment = roundDecimals(order.getOpenPrice() - breakEvenPips); print("Trailing SL check for SHORT %s: originalSL=%f; newSL=%f; compared=%d", order.getLabel(), originalSL, increment, Double.compare(originalSL, increment)); if (Double.compare(originalSL, increment) == 0) // check if we already modified the SL { print("Trailing SL already activated for SHORT %s (SL=%f)", order.getLabel(), order.getStopLossPrice()); continue; } else order.setStopLossPrice(order.getOpenPrice() - breakEvenPips); } order.waitForUpdate(2000); print("Trailing SL activated for %s. Open price=%.5f. Old SL=%.5f. New SL=%.5f", order.getLabel(), order.getOpenPrice(), originalSL, order.getStopLossPrice()); }
And the round function (I had to use this, otherwise the compare gave fail results): private double roundDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.#####"); return Double.valueOf(twoDForm.format(d)); }
|
|
|
|
 |
API Support
|
Post subject: Re: Attempt to change stop loss of the order |
Post rating: 0
|
Posted: Thu 29 Nov, 2012, 08:17
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|