Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Maximum overkill Close-All script (https://www.forexfactory.com/showthread.php?t=716973)

Nicholishen Nov 22, 2017 9:15am | Post# 1

Maximum overkill Close-All script
 
2 Attachment(s)
The goal of this script is to minimize risk in all situations, especially when the shit hits the fan. This script provides the best and the fastest possible risk mitigation available thru MT4 by utilizing some unique logic not found in any other close-all scripts.

Operation Summary:

  1. Net positions are calculated per symbol (sum of all open trades).
  2. Symbols are prioritized to manage net-positions from larger (more risk) to smaller.
  3. Hedge orders are sent in the order of priority to close all exposure at once regardless of the number of trades.
  4. Once all open-expo is hedged the program prioritizes pending orders
  5. Pending orders are deleted in order of highest risk of being executed
  6. Remaining hedged positions (closed by hedge) are then reconciled on the broker server and there are no costs/spread/commissions charged on the hedge (it is literally treated like a closed position)

Note: This only works on non-FIFO hedging brokers. I have also included a different script that closes orders on FIFO brokers (slowww compared to hedging brokers).
You may also like the Hedge Trade Panel
FIFO_CloseAll_Options.mq4
HedgeAccountCloseAll.mq4


rosalieone Nov 23, 2017 9:40am | Post# 2

Interesting, thanks !

Jj1357 Aug 28, 2018 1:12pm | Post# 3

Very good thanks. But it would be better in the form of EA

tzamo Aug 28, 2018 10:59pm | Post# 4

Dear Nicholishen,

Many thanks again for providing these. I am just wondering, for the brokers that charge upfront full commissions, eg. $7 per lot as soon as the trade is opened and don't charge on close of that trade. If hedging trades are opened, would you still lose the extra commission fee?

Kind Regards,
Tzamo

Nicholishen Aug 28, 2018 11:22pm | Post# 5

Dear Nicholishen, Many thanks again for providing these. I am just wondering, for the brokers that charge upfront full commissions, eg. $7 per lot as soon as the trade is opened and don't charge on close of that trade. If hedging trades are opened, would you still lose the extra commission fee? Kind Regards, Tzamo
It's important to understand the difference between closing offsetting orders separately vs. closing them using closeby.

When you close offsetting orders separately: you get charged twice; once for each individual order.

When you use closeby: your platform will show individual orders with separate commissions while they are open, however, closeby sends a special command to the broker server to reconcile the offsetting orders like an options deal. That is, one order was OPEN and the second was CLOSE. It will then be reconciled as a round-trip trade and you're only charged one commission.

Jj1357 Aug 29, 2018 7:17am | Post# 6

What is the difference between HedgeAccountCloseAll.mq4 and HedgeAccountCloseAll__2.mq4?

Nicholishen Aug 29, 2018 1:18pm | Post# 7

What is the difference between HedgeAccountCloseAll.mq4 and HedgeAccountCloseAll__2.mq4?
No offense, but the source code is all there and you should be able to run a diff and see for yourself.

Jj1357 Aug 29, 2018 4:18pm | Post# 8

I do not know what a diff is

Do not you want to tell what the difference is? Are yours.

Nicholishen Aug 29, 2018 4:46pm | Post# 9

I do not know what a diff is Do not you want to tell what the difference is? Are yours.
No. I am not interested in taking time to explain it, and I posted the source codes so you can modify it however you want. You can do a diff on the files and spot the changes yourself. If you don't know how to diff you can try here.

Jj1357 Aug 29, 2018 5:11pm | Post# 10

But what interests me is whether the two are equally reliable.

Jj1357 Aug 31, 2018 5:59am | Post# 11

He does not want to talk about this topic

Nicholishen Aug 31, 2018 9:04am | Post# 12

He does not want to talk about this topic
I'm happy to talk concepts and theory with you, however, I find your query to be extremely trivial. Even a junior level developer should know how to diff two files, and I just don't feel like using my time to hold your hand through something you should be able to figure out on your own. I'm sorry if that sounds harsh but you are digging through source code, and you should know how to read it before asking questions that you likely won't understand the answers to.

If you have a specific question about the script's operation then please ask it, but I won't be offering classes on how to diff, ITT. Thanks.

Jj1357 Aug 31, 2018 10:47am | Post# 13

{quote} I'm happy to talk concepts and theory with you, however, I find your query to be extremely trivial. Even a junior level developer should know how to diff two files, and I just don't feel like using my time to hold your hand through something you should be able to figure out on your own. I'm sorry if that sounds harsh but you are digging through source code, and you should know how to read it before asking questions that you likely won't understand the answers to. If you have a specific question about the script's operation then please ask...
You say: "extremely trivial" and "you likely won't understand the answers to"
I'm not a developer.
I'm not investigating the source code.
I just want to know what is the best script.
And nothing more.

Nicholishen Aug 31, 2018 11:31am | Post# 14

Use the one in the OP.

Jj1357 Aug 31, 2018 11:42am | Post# 15

Use the one in the OP.
What is the OP?

VEEFX Sep 1, 2018 12:50am | Post# 16

{quote} You say: "extremely trivial" and "you likely won't understand the answers to" I'm not a developer. I'm not investigating the source code. I just want to know what is the best script. And nothing more.
Then be nice to someone who is freely sharing the source code of his effort
This shit takes time and effort to develop and test. And you are a complete noob to trading and to forums.

{quote} What is the OP?
OP = Opening Post i.e. the very first post in the thread. It is also refered to the thread starter/owner

Jj1357 Sep 1, 2018 7:34am | Post# 17

{quote} Then be nice to someone who is freely sharing the source code of his effort This shit takes time and effort to develop and test. And you are a complete noob to trading and to forums. {quote} OP = Opening Post i.e. the very first post in the thread. It is also refered to the thread starter/owner
I just asked a question!
Surely I am more profitable than you

Nicholishen Sep 1, 2018 8:58am | Post# 18

{quote} Then be nice to someone who is freely sharing the source code of his effort This shit takes time and effort to develop and test. And you are a complete noob to trading and to forums. {quote} OP = Opening Post i.e. the very first post in the thread. It is also refered to the thread starter/owner
I'm pretty sure this is a sockpuppet account of someone on my ignore list. Welp, they're back on it and won't be trolling my threads anymore...

VEEFX Sep 1, 2018 1:32pm | Post# 19

{quote} I'm pretty sure this is a sockpuppet account of someone on my ignore list. Welp, they're back on it and won't be trolling my threads anymore...
Yep. The swamp keeps growing with very few good guys left here to interact with.
When I ignore, many times, their buddies circle also go to the gutter :-)
Look around man. Every idiot is popping ex4 just about everyday with no evidence of any track record and noobs are jumping right on it willing to test without even understanding the inner workings of how the code was developed. They will never succeed in life if they depend on someone else to tell them how to trade. It's just a sad state of affairs that forums spread.

Keep up the good work brother. We need more guys like you around here!

MoneyZilla Sep 11, 2018 12:04pm | Post# 20

2 Attachment(s)
This is amazing work by Sir Nico.

Pity traders here do not recognize it.

Here is a small contribution by me.

Close All Pairs button is Sir Nico's super mighty close by hedging script, as a button inside this EA.

Not a coder myself, so I can't clean up my close by EA from the functions I am actively using.

So please ask me not what is this and that. Will not answer a thing.

Reporting and fixing bugs is another story. Will fully cooperate in the bug fixing department.

Haven't really tested all new stuffs in this EA.

Just very grateful to Sir Nico and would like to contribute a little here, too.
Click to Enlarge

Name: CloseZilla.png
Size: 83 KB
CloseZilla 1.20.mq4

VEEFX Sep 14, 2018 1:06pm | Post# 21

The goal of this script is to minimize risk in all situations, especially when the shit hits the fan. This script provides the best and the fastest possible risk mitigation available thru MT4 by utilizing some unique logic not found in any other close-all scripts. Operation Summary: Net positions are calculated per symbol (sum of all open trades). Symbols are prioritized to manage net-positions from larger (more risk) to smaller. Hedge orders are sent in the order of priority to close all exposure at once regardless of the number of trades. Once...
Thinking about implementing this in my EA but won't help much for unidirectional trades.
I have been a tad busy this week to fine-tune my "old and faithful" manual strategy that I used to do with FXCM on their TS2 for USA.... and trying to replicate the exit logic in mt4. I could manually trade over 200+ trades during events with significant moves

Here's my ask to see if there is a better performance-tuned solution for my exits within the context of doing a Close-All @ %equity reached

Open Trade Volume: could be over 2000 trades to close when some % of equity has reached
All uni-directional trades to close on just one instrument
Looping all open trades is very slow and drains on my floating profit/loss while the loop executes. Not GOOD !

Possible Solution:

  1. Open one hedge trade of the amount of total Lot size. This will freeze my floating profit or loss.
  2. Now loop thru all open positions and do an alternating "Partial Close" of one MINLOT in profit and one MINLOT in loss at a time with some pause to avoid overloading the broker's server

Leaving slippage aside, Is there a better solution to do a massive close-all?


VEEFX Sep 14, 2018 1:19pm | Post# 22

This is amazing work by Sir Nico. Pity traders here do not recognize it. Here is a small contribution by me. Close All Pairs button is Sir Nico's super mighty close by hedging script, as a button inside this EA. Not a coder myself, so I can't clean up my close by EA from the functions I am actively using. So please ask me not what is this and that. Will not answer a thing. Reporting and fixing bugs is another story. Will fully cooperate in the bug fixing department. Haven't really tested all new stuffs in this EA. Just very grateful to Sir Nico...
Thank you for sharing the source code MZ. This is how many folks should learn how to code on their own and be self-sufficient instead of relying on developers to create ex4 files that may expire some time in the future leaving the trader hanging upside down!

Nicholishen Sep 14, 2018 1:38pm | Post# 23

{quote} Thinking about implementing this in my EA but won't help much for unidirectional trades. I have been a tad busy this week to fine-tune my "old and faithful" manual strategy that I used to do with FXCM on their TS2 for USA.... and trying to replicate the exit logic in mt4. I could manually trade over 200+ trades during events with significant moves Here's my ask to see if there is a better performance-tuned solution for my exits within the context of doing a Close-All @ %equity reached Open Trade Volume: could be over 2000 trades to close...
Is your broker in the US or not?

VEEFX Sep 14, 2018 1:40pm | Post# 24

{quote} Is your broker in the US or not?
offshore "ECN" and unregulated :-(

MoneyZilla Sep 14, 2018 2:24pm | Post# 25

Not a coder here, but having some experience with large number of orders close by closures. One way to save a buck or two is to close by at a desired low spread. For example if the spread is 0.5 pips on average, but it floats from 0.0 to 1.0, then setting 0.1 as a closure target will slow down the speed of closures, not loading the broker server, cause 0.1 spread is happening not very often and not for much long.

The problem here is the mt4 terminal gets heavy loaded while chasing the 0.1 spread. Not sure why exactly, but it does. And 2k trades is a super heavy burden for the buggy terminal, already... It may become unaccessible for large portions of time.

Another solution, might be, to close by from a different terminal. Do not close by from the same terminals where doing the trading.

Nicholishen Sep 14, 2018 2:32pm | Post# 26

{quote} offshore "ECN" and unregulated :-(
I'm not sure where the confusion is then... Sum up all your positions and set one limit order of equal size in the opposite direction. All positions instantly closed. Done. Then all you have to do is reconcile them as closed either manually or programmatically. That is, afterall, how this script works.

VEEFX Sep 14, 2018 2:33pm | Post# 27

Not a coder here, but having some experience with large number of orders close by closures. One way to save a buck or two is to close by at a desired low spread. For example if the spread is 0.5 pips on average, but it floats from 0.0 to 1.0, then setting 0.1 as a closure target will slow down the speed of closures, not loading the broker server, cause 0.1 spread is happening not very often and not for much long. The problem here is the mt4 terminal gets heavy loaded while chasing the 0.1 spread. Not sure why exactly, but it does. And 2k trades...
Agree... I already have maxspread filters for entries and exits

double MAX_SPREAD_ENTRY_FILTER= n.n; // (in pips) Enter 999 to ignore spread widening. Check for MAX_SPREAD_ENTRY_FILTER allowed prior to opening any positions //
double MAX_SPREAD_EXIT_FILTER= n.n; // (in pips) Enter 999 to ignore spread widening. Check for MAX_SPREAD_EXIT_FILTER allowed prior to closing any positions //

I manually enter and then EA takes over the aggressiveness when price goes in my direction.... and as long as my average price is above/below the current bid/ask and as long as I have margin freeing up with floating profits, I keep adding on every tick... until my %equity target is reached. Go big or Eat sh*t type of a deal (on a small sub-account during events or special conditions... 1500% within a 30-50 pip move :-

VEEFX Sep 14, 2018 2:40pm | Post# 28

{quote} I'm not sure where the confusion is then... Sum up all your positions and set one limit order of equal size in the opposite direction. All positions instantly closed. Done. Then all you have to do is reconcile them as closed either manually or programmatically. That is, afterall, how this script works.
That's great. I have not studied your script yet. My bad.... I assumed your script would work only with hedged positions of same position size. Would it work exactly the same way in both the scenarios.... and FASTER/less overhead compared to loop and individually close each trade?

0.1 minilot buy >> 0.1 minilot sell
1000+ buy trades of varying sizes totaling 20lots >> one 23 lots sell
varying lotsize and tradecount buy trades >> varying lotsize and tradecount sell trades....... your script will close only hedged trade sizes and leave the unhedged trades open/floating?

Keeping in mind that I am not too worried of trading cost.... speed/execution/performance overhead is what I am concerned

MoneyZilla Sep 14, 2018 2:45pm | Post# 29

{quote}.... and as long as my average price is above/below the current bid/ask and as long as I have margin freeing up with floating profits, I keep adding on every tick... until my %equity target is reached. Go big or Eat sh*t type of a deal (on a small sub-account during events or special conditions... 1500% within a 30-50 pip move :-
Honestly, I have similar minset. NHowever, not as brave as you are. These 1,500% really look fantastic. If you can pull out them in 1 out of XX tries? That would be amazing! I will think it over. It is a good idea for me. My signals are not really with high quality, I was hoping for making 100% to 800% a month. But it is win all or lose all situation and 10k is the minimum account balance required. Scary.

VEEFX Sep 14, 2018 2:51pm | Post# 30

{quote} Honestly, I have similar minset. NHowever, not as brave as you are. These 1,500% really look fantastic. If you can pull out them in 1 out of XX tries? That would be amazing! I will think it over. It is a good idea for me. My signals are not really with high quality, I was hoping for making 100% to 800% a month. But it is win all or lose all situation and 10k is the minimum account balance required. Scary.
sorry.. I mean 1500% in a week and not from one single setup. I am against such aggressive styles and a opponent of rags2riches type MM styles. I focus on trade volume and speed/frequency to stack when things go in my favor. The absolute key driver to this style is "%Entry Accuracy" measure. if most of your trades are going 3-5 pips in profit within minutes, this style makes sense

EDIT : HAve you played with "MoveSLToAverageBE"... trail the average price and get the f*ck out

Nicholishen Sep 14, 2018 3:16pm | Post# 31

{quote} That's great. I have not studied your script yet. My bad.... I assumed your script would work only with hedged positions of same position size. Would it work exactly the same way in both the scenarios.... and FASTER/less overhead compared to loop and individually close each trade? 0.1 minilot buy >> 0.1 minilot sell 1000+ buy trades of varying sizes totaling 20lots >> one 23 lots sell varying lotsize and tradecount buy trades >> varying lotsize and tradecount sell trades....... your script will close only hedged trade sizes and leave the...
Don't overcomplicate it. It's simple. You hedge the net position size with a single order so your position is closed (nullified). There is no faster way to shut down a position comprised of multiple trades.

When everything is hedged - then you can manually do a multiple-closeby from the terminal to reconcile them all at once or programmatically loop the trades and use the closeby func. Time is NOT of the essence for this part of the process because your position is already closed.

It's sad that not many traders know they can manage their positions in this manner so they end up wasting money by not using closeby -- and waste even more money by closing down individual trades one by one while price moves against them (inevitable with FIFO brokers).

MoneyZilla Sep 14, 2018 3:22pm | Post# 32

{quote} EDIT : HAve you played with "MoveSLToAverageBE"... trail the average price and get the f*ck out
Month/week/day, does not matter. We are not bankers to trade huge volumes and be uber happy returns around 25% per year. As a hobby trader, I see it pointless to trade for such a small income, when I am short of 10 mil in my bank account.

To answer your question, I am researching the distance trading from the average price. On demo, these are my most successfult setups. But they come with a DD most of the time, and now, after this week of worst signals ever, I am thinking of setting the average setups, live during the weekend. Not surprisingly, the average price just works great, cause closing behind the original entry is common style there.

MoneyZilla Sep 14, 2018 3:41pm | Post# 33

{quote} It's sad that not many traders know they can manage their positions in this manner so they end up wasting money by not using closeby -- and waste even more money by closing down individual trades one by one while price moves against them (inevitable with FIFO brokers).
I have been trying to promote the hedge close by here from 2016. Got only negative responses. It is funny almost nobody knows about that. Even the so called experienced traders.

Hats down to you, cause you did it with respect and in style.

About hedging. They hate it here. Again, cause they understand hedging, as much as they do understand the hedge close by...

VEEFX Sep 14, 2018 3:47pm | Post# 34

{quote} I have been trying to promote the hedge close by here from 2016. Got only negative responses. It is funny almost nobody knows about that. Even the so called experienced traders. Hats down to you, cause you did it with respect and in style. About hedging. They hate it here. Again, cause they understand hedging, as much as they do understand the hedge close by...
Agree.. I personally did not know but I have not traded manually in mt4 much at all. I will incorporate closeby in my EA this weekend. might save me a ton on trading costs :-

MoneyZilla Sep 14, 2018 3:59pm | Post# 35

{quote} Agree.. I personally did not know but I have not traded manually in mt4 much at all. I will incorporate closeby in my EA this weekend. might save me a ton on trading costs :-
A ton? It will save you a fortune! I have seen it adding 200 to 500 bucks, just like that. On a single closure. Because of lower spread target and 1 commission less paid. Especially, if you trade with thousands of orders? And if you happen to have some nice volumes of lots opened alongside those thousands of orders?

Sir Nico will go to Heaven. I am sure!

VEEFX Sep 14, 2018 4:09pm | Post# 36

1 Attachment(s)
{quote} Month/week/day, does not matter. We are not bankers to trade huge volumes and be uber happy returns around 25% per year. As a hobby trader, I see it pointless to trade for such a small income, when I am short of 10 mil in my bank account. To answer your question, I am researching the distance trading from the average price. On demo, these are my most successfult setups. But they come with a DD most of the time, and now, after this week of worst signals ever, I am thinking of setting the average setups, live during the weekend. Not surprisingly,...
Personally, I simply could NOT add/stack without knowing the average BE price across all trades. Without an eye ball on average price, I don't even know how one can trade/avoid drawdown. Here's what I use... my life line to monitor with my exits.

p.s. veefx in filename only means I like/use/tweaked the code. I am not the original developer.
Consolidated Positions(VeeFX).mq4

VEEFX Sep 14, 2018 4:14pm | Post# 37

2 Attachment(s)
here's another useful tool I developed. Order line on MT4 is crapshit. it only draws a line bit when not tell you when/where on which candle or on which timeframe the order of taken. when trading multiTF, it's absolutely critical to track our trades by timeframe....to keep track of scale-bility etc

EDIT: I use the ex4 version but strangely can't to find the mql code for it.
OpenOrders_line.mq4
OpenOrders_line(VeeFX).ex4

Nicholishen Sep 14, 2018 5:08pm | Post# 38

{quote} I have been trying to promote the hedge close by here from 2016. Got only negative responses. It is funny almost nobody knows about that. Even the so called experienced traders. Hats down to you, cause you did it with respect and in style. About hedging. They hate it here. Again, cause they understand hedging, as much as they do understand the hedge close by...
In all honesty it's not "hedging", it's managing net position via offsetting orders. I think if more traders understood the dynamic as such then they'd be open to shifting their paradigm. Ironically, some of these same people will open a GRID with offsetting orders; just paying the freakin' broker for their own stupidity. I was actually banned from MQL5.com for calling out one of the moderators for his idiotic waste of capital (he was coaching other traders to do so) and no matter how many times I explained it, his ego wouldn't accept the reality which we all know.

MoneyZilla Sep 15, 2018 1:39am | Post# 39

{quote} In all honesty it's not "hedging", it's managing net position via offsetting orders.
You have nail it. Yes, this is all about. The net orders management.

I still call it hedging, in my own trading terms. There are two major ways to hedge for me:

1. By opening buys/sells.
a. and keep both opened all/most of the time.
b. same as above, but close by the hedged lots and keep the net lots opened only, somewhere around the end of the day, to avoid extra swaps.

How you close by one against the other by leaving opened the net lots remained, is crucial. If you have 1.0 lot buys and 0.5 lots sells and close by randomly any 0.5 buys vs any 0.5 sells is not the same as:

2. closing a buy instead of opening a new sell.

The developper I work with 4+ years already, told me it is the same (1b and 2), but in reality the result is so different. 2 can hardly being called hedging, yet it is for me, as I instead of opening a sell, just close the first buy opened, ext.

So, managing the net opened orders is all about in trading. How you get there, hedging, nedging, gridding, lot(to), ext. does not matter.

VEEFX Sep 17, 2018 4:13am | Post# 40

{quote} In all honesty it's not "hedging", it's managing net position via offsetting orders. I think if more traders understood the dynamic as such then they'd be open to shifting their paradigm. Ironically, some of these same people will open a GRID with offsetting orders; just paying the freakin' broker for their own stupidity. I was actually banned from MQL5.com for calling out one of the moderators for his idiotic waste of capital (he was coaching other traders to do so) and no matter how many times I explained it, his ego wouldn't accept the...
Perhaps I am over-thinking this CloseBy thingy :-)
Bare with me please as I want to see if this scenario will have any adverse impact to my overall strategy. first, I base by position size only on AccountFreeMargin() and not accountEquity(). If I have free margin, i take the next available signal.

With this being the case from MM standpoint and assuming all my SL and TP for the same pair is at the same price level and away from the average entry price... Can I use two "consolidated hedge" Sell hedge orders one for SL and another one for TP with hedged size=total size of buy positions?

if the answer is yes, then it will free up AccountFreeMargin() as now the positions are hedged. This will effect the position size of my next signal i.e. increasing my exposure on the pair which the new signal is generated on. Not good BUT it is REALLY good if I am leaving the "consolidated Hedge" running at TP and Closing the "Consolidated Hedge" by immediately executing your closeby script.... this way, I am only FREEZING the profits from average entry price (i.e. TP triggered positions) and unlocking/exiting the losers right away. At the end of week/month, I can do a closeby across all positions to realize the cost savings on my profitable hedges. Win-Win at the end I think. Do I have this right?


© Forex Factory