Position Information
PowerLanguage offers several built-in functions that assist in obtaining market position, entry price, exit price, unrealized and realized profits.
Using position information to open or close positions.
Based on market position and the crossing of Moving Averages (SMA).
Inputs: fastLength(10), slowLength(20);
Vars: 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;
Based on market position and profit or loss.
// 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;
Show the position information on the chart.
The following code utilizes text drawing to visualize position information for recent closures on a chart, including profit, entry price, exit price.
vars:MP(0);
MP=marketposition*currentcontracts;
if MP=0 and MP[1]<>0 then begin
value1=TEXT_New(D, T, H,"");
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 Description
Market Position
API | MarketPosition(PosBack) |
Explanation | – Returns a numerical value, indicating the type of the specified position. – This function can only be used in signals. – Use i_MarketPosition in indicators.– To get the position size, use CurrentContracts or its equivalent CurrentShares . |
PosBack | 0 or Not Specified- open position;1 – one position back (the last position closed);2 – two positions back, etc. |
Return | 1 : indicates a long position-1 : indicates a short position0 : indicates that the current position is flat. |
API | CurrentContracts |
Explanation | – Returns an absolute numerical value, indicating the number of contracts or shares held in the current position. – To obtain the position size with a sign (positive or negative), multiply the value of the CurrentContracts with the MarketPosition .– This function can only be used in signals. – Use i_ in indicators. |
Return | Returns an absolute numerical value, indicating the number of contracts or shares held in the current position. |
Example | MarketPosition(0) * CurrentContracts Will return a value of -9 if the strategy is short 9 contracts,will return a value of 4 if the strategy is long 4 contracts. |
Entry Price
API |
|
Explanation | – Returns a numerical value, indicating the price at the initial entry into the specified position. – This function can only be used in signals. |
PosBack | 0 or Not Specified- open position;1 – one position back (the last position closed);2 – two positions back, etc. |
Return | Returns a numerical value, indicating the price at the initial entry into the specified position. |
API |
|
Explanation | – Returns an absolute numerical value, indicating the execution price of trade entry order. – This function can only be used in signals. |
| 0 or Not Specified- open position;1 – one position back (the last position closed);2 – two positions back, etc. |
TradeNumber | a numerical expression, specifying the number of trade (zero-based). |
Return | Returns an absolute numerical value, indicating the execution price of trade entry order. |
Example | Will return a value of the second trade of the open position.PosTradeEntryPrice(0, 1); |
Exit Price
API |
|
Explanation | – Returns a numerical value, indicating the price at a complete exit from the specified position. – This function can only be used in signals. |
PosBack | 1 – one position back (the last position closed);2 – two positions back, etc. |
Return | Returns a numerical value, indicating the price at a complete exit from the specified position. |
API |
|
Explanation | – Returns an absolute numerical value, indicating the execution price of trade exit order. – This function can only be used in signals. |
| 0 or Not Specified- open position;1 – one position back (the last position closed);2 – two positions back, etc. |
TradeNumber | a numerical expression, specifying the number of trade (zero-based). |
Return | Returns an absolute numerical value, indicating the execution price of trade exit order. |
Example | Will return a value of the second closed trade of the open position.PosTradeExitPrice(0, 1); |
Unrealized and Realized Profits
API |
|
說明 | – Returns a numerical value, indicating the current unrealized profit or loss for the open position. – This function can only be used in signals. |
返回值 | Returns a numerical value, indicating the current unrealized profit or loss for the open position. |
API |
|
說明 | – Returns a numerical value, indicating the total realized profit or loss for the specified closed position. – This function can only be used in signals. |
| 0 or Not Specified- open position;1 – one position back (the last position closed);2 – two positions back, etc. |
返回值 | Returns a numerical value, indicating the total realized profit or loss for the specified closed position. |
Example | – Returns a value of -5 if the most recently closed position has generated $5 loss.Value1 = PositionProfit(1); – Returns a value of 0 if the position is open and there were no partial exits from it. Value2 = PositionProfit(0); – Returns a value of 5 if the position was partially closed with $5 profit. Value3 = PositionProfit(0); – Returns a value of 0 if there is no open position. 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