Closing out half lots. - page 17

 
SDC:

You are not thinking logically at all. How would modifying the magic number help you more than knowing the OrderOpenTime() ? It amounts to the exact same thing.


Because I need to do multiple partial closes with one order... If I do one OrderClose() and compare with OrderOpenTime() and then do another OrderClose() with the same Order, but again, compare using the OrderOpenTime(), it won't work, because there will be a closed order within the history and it won't close the second OrderClose() function...

Now, if I could OrderClose() using a Magicnumber 1234 and then after the OrderClose() was complete, I could modify the orders magic number to 12345, therefore, the first function that called the MagicNumber 1234 would no longer be applicable to the remaining order as the remaining order has now been assigned a magic number 12345. Now with this MagicNumber 12345, I could then use that with another OrderClose() function (assuming it is applicable) and repeat the process until what I have, is working and running how I want it to.

Until then, OrderOpenTime() can only work once...
 
I just had thought, and correct me if I am wrong, but what if I used OrderOpenTime() AND OrderCloseTime()?

If I compare the current OrderOpenTime() with the historically closed positions on the same Symbol() and find NO match, then CloseOrder() will run. Now, if the second CloseOrder() function is called (a price higher than the first CloserOrder(), and I want to close out some more lots on the same order), I could compare the OrderOpenTime() AND the OrderCloseTime(). If the OrderOpenTime(OpenPosition) == OrderOpenTime(History) && OrderCloseTime(history) > OrderOpenTime(OpenPosition) THEN OrderClose() the second function...?
 

Well you never said anything about multiple partial closes before, you said you just wanted to half close the order, but eitherways you are way overthinking this and completely missing the obvious.

Why does OrderOpenTime() only work once ?

static datetime partclosedonce;

static datetime partclosedtwice;

if order meets part closeing criteria level 1 and opentime does not match partclosedonce, part close it, add timestamp to partclosedonce.

if order meets part closing criteria level 1 and its opentime matches partclosedonce it was already partclosed once so do not part close it again yet.

if order meets part closing criteria level 2 and does not match partclosedtwice, part close it and add the timestamp to variable, partclosedtwice.

 

Also you should avoid doing anything with OrdersHistory. If you run a long backtest you may have hundreds, even thousands of orders in the history, your EA would be comparing your open order with all of them on every tick. It is very slow to do that and your backtests will get painfully slower and slower as the longer they run the bigger the history gets.

 
SDC:

Also you should avoid doing anything with OrdersHistory. If you run a long backtest you may have hundreds, even thousands of orders in the history, your EA would be comparing your open order with all of them on every tick. It is very slow to do that and your backtests will get painfully slower and slower as the longer they run the bigger the history gets.


How else can I do it then...?
 

i just showed you how to do it !! If there is a more straightforward way of doing it than that I would like to know what it is. Do it with the history if you want to, I was just giving you my opinion on doing it that way is all.

 
SDC:

i just showed you how to do it !! If there is a more straightforward way of doing it than that I would like to know what it is. Do it with the history if you want to, I was just giving you my opinion on doing it that way is all.


Oh I see static datetime holds the date and time of the OrderClose() so I can compare it with the initial opening time of the current order, as opposed to looking at anything to do with the history?
 

yes

 
Nice one - thanks for your help!

Static Datetime is fairly new to me, so I will be working on this tomorrow and see where I get! Hopefully then I'll get this annoying part of my code sorted :)
 
SDC:

Well you never said anything about multiple partial closes before, you said you just wanted to half close the order, but eitherways you are way overthinking this and completely missing the obvious.

Why does OrderOpenTime() only work once ?

static datetime partclosedonce;

static datetime partclosedtwice;

if order meets part closeing criteria level 1 and opentime does not match partclosedonce, part close it, add timestamp to partclosedonce.

if order meets part closing criteria level 1 and its opentime matches partclosedonce it was already partclosed once so do not part close it again yet.

if order meets part closing criteria level 2 and does not match partclosedtwice, part close it and add the timestamp to variable, partclosedtwice.


Banging! This is worked a treat - A lot easier than I imagined too! Just started looking at it, 5 minutes later, I've got it working :) Thank you very much for your help! I will now incorporate my multiple OrderClose() functions and see if I can get it running. Once I have done this, I am going to make another thread to give back the help I've received from others on here! I think this is definitely something others could use in once concise post.

I'm not out of the woods just yet, but that static datetime is working perfectly with this one orderclose(). I don't see why it shouldn't for the other ones either :)
Reason: