MultiCharts | PowerLanguage Tutorials | CH11: Position Information

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

APIMarketPosition(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.
PosBackor Not Specified- open position;
– one position back (the last position closed);
– two positions back, etc.
Return1: indicates a long position
-1: indicates a short position
0: indicates that the current position is flat.
APICurrentContracts
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_CurrentContracts in indicators.
ReturnReturns an absolute numerical value, indicating the number of contracts or shares held in the current position.
ExampleMarketPosition(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

APIEntryPrice(PosBack)
Explanation– Returns a numerical value, indicating the price at the initial entry into the specified position.
– This function can only be used in signals.
PosBackor Not Specified- open position;
– one position back (the last position closed);
– two positions back, etc.
ReturnReturns a numerical value, indicating the price at the initial entry into the specified position.
APIPosTradeEntryPrice(PosAgo, TradeNumber)
Explanation– Returns an absolute numerical value, indicating the execution price of trade entry order.
– This function can only be used in signals.
PosAgoor Not Specified- open position;
– one position back (the last position closed);
– two positions back, etc.
TradeNumbera numerical expression, specifying the number of trade (zero-based).
ReturnReturns an absolute numerical value, indicating the execution price of trade entry order.
ExampleWill return a value of the second trade of the open position.
PosTradeEntryPrice(0, 1);

Exit Price

APIExitPrice(PosBack)
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– one position back (the last position closed);
– two positions back, etc.
ReturnReturns a numerical value, indicating the price at a complete exit from the specified position.
APIPosTradeExitPrice(PosAgo, TradeNumber)
Explanation– Returns an absolute numerical value, indicating the execution price of trade exit order.
– This function can only be used in signals.
PosAgoor Not Specified- open position;
– one position back (the last position closed);
– two positions back, etc.
TradeNumbera numerical expression, specifying the number of trade (zero-based).
ReturnReturns an absolute numerical value, indicating the execution price of trade exit order.
ExampleWill return a value of the second closed trade of the open position.
PosTradeExitPrice(0, 1);

Unrealized and Realized Profits

APIOpenPositionProfit
說明– 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.
APIPositionProfit(PosBack)
說明– Returns a numerical value, indicating the total realized profit or loss for the specified closed position.
– This function can only be used in signals.
PosBackor Not Specified- open position;
– one position back (the last position closed);
– 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

Leave a Reply