Skip to main content

Trade


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

Place Order

We want to buy 2000 share of Fubon Financial Holdings at a price of 66.00, you can execute program as the example

from fubon_neo.sdk import FubonSDK, Order
from fubon_neo.constant import TimeInForce, OrderType, PriceType, MarketType, 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 = Order(
buy_sell = BSAction.Buy,
symbol = "2881",
price = "66",
quantity = 2000,
market_type = MarketType.Common,
price_type = PriceType.Limit,
time_in_force= TimeInForce.ROD,
order_type = OrderType.Stock,
user_def = "From_Py" # optional field
)


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

Please refer to the Python Libary for placing orders for whole shares and odd lots

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:

orderResults = sdk.stock.get_order_results(accounts.data[0])
print(orderResults)

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

[
{
...
buy_sell: Buy, # Transaction direction. (BSAction)
price: 66, #The original order price (float)
quantity: 2000, #The original order quantity (int)
after_price: 66, #The Valid order price (float)
after_qty: 2000, #The Valid order quantity (int)
filled_qty: 0, #Filled quantity (int)
filled_money: 0, #Filled Vaule (int)
stock_no: "2881", #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 66.50 for the purchase:

orderResults = sdk.stock.get_order_results(accounts.data[0])

modified_pirce = sdk.stock.make_modify_price_obj(orderResults.data[0],"66.5")
sdk.stock.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.stock.get_order_results(accounts.data[0])
print(orderResults.data[0])
[
{
...
buy_sell: Buy, # Transaction direction. (BSAction)
price: 66, #The original order price (number)
quantity: 2000, #The original order quantity (number)
after_price: 66.5, #The Valid order price (number)
after_qty: 1000, #The Valid order quantity (number)
filled_qty: 1000, #Filled quantity (number)
filled_money: 66000, #Filled Vaule (number)
stock_no: "2881", #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 1000 share of Fubon Financial Holdings.

#Create order object
order = Order(
buy_sell = BSAction.Sell,
symbol = "2881",
price = "67",
quantity = 1000,
market_type = MarketType.Common,
price_type = PriceType.Limit,
time_in_force= TimeInForce.ROD,
order_type = OrderType.Stock,
user_def = "From_Py" # optional field
)


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