Open and Close a Trade with the OANDA API
Part of the galgoz series — article #2
Open and Close a Trade with the OANDA API
Part of the galgoz series — article #2
To view all the articles in chronological order, go to gzarruk.com/galgoz. Or download the latest version of the code from the galgoz repository.
Photo by Tonia Kraakman on Unsplash
This will be a short one. In the previous story I created methods in the galgoz class to fetch all available instruments and download candle data for an instrument.
[embed]Connect to OANDA and Fetch Data Part of the galgoz series — article #1medium.com
Now, it is time to develop and test methods to execute trades. Two methods were added to the class: create_order and close_trade.
def create_order(self, units: str, type: str = "MARKET"):
data = {
"order": {
"instrument": self.instrument,
"units": units,
"timeInForce": "FOK",
"type": type,
"positionFill": "DEFAULT",
}
}
r = orders.OrderCreate(accountID=self.account_id, data=data)
response = self.client.request(r)
return response
def close_trade(self, trade_id: str, units: str = "ALL"):
data = {
"units": units
}
r = trades.TradeClose(accountID=self.account_id, tradeID=trade_id, data=data)
response = self.client.request(r)
return response
And there is a notebook in the repo (open_and_close_trade.ipynb) with an example of how to use these methods.
Let’s try it out.
Open a Trade
To verify that order are being opened and closed I will use:
- OANDA’s web platform: to visualise the operation and trade execution
- JSON Formatter: To inspect the response from the API
After calling the Galgoz class, opening a trade is simple. By default we are trading GBP/JPY and market orders. The only thing we have to provide are the units to trade. If we want to place a LONG trade, units have to be positive, and negative for a SHORT position. Opening a trade for 100 unit of GBP/JPY looks like this:
from galgoz import Galgoz
gz=Galgoz()
res = gz.create_order(
units=100,
)
print(res)
There are many other parameters that can be added to the open order method but, staying true to laser focus and simplicity, I am doing the bare minimum to achieve the objective of these series. In the future, and only if necessary, complexity can be added to the methods.
In the trading platform we see a BUY trade has been opened, with a corresponding ticket number (328):

Inspecting the response we get a dictionary with a lot of information. Using the JSON formatter, and hiding most of the info for practical reasons, we see it is divided in three sections plus the lastTranscationID:
- orderCreateTransaction: with information about the order we submitted to the API.
- orderFillTranscation: with the information when the trade was created.
- relatedTransactionIDs: with the order IDs from the two previous fields

Close the Trade
To close the trade we only need the transaction ID, which is the one associated with the orderFillTranscation, 328. Just to double check, querying the response for the trade_id and the units, we get:
trade_id = res['orderFillTransaction']['id']
units = res['orderFillTransaction']['units']
print(f"Filled order id: {trade_id}")
print(f"Open units: {units}")
Filled order id: 328
Open units: 100
And to close the trade we call the galgoz method:
res = gz.close_trade(trade_id=trade_id, units=units)
Note that it is possible to do a partial close by setting units to a value lower than the current open units.
Printing the response for the close order call we get the same structure as the create order response. Expanding the order fill section we see in the highlighted section, that we did a market order close, for 100 units, and closed position 328.

Success … galgoz can now open and close trades in an algorithmic fashion!
I am ready to start working on developing a new trading strategy and backtesting it.
Visit us at *DataDrivenInvestor.com*
Subscribe to DDIntel *here*.
Join our creator ecosystem *here*.
DDI Official Telegram Channel: https://t.me/+tafUp6ecEys4YjQ1
Follow us on *LinkedIn, [Twitter](https://twitter.com/@DDInvestorHQ), [YouTube](https://www.youtube.com/c/datadriveninvestor), and [Facebook](https://www.facebook.com/datadriveninvestor)*.
메타데이터
- post_id
- 91cf5ec2d4fe
- slug
- open-and-close-a-trade-with-the-oanda-api-91cf5ec2d4fe
- url
- https://medium.datadriveninvestor.com/open-and-close-a-trade-with-the-oanda-api-91cf5ec2d4fe
- canonical_url
- https://medium.datadriveninvestor.com/open-and-close-a-trade-with-the-oanda-api-91cf5ec2d4fe
- author_url
- https://medium.com/@gustavozarruk
- status
- ok
- fetched_at
- 2026-07-21 13:44:03