Skip to main content

Trade


This tutorial will demonstrate how to complete the entire buying and selling process

Place Order

We want to buy 2 lot of TXF at a point of 20000, you can execute program as the example

from fubon_neo.sdk import FubonSDK, FutOptOrder
from fubon_neo.constant import TimeInForce, FutOptOrderType, FutOptPriceType, FutOptMarketType, BSAction

sdk = FubonSDK()

accounts = sdk.login("Your ID", "Your password", "Your Cert Path", "Your Cert Password") #If there is consolidation, multiple account information will be returned

#Create order object
order = FutOptOrder(
buy_sell = BSAction.Buy,
symbol = "TXFD4",
price = "20000",
lot = 2,
market_type = FutOptMarketType.Future,
price_type = FutOptPriceType.Limit,
time_in_force= TimeInForce.ROD,
order_type = FutOptOrderType.Auto,
user_def = "From_Py" # optional field
)


sdk.futopt.place_order(accounts.data[0], order) #place order

Confirm the order and trade notifications

If you want to confirm the status of that order, you can query the specified order using the example below: ( Need to specify the market type for the query. : Future, Option, FutureNight, OptionNight )

orderResults = sdk.futopt.get_order_results(accounts.data[0], FutOptMarketType.Future)
print(orderResults)

Based on the result, we can determine whether this order has been executed and the lot that has been filled:

[
{
...
buy_sell: Buy, #Transaction direction. (BSAction)
price: 20000, #The original order price (float)
lot: 2, #The original order lot (int)
after_price: 20000, #The Valid order price (float)
after_lot: 2, #The Valid order lot (int)
filled_lot: 0, #Filled lot (int)
filled_money: 0, #Filled Vaule (float)
symbol: "TXFD4", #symbol (string)
order_no: "bA888", #The order number(string)
last_time: "10:10:10.123", #The last modification time (string)
...
}
]

Modified order price

Since the original price was unable to execute, we adjusted the original order price and changed it to 20100 for the purchase:

orderResults = sdk.futopt.get_order_results(accounts.data[0], FutOptMarketType.Future)

modified_pirce = sdk.futopt.make_modify_price_obj(orderResults.data[0],"20100")
sdk.futopt.modify_price(accounts.data[0], modified_pirce)

"A few minutes later, we queried the order status again and found that it had been executed.

orderResults = sdk.futopt.get_order_results(accounts.data[0], FutOptMarketType.Future)
print(orderResults.data[0])
[
{
...
buy_sell: Buy, # Transaction direction. (BSAction)
price: 20000, #The original order price (number)
lot: 2, #The original order lot (number)
after_price: 20100, #The Valid order price (number)
after_lot: 2, #The Valid order lot (number)
filled_lot: 1, #Filled lot (number)
filled_money: 20100, #Filled Vaule (number)
symbol: "TXFD4", #symbol (string)
order_no: "bA888", #The order number (string)
last_time: "10:13:12.123", #The last modification time (string)
...
}
]

Sell Position

Before the close, we decided to Sell 1 lot of TXF.

#Create order object
order = FutOptOrder(
buy_sell = BSAction.Sell,
symbol = "TXFD4",
price = "20100",
lot = 1,
market_type = FutOptMarketType.Future,
price_type = FuturePriceType.Limit,
time_in_force= TimeInForce.ROD,
order_type = FutureOrderType.Auto,
user_def = "From_Py" # optional field
)


sdk.futopt.place_order(accounts.data[0], order) #Place Order