Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Created July 6, 2012 08:28
Show Gist options
  • Save currencysecrets/3058950 to your computer and use it in GitHub Desktop.
Save currencysecrets/3058950 to your computer and use it in GitHub Desktop.
MQL4: Get Last Swing Price
/*
* Get the price of the Stop Loss according to last swing price.
* @param sym string Symbol of currency being analysed
* @param TF int TimeFrame of the currency being analysed
* @param ord int OrderType() = OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP
* @param entry double Entry price of the order
* @return double Last swing point
*/
double getLastSwingPrice(string sym, int TF, int ord, double entry) {
int bars = iBars(sym,TF);
double stop;
int dir;
if ( ord == OP_BUYLIMIT || ord == OP_BUYSTOP ) {
dir = 1;
} else if ( ord == OP_SELLLIMIT || ord == OP_SELLSTOP ) {
dir = -1;
}
// do NOT use 0 as the current bar is still evolving!
for ( int i = 1; i < bars; i++ ) {
if ( dir == -1 && iHigh(sym, xTimeFrame, i) < iHigh(sym, xTimeFrame, i+1) &&
iHigh(sym, xTimeFrame, i+1) >= iHigh(sym, xTimeFrame, i+2) &&
iHigh(sym, xTimeFrame, i+1) > entry ) {
stop = iHigh(sym, xTimeFrame, i+1);
break;
}
if ( dir == 1 && iLow(sym, xTimeFrame, i) > iLow(sym, xTimeFrame, i+1) &&
iLow(sym, xTimeFrame, i+1) <= iLow(sym, xTimeFrame, i+2) &&
iLow(sym, xTimeFrame, i+1) < entry ) {
stop = iLow(sym, xTimeFrame, i+1);
break;
}
}
return ( stop );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment