Jump to content

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...