Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Created July 17, 2012 21:00
Show Gist options
  • Save currencysecrets/3132041 to your computer and use it in GitHub Desktop.
Save currencysecrets/3132041 to your computer and use it in GitHub Desktop.
MQL4: RoR Analysis
//+------------------------------------------------------------------+
//| Trades Analysis.mq4 |
//| Ryan Sheehy, CurrencySecrets.com |
//| http://www.currencysecrets.com |
//+------------------------------------------------------------------+
#property copyright "Ryan Sheehy, CurrencySecrets.com"
#property link "http://www.currencysecrets.com"
/*
* With this script we will be running through all trades that have
* been closed and will output their results to a CSV file for input
* into a spreadsheet for further analysis. (Currently outputs to
* expert messages)
* Aspects that we will be looking for in our analysis will be:
* 1. Entry Time
* 2. Currency Pair traded
* 3. Entry Price
* 4. Initial Stop loss (previous swing point + spread)
* 5. Exit Time
* 6. Exit Price
* 7. RoR ratio (Raw)
* 8. RoR ratio (AUD)
* 9. P/L pips (Raw)
* 10. P/L pips (AUD)
*/
int TF = PERIOD_H4;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
getTrades(TF);
//----
return(0);
}
//+------------------------------------------------------------------+
void getTrades(int TF) {
int d, tot = OrdersHistoryTotal();
double p, ini, r, lotVal, tickVal;
for ( int i = tot; i >= 0; i-- ) {
if ( OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) ) {
// differentiate pending orders that were never complete from actual orders
if ( OrderType() == OP_BUY || OrderType() == OP_SELL ) {
lotVal = OrderLots() * MarketInfo(OrderSymbol(), MODE_LOTSIZE);
if ( OrderClosePrice() == OrderOpenPrice() ) {
tickVal = MarketInfo(OrderSymbol(), MODE_TICKVALUE);
} else if ( OrderType() == OP_BUY ) {
tickVal = OrderProfit() / (lotVal * (OrderClosePrice() - OrderOpenPrice()));
} else if ( OrderType() == OP_SELL ) {
tickVal = OrderProfit() / (lotVal * (OrderOpenPrice() - OrderClosePrice()));
}
if ( OrderType() == OP_BUY ) {
p = getLastSwing( 1, OrderSymbol(), TF, OrderOpenTime() );
ini = OrderOpenPrice() - p;
r = OrderClosePrice() - OrderOpenPrice();
} else if ( OrderType() == OP_SELL ) {
p = getLastSwing( -1, OrderSymbol(), TF, OrderOpenTime() );
ini = p - OrderOpenPrice();
r = OrderOpenPrice() - OrderClosePrice();
}
d = MarketInfo(OrderSymbol(), MODE_DIGITS);
Print( "," + TimeToStr( OrderOpenTime() ) + "," + OrderSymbol() + "," +
DoubleToStr( OrderOpenPrice(), d ) + "," + DoubleToStr( p, d ) + "," +
TimeToStr( OrderCloseTime() ) + "," + DoubleToStr( OrderClosePrice(), d ) + "," +
DoubleToStr( r / ini, 2 ) + "," + DoubleToStr( (r / ini) * tickVal, 2 ) + "," +
DoubleToStr( r / ( MarketInfo( OrderSymbol(), MODE_POINT ) * 10 ), d ) + "," +
DoubleToStr( (r * tickVal) / ( MarketInfo( OrderSymbol(), MODE_POINT ) * 10 ), d )
);
}
}
}
// print headers
Print( ",Entry Time,Currency,Entry Price,Initial Stop Loss,Exit Time,Exit Price,RoR (Raw),RoR (AUD),P/L pips (Raw),P/L pips (AUD)" );
}
double getLastSwing( int dir, string sym, int TF, datetime from ) {
int bars = iBars(sym, TF);
for ( int i = 0; i < bars; i++ ) {
if ( dir == -1 && iTime(sym,TF,i) < from ) {
if ( iHigh(sym, TF, i) < iHigh(sym, TF, i+1) && iHigh(sym, TF, i+1) >= iHigh(sym, TF, i+2) ) {
return( iHigh(sym, TF, i+1) );
}
} else if ( dir == 1 && iTime(sym,TF,i) < from ) {
if ( iLow(sym, TF, i) > iLow(sym, TF, i+1) && iLow(sym, TF, i+1) <= iLow(sym, TF, i+2) ) {
return( iLow(sym, TF, i+1) );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment