Closing all open trades automatically results in losing price value even when the difference is positive profits - page 2

 
Keith Watford:

I am listening but you are making no sense at all.

I am trying to help you write better code.

Your code

My suggestion (Assuming that you don't use pending orders)

Can you not see how much simpler it is?

Programmer to programmer, I see where you were getting at, and I can understand why you are getting confused and doesn't seem to make sense of it; if you didn't write it it's natural to get lazy and confused comprehending other's coding practice, but a shorter code is not on my agenda right now. I see you're using stdlib to make things simpler, I appreciate that and totally agree it helps in writing better code and thanks for your suggestions, but intend to do without using that library from the very beginning and again I've already achieved getting the sum/difference in my own way (simpler/elegant coding or otherwise), and I reckon your code would give more or less the same result (which I'll explain later), seeing the logic is more or less the same. That's part of the step 3 of my code which I mentioned have no problems with it at all. My real aim is to find out the last part why it's giving a result that seemingly contradicts the algorithm of my last module. (The explanation -->)  Seng Joo Thio remind me of the swap which I believe is the better angle to this one, as I have not put it into the program's equation yet, which explains the significant reduction to my account balance, as I don't know how to right now. Maybe you could provide suggesting a code for that... ;-)

 
Seng Joo Thio:

For every open order, when you sum up OrderProfit()+OrderSwap()+OrderCommission(), you will get very close to the resulting profit if you close the order there and then (negligible variation due to tick-wise fluctuation, if you have to call RefreshRates() in between).

That's great! Being new to MQL4 I missed those two variables. need to eat a lot more of MQL4 code :-D, I've just been doing this for a few months . Thanks, I'm going to do it as suggested now.


Seng Joo Thio and Keith Watford:


Thanks a lot for your support! #DoubleThumbsUpToYouGuys

 
tompips2018:

Programmer to programmer, I see where you were getting at, and I can understand why you are getting confused and doesn't seem to make sense of it; if you didn't write it it's natural to get lazy and confused comprehending other's coding practice, but a shorter code is not on my agenda right now............................. and I reckon your code would give more or less the same result (which I'll explain later), seeing the logic is more or less the same.

Wow!

I am not confused at all!

I was trying to gently lead you towards writing better code without actually pointing out how badly your CloseAll() function is coded.

My code woudn't give more or less the same result, it will work correctly every time and give better results than yours.

I am actually amazed that your code works at all. Maybe you are running it in the strategy tester with zero spread.

 
Keith Watford:

Wow!

I am not confused at all!

I was trying to gently lead you towards writing better code without actually pointing out how badly your CloseAll() function is coded.

My code woudn't give more or less the same result, it will work correctly every time and give better results than yours.

I am actually amazed that your code works at all. Maybe you are running it in the strategy tester with zero spread.


"I am not confused at all!

I was trying to gently lead you towards writing better code without actually pointing out how badly your CloseAll() function is coded."

Bad or not, it works. Ya? ;-)

Again, as mentioned, I have no problem with it at all, since I have solved that already, whether it's a good or bad code. I'm more on the end result than the means. When you are working on a project, your bosses don't care much about your "elegant" code. If it gives the desired results notwithstanding, that's what matters to them. I appreciate your "gently" guiding mo to write better code, but my aim again (how many times should I say that, again? :-D),  is to find the root cause, not how  you 'badly' write a code that is working fine nonetheless. Why reinvent it if it's already doing the job it requires? ;-)


But make no mistake, I appreciate your inputs and although being a T-1000, I'm still a learning computer and I collate all the learning I gather over the years. Thanks again ;-).

"My code woudn't give more or less the same result,"


It will work correctly for sure - even if you give it BAD data. But that, would definitely provide you a BAD result ;-) :-D.


" it will work correctly every time and give better results than yours."

But of course! You are a better programmer than I am, surely you have years of MQL4 expertise on your belt. I've just joined the bandwagon less than a year ago. One thing's for sure, 1+1=2 is mathematically the same across the board in all dimensions, no matter how "elegant" you wrote it down on your code. I'm sure my 'crude' version gets it done just as well, ya? ;-)


"I am actually amazed that your code works at all. Maybe you are running it in the strategy tester with zero spread."


With peanut butter and strawberry jam cheese spread, actually. :-D:-D:-D:-D:-D


PS: I'm not sure if  Seng Joo Thio  is a far lesser programmer than you or not, but one thing's for sure: he got the commonsensical gist to figure out what I was looking for in a few words or less.  THAT, is the kind of guy I'll hire to work for me. 3:-)

 

Seng Joo Thio, That is the SILVER BULLET! You are right! 😃 😃 😃


Thank you very much for pointing that out! It's a very big help. (◔◡◔)

Files:
 

This thread can be closed. If I need your help again peeps, whether you give short straight-to-the-point response or giving the right answers to the wrong questions, for sure, 

:

:

;-)

V

V
Files:
 
tompips2018:

PS: I'm not sure if  Seng Joo Thio  is a far lesser programmer than you or not, but one thing's for sure: he got the commonsensical gist to figure out what I was looking for in a few words or less.  THAT, is the kind of guy I'll hire to work for me. 3:-)

Judging by Seng's contributions to the forum, he is certainly not a lesser programmer than I am.

I told you in reply #2

First of all, your function is not the reason for the trades closing at a loss. The reason is whatever logic you use to call the function.

As it happens you were not calculating the total profit correctly.

I have shown you a much better and correct version of the CloseAll() function but you refuse to accept it. Instead you had to argue that there is nothing wrong with your function and that it does the job.

The fact is that your function will not work correctly in all conditions whereas mine will. You will find that out one day.

 
Keith Watford:

Judging by Seng's contributions to the forum, he is certainly not a lesser programmer than I am.

I told you in reply #2

As it happens you were not calculating the total profit correctly.

I have shown you a much better and correct version of the CloseAll() function but you refuse to accept it. Instead you had to argue that there is nothing wrong with your function and that it does the job.

The fact is that your function will not work correctly in all conditions whereas mine will. You will find that out one day.

"Judging by Seng's contributions to the forum, he is certainly not a lesser programmer than I am."

That's good to know. :-)

"I told you in reply #2

As it happens you were not calculating the total profit correctly."


You said, in exactly the words you said, "First of all, your function is not the reason for the trades closing at a loss. The reason is whatever logic you use to call the function."

The logic IS correct; as I said, I have no problem with it at all, what was missing are the additional variables involved; that's not part of the logic, you didn't exactly say I was "not calculating the total profit correctly.". There's a BIG difference to that :-D. I am thinking along the lines of what is missing as I have said, viz, - " I am not convinced myself :D" and was "looking at other angles"  like the slippage and the gap price between the Ask and the Bid - because I know there is something missing, but cannot figure out at the time what it is. Your suggestions are along the lines of my code being badly written and getting to re-write a "better code" like, " close it immediately just close it at OrderClosePrice()." for something that is *already programmatically working properly* at least on the part of Computing the Trades. If I apply your "better" code, it would STILL give me the same results, because as I have said (again): "would give more or less the same result... seeing the logic is more or less the same.", which happened to be right because it was NOT the logic it was THE swap and commission values.

Now DON'T get me wrong! I appreciate your guiding to write a better code which IT IS, I admit superior to mine as per your suggestion, but as I have said (agaaaaaiinnnn), "
have no problems with it (i.e., my code) at all.". It may be crude and makes no sense to you at all, but that part of the code WORKS! Besides it would compel me to re-write portions of it  which I'd rather *not* touch and re-combine it with the problematic CloseTrades function. I already have my code modularised into small functions that makes it easier for me to debug. Again, AGAIN - my real aim is to find out the last part why it's giving a result that seemingly contradicts the algorithm of my last module. <<--- now that's copy/paste for ya from the abovementioned my dear. :-D.   Seng Joo Thio just nailed it already and thanked him for that. ;-)

"
I have shown you a much better and correct version of the CloseAll() function but you refuse to accept it. "

I refused to accept it, for the ABOVE reasons. Don't be offended. I don't mean to. In fact, I will be planning to implement your code snippet for other applications in the future. It's just that it's NOT the right target to aim at in my case. The program's logic *is* working - Seng just happened to figure out the missing links for me that I *REALLY* need. 

"I have shown you a much better and correct version of the CloseAll() function but you refuse to accept it. Instead you had to argue that there is nothing wrong with your function and that it does the job."

Answered that already above. No need to sound like a broken record. ;-) :-D.

" The fact is that your function will not work correctly in all conditions whereas mine will. You will find that out one day."

I don't care. If it doesn't, re-work it. I'm a modular programmer, and my approach is creating small, ready-to-use, of-the-shelf-able functions that would be easily managed and handled, and that part of the function is the only thing I need to work on. Besides, it would be an exciting thing in life to have something to work on again if it needed be and I don't mind at all (especially if you're paid well for the work you love doing) but I don't expect a perfect one-size-fits-all solution (hence my modular approach) as program updates and software innovations come and go, you will be forced to re-arrange and re-learn everything you knew prior, even with your perfect-today-and-hopefully-tomorrow code, you will find that out one day too :-D :-D. Being in the IT industry for decades, I know for a fact that the only constant, is CHANGE. Just like everything in life. As I said (how many agains have I said already? :-D)  the proper code is not my target, it's another thing which happened to be the missing variables. Even if I applied it without that Swap and Commissions, however correctly incorporated and "much more superior" your code will be, it will STILL FAIL without the right variables. Remember what I said above - "It will work correctly for sure - even if you give it BAD data. But that, would definitely provide you a BAD result" ;-) :-D.


Again, - hope that would be the last "again" ;-) - I really appreciate your efforts, your points taken and you are indeed a better programmer based on your answers, I will definitely employ some of your better coding styles when new projects arises, but for now, I'll stick to my current approach that already worked. Thanks to both of you.

Since this is already solved, there's no need for us to go through discussing further on this at length. Problem solved on my end. 


Thank you all very much for your time. You guys in this forum ROCKS!!!!!!     \m/_  ( ͡° ͜ʖ ͡°)


 

 
tompips2018:

The logic IS correct; 

The logic was not correct as the logic did not take into account swap costs and possibly commissions.

 
Keith Watford:

The logic was not correct as the logic did not take into account swap costs and possibly commissions.

C'mon dude, move on. This thread is done, stop wailin' man. 


If you were so right you should have been the first to figure it out.


Anyways, the logic was RIGHT. For me. It's just wrong for you.

Fair enough?

Now give it up. Close the thread. 3:) :P