Order accepted with SL or 10.4 pips, but actual SL is zero (0) !
Order accepted with SL or 10.4 pips, but actual SL is zero (0) !
10 Jul 2019, 12:26
Hi everyone!
I am getting these very intermittent (eg, this happens about once per week with nearly 100 positions opened/closed/modified) issues with cTrader Automate when placing orders with it not setting stoplosses or placing stoplosses in the corrrect location.
See screen capture.
The relevant portion of code where it places the order, with the SL, and then gets an SL of 0, is shown below. This issue cannot be reproduce all the time. It happens very infrequently, but frequently enough to be noticed.
An order was placed to open a BUY position of GBPCAD with a SL of 10.4 pips.
You can see from the scale on cTrader that it's at 5 pips, and the points indicating the entry/exit locations are not 10.4 pips.
Also, see that when the order was confirmed, the p.StopLoss.GetValueOrDefault() in the Print statement returned 0 (zero) as well for the SL even though the order went through with a SL of 10.4 pips.
Here is the relevant portion of code. As you can see, there's nothing there between when the order is placed, and printing out the results that the SL isset to zero:
//You can see in the log output that the value for _stopLossInPips is 10.4 string commentString = _positionLabel + "; Long Buy; " + String.Format("{0:#,###}", MaxPositionSize) + "; SL in Pips:" + String.Format("{0:#,###.0000}", _stopLossInPips) + "; Approx Time:" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss"); Print("L1 Executing Long Market Order. {0}", commentString); TradeResult r = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, MaxPositionSize, _positionLabel, _stopLossInPips, null, commentString, false); if (r.IsSuccessful) { Position p = Positions.Find(_positionLabel, Symbol.Name); _isAlreadyInOrder = true; _currentStopLossLocation = RoundPipsOrPrice(Symbol, p.StopLoss.GetValueOrDefault(), false); _enteredPositionThisBar = true; //See the bug? It has the position ID, Label, and even the Entry Price. But no Stoploss! //rawSL output is p.StopLoss.GetValueOrDefault(). Print("L2 Long Order Successful. \"{0} {1}\" Entry Price: {2}, SL: {3} (rawSL {4}), {5}", p.Id, p.Label, p.EntryPrice, _currentStopLossLocation, p.StopLoss.GetValueOrDefault(), p.Comment); r = null; commentString = null; if (p.StopLoss.GetValueOrDefault() == 0) { Print("WARNING! Stop loss is zero! Why?! Closing position \"{0} {1}\".", p.Id, p.Label); ClosePosition(p); Stop(); return; } } else { Print("L3 WARNING! Execute Market Order NOT successful! {0}", commentString); Stop(); return; }
And here is the entry from the cTrader "Journal" tab. See how order is ACCEPTED withan SL of 10.4 pips, then 16 seconds later is accepts the order to close the position, because the code instructed it to when the SL is zero.
Any ideas anyone?
Thank you.
Replies
... Deleted by UFO ...
PanagiotisCharalampous
11 Jul 2019, 12:23
Hi FireMyst,
We have investigated the issue on our side and there is nothing suspicious. SL is placed as expected on the server for the specific order. Could you please send us troubleshooting information the next time this happens so that we can check what is happening on your side as well?
Best Regards,
Panagiotis
@PanagiotisCharalampous
firemyst
11 Jul 2019, 16:16
RE:
Panagiotis Charalampous said:
Hi FireMyst,
We have investigated the issue on our side and there is nothing suspicious. SL is placed as expected on the server for the specific order. Could you please send us troubleshooting information the next time this happens so that we can check what is happening on your side as well?
Best Regards,
Panagiotis
Sure. I have 23 instances of the bot running where this happens, so we'll see if it happens again next week.
What kind of "trouble shooting information" would help your team out? Was what I posted enough?
@firemyst
PanagiotisCharalampous
11 Jul 2019, 16:20
Hi FireMyst,
Sorry for not clarifying, I thought you were familiar with this. The next time this happens, press Ctrl+Alt+Shift+T in cTrader and a pop up will appear. Paste a link to this discussion into the test box and press submit. This action will send us all the necessary information we need. Leave a comment here so that we can go and check.
Best Regards,
Panagiotis
@PanagiotisCharalampous
firemyst
17 Aug 2019, 18:01
( Updated at: 21 Dec 2023, 09:21 )
RE:
Panagiotis Charalampous said:
Hi FireMyst,
Sorry for not clarifying, I thought you were familiar with this. The next time this happens, press Ctrl+Alt+Shift+T in cTrader and a pop up will appear. Paste a link to this discussion into the test box and press submit. This action will send us all the necessary information we need. Leave a comment here so that we can go and check.
Best Regards,
Panagiotis
@Panagiotis:
I'll do you one better. Here's the source code needed to reproduce this issue. Details on what to do are in the comments.
Screen capture below too from Visual Studio showing the the breakpoints, and that it goes into the code block that the position's stoploss is zero immediately after fulfilling the order with a 1.2 pip SL.
Set these values when backtesting:
Here's the code:
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Timers; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; using Microsoft.Win32; namespace cAlgo.Robots { //THIS CODE DEMONSTRATES THE BUG OF PLACING AN ORDER WITH A STOP LOSS of 1.2 pips, //AND UPON SUCCESS, Position.StopLoss.GetValueOrDefault() returns 0! //Run this in Back testing mode Aug 12 - 16 //Market: US30 //M1 time frame //Use the setting "Tick data from server (accurate)" //It will bomb out shortly after 1am server time. [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class BugBot : Robot { private string _positionLabel; private string _comment; private ParabolicSAR _psar; protected override void OnStart() { _psar = Indicators.ParabolicSAR(0.02, 0.2); _positionLabel = MarketSeries.TimeFrame + " " + Symbol.Name + " Bug Bot"; _comment = "this is a bug hunt test."; Print("OnStart: Starting \"{0}\"", _positionLabel); } protected override void OnTick() { double _psarLastValue = _psar.Result.Last(1); if (Server.Time.Hour >= 1) { double psarSL = RoundPipsOrPrice(Symbol, DifferenceInPips(Symbol, Symbol.Bid, _psarLastValue, false), true); if (psarSL == 1.2) { TradeResult r = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 1, _positionLabel, psarSL, psarSL * 5, "Does this have a stop loss after the order is executed?", false); if (r.IsSuccessful) { Position _p = Positions.Find(_positionLabel, Symbol.Name); Print("L2 Long Order Successful. \"{0} {1}\" Entry Price: {2}, SL: {3}, {4}", _p.Id, _p.Label, _p.EntryPrice, _p.StopLoss.GetValueOrDefault(), _p.Comment); if (_p.StopLoss.GetValueOrDefault() == 0) { Print("WARNING! Stop loss is zero! Why?! Closing position \"{0} {1}\".", _p.Id, _p.Label); ClosePosition(_p); Stop(); return; } } } } } protected override void OnStop() { } private double DifferenceInPips(Symbol s, double firstPrice, double secondPrice, bool returnAbs) { if (returnAbs) return Math.Abs((firstPrice - secondPrice) / s.PipSize); else return (firstPrice - secondPrice) / s.PipSize; } private double RoundPipsOrPrice(Symbol s, double pipsOrPrice, bool isPips) { string k = s.Name.ToUpper(); decimal result = 0; decimal factor = 0; if (!isPips) factor = (decimal)Math.Pow(10, s.Digits); else factor = (decimal)Math.Pow(10, 1); result = Math.Truncate(factor * (decimal)pipsOrPrice) / factor; return Convert.ToDouble(result); } } }
And here's a screen capture from Visual Studio in debug mode attached to cTrader:
What's happening to cause this?
Thank you.
@firemyst
srubtsov
26 Aug 2019, 12:38
RE: RE:
FireMyst said:
I'll do you one better. Here's the source code needed to reproduce this issue. Details on what to do are in the comments.
Screen capture below too from Visual Studio showing the the breakpoints, and that it goes into the code block that the position's stoploss is zero immediately after fulfilling the order with a 1.2 pip SL.
What's happening to cause this?
Thank you.
In that case in backtesting problem is stop loss pips is less than spread. Spread is 1.6, StopLossPips is 1.2. Empty StopLoss for that is ok.
That is not so clear, currently in that case `TradeResult.IsSuccessful = true; TradeResult.ErrorCode = null`. In future versions it will be more clear, `ErrorCode` won't be null.
@srubtsov
firemyst
27 Aug 2019, 08:59
RE: RE: RE:
srubtsov said:
FireMyst said:
I'll do you one better. Here's the source code needed to reproduce this issue. Details on what to do are in the comments.
Screen capture below too from Visual Studio showing the the breakpoints, and that it goes into the code block that the position's stoploss is zero immediately after fulfilling the order with a 1.2 pip SL.
What's happening to cause this?
Thank you.
In that case in backtesting problem is stop loss pips is less than spread. Spread is 1.6, StopLossPips is 1.2. Empty StopLoss for that is ok.
That is not so clear, currently in that case `TradeResult.IsSuccessful = true; TradeResult.ErrorCode = null`. In future versions it will be more clear, `ErrorCode` won't be null.
It doesn't matter what the spread is -- when a stop loss is set it should be set. Spread size has nothing to do with it. The spread is the difference between the bid/ask prices; a SL for a buy order is measured from below the BID; a SL for a sell order is measured from above the Ask.
Also, as with the original post, this same situation occurred when I had a stoploss of 10.4 pips.
There's a bug with cTrader and/or the server, and it needs to be fixed.
@firemyst
PanagiotisCharalampous
27 Aug 2019, 10:16
Hi FireMyst,
Here you are wrong
a SL for a buy order is measured from below the BID; a SL for a sell order is measured from above the Ask
SL is measured from the entry price, else it would not be accurate. So it is the opposite of what you have describled.
srubtsov is right. When the stop loss falls within the spread, cTrader ignores it. This has been discussed several times together with the reasons this happens.
Best regards,
Panagiotis
@PanagiotisCharalampous
firemyst
27 Aug 2019, 14:27
@srubtsov, @Panagiotis:
Yes,after rereading everything, you both are right. My apologies.
Fair question though -- is it good behaviour for cTrader to ignore a SL when it falls within a spread and not even give a warning or something? Otherwise, unless there's specific checks for a stoploss after placing an order with a stoploss, the bots would never know.
Or is the behaviour srubtsov mentioned going to be released in a future version?
Thank you.
@firemyst
PanagiotisCharalampous
27 Aug 2019, 14:41
Hi FireMyst,
Fair question though -- is it good behaviour for cTrader to ignore a SL when it falls within a spread and not even give a warning or something? Otherwise, unless there's specific checks for a stoploss after placing an order with a stoploss, the bots would never know.
If we close the position automatically then this means a certain loss for the trader for something that is obviously not intended. Setting the SL inside the spread is an obvious mistake, why would somebody do that? Better not open the position in the first place. Moreover, in certain cases it can lead to a loop of opening and closing positions in loss, risking blowing up an account. Therefore we ignore the SL and we let the trader choose how to fix this problem e.g. manually place a SL or detect this in the code and place it in a valid region. The improvements we will introduce in the future is that we should notify in a more explicit way the cBot that the SL has not been placed so that the cBot can take the appropriate action.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
10 Jul 2019, 16:00
Hi FireMyst,
Can you tell us the broker and the account number so that we can investigate further?
Best Regards,
Panagiotis
@PanagiotisCharalampous