Skip to content

Commit 8f5353f

Browse files
committed
introduce optional take_profit and stop_loss fields for Order
1 parent be3f397 commit 8f5353f

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/order.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mutable struct Order{OData,IData}
66
const date::DateTime
77
const price::Price
88
const quantity::Quantity # negative = short selling
9+
take_profit::Price
10+
stop_loss::Price
911
metadata::OData
1012

1113
function Order(
@@ -15,9 +17,20 @@ mutable struct Order{OData,IData}
1517
price::Price,
1618
quantity::Quantity
1719
;
20+
take_profit::Price=Price(NaN),
21+
stop_loss::Price=Price(NaN),
1822
metadata::OData=nothing
1923
) where {OData,IData}
20-
new{OData,IData}(oid, inst, date, price, quantity, metadata)
24+
new{OData,IData}(
25+
oid,
26+
inst,
27+
date,
28+
price,
29+
quantity,
30+
take_profit,
31+
stop_loss,
32+
metadata,
33+
)
2134
end
2235
end
2336

@@ -27,10 +40,14 @@ end
2740

2841
function Base.show(io::IO, o::Order{O,I}) where {O,I}
2942
date_formatter = x -> Dates.format(x, "yyyy-mm-dd HH:MM:SS")
43+
tp_str = isnan(o.take_profit) ? "" : "$(format_quote(o.inst, o.take_profit)) $(o.inst.quote_symbol)"
44+
sl_str = isnan(o.stop_loss) ? "" : "$(format_quote(o.inst, o.stop_loss)) $(o.inst.quote_symbol)"
3045
print(io, "[Order] $(o.inst.symbol) " *
3146
"date=$(date_formatter(o.date)) " *
3247
"price=$(format_quote(o.inst, o.price)) $(o.inst.quote_symbol) " *
33-
"quantity=$(format_base(o.inst, o.quantity)) $(o.inst.base_symbol) ")
48+
"quantity=$(format_base(o.inst, o.quantity)) $(o.inst.base_symbol) " *
49+
"tp=$(tp_str) " *
50+
"sl=$(sl_str)")
3451
end
3552

3653
Base.show(order::Order{O,I}) where {O,I} = Base.show(stdout, order)

src/print.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function print_trades(
2828
Dict(:name => "Filled", :val => t -> t.fill_qty, :fmt => (t, v) -> format_base(t.order.inst, v)),
2929
# Dict(:name => "Remain. qty", :val => t -> t.remaining_qty, :fmt => (t, v) -> format_base(t.order.inst, v)),
3030
Dict(:name => "Price", :val => t -> t.fill_price, :fmt => (t, v) -> isnan(v) ? "" : format_quote(t.order.inst, v)),
31+
Dict(:name => "TP", :val => t -> t.order.take_profit, :fmt => (t, v) -> isnan(v) ? "" : format_quote(t.order.inst, v)),
32+
Dict(:name => "SL", :val => t -> t.order.stop_loss, :fmt => (t, v) -> isnan(v) ? "" : format_quote(t.order.inst, v)),
3133
Dict(:name => "Ccy", :val => t -> t.order.inst.quote_symbol, :fmt => (t, v) -> v),
3234
Dict(:name => "P&L", :val => t -> t.realized_pnl, :fmt => (t, v) -> isnan(v) ? "" : format_quote(t.order.inst, v)),
3335
Dict(:name => "Return", :val => t -> realized_return(t), :fmt => (t, v) -> @sprintf("%.2f%%", 100v)),

0 commit comments

Comments
 (0)