From 668b822e66f29da45daad9299f413f640b9213b2 Mon Sep 17 00:00:00 2001 From: nickbair Date: Wed, 23 Oct 2024 16:43:33 -0600 Subject: [PATCH 1/2] Fix close position and other cast issues --- lib/code_gen/code_generator.ex | 1 + ...guaranteed_stop_loss_order_reject_transaction.ex | 3 +-- .../guaranteed_stop_loss_order_transaction.ex | 3 +-- .../transaction/market_order_reject_transaction.ex | 13 +------------ .../transaction/market_order_transaction.ex | 13 +------------ ...r_client_extensions_modify_reject_transaction.ex | 2 -- .../order_client_extensions_modify_transaction.ex | 4 +--- .../stop_loss_order_reject_transaction.ex | 3 +-- .../transaction/stop_loss_order_transaction.ex | 3 +-- .../take_profit_order_reject_transaction.ex | 3 +-- .../transaction/take_profit_order_transaction.ex | 3 +-- ...e_client_extensions_modify_reject_transaction.ex | 1 - .../trade_client_extensions_modify_transaction.ex | 3 +-- .../trailing_stop_loss_order_reject_transaction.ex | 3 +-- .../trailing_stop_loss_order_transaction.ex | 3 +-- lib/models/request/positions/close_position.ex | 12 ++++++++++-- 16 files changed, 23 insertions(+), 50 deletions(-) diff --git a/lib/code_gen/code_generator.ex b/lib/code_gen/code_generator.ex index 3ee1336..f6c8b06 100644 --- a/lib/code_gen/code_generator.ex +++ b/lib/code_gen/code_generator.ex @@ -270,6 +270,7 @@ defmodule ExOanda.CodeGenerator do |> to_camel() |> Enum.into(%{}) |> NestedFilter.drop_by_value([nil]) + |> IO.inspect end @doc false diff --git a/lib/models/definitions/transaction/guaranteed_stop_loss_order_reject_transaction.ex b/lib/models/definitions/transaction/guaranteed_stop_loss_order_reject_transaction.ex index 87e311c..c3a8adf 100644 --- a/lib/models/definitions/transaction/guaranteed_stop_loss_order_reject_transaction.ex +++ b/lib/models/definitions/transaction/guaranteed_stop_loss_order_reject_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/guaranteed_stop_loss_order_transaction.ex b/lib/models/definitions/transaction/guaranteed_stop_loss_order_transaction.ex index 0a48029..473bdf8 100644 --- a/lib/models/definitions/transaction/guaranteed_stop_loss_order_transaction.ex +++ b/lib/models/definitions/transaction/guaranteed_stop_loss_order_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/market_order_reject_transaction.ex b/lib/models/definitions/transaction/market_order_reject_transaction.ex index 0bbeb09..a619eb1 100644 --- a/lib/models/definitions/transaction/market_order_reject_transaction.ex +++ b/lib/models/definitions/transaction/market_order_reject_transaction.ex @@ -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) diff --git a/lib/models/definitions/transaction/market_order_transaction.ex b/lib/models/definitions/transaction/market_order_transaction.ex index 0900e60..6cf31ed 100644 --- a/lib/models/definitions/transaction/market_order_transaction.ex +++ b/lib/models/definitions/transaction/market_order_transaction.ex @@ -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) diff --git a/lib/models/definitions/transaction/order_client_extensions_modify_reject_transaction.ex b/lib/models/definitions/transaction/order_client_extensions_modify_reject_transaction.ex index cb83a0c..9a7c3c6 100644 --- a/lib/models/definitions/transaction/order_client_extensions_modify_reject_transaction.ex +++ b/lib/models/definitions/transaction/order_client_extensions_modify_reject_transaction.ex @@ -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 ]) diff --git a/lib/models/definitions/transaction/order_client_extensions_modify_transaction.ex b/lib/models/definitions/transaction/order_client_extensions_modify_transaction.ex index 310a945..b43240e 100644 --- a/lib/models/definitions/transaction/order_client_extensions_modify_transaction.ex +++ b/lib/models/definitions/transaction/order_client_extensions_modify_transaction.ex @@ -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) diff --git a/lib/models/definitions/transaction/stop_loss_order_reject_transaction.ex b/lib/models/definitions/transaction/stop_loss_order_reject_transaction.ex index 4e367ca..3988a17 100644 --- a/lib/models/definitions/transaction/stop_loss_order_reject_transaction.ex +++ b/lib/models/definitions/transaction/stop_loss_order_reject_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/stop_loss_order_transaction.ex b/lib/models/definitions/transaction/stop_loss_order_transaction.ex index 113a8e7..b2991de 100644 --- a/lib/models/definitions/transaction/stop_loss_order_transaction.ex +++ b/lib/models/definitions/transaction/stop_loss_order_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/take_profit_order_reject_transaction.ex b/lib/models/definitions/transaction/take_profit_order_reject_transaction.ex index cd989a8..1b8ee69 100644 --- a/lib/models/definitions/transaction/take_profit_order_reject_transaction.ex +++ b/lib/models/definitions/transaction/take_profit_order_reject_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/take_profit_order_transaction.ex b/lib/models/definitions/transaction/take_profit_order_transaction.ex index 9ae7705..7875b50 100644 --- a/lib/models/definitions/transaction/take_profit_order_transaction.ex +++ b/lib/models/definitions/transaction/take_profit_order_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/trade_client_extensions_modify_reject_transaction.ex b/lib/models/definitions/transaction/trade_client_extensions_modify_reject_transaction.ex index d1c8fd0..add4dbb 100644 --- a/lib/models/definitions/transaction/trade_client_extensions_modify_reject_transaction.ex +++ b/lib/models/definitions/transaction/trade_client_extensions_modify_reject_transaction.ex @@ -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) diff --git a/lib/models/definitions/transaction/trade_client_extensions_modify_transaction.ex b/lib/models/definitions/transaction/trade_client_extensions_modify_transaction.ex index 4db902b..775d909 100644 --- a/lib/models/definitions/transaction/trade_client_extensions_modify_transaction.ex +++ b/lib/models/definitions/transaction/trade_client_extensions_modify_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/trailing_stop_loss_order_reject_transaction.ex b/lib/models/definitions/transaction/trailing_stop_loss_order_reject_transaction.ex index 28d3296..2e72e02 100644 --- a/lib/models/definitions/transaction/trailing_stop_loss_order_reject_transaction.ex +++ b/lib/models/definitions/transaction/trailing_stop_loss_order_reject_transaction.ex @@ -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 diff --git a/lib/models/definitions/transaction/trailing_stop_loss_order_transaction.ex b/lib/models/definitions/transaction/trailing_stop_loss_order_transaction.ex index 4fa7aea..6fecb2b 100644 --- a/lib/models/definitions/transaction/trailing_stop_loss_order_transaction.ex +++ b/lib/models/definitions/transaction/trailing_stop_loss_order_transaction.ex @@ -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 diff --git a/lib/models/request/positions/close_position.ex b/lib/models/request/positions/close_position.ex index 633af1a..286dc65 100644 --- a/lib/models/request/positions/close_position.ex +++ b/lib/models/request/positions/close_position.ex @@ -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 @@ -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 From 868cb6499375aaaaefc033397316b7af3b6fb534 Mon Sep 17 00:00:00 2001 From: nickbair Date: Wed, 23 Oct 2024 16:44:46 -0600 Subject: [PATCH 2/2] Clean up --- lib/code_gen/code_generator.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/code_gen/code_generator.ex b/lib/code_gen/code_generator.ex index f6c8b06..3ee1336 100644 --- a/lib/code_gen/code_generator.ex +++ b/lib/code_gen/code_generator.ex @@ -270,7 +270,6 @@ defmodule ExOanda.CodeGenerator do |> to_camel() |> Enum.into(%{}) |> NestedFilter.drop_by_value([nil]) - |> IO.inspect end @doc false