HELP NEEDED: Help with this function. The function should return true if the last two consecutive trades closed with a loss or with 0 profit.
You applied ArraySize to an uninitiated array(profitLossArray). I am not sure what the output can be. If it is zero then profitLossArray[1] is out of range in first iteration of loop.
if(profitLossArray[totalCount-1]<=0 && profitLossArray[totalCount-2]<=0) //If last two trades ended in a loss
Also when iterating through deals to find the DEAL_ENTRY_IN, you don't wanna return false and break if the current deal is not what you're looking for.
it's easy to catch two consecutive loss trades using OnTrade() or OntradeTransaction() exactly when the loss happens. but if you have to dig the deals history, then your code needs a few corrections. one mentioned above, and one here.
it's easy to catch two consecutive loss trades using OnTrade() or OntradeTransaction() exactly when the loss happens. but if you have to dig the deals history, then your code needs a few corrections. one mentioned above, and one here.
Yashar Seyyedin #:
You applied ArraySize to an uninitiated array(profitLossArray). I am not sure what the output can be. If it is zero then profitLossArray[1] is out of range in first iteration of loop.
You applied ArraySize to an uninitiated array(profitLossArray). I am not sure what the output can be. If it is zero then profitLossArray[1] is out of range in first iteration of loop.
Thanks for the help. I'm new in MQL5
Joseph Obasi #:
if(profitLossArray[totalCount-1]<=0 && profitLossArray[totalCount-2]<=0) //If last two trades ended in a loss
Thanks for the help!
I did not test this:
bool LastTwoTradesLost() { if(HistorySelect(0, TimeCurrent())==false) return false; if(HistoryDealsTotal()<=1) return false; int totalCount =0; for(int i=HistoryDealsTotal()-1; i>=0; i--) { ulong dealTicket = HistoryDealGetTicket(i); if(dealTicket==0) continue; if(HistoryDealGetString(deal_Ticket, DEAL_SYMBOL)!=_Symbol) continue; if(HistoryDealGetInteger(dealTicket,DEAL_ENTRY)!=DEAL_ENTRY_OUT)continue; double PnL = HistoryDealGetDouble(dealTicket,DEAL_PROFIT); totalCount++; if(PnL>0) return false; if(totalCount==2) return true; } return false; }
// Returns true if the last two closed trades ended up in a loss or 0 (break-even) bool LastTwoTradesLost() { double lastProfit = 0; // Variable to store the profit of the last closed trade double secondLastProfit = 0; // Variable to store the profit of the second last closed trade int count = 0; // Counter for the number of trades checked HistorySelect(0, TimeCurrent()); // Select the trade history till the current time // Loop through the trade history from the most recent trade for(int i = HistoryDealsTotal() - 1; i >= 0 && count < 2; i--) { ulong dealTicket = HistoryDealGetTicket(i); if(dealTicket > 0) { if(HistoryDealSelect(dealTicket)) { if(HistoryDealGetString(dealTicket, DEAL_SYMBOL) == _Symbol) { if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT) { // For the first trade found, store its profit in lastProfit if(count == 0) { lastProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT); count++; } // For the second trade found, store its profit in secondLastProfit else if(count == 1) { secondLastProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT); count++; } } } } } } // Check if both trades ended in a loss or break-even if(count == 2 && lastProfit <= 0 && secondLastProfit <= 0) { return true; } else { return false; } }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
The function should return true if the last two consecutive trades closed in a profit of 0 or at a loss.