Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Created October 3, 2012 08:41
Show Gist options
  • Save currencysecrets/3825836 to your computer and use it in GitHub Desktop.
Save currencysecrets/3825836 to your computer and use it in GitHub Desktop.
MQL4: Calculate Profit or Loss on Trade
/**
* This method calculates the profit or loss of a position in the home currency of the account
* @param string sym
* @param int type 0 = buy, 1 = sell
* @param double entry
* @param double exit
* @param double lots
* @result double profit/loss in home currency
*/
double calcPL(string sym, int type, double entry, double exit, double lots) {
var result;
if ( type == 0 ) {
result = (exit - entry) * lots * (1 / MarketInfo(sym, MODE_POINT)) * MarketInfo(sym, MODE_TICKVALUE);
} else if ( type == 1 ) {
result = (entry - exit) * lots * (1 / MarketInfo(sym, MODE_POINT)) * MarketInfo(sym, MODE_TICKVALUE);
}
return ( result );
}
@Danger-Mkh
Copy link

for someone who likes to be shorter

double calcPL(string sym, int type, double entry, double exit, double lots) {
    double diff = ( type == 0 ) ? (exit - entry) : (entry - exit);
    double result = diff * lots * (1 / MarketInfo(sym, MODE_POINT)) * MarketInfo(sym, MODE_TICKVALUE);

    return NormalizeDouble(result, 2);
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment