Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active December 22, 2015 08:58
Show Gist options
  • Save currencysecrets/6448513 to your computer and use it in GitHub Desktop.
Save currencysecrets/6448513 to your computer and use it in GitHub Desktop.
Report on Open Positions - this function obtains the currently held open positions and returns a string.
int extBarsBack = 60;
// this function returns the profit/loss if the stop is hit
double profitAtStop( double open, double stop, double lots, bool isLong, string sym = "" ) {
if ( sym == "" ) { sym = Symbol(); }
double result = lots * MarketInfo( sym, MODE_TICKVALUE ) / MarketInfo( sym, MODE_TICKSIZE );
if ( isLong ) {
return( N( ( stop - open ) * result, 2 ) );
} else {
return( N( ( open - stop ) * result, 2 ) );
}
}
// convert double to string
string D( double price, int num = -1 ) {
if ( num == -1 ) { num = Digits; }
return( DoubleToStr( price, num ) );
}
// report on open positions
string getOpenPositions() {
int tot = OrdersTotal();
string result;
string header = "Symbol, Dir, Lots, StopDist, ATR(" + extBarsBack + "), Open P/L, Stop P/L \n";
string temp, dir, sym;
double dist,stopPL,accumStopPL;
// get all open trades
for( int i = 0; i < tot; i++ ) {
temp = "";
if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES ) ) {
sym = OrderSymbol();
if ( OrderType() == OP_BUY ) {
dir = "B";
dist = ( MarketInfo( sym, MODE_BID ) - OrderStopLoss() ) / ( MarketInfo( sym, MODE_TICKSIZE ) * 10 );
stopPL = profitAtStop( OrderOpenPrice(), OrderStopLoss(), OrderLots(), true, sym );
} else {
dir = "S";
dist = ( OrderStopLoss() - MarketInfo( sym, MODE_ASK ) ) / ( MarketInfo( sym, MODE_TICKSIZE ) * 10 );
stopPL = profitAtStop( OrderOpenPrice(), OrderStopLoss(), OrderLots(), false, sym );
}
accumStopPL += stopPL;
temp = sym + ", " + dir + ", " + D( OrderLots(), 2 ) + ", " + D( dist, 1 ) + ", " +
D( iATR( sym, PERIOD_H4, extBarsBack, 0 ) / ( MarketInfo( sym, MODE_TICKSIZE ) * 10 ), 1 ) +
", $" + D(OrderProfit(), 2) + ", $" + D( stopPL, 2 ) + "\n";
}
result = result + temp;
}
result = "Account Balance if all stops hit = $" + D( AccountBalance() + accumStopPL, 2 ) + "\n\n" + header + result;
return( result );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment