Skip to content

Commit

Permalink
Update unrealized_pnl based on closing_price if known
Browse files Browse the repository at this point in the history
If we know the `closing_price` then it is used for updating the unrealized pnl.
Note that the realized pnl is *only* set once we update it based on the closed contract. The realized pnl is not updated based on the execution price, because there is no execution guarantee.
  • Loading branch information
da-kami committed Jul 21, 2023
1 parent 6d15d74 commit 0691230
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE
"positions" DROP COLUMN "closing_price";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Your SQL goes here
ALTER TABLE
positions
ADD
COLUMN "closing_price" REAL;
4 changes: 4 additions & 0 deletions coordinator/src/db/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Position {
pub temporary_contract_id: Option<String>,
pub realized_pnl: Option<i64>,
pub unrealized_pnl: Option<i64>,
pub closing_price: Option<f32>,
}

impl Position {
Expand Down Expand Up @@ -92,12 +93,14 @@ impl Position {
pub fn set_open_position_to_closing(
conn: &mut PgConnection,
trader_pubkey: String,
closing_price: f32,
) -> Result<()> {
let effected_rows = diesel::update(positions::table)
.filter(positions::trader_pubkey.eq(trader_pubkey.clone()))
.filter(positions::position_state.eq(PositionState::Open))
.set((
positions::position_state.eq(PositionState::Closing),
positions::closing_price.eq(Some(closing_price)),
positions::update_timestamp.eq(OffsetDateTime::now_utc()),
))
.execute(conn)?;
Expand Down Expand Up @@ -178,6 +181,7 @@ impl From<Position> for crate::position::models::Position {
temporary_contract_id: value.temporary_contract_id.map(|contract_id| {
ContractId::from_hex(contract_id.as_str()).expect("contract id to decode")
}),
closing_price: value.closing_price,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ impl Node {
db::positions::Position::set_open_position_to_closing(
&mut connection,
position.trader.to_string(),
closing_price
.to_f32()
.expect("Closing price to fit into f32"),
)
}

Expand Down
13 changes: 9 additions & 4 deletions coordinator/src/node/unrealized_pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ fn sync_position(
position: &Position,
quote: Quote,
) -> Result<()> {
let current_price = match position.direction {
trade::Direction::Long => quote.bid_price,
trade::Direction::Short => quote.ask_price,
let closing_price = match position.closing_price {
None => match position.direction {
trade::Direction::Long => quote.bid_price,
trade::Direction::Short => quote.ask_price,
},
Some(closing_price) => {
Decimal::try_from(closing_price).expect("f32 closing price to fit into decimal")
}
};

let average_entry_price = Decimal::try_from(position.average_entry_price)
Expand All @@ -53,7 +58,7 @@ fn sync_position(

let pnl = calculate_pnl(
average_entry_price,
current_price,
closing_price,
position.quantity,
long_leverage,
short_leverage,
Expand Down
1 change: 1 addition & 0 deletions coordinator/src/position/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ pub struct Position {
/// This field is optional for backwards compatibility because we cannot deterministically
/// associate already existing contracts with positions.
pub temporary_contract_id: Option<ContractId>,
pub closing_price: Option<f32>,
}
1 change: 1 addition & 0 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ diesel::table! {
temporary_contract_id -> Nullable<Text>,
realized_pnl -> Nullable<Int8>,
unrealized_pnl -> Nullable<Int8>,
closing_price -> Nullable<Float4>,
}
}

Expand Down

0 comments on commit 0691230

Please sign in to comment.