How do I stop trading for half hour after 2 consecutive losses?
What is the code I can implement in my mq4 for this function?
What is the code I can implement in my mq4 for this function?
Do you believe tight stop-losses cause losses in the long run? 76 replies
Do you believe fixed stop-losses cause losses in the long run? 16 replies
X-1102 1 hour , 4 hour and daily Tf trades 7 replies
Create a verticle line at certain hour for every certain hour 16 replies
Buy Stop and Sell Stop orders with OCO and Trailing Stop 0 replies
int Losses()
{
int Count = 0;
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderProfit()>0) break;
Count++;
}
return(Count);
}
bool bNewTrade=True;
if(Losses()>=2)
{
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(TimeCurrent()-OrderCloseTime() < 30*60)
bNewTrade = False;
}
}
if(bNewTrade==False)
{
Print("Wait for half hour after last 2 consecutive losses");
...
return;
}
DislikedHi, I know this is an old thread but I'm new to the forum so unable to create my own thread. I want to add a code to my EA that If the last 3 trades were losing trades then not to open another trade until 3 hours has passed since the last losing trade. I am creating my EA in FxPro Quant and i've been told by their support team that the feature isn't available in quant but I thought maybe I can just add it to the code after. Does anyone have any suggestions. I tried the above code posted but It didn't work. Thanks, SWPGIgnored
//+------------------------------------------------------------------+ //| StopTradingForXafterN.mq4 | //| nicholishen | //| http://www.reddit.com/u/nicholishenFX | //+------------------------------------------------------------------+ #property copyright "nicholishen" #property link "http://www.reddit.com/u/nicholishenFX" #property version "1.00" #property strict #include <MQL4OrderPool.mqh> input int magic_number = 12345; input int num_losing_trades = 3; input int wait_hours = 3; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ MQL4OrderPool pool; int OnInit() { pool.Init(Symbol(),magic_number,MODE_HISTORY,ORDERS_FILLED,SORT_CLOSE_TIME_DESCENDING); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnTick() { if(IsWaitTime()) return; } //+------------------------------------------------------------------+ bool IsWaitTime() { pool.Refresh(); int cnt=0; for(int i=0;i<pool.Total();i++) { if(pool[i].OrderProfit() < 0.0) cnt++; else break; } if(cnt >= num_losing_trades && TimeCurrent() - pool[0].OrderCloseTime() < wait_hours * 60 *60) return true; return false; }
Disliked{quote} The problem with the suggestions above is that they assume the history orders are sorted by close time, but they aren't. In order to begin tracking the order of your trades you first have to sort them programmatically. After you sort them - then you count the consecutive losses, and when they meet your loss criteria then you would subtract the most recent order's close time from the current time to evaluate how much time has lapsed. This is how I would implement it. //+------------------------------------------------------------------+ //|...Ignored
//Binary Option Call if(iCustom(NULL, PERIOD_CURRENT, "the hunter indicator", 1, 1) != 0 && iCustom(NULL, PERIOD_CURRENT, "the hunter indicator", 1, 1) != EMPTY_VALUE //the hunter indicator is not equal to fixed value ) { RefreshRates(); price = Ask; //-------------------------------------------------+ //******TESTING HERE******* //-------------------------------------------------+ if (MM_Size() >= 80 && TimeCurrent() - LastTradeTime < NextOpenTradeAfterBars * PeriodSeconds()) displayText("Lasttrd time : "+DoubleToStr(LastTradeTime,2)+" $",2,clrYellow); return; //next open trade after time if(MM_Size() == 20) if(IsTradeAllowed()) { ticket = myOrderSend(OP_BUY, price, MM_Size(), "BO exp:"+IntegerToString(5 * 60)); //binary option order if(ticket <= 0) return; } else //not autotrading => only send alert myAlert("order", ""); LastTradeTime = TimeCurrent(); }