Close (Losing) Trades any time, not just at new bar.

 

I've been trying to get this EA to close (losing) trades at anytime (that the StopLoss is reached) and not just at the end/beginning of a new bar as it currently does. This often results in losing trades taking heavy losses that I could avoid IF I can get the EA to close out these (losing) trades at ANYTIME. I've spent quite a bit of time trying to achieve this. I have added and refined the EA quite a bit, but I can NOT get this aspect of it to work as I need it to. Yup: I'm a NuB

Any and all help will be greatly appreciated:

Change CurrencyPairs 2.mqh to CurrencyPairs.mqh
 
double LossToClose = -150.5; //150.5$ lost to close the order immediately


for (int i = OrdersTotal() - 1; i >= 0; i--) //loop all the orders
   {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //select one after another
   //----
   if (OrderSymbol() == Symbol() && OrderMagicNumber() == xxxyyy) // check if it's the one u looking for
      {
      if (OrderProfit() <= LossToClose) // check if it's losing more or equal to what u decided
         {
         OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 5, Red); // close it
         }
      }
   }
 

Hi Jolqwer,

I recognize that I do not know enough about MQL4 to competently understand and follow the flow, path and especially the sequence and timing of events which I think is most likely the reason that I am having this problem.

This EA has significant capabilities to make profits, but because of my inability to be able to close losing trades out before they get to large, this makes it into a loser that will drain an account every time as it stands.

With this in mind, any suggestions on where to splice this into the EA at ?

Controlling when and where to put it such that it will process any (losing) trade at any point in time or level at and not just at the beginning of a new bar so that I can close out any (losing) trade when it is needed. My primary focus is on losing trades, but the same would be most helpful for any trade at any time and level. If I am able to get it to close out losers when I want to, I think that I will be able to modify it for any trade, regardless of the timing, level or status.

If my forward testing eventually proves this to be a safe and profitable EA, would you like a copy of it?

I recognize that if you are able to assist me with this that you are very likely capable of 'fixing it' yourself, but you may not have the time nor interest in doing so. Regardless, I want to make the offer in return for your help with this.

Either way, thanks for your assistance. It IS greatly appreciated regardless of the outcome (< 8)

 

The problem is not how to close (losing) trades, It's WHEN they close.

I want them to close at ANY time, not just at the start of a new bar which is the way it is now. I haven't been able to get it to do this yet.

 

as i have wrote to u a PM

put your code right after

int start()
   }
   //your code to close ...

it's ganna check every tick

https://docs.mql4.com/basis/functions/special

 
FourX:

The problem is not how to close (losing) trades, It's WHEN they close.

I want them to close at ANY time, not just at the start of a new bar which is the way it is now. I haven't been able to get it to do this yet.


easy enough. to make it simple without going into too much detail, try placing it above what you have there already.

A little tweaking of the code gjol gave you and you'll have it. Try to copy what you did already with void CloseBuysInProfit()

void CloseLosingTrades()
{

<--------------- place qjol's code here ----------------->

   }
   return;
}


void CloseBuysInProfit()
{

<--------------- you have code here now ----------------->

   }
   return;
}

Now if its not closing at anytime, but at new bar only.... well I have to ask what are your rules for closing orders? In other words, what is the logic in the code you are using to determine trades should now be closed??


and is this something you are experiencing in the tester using open prices or every tick or is this happening when trading live/demo ?

This information helps, but bottom line is the rules you have in place for closing orders will determine when your orders will be closed.

 

The trades ARE closing at any time, this was faulty analysis on my part. Sorry )< 8) Though the time factor may be (part of the) problem:

Some of both the winners and the LOSERS don't get closed out at all and they keep getting larger and larger and more and more of these accumulate ad infinitum. Alas about twice as many losers do this than winners. It seems that the EA tends to only close the trades of smaller value. The larger they get, the less likely it is to close them. In this condition it WILL drain an account EVERY time )< 8(

IF we can figure out how to close out ALL of the (losing) trades more reliably: ALWAYS WHEN wanted and needed; this should be quite a prolifically profitable EA.

It makes sense to let these winners ride to as high a level as is possible. But there still needs to be a way to close these out reliably as well. Preferably when they have peaked. A (2 stage) Trailing Stop Loss would seem to be a good exit strategy, but I'm open to other suggestions.


What it really needs is both 'CloseBuysInLoss' and a 'CloseSellsInLoss' External Variables, algorithms and code sections. I did make some with these included, but they were unsuccessful. I also had problems with these only working (somewhat) at the start of a new bar.

So far I've been stumped on this. As should be readily apparent, I have very little MQL4 programming skills. But I do recognize something like this that has excellent potential. Thus my extensive futile efforts to date.

This is ALL on forward trading on different pairs. Which pairs doesn't seem to matter. I don't utilize back testing anymore at all. I find that it is so unreliable that it is more of a misleading hindrance than assistance.

FYI: This EA was originally named 'PipMaker' if we can make this into a going concern, by all means profit from it yourselves.

But if successful I'm hoping to sell this as a commercial EA. Thus please use the original name of 'PipMaker' and leave the 'PipPumper' name for me to market and sell it with.

I've been 'trying' to do some marketing and sales of ForEx products, mostly EAs to date. It has turned out to be a LOT harder than anticipated to achieve any significant success and market penetration and sales. An eMail campaign to 30 million addresses from an Opt In list yielded only 12 sales! )< 8( Half of the 30M eMails were my composition and the other half by the successful professional InterNet marketer that owns the list. All of the 'deluge' of 12 sales came from the eMail that I wrote.

 

The addition of more modern account control features, including (better) Money Management would be beneficial in this EA as well.

The ability to set both the Account and Chart Margin's, % Risk (per Trade / Chart) to ensure long term account profitability, safety and avoid being 'Stopped Out' by brokers would enhance this EA with it's inherent significant potential.

If set for it, this EA will generate hundreds of trades per hour! Nice in some ways, but likely frowned upon by most broker's. The 'WaitTime' to close orders can help control this, but some External Variables to directly control the numbers of the (various types of) trades makes sense. There needs to be some adjustable safety limitations in this EA

 
I think that my problem lies not so much in that I am unable to ascertain and fumble my way through cobbling together a specific section or 'module' that performs a given task. But can't follow through the sequence or route, including condition branching, that this EA follows.
Reason: