Skip to content

Instantly share code, notes, and snippets.

@rajuashok
Last active January 22, 2020 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajuashok/c3bc21649e9fdbf9efa7124c85635dce to your computer and use it in GitHub Desktop.
Save rajuashok/c3bc21649e9fdbf9efa7124c85635dce to your computer and use it in GitHub Desktop.
Portfolio Positions Orders Trades
interface Trade {
id: string
security: Security
strategy: Strategy
legs: Legs[]
}
interface TradePosition {
id: string
portfolioId?: string // null until order FILLED
tradeId: string
contracts: number
startPrice: number
premiumCaptured: number
gainLoss: number
gainLossPct: number
fillDate: number
// rolled up values
isExiting: boolean // if SELECT * FROM trade_order WHERE trade_position_id = x AND status = EXIT_REQUEST
isClosed: boolean // if SELECT * FROM trade_order WHERE trade_position_id = x AND status = CLOSED
}
interface TradeOrder {
id: string
userId: string
tradePositionId: string
orderDate: number
status: TradeOrderStatus // {OPEN, FILLED, EXPIRED, EXIT_REQUEST, CLOSED}
expectedGain: number // non-nill for EXIT_REQUEST orders
}
interface Portfolio {
id: string
userId: string
cash: number
buyingPower: number
invested: number
// Calculate accountValue, gainLoss(Pct) on demand as they will change depending on performance of positions
}
@rajuashok
Copy link
Author

rajuashok commented Jan 22, 2020

Order Creation:

  • Create TradePosition to define position tied to a Trade and tied to user portfolioID and status = PENDING
  • Create Order in ViewTrade
  • Create TradeOrder referencing above Order (OrderRef) and TradePosition
  • Poll ViewTrade and update TradeOrder status with FILLED, EXPIRED
  • If FILLED, update TradePosition status = FILLED

Exiting TradePosition

  • Create Order in ViewTrade
  • Create TradeOrder referencing above Order (OrderRef) and TradePosition
  • Poll ViewTrade and update TradeOrder status with CLOSED or back to FILLED (if exit request didn't fill)
  • if CLOSED, update TradePosition with status = CLOSED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment