Created
October 3, 2012 08:41
-
-
Save currencysecrets/3825836 to your computer and use it in GitHub Desktop.
MQL4: Calculate Profit or Loss on Trade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); | |
} |
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
How about using buy and sell rather than entry exit so one does not need to specify whether its long or short?
double calcPL(string sym, double buy, double sell, double lots) { return (sell - buy) * lots .... }