-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
create order position with takeprofit stoploss
github-actions[bot] edited this page Jun 11, 2026
·
1 revision
using ccxt;
using ccxt.pro;
namespace examples;
partial class Examples
{
async public Task createOrderPositionWithTakeprofitStoploss()
{
Console.WriteLine("CCXT Version:" + ccxt.Exchange.ccxtVersion);
var exchange = new ccxt.okx(new Dictionary<string, object>() {
{ "apiKey", "YOUR_API_KEY" },
{ "secret", "YOUR_API_SECRET" },
{ "password", "YOUR_API_PASSWORD" },
});
var symbol = "DOGE/USDT:USDT";
var side = "buy"; // 'buy' | 'sell'
var orderType = "limit"; // 'market' | 'limit'
var amount = 1; // how many contracts (see `market(symbol).contractSize` to find out coin portion per one contract)
await exchange.LoadMarkets();
var market = exchange.Market(symbol);
var ticker = await exchange.FetchTicker(symbol);
var lastPrice = ticker.last;
var askPrice = ticker.ask;
var bidPrice = ticker.bid;
// if order_type is 'market', then price is not needed
double? price = null;
// if order_type is 'limit', then set a price at your desired level
if (orderType == "limit")
{
price = (side == "buy") ? bidPrice * 0.95 : askPrice * 1.05; // i.e. 5% from current price
}
// set trigger price for stop-loss/take-profit to 2% from current price
// (note, across different exchanges "trigger" price can be also mentioned with different synonyms, like "activation price", "stop price", "conditional price", etc. )
var slTriggerPrice = (orderType == "market" ? lastPrice : price) * (side == "buy" ? 0.98 : 1.02);
var tkTriggerPrice = (orderType == "market" ? lastPrice : price) * (side == "buy" ? 1.02 : 0.98);
// when symbol's price reaches your predefined "trigger price", stop-loss order would be activated as a "market order". but if you want it to be activated as a "limit order", then set a 'price' parameter for it
var parameters = new Dictionary<string, object>() {
{ "stopLoss", new Dictionary<string, object>() {
{ "triggerPrice", slTriggerPrice },
{ "price", slTriggerPrice * 0.98 },
}},
{ "takeProfit", new Dictionary<string, object>() {
{ "triggerPrice", tkTriggerPrice },
{ "price", tkTriggerPrice * 0.98 },
}},
};
var positionAmount = market.contractSize * amount;
var positionValue = positionAmount * lastPrice;
try
{
var createOrder = await exchange.CreateOrder(symbol, orderType, side, amount, price, parameters);
Console.WriteLine("Created an order" + createOrder.id);
// Fetch all your open orders for this symbol
// - use 'fetchOpenOrders' or 'fetchOrders' and filter with 'open' status
// - note, that some exchanges might return one order object with embedded stoploss/takeprofit fields, while other exchanges might have separate stoploss/takeprofit order objects
var openOrders = await exchange.FetchOpenOrders(symbol);
Console.WriteLine("Fetched all your orders for this symbol");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}(If the page is not being rendered for you, you can refer to the mirror at https://docs.ccxt.com/)
- Install
- Examples
- Manual
- CCXT Pro
- Contributing
- Supported Exchanges
- Exchanges By Country
- API Spec By Method
- FAQ
- Changelog
- Awesome
- API Spec by Exchange
- fetchCurrencies
- alpaca
- apex
- ascendex
- aster
- backpack
- bigone
- binance
- bingx
- bit2c
- bitbank
- bitbns
- bitfinex
- bitflyer
- bitget
- bithumb
- bitmart
- bitmex
- bitopro
- bitrue
- bitso
- bitstamp
- bitteam
- bittrade
- bitvavo
- blockchaincom
- blofin
- btcbox
- btcmarkets
- btcturk
- bullish
- bybit
- bydfi
- cex
- coinbase
- coinbaseexchange
- coinbaseinternational
- coincheck
- coinex
- coinmate
- coinmetro
- coinone
- coinsph
- coinspot
- cryptocom
- cryptomus
- deepcoin
- delta
- deribit
- derive
- digifinex
- dydx
- exmo
- extended
- foxbit
- gate
- gemini
- grvt
- hashkey
- hibachi
- hitbtc
- hollaex
- htx
- hyperliquid
- independentreserve
- indodax
- kraken
- krakenfutures
- kucoin
- fetchBidsAsks
- latoken
- lbank
- lighter
- luno
- mercado
- mexc
- modetrade
- ndax
- novadax
- okx
- onetrading
- p2b
- pacifica
- paradex
- paymium
- phemex
- poloniex
- tokocrypto
- toobit
- upbit
- weex
- whitebit
- woo
- woofipro
- xt
- zaif
- fetchStatus