Library "Position"
Allows for simulating trades within an indicator.
newTrade(size, price, timestamp)
Creates a new trade object.
Parameters:
size: The size of the trade (number of shares or contracts).
price: The price at which the trade took place.
timestamp: The timestamp of the trade. Defaults to the current time.
Returns: A new trade object.
start(size, price, timestamp)
Starts a new position.
Parameters:
size: The size of the position (number of shares or contracts).
price: The price at which the position was started.
timestamp: The timestamp of the start of the position. Defaults to the current time.
Returns: A new position object.
trade(pos, size, price, timestamp)
Modifies an existing position.
Parameters:
pos: The position to be modified.
size: The size of the trade (number of shares or contracts).
price: The price at which the trade took place.
timestamp: The timestamp of the trade. Defaults to the current time.
Returns: The modified position object.
exit(pos, price, timestamp)
Closes a position by trading the entire position size at a given price and timestamp.
Parameters:
pos: The position being closed.
price: The price at which the position is being closed.
timestamp: The timestamp of the trade, defaults to the current time.
Returns: The updated position after the trade.
unrealized(pos, price)
Calculates the unrealized gain or loss for a given position and price.
Parameters:
pos: The position for which to calculate unrealized gain/loss.
price: The current market price.
Returns: The calculated unrealized gain or loss.
Trade
Represents a single trade.
Fields:
size: Size of the trade in units.
price: Price of the trade in currency.
value: Total value of the trade in currency units.
time: Timestamp of the trade.
Position
Represents a single position.
Fields:
size: Size of the position in units.
price: Average price of the position in currency.
value: Total value of the position in currency units.
start: Timestamp of the first trade that opened the position.
net: Realized gains and losses of the position in currency units.
history: Array of trades that make up the position.