Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix close position and other cast issues #31

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ defmodule ExOanda.GuaranteedStopLossOrderRejectTransaction do
:reason,
:order_fill_transaction_id,
:intended_replaces_order_id,
:reject_reason,
:client_extensions
:reject_reason
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ defmodule ExOanda.GuaranteedStopLossOrderTransaction do
:reason,
:order_fill_transaction_id,
:replaces_order_id,
:cancelling_transaction_id,
:client_extensions
:cancelling_transaction_id
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,7 @@ defmodule ExOanda.MarketOrderRejectTransaction do
:time_in_force,
:price_bound,
:position_fill,
:reason,
:trade_close,
:long_position_closeout,
:short_position_closeout,
:margin_closeout,
:delayed_trade_close,
:client_extensions,
:take_profit_on_fill,
:stop_loss_on_fill,
:trailing_stop_loss_on_fill,
:guaranteed_stop_loss_on_fill,
:trade_client_extensions
:reason
])
|> cast_embed(:trade_close)
|> cast_embed(:long_position_closeout)
Expand Down
13 changes: 1 addition & 12 deletions lib/models/definitions/transaction/market_order_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,7 @@ defmodule ExOanda.MarketOrderTransaction do
:time_in_force,
:price_bound,
:position_fill,
:reason,
:trade_close,
:long_position_closeout,
:short_position_closeout,
:margin_closeout,
:delayed_trade_close,
:client_extensions,
:take_profit_on_fill,
:stop_loss_on_fill,
:trailing_stop_loss_on_fill,
:guaranteed_stop_loss_on_fill,
:trade_client_extensions
:reason
])
|> cast_embed(:trade_close)
|> cast_embed(:long_position_closeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ defmodule ExOanda.OrderClientExtensionsModifyRejectTransaction do
:type,
:order_id,
:client_order_id,
:trade_client_extensions_modify,
:order_client_extensions_modify,
:transaction_reject_reason,
:reject_reason
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ defmodule ExOanda.OrderClientExtensionsModifyTransaction do
:request_id,
:type,
:order_id,
:client_order_id,
:trade_client_extensions_modify,
:order_client_extensions_modify
:client_order_id
])
|> cast_embed(:trade_client_extensions_modify)
|> cast_embed(:order_client_extensions_modify)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ defmodule ExOanda.StopLossOrderRejectTransaction do
:reason,
:order_fill_transaction_id,
:intended_replaces_order_id,
:reject_reason,
:client_extensions
:reject_reason
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ defmodule ExOanda.StopLossOrderTransaction do
:reason,
:order_fill_transaction_id,
:replaces_order_id,
:cancelling_transaction_id,
:client_extensions
:cancelling_transaction_id
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ defmodule ExOanda.TakeProfitOrderRejectTransaction do
:reason,
:order_fill_transaction_id,
:intended_replaces_order_id,
:reject_reason,
:client_extensions
:reject_reason
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ defmodule ExOanda.TakeProfitOrderTransaction do
:order_fill_transaction_id,
:replaces_order_id,
:cancelling_transaction_id,
:reject_reason,
:client_extensions
:reject_reason
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ defmodule ExOanda.TradeClientExtensionsModifyRejectTransaction do
:type,
:trade_id,
:client_trade_id,
:trade_client_extensions_modify,
:reject_reason
])
|> cast_embed(:trade_client_extensions_modify)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ defmodule ExOanda.TradeClientExtensionsModifyTransaction do
:request_id,
:type,
:trade_id,
:client_trade_id,
:trade_client_extensions_modify
:client_trade_id
])
|> cast_embed(:trade_client_extensions_modify)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ defmodule ExOanda.TrailingStopLossOrderRejectTransaction do
:reason,
:order_fill_transaction_id,
:intended_replaces_order_id,
:reject_reason,
:client_extensions
:reject_reason
])
|> cast_embed(:client_extensions)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ defmodule ExOanda.TrailingStopLossOrderTransaction do
:reason,
:order_fill_transaction_id,
:replaces_order_id,
:cancelling_transaction_id,
:client_extensions
:cancelling_transaction_id
])
|> cast_embed(:client_extensions)
end
Expand Down
12 changes: 10 additions & 2 deletions lib/models/request/positions/close_position.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ defmodule ExOanda.Request.ClosePosition do
@primary_key false

typed_embedded_schema do
field(:long_units, CloseoutUnits, default: "ALL")
field(:short_units, CloseoutUnits, default: "ALL")
field(:long_units, CloseoutUnits)
field(:short_units, CloseoutUnits)

embeds_one :long_client_extensions, ClientExtensions
embeds_one :short_client_extensions, ClientExtensions
Expand All @@ -29,5 +29,13 @@ defmodule ExOanda.Request.ClosePosition do
])
|> cast_embed(:long_client_extensions)
|> cast_embed(:short_client_extensions)
|> validate_required_one_of([:long_units, :short_units])
end

defp validate_required_one_of(changeset, fields) do
case Enum.any?(fields, fn field -> get_field(changeset, field) != nil end) do
true -> changeset
false -> add_error(changeset, hd(fields), "at least one of #{inspect(fields)} must be present")
end
end
end
Loading