You are here: Trading System Programming > Reference > Classes > Position > Position Methods > getPrice

getPrice

The getPrice method returns the purchase price for position.

Syntax

var getPrice();

Return Value

This method returns a floating value representing original purchase price.

Example

The following example demonstrates the use of getPrice() method. The code attempts to close all losing positions.

 

function start()

{

//close all losing positions first

closeAllLosingPositions();

 

//continue on...

}

 

function closeAllLosingPositions()

{

//retrieve all long positions for current symbol

var positions = getAccount().getPositions(getSymbol(), POSITIONTYPE_LONG);

 

//if no long positions exist for current symbol, return

if(positions.length == 0)

return;

 

//get current market price from the last bar in time series object

var timeSeries = getTimeSeries(getSymbol());

var lastPrice = timeSeries.getClose(timeSeries.getCount() - 1);

 

//loop through all positions, and close losing ones

for(var i = 0; i < positions.length; i++)

{

var position = positions[i];

 

//if lastPrice < position's purchase price, then this position is losing

if(lastPrice < position.getPrice())

{

//place sell order

account.placeMarketOrder(position.getSymbol(),

position.getQuantity(),

ACTIONTYPE_BUYTOCOVER,

TIFTYPE_GTC);

}

}

}

See Also

start(), getAccount(), Account class, getTimeSeries(), TimeSeries class

 


Copyright © 2006-2009 ActiveTick LLC