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
- Python
- Node.js
- C#
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
Please refer to the Python Libary for placing orders for whole shares and odd lots
const { FubonSDK, BSAction, TimeInForce, OrderType, PriceType, MarketType } = require('fubon-neo');
const sdk = new FubonSDK();
const accounts = sdk.login("Your ID","Your password", "Your cert path", "Your cert password");
const order = {
buySell: BSAction.Buy,
symbol: "2881",
price: "66",
quantity: 2000,
marketType: MarketType.Common,
priceType: PriceType.Limit,
timeInForce: TimeInForce.ROD,
orderType: OrderType.Stock,
userDef: "from Js"
};
sdk.stock.placeOrder(accounts.data[0],order);
Please refer to the Node.Js Libary for placing orders for whole shares and odd lots
using FubonNeo.Sdk;
var sdk = new FubonSDK();
var accounts = sdk.Login("Your ID","Your password", "Your cert path", "Your cert password"); // If there is consolidation, multiple account information will be returned
var order = new Order(
BsAction.Buy,
"2881",
"66",
2000,
MarketType.Common,
PriceType.Limit,
TimeInForce.ROD,
OrderType.Stock,
null
);
sdk.Stock.PlaceOrder(accounts.data[0],order); // To place an block order
Please refer to the C# 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:
- Python
- Node.js
- C#
orderResults = sdk.stock.get_order_results(accounts.data[0])
print(orderResults)
const orderResults = sdk.stock.getOrderResults(accounts.data[0])
console.log(orderResults)
var orderResults = sdk.Stock.GetOrderResults(accounts.data[0]);
foreach (var results in orderResults)
{
Console.WriteLine(results);
}
Based on the result, we can determine whether this order has been executed and the quantity that has been filled:
- Python
- Node.js
- C#
[
{
...
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)
...
}
]
[
{
...
"buySell": "Buy", // Transaction direction. (string)
"price": 66, //The original order price (number)
"quantity": 2000, //The original order quantity (number)
"afterPrice": 66, //The Valid order price (number)
"afterQty": 2000, //The Valid order quantity (number)
"filledQty": 0, //Filled quantity (number)
"filledMoney": 0, //Filled Vaule (number)
"symbol": "2881", //symbol (string)
"orderNo": "bA888", //The order number (string)
"lastTime": "10:10:10.123", //The last modification time (string)
...
}
]
{
...
buySell = Buy, // Transaction direction. (BSAction)
price = 66, //The original order price (double)
quantity = 2000, //The original order quantity (int)
afterPrice = 66, //The Valid order price (double)
afterQty = 2000, //The Valid order quantity (int)
filledQty = 0, //Filled quantity (int)
filledMoney = 0, //Filled Vaule (int)
symbol = "2881", //symbol (string)
orderNo = "bA888", //The order number(string)
lastTime = "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:
- Python
- Node.js
- C#
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)
orderResults = sdk.stock.getOrderResults(accounts.data[0])
const modified_pirce = sdk.stock.makeModifyPriceObj(orderResults.data[0],"66.5")
sdk.stock.modifyPrice(accounts.data[0],modified_pirce)
orderResults = sdk.Stock.GetOrderResults(accounts.data[0]);
var modified_pirce = sdk.Stock.MakeModifyPriceObj(orderResults.data[0],"66.5");
sdk.Stock.ModifyPrice(accounts.data[0],modified_pirce);
"A few minutes later, we queried the order status again and found that it had been executed.
- Python
- Node.js
- C#
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)
...
}
]
orderResults = sdk.stock.getOrderResults(accounts.data[0])
console.log(orderResults.data[0])
{
...
"buySell" : "Buy", // Transaction direction. (BSAction)
"price" : 66, //The original order price (number)
"quantity" : 2000, //The original order quantity (number)
"afterPrice" : 66.5, //The Valid order price (number)
"afterQty" : 1000, //The Valid order quantity (number)
"filledQty" : 1000, //Filled quantity (int)
"filledMoney" : 66000, //Filled Vaule (int)
"stockNo" : "2881", //symbol (string)
"orderNo" : "bA888", //The order number (string)
"lastTime" : "10:13:12.123", //The last modification time (string)
...
}
orderResults = sdk.Stock.GetOrderResults(accounts.data[0]);
Console.WriteLine(orderResults.data[0]);
{
...
buySell = Buy, // Transaction direction. (BSAction)
price = 66, //The original order price (double)
quantity = 2000, //The original order quantity (int)
afterPrice = 66.5, //The Valid order price (double)
afterQty = 1000, //The Valid order quantity (int)
filledQty = 1000, //Filled quantity (int)
filledMoney = 66000, //Filled Vaule (int)
stockNo = "2881", //symbol (string)
orderNo = "bA888", //The order number (string)
lastTime = "10:13:12.123", //The last modification time (string)
...
}
Sell Position
Before the close, we decided to sell 1000 share of Fubon Financial Holdings.
- Python
- Node.js
- C#
#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
order = {
buySell: BSAction.Sell,
symbol: "2881",
price: "66",
quantity: 1000,
marketType: MarketType.Common,
priceType: PriceType.Limit,
timeInForce: TimeInForce.ROD,
orderType: OrderType.Stock,
userDef: "from Js"
};
sdk.stock.placeOrder(accounts.data[0],order)
order = new Order(
BsAction.Sell,
"2881",
"66",
1000,
MarketType.Common,
PriceType.Limit,
TimeInForce.ROD,
OrderType.Stock,
null
);
sdk.Stock.PlaceOrder(accounts.data[0],order); // To place an block order