Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active October 31, 2016 07:59
Show Gist options
  • Save currencysecrets/9672338 to your computer and use it in GitHub Desktop.
Save currencysecrets/9672338 to your computer and use it in GitHub Desktop.
Trailing stop method.
// this function requires the following parameters:
// sym = currency
// op = opening price of the current trade
// sl = stop loss price of the current trade
// ot = opening datetime of the current trade
// per = active chart's period - for calculating ATR distance properly
// type = order type of the current trade
double getTrailingStop( string sym, double op, double sl, datetime ot, int per, int type ) {
double temp;
// make the result for this function initially equal the stop loss
double result = sl;
// calculate the trailing stop distance
double atr = iATR( sym, per, EXT_ATR, 1 ) * EXT_ATR_TRAILSTOP;
// check if we are operating in the bar of entry, if so move to minute charts to find out
// what the highest high or lowest low is since entry (rather than using the whole bar)
// As Pepperstone timestamp their bars with the opening time of that bar we can frame
// our logic by checking if the current bar is less than or equal to the current bar
// if it is, then the current bar *IS* the bar of entry.
if ( iTime( sym, per, 0 ) <= ot ) {
temp = getMinutePrice( sym, ot, atr, type );
if ( type == OP_BUY && temp > result ) result = temp;
if ( type == OP_SELL && temp < result ) result = temp;
}
// get bars of current periods' chart
int b = iBars( sym, per );
// let's loop through the bars now, starting with the most recent bar
for ( int i = 0; i < b; i += 1 ) {
// if the time of the active chart is LESS than the time of when the trade opened, end
if ( iTime( sym, per, i ) < ot ) break;
// calculate the ATR trailing distance - using active chart's period
// and always using the previous bar's ATR as the current bar is still
// forming
atr = iATR( sym, per, EXT_ATR, i+1 ) * EXT_ATR_TRAILSTOP;
// for long trades
if ( type == OP_BUY ) {
// using the high of the minute chart subtract the ATR distance
temp = iHigh( sym, per, i ) - atr;
// compare against the current best result, and if it is better modify result
if ( temp > result ) result = temp;
// for short trades
} else if ( type == OP_SELL ) {
// using the low of the minute chart add the ATR distance
temp = iLow( sym, per, i ) + atr;
// compare against the current best result, and if it is better modify result
if ( temp < result ) result = temp;
}
}
// return the result, normalizing the double
return ( N( result ) );
}
// this function will return the highest high or lowest low in the minute chart
// parameters of this function:
// sym = currency's symbol
// ot = opening datetime of the trade
// atr = atr value used as trailing distance
// type = order type of the trade
double getMinutePrice( string sym, datetime ot, double atr, int type ) {
int per = PERIOD_M1;
int b = iBars( sym, per );
// better to start with a value, rather than 0
double result = iClose( sym, per, 0 );
for ( int i = 0; i < b; i += 1 ) {
// once the active bar is less than the opening time then we are analysing
// bars PRIOR to entry, therefore exit
if ( iTime( sym, per, i ) < ot ) break;
if ( type == OP_BUY && iHigh( sym, per, i ) > result ) result = iHigh( sym, per, i );
if ( type == OP_SELL && iLow( sym, per, i ) < result ) result = iLow( sym, per, i );
}
// now that we have found the highest value since entry let's subtract the atr distance
if ( type == OP_BUY ) return( result - atr );
// now that we hav found the lowest value since entry let's add the atr distance
if ( type == OP_SELL ) return ( result + atr );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment