Jump to content

⤴️-Paid Ad- Check advertising disclaimer here. Add your banner here.🔥

Recommended Posts

Posted

Could anyone please help me how can i get the number of the last consecutive losing trades and the number of the last consecutive winning trades from the orders history?

 

Thank you for your help!

Posted (edited)

The solution

 

Okay Chris, here is the solution:

 

First we need to get the last closed order and its profit from the orders history.

 

int LastCloseTime = 0;
double LastProfit = 0;
for (int cnt = 0; cnt < OrdersHistoryTotal(); cnt++) {
  OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
  if (OrderSymbol()== Symbol() && OrderMagicNumber()== Magic) {
     if (OrderCloseTime() > LastCloseTime) {
        LastCloseTime = OrderCloseTime();
        LastProfit = OrderProfit();  
     }   
  }
}

 

Then we need two integers for counting the last consecutive winning and losing trades, and a global variable to save the last order's close time.

 

int CntWinners=0;
int CntLosers=0;
if (!GlobalVariableCheck("LastCloseTime")) GlobalVariableSet("LastCloseTime",0);

 

In the last step we need this code for counting:

 

if (LastProfit>=0 && LastCloseTime>GlobalVariableGet("LastCloseTime")) {
  GlobalVariableSet("LastCloseTime",LastCloseTime);
  CntWinners+=1;  // adding one to the winners
  CntLosers=0;      // nulling the losers
}
if (LastProfit<0 && LastCloseTime>GlobalVariableGet("LastCloseTime")) {
  GlobalVariableSet("LastCloseTime",LastCloseTime);
  CntWinners=0;   // nulling the winners
  CntLosers+=1;   // adding one to the losers
}      

 

If we want to print the actual number of the last consecutive winner and loser trades on to the chart we can do it this way:

 

Comment("Last Consecutive Winners: ", CntWinners, "\n", "Last Consecutive Losers: ", CntLosers);

 

I hope you find it useful!

Edited by HunFxTrader
  • 1 month later...
Posted

Need help in writing EA

 

Hello

Can someone please assist in writing an Ea to change stop loss based on 34EMA of high of the last closed bar for open sell trades and 34EMA of low for open buy trades Thanks

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


⤴️-Paid Ad- Check advertising disclaimer here. Add your banner here.🔥

×
×
  • Create New...