Position Information#

PowerLanguage provides several built-in functions to help obtain position status, entry price, exit price, unrealized and realized profits, and other position information.

Using Position Information to Open and Close Positions#

Position information and Moving Average (SMA) crossover to open or close positions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Inputs: fastLength(10), slowLength(20);
Variables: fastSMA(0), slowSMA(0);

fastSMA = Average(Close, fastLength);
slowSMA = Average(Close, slowLength);

// Open position
If MarketPosition = 0 and fastSMA crosses above slowSMA Then
  Buy ("Long Entry") next bar at market;

// Close position
If MarketPosition = 1 and fastSMA crosses below slowSMA Then
  Sell ("Exit Long") next bar at market;

Position information and profit/loss to close positions.

1
2
3
4
5
6
7
// Close the position if the profit exceeds 500 points
If MarketPosition = 1 and (Close - EntryPrice) * BigPointValue > 500 Then
  Sell ("Profit Exit") next bar at market;

// Close the position if the loss exceeds 300 points
If MarketPosition = 1 and (EntryPrice - Close) * BigPointValue > 300 Then
  Sell ("Stop Loss") next bar at market;

Displaying Position Information on the Chart#

The following code uses text drawing to display the position information (profit, entry price, exit price) of the last closed position on the chart.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Variables: MP(0);

MP = MarketPosition * CurrentContracts;

If MP = 0 and MP[1] <> 0 Then Begin
  Value1 = Text_New(Date, Time, High, "");
  Text_SetString(Value1,
    "+$" + NumToStr(PositionProfit(1), 3) + NewLine
    + "EntryPrice: " + NumToStr(EntryPrice(1), 3) + NewLine
    + "ExitPrice: " + NumToStr(ExitPrice(1), 3) + NewLine
    + "PositionProfit: " + NumToStr(PositionProfit(1), 3) + NewLine
    + "OpenPositionProfit: " + NumToStr(OpenPositionProfit, 3)
  );
  Text_SetStyle(Value1, 2, 1);
  Text_SetColor(Value1, RGB(232, 232, 0));
  Text_SetFontName(Value1, "Showcard Gothic");
  Text_SetSize(Value1, 14);
  Text_SetAttribute(Value1, 1, true);
  Text_SetBorder(Value1, true);
  Text_SetLocation(Value1, Date, Time, High + 200);
End;

The above code will produce output similar to the following:

Common Position Functions#

Position Status#

MarketPosition(PosBack)#

Returns the status of the specified position. Can only be used in signal scripts; use i_MarketPosition in indicators. To get the position size, use CurrentContracts or CurrentShares.

ItemDescription
PosBack0 or not specified: current position. 1: the last closed position. 2: the position closed before last, and so on
Return value1: long position. -1: short position. 0: no position

CurrentContracts#

Returns the number of contracts held in the current position as an absolute value (no sign). To get the position size with a sign (positive or negative), multiply CurrentContracts by the value of MarketPosition. Can only be used in signal scripts; use i_CurrentContracts in indicators.

1
2
3
// Returns -9 if the strategy holds 9 short contracts
// Returns 4 if the strategy holds 4 long contracts
MarketPosition(0) * CurrentContracts;

Entry Price#

EntryPrice(PosBack)#

Returns the entry (opening) price of the specified position. Can only be used in signal scripts.

ItemDescription
PosBack0 or not specified: current position. 1: the last closed position. 2: the position closed before last, and so on
Return valueThe entry (opening) price of the specified position

PosTradeEntryPrice(PosAgo, TradeNumber)#

Returns the entry price of the specified trade. Can only be used in signal scripts.

ItemDescription
PosAgo0 or not specified: current position. 1: the last closed position. 2: the position closed before last, and so on
TradeNumberThe TradeNumber-th trade of the position. 0 is the first trade (opening), and so on
Return valueThe entry price of the specified trade
1
2
// Returns the entry price of the second trade of the current position
PosTradeEntryPrice(0, 1);

Exit Price#

ExitPrice(PosBack)#

Returns the exit (closing) price of the specified position. Can only be used in signal scripts.

ItemDescription
PosBack1: the last closed position. 2: the position closed before last, and so on
Return valueThe exit (closing) price of the specified position

PosTradeExitPrice(PosAgo, TradeNumber)#

Returns the exit price of the specified trade. Can only be used in signal scripts.

ItemDescription
PosAgo0 or not specified: current position. 1: the last closed position. 2: the position closed before last, and so on
TradeNumberThe TradeNumber-th trade of the position. 0 is the first trade (opening), and so on
Return valueThe exit price of the specified trade
1
2
// Returns the exit price of the second trade of the current position
PosTradeExitPrice(0, 1);

Unrealized and Realized Profits#

OpenPositionProfit#

Returns the unrealized profit or loss of the current position. Can only be used in signal scripts.

PositionProfit(PosBack)#

Returns the realized profit or loss of the specified position. If the position is only partially closed, returns the realized portion of the profit or loss. Can only be used in signal scripts.

ItemDescription
PosBack0 or not specified: current position. 1: the last closed position. 2: the position closed before last, and so on
Return valueThe realized profit or loss of the specified position
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Get the profit or loss of the last closed position; returns -5 if the loss is $5
Value1 = PositionProfit(1);

// If the position is still open and has not been partially closed, returns 0
Value2 = PositionProfit(0);

// If the position has been partially closed with a realized profit of $5, returns 5
Value3 = PositionProfit(0);

// If there is no open position, returns 0
Value4 = PositionProfit(0);

Reference#

https://www.multicharts.com/trading-software/index.php?title=Category:Strategy_Position

https://www.multicharts.com/trading-software/index.php?title=Category:Strategy_Position_Trades

© 2026 CodeReindeer. All rights reserved.