HELP!! Trailing Stop issues because of lag

 
if(OrderMagicNumber()==00000 && OrderSymbol()==Symbol() && OrderProfit()>(-OrderCommission()+-OrderSwap()))
OrderModify(OrderTicket(),0,Ask,0,0);

The above code sometimes nails it, sometimes doesn't. Maybe works right like 90 per cent of times or so. Sometimes misses the right moment, yet then later I'm lucky and the order comes back to profit and it executes the trailing stop correctly. This really sets me off and can potentially lead to great loss. I want it to automatically set the trailing stop at the precise exact moment when there is actual real profit; as in when after you subtract the commissions and swaps the order profit is still greater (by anything really ill take a cent even for my scalping strategy) and updating your account balance positively correctly.

Lag seems to prevent it from doing it right all the time. Convinced I must be wrong and there might be a much better way to do this so that I don't end up placing the trailing profit at below the actual real profit point.


Have tried various EAs and none seems to do this the way I wanted, as I have explained. This is the closest Ive gotten to it and it still misses sometimes.


1. Is it just the lag at the super tight stop level I'm going for? 

2. How do I improve this code? How do I get the Trailing Stop to correctly trigger all the time at the exact point where there is a real profit made (after the commissions and swap charges)?

 
Heres a screen of the apparent lag caused issue. Thanks in advanced.
Files:
trailinglag.png  66 kb
 
OrderModify(OrderTicket(),0,Ask,0,0);
  1. Bogus call. You can not change the price of your open order.
  2. You would know this had you bothered to Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You can not move your stops closer than stop level (sometimes as much as 3 pips.) What is your broker's value? Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  4. For sell orders, stops are relative to the Ask. If you could move it there, the order would close immediately. For Buy orders, relative to the Bid. Bogus call.
  5. Lag is irrelevant.
 
whroeder1:
  1. Bogus call. You can not change the price of your open order.
  2. You would know this had you bothered to Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You can not move your stops closer than stop level (sometimes as much as 3 pips.) What is your broker's value? Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  4. For sell orders, stops are relative to the Ask. If you could move it there, the order would close immediately. For Buy orders, relative to the Bid. Bogus call.
  5. Lag is irrelevant.

Thanks a ton, I really appreciate your time into helping me fix this issue.

Ill enumerate my answers to your points for clarification

In answers to your points :

1. How exactly do I fix this code so as to achieve my desired result? What do I put there to reach my goal of having the trailing stop trigger at the exact point where there is real profit after subtracting commissions and swap?

The previous posted code and the following new attempt with what I could interpret from your advice return error 1 no matter what I try.

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderProfit()>(-OrderCommission()+-OrderSwap()))
OrderModify(OrderTicket(),OrderOpenPrice(),Ask,0,0);

Tried changing the highlighted 0 you suggested to OrderOpenPrice(); so as to *not* have MQL  interpret that I am "trying to change" any open price, yet this still yields the same error 1 code. I thought the value which you have highlighted, the 0 in the order modify in the price field, would have MQL interpret that I'm not trying to change anything.  Error codes from https://book.mql4.com/appendix/errors just say that

ERR_NO_RESULT 1 No error returned, but the result is unknown.

Where do I go from here?

My logical thought about it was that if its the same symbol, the same order type, and the order profit is greater than order commissions+swaps, then proceed to modify the selected Order, not change the price, change the Stop Loss to current Ask (where theres already real profit after broker subtractions), not change the take profit, and not change any expiration or arrows. How is this wrong?

2. How would I have to change this code so that it does not yield error 1? Im just using

Alert(OrderTicket(), " ", GetLastError() );

in order to find out the "return codes" via checking the last error. Is this correct? The links you provided just tell you to try "! < code>"  to check the return values for bools, and GetLastError() for ints. Im a little confused, do you mean the error or the return value of functions? How exactly do I check the exact return values themselves?

3. I'm lucky my broker does not have an issue with stops anywhere.

4.That was in the know from beforehand

5. Really glad lag is not relevant yet it sounds too good to be true; are you 100 per cent positively sure that on working cable (just got upgraded and its usually steady around the 60mbps, will prolly get VPS after Im done code) lag will never ever be an issue for this trailing stop after real profit situation though? Ever? Not even a single cent?

Error Codes - Appendixes - MQL4 Tutorial
Error Codes - Appendixes - MQL4 Tutorial
  • book.mql4.com
Error Codes - Appendixes - MQL4 Tutorial
 
whroeder1:
  1. Bogus call. You can not change the price of your open order.
  2. You would know this had you bothered to Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You can not move your stops closer than stop level (sometimes as much as 3 pips.) What is your broker's value? Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  4. For sell orders, stops are relative to the Ask. If you could move it there, the order would close immediately. For Buy orders, relative to the Bid. Bogus call.
  5. Lag is irrelevant.
Bah, had some wrong parameter count in the code. Added clrNONE, no more error code.  What do I do with the price field and others to leave them untouched though? Progress.
OrderModify(OrderTicket(),OrderOpenPrice(),Ask,0,0,clrNONE);

?

So we can use NULL, clrNONE, and 0 to denote no changes in certain specific cases only? How do I know when to use 0 and when to use NULL? Ive even seen " " used sometimes to denote empty field/no change.NULL doesnt work to denote no change in every case? Id like my code to be as minimalistic/less byte consuming as possible so I thought using NULL everywhere would remove lag. So where can I use it and when not? Does it really save bytes at all anyway?

 

https://docs.mql4.com/basis/types/void

Void Type and NULL Constant

Syntactically the void type is a fundamental type along with types of char, uchar, bool, short, ushort, int, uint, color, long, ulong, datetime, float, double and string. This type is used either to indicate that the function does not return any value, or as a function parameter it denotes the absence of parameters.


/////////////////

NULL only for void ones? Only for strings ?

https://docs.mql4.com/trading/ordersend

States it can be used in the int function OrderSend().

Why can't I put NULL everywhere to be as minimalistic as possible then?

Void Type and NULL Constant - Data Types - Language Basics - MQL4 Reference
Void Type and NULL Constant - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Void Type and NULL Constant - Data Types - Language Basics - MQL4 Reference
 

I just got the same issue again with my presumed allegedly fixed, updated code:

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderProfit()>(-OrderCommission()+-OrderSwap()))
OrderModify(OrderTicket(),OrderOpenPrice(),Ask,0,0,clrNONE);      

Still the issue of the below real profit, heres a screen. Seems more keen code, yet same problem happened. Any ideas how to make it snappier so it never misses a cent and always gets profit above the subtractions?

 
nadiawicket: Why can't I put NULL everywhere to be as minimalistic as possible then?

You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.

Zero is the same as PERIOD_CURRENT which means _Period.

No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[]

 
whroeder1:

You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.

Zero is the same as PERIOD_CURRENT which means _Period.

No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[]

Awesome thanks a ton!
Reason: