Stop Loss Not Showing Up After Order Entry

 

Hi Everyone, 

This is my first post, except for some code I posted to help someone determine if the market is open. So I guess I mean this is my first new topic post. I am trying to code a trailing stoploss and got hung up for a few hours on something that should be pretty simple for me (I have been a software dev for 20 years but this is my first attempt at MQL5). I found some very strange behavior and was wondering if anyone else has noticed it. At the moment I just have an ontick event calling my function if PositionTotal()>0 and in my function I am just trying to print the current stop loss to a log because I wanted to see what was going wrong. I noticed that the order is placed correctly with the correct stop loss but the log entry is showing 0.0. I used two different methods to try and get my current stop loss PositionGetDouble(POSITION_SL) and creating a CPositionInfo object calling the cpo.StopLoss() and they are both giving the same result which is zero. The odd thing is after a couple hours it started showing up. Here is a small snippet of the output from the journal log:

2021.09.30 16:36:51   Current SL: 0.00000000

2021.09.30 18:53:10   Current SL: 87.61800000

Obviously there are 100's more entries than this but I posted these because this is the last "0" entry before the "87.61" entry. As you can see it was hours later. I have a couple questions about this. The first is if there are any ideas why this is happening? The only thing I can think of is if your broker doesnt allow you to have a stop loss until after you have been in the trade a certain amount of time (which I know there are brokers that do this), but this never happens when I set a stop loss manually on my account. Also why did the ontick event not fire for 2 hours to at least keep printing that the value is zero? 


I ran it a couple more times and got different stop times which leads me to believe this isnt the event not firing but rather the tester app just stops printing these entries after a time (when it decides it is overloaded perhaps?)

2021.09.30 14:54:49   Current SL: 0.00000000

2021.09.30 14:54:50   Current SL: 0.00000000


2021.09.30 06:38:59   Current SL: 0.00000000

2021.09.30 18:06:56   Current SL: 87.61800000


The log entries arent that big of a deal as it is just for testing purposes but the fact that my sl isnt returning the actual order's sl for 6 hours is obviously annoying me. Here is the logs order entry confirmation with the entry time stamp:

2021.09.29 21:39:40   CTrade::OrderSend: instant buy 1.00 CADJPY at 87.868 sl: 87.618 [done at 87.868]

and the next tick log entry showing it as zero:          2021.09.29 21:39:40   Current SL: 0.00000000

and every tick after that until:                                    2021.09.30 18:06:56   Current SL: 87.61800000


Any ideas what is going on here? Thanks in advance

Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
  • www.mql5.com
CPositionInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Bodhi Mantra: my current stop loss PositionGetDouble(POSITION_SL)
No such thing as positions in MT4.
 
William Roeder #:
No such thing as positions in MT4.
Do you ever give an actually useful reply on this forum?
 

I figured I would add a little more to this with a screenshot in case it helps anyone else or helps anyone respond and help me out with this issue. As you can see in the screenshot there is a stop loss included with the CTrade object and the order worked as I test for an open order and print to the journal log if there is. Also in the journal the order confirmation shows the correct stop loss but when I try to retrieve it from the server it comes back as 0.0. I am wondering if maybe it is something buggy on the test server? I cant think of any way it can immediately lose the sl within a millisceond before it runs the very next line of code.

Again any help will be appreciated and I will post updates if I solve it

Files:
forum.jpg  354 kb
 
It appears to be a test server issue and only the first order you create cant have its values read. I dont know if a live server will also do it until I test the EA on a live account. Very strange, I added an "If stop loss=0" clause to my checks as a work around and it appears to be working. Image attached
Files:
forum2.jpg  278 kb
 
Bodhi Mantra #: Do you ever give an actually useful reply on this forum?
  1. All the time.
  2. Since you couldn't post an understandable question, we have no idea whether you mean MT4 (where you posted) or MT5 (what you posted), we can't answer you.
  3. Since you have twice insulted me, I will never help you; On my do not help list. Live in ignorance.
 
William Roeder #:
  1. All the time.
  2. Since you couldn't post an understandable question, we have no idea whether you mean MT4 (where you posted) or MT5 (what you posted), we can't answer you.
  3. Since you have twice insulted me, I will never help you; On my do not help list. Live in ignorance.

>implying I need help from a jackass like you

I lol'd, you appear to be a bitter retard to me so I doubt you can help anyone with anything mate. As you can see I solved the issue myself and unlike you left some valuable and time consuming to acquire knowledge for the next man