question for hootnot 2 #200
-
CONTEXT
QUESTIONDo you know any tricks to test which trade is "allowed" to be closed per US FIFO law? Or stated differently, given a list of trades, can we determine which ones are NOT eligible to be closed? I understand I could query for the lowest trade_id among such a group, and that should, by definition, work. But I thought maybe you might know a better way. This is all the info I see when I query for a trade... nothing lends itself to "can be closed", or im just blind
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
personally I use PositionClose and close a pos. completely regardless how many trades it took to build the pos. regarding your JSON response above: if a trade has the state OPEN that also says that it can be closed. Using TradeClose() with the trade id, you should be able to close a specific trade. There is also: positions.PositionDetails(accountID=accountID, instrument) it gives the tradeids involved regarding a position. In this example 1. But in your case you should see 144, 146, 148
|
Beta Was this translation helpful? Give feedback.
-
Since you like the Pydantic concept ... the code below will get you the firstToClose id, without having to check whether the pos. is on the long side or short side extending the above with:
this will consume the JSON response of PositionDetails() like:
The pydantic model can be extended to get the parameters you want as attributes of the model. To get your firstToClose id you can make use of validators, like:
With the response model code in responsemodel.py, you can test it using:
|
Beta Was this translation helpful? Give feedback.
personally I use PositionClose and close a pos. completely regardless how many trades it took to build the pos.
regarding your JSON response above: if a trade has the state OPEN that also says that it can be closed.
Using TradeClose() with the trade id, you should be able to close a specific trade.
There is also: positions.PositionDetails(accountID=accountID, instrument)
https://oanda-api-v20.readthedocs.io/en/latest/endpoints/positions/positiondetails.html
it gives the tradeids involved regarding a position. In this example 1. But in your case you should see 144, 146, 148