I think this has it, but I've only tested over a short time sample:
for a parameter of how many losses before disconnecting, in external variables:
extern int MaxLosses = 3;
then to initialize the loss count for each session (when I attach the expert and leave it to run for the night):
// declare somewhere outside any function
int ordersHistoryInit = 0;
// set in init function for starting point
int init() {
...
ordersHistoryInit = OrdersHistoryTotal();
Down in the Server() function, this code checks the loss count, and disconnects FST if the max has been hit:
int Server() {
...
while (!IsStopped())
{
// count previous orders, be sure less than max number of allowed losses
// if more, then return -1 to end server
int LossCount=0;
int OrdHistory=OrdersHistoryTotal();
if(OrdHistory>0) {
for(int cnt=OrdHistory-ordersHistoryInit; cnt>=1; cnt--){
if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY)){
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Expert_Magic) {
if (OrderProfit()>=0) {
LossCount=0;
}
else {
LossCount++;
}
}
}
}
}
if (LossCount >= MaxLosses) {
message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + "Max Losses (" + LossCount + "/" + MaxLosses + ") hit, disconnecting FST";
Comment(message);
Print("Max Losses hit: Loss Count: " + LossCount + " MaxLosses: " + MaxLosses);
return (-1);
}
Also in the Server() function, to display how many losses there are up to now:
if (time > tickTime) {
...
if (tickResponce == 1)
{
FST_Connected = true;
TimeLastPing = TimeLocal();
message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + " Forex Strategy Trader is connected." + "\n";
// add count to display info here
message = message + "Loss Count = " + LossCount + " / Max Loss = " + MaxLosses;
}
I'll try this out for the week, check if it works or has bugs.
Also, the way this is coded, it will count consecutive losses, not total losses in the session. This is best if your limit is larger than your stop. If your stop is larger than your limit, it would not be so good, as you could have the case of win-loss-win-loss-win-loss ... n, which would not disconnect and could run a large drawdown.
So far in my short testing, this code was able to run separate counts for 2 instances of FST running on 2 different crosses.
Further, I think this requires removing and attaching the Expert for each session, in order to set the max number of losses, and to reset the count.