chronoxor / CppTrader

High performance components for building Trading Platform such as ultra fast matching engine, order book processor
MIT License
821 stars 254 forks source link

Pegged Orders #15

Open ghost opened 4 years ago

ghost commented 4 years ago

Is there support for pegged order types ? or support for limit orders that dynamically move with the best market quotes ?

chronoxor commented 4 years ago

Yes trailing orders are supported.

Trailing stop order - A trailing stop order is entered with a stop parameter
    that creates a moving or trailing activation price, hence the name. This parameter
    is entered as a percentage change or actual specific amount of rise (or fall) in the
    security price. Trailing stop sell orders are used to maximize and protect profit as
    a stock's price rises and limit losses when its price falls.
Trailing stop-limit order - A trailing stop-limit order is similar to a trailing
    stop order. Instead of selling at market price when triggered, the order becomes a limit
    order.
ghost commented 4 years ago

Makes sense, very nice. Thanks.

I think pegged orders are a bit different from trialing orders, no ?

ghost commented 4 years ago

I get a memory leak with the following example, using trailing stop limit sell:

CppTrader::Matching::MarketManager market;

// Prepare symbol & order book
const char name[8] = "test";
CppTrader::Matching::Symbol symbol{0, name};
market.AddSymbol(symbol);
market.AddOrderBook(symbol);

// Enable automatic matching
market.EnableMatching();

uint64_t order_id = 1;

// Add buy limit orders
market.AddOrder(CppTrader::Matching::Order::BuyLimit(order_id++, 0, 2521, 200));
market.AddOrder(CppTrader::Matching::Order::BuyLimit(order_id++, 0, 2519, 300));
market.AddOrder(CppTrader::Matching::Order::BuyLimit(order_id++, 0, 2515, 785));
market.AddOrder(CppTrader::Matching::Order::BuyLimit(order_id++, 0, 2510, 170));

// Add sell limit orders
market.AddOrder(CppTrader::Matching::Order::SellLimit(order_id++, 0, 2556, 300));
market.AddOrder(CppTrader::Matching::Order::SellLimit(order_id++, 0, 2555, 1000));
market.AddOrder(CppTrader::Matching::Order::SellLimit(order_id++, 0, 2535, 200));
market.AddOrder(CppTrader::Matching::Order::SellLimit(order_id++, 0, 2530, 150));

market.AddOrder(CppTrader::Matching::Order::TrailingSellStopLimit(9999, 0, 2500, 2520, 150, 1));
market.AddOrder(CppTrader::Matching::Order::BuyLimit(order_id++, 0, 2550, 500));

market.AddOrder(CppTrader::Matching::Order::SellLimit(order_id++, 0, 2529, 500));
Assertion failed: ((_allocated == 0) && "Memory leak detected! Allocated memory size must be zero!"), function clear, file allocator_pool.inl, line 338.

The leak seam to be caused because of the TrailingSellStopLimit and after the submission of the last SellLimit

ghost commented 4 years ago

Any suggestions on this ? Thank you

chronoxor commented 1 year ago

Visual Leak Detector read settings from: Z:\soft\Development\VisualLeakDetector\vld.ini Visual Leak Detector Version 2.5.4 installed.

Trading details Add symbol: Symbol(Id=0; Name="test ") Add order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=0; Asks=0; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) Add order: Order(Id=1; SymbolId=0; Type=LIMIT; Side=BUY; Price=2521; StopPrice=0; Quantity=200; ExecutedQuantity=0; LeavesQuantity=200; GTC) Add level: Level(Type=BID; Price=2521; TotalVolume=200; HiddenVolume=0; VisibleVolume=200; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=1; Asks=0; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) - Top of the book! Add order: Order(Id=2; SymbolId=0; Type=LIMIT; Side=BUY; Price=2519; StopPrice=0; Quantity=300; ExecutedQuantity=0; LeavesQuantity=300; GTC) Add level: Level(Type=BID; Price=2519; TotalVolume=300; HiddenVolume=0; VisibleVolume=300; Orders=1) Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=2; Asks=0; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) Add order: Order(Id=3; SymbolId=0; Type=LIMIT; Side=BUY; Price=2515; StopPrice=0; Quantity=785; ExecutedQuantity=0; LeavesQuantity=785; GTC) Add level: Level(Type=BID; Price=2515; TotalVolume=785; HiddenVolume=0; VisibleVolume=785; Orders=1) Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=3; Asks=0; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) Add order: Order(Id=4; SymbolId=0; Type=LIMIT; Side=BUY; Price=2510; StopPrice=0; Quantity=170; ExecutedQuantity=0; LeavesQuantity=170; GTC) Add level: Level(Type=BID; Price=2510; TotalVolume=170; HiddenVolume=0; VisibleVolume=170; Orders=1) Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=0; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) Add order: Order(Id=5; SymbolId=0; Type=LIMIT; Side=SELL; Price=2556; StopPrice=0; Quantity=300; ExecutedQuantity=0; LeavesQuantity=300; GTC) Add level: Level(Type=ASK; Price=2556; TotalVolume=300; HiddenVolume=0; VisibleVolume=300; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=1; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) - Top of the book! Add order: Order(Id=6; SymbolId=0; Type=LIMIT; Side=SELL; Price=2555; StopPrice=0; Quantity=1000; ExecutedQuantity=0; LeavesQuantity=1000; GTC) Add level: Level(Type=ASK; Price=2555; TotalVolume=1000; HiddenVolume=0; VisibleVolume=1000; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=2; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) - Top of the book! Add order: Order(Id=7; SymbolId=0; Type=LIMIT; Side=SELL; Price=2535; StopPrice=0; Quantity=200; ExecutedQuantity=0; LeavesQuantity=200; GTC) Add level: Level(Type=ASK; Price=2535; TotalVolume=200; HiddenVolume=0; VisibleVolume=200; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=3; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) - Top of the book! Add order: Order(Id=8; SymbolId=0; Type=LIMIT; Side=SELL; Price=2530; StopPrice=0; Quantity=150; ExecutedQuantity=0; LeavesQuantity=150; GTC) Add level: Level(Type=ASK; Price=2530; TotalVolume=150; HiddenVolume=0; VisibleVolume=150; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=4; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0) - Top of the book! Add order: Order(Id=9999; SymbolId=0; Type=TRAILING-STOP-LIMIT; Side=SELL; Price=2520; StopPrice=2500; Quantity=150; ExecutedQuantity=0; LeavesQuantity=150; GTC; TrailingDistance=1; TrailingStep=0) Add order: Order(Id=9; SymbolId=0; Type=LIMIT; Side=BUY; Price=2550; StopPrice=0; Quantity=500; ExecutedQuantity=0; LeavesQuantity=500; GTC) Execute order: Order(Id=8; SymbolId=0; Type=LIMIT; Side=SELL; Price=2530; StopPrice=0; Quantity=150; ExecutedQuantity=0; LeavesQuantity=150; GTC) with price 2530 and quantity 150 Delete order: Order(Id=8; SymbolId=0; Type=LIMIT; Side=SELL; Price=2530; StopPrice=0; Quantity=150; ExecutedQuantity=150; LeavesQuantity=0; GTC) Delete level: Level(Type=ASK; Price=2530; TotalVolume=0; HiddenVolume=0; VisibleVolume=0; Orders=0) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=3; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=1) - Top of the book! Execute order: Order(Id=9; SymbolId=0; Type=LIMIT; Side=BUY; Price=2550; StopPrice=0; Quantity=500; ExecutedQuantity=0; LeavesQuantity=500; GTC) with price 2530 and quantity 150 Execute order: Order(Id=7; SymbolId=0; Type=LIMIT; Side=SELL; Price=2535; StopPrice=0; Quantity=200; ExecutedQuantity=0; LeavesQuantity=200; GTC) with price 2535 and quantity 200 Delete order: Order(Id=7; SymbolId=0; Type=LIMIT; Side=SELL; Price=2535; StopPrice=0; Quantity=200; ExecutedQuantity=200; LeavesQuantity=0; GTC) Delete level: Level(Type=ASK; Price=2535; TotalVolume=0; HiddenVolume=0; VisibleVolume=0; Orders=0) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=2; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=1) - Top of the book! Execute order: Order(Id=9; SymbolId=0; Type=LIMIT; Side=BUY; Price=2550; StopPrice=0; Quantity=500; ExecutedQuantity=150; LeavesQuantity=350; GTC) with price 2535 and quantity 200 Add level: Level(Type=BID; Price=2550; TotalVolume=150; HiddenVolume=0; VisibleVolume=150; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=5; Asks=2; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=1) - Top of the book! Update order: Order(Id=9999; SymbolId=0; Type=TRAILING-STOP-LIMIT; Side=SELL; Price=2554; StopPrice=2534; Quantity=150; ExecutedQuantity=0; LeavesQuantity=150; GTC; TrailingDistance=1; TrailingStep=0) Add order: Order(Id=10; SymbolId=0; Type=LIMIT; Side=SELL; Price=2529; StopPrice=0; Quantity=500; ExecutedQuantity=0; LeavesQuantity=500; GTC) Execute order: Order(Id=9; SymbolId=0; Type=LIMIT; Side=BUY; Price=2550; StopPrice=0; Quantity=500; ExecutedQuantity=350; LeavesQuantity=150; GTC) with price 2550 and quantity 150 Delete order: Order(Id=9; SymbolId=0; Type=LIMIT; Side=BUY; Price=2550; StopPrice=0; Quantity=500; ExecutedQuantity=500; LeavesQuantity=0; GTC) Delete level: Level(Type=BID; Price=2550; TotalVolume=0; HiddenVolume=0; VisibleVolume=0; Orders=0) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=2; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=1) - Top of the book! Execute order: Order(Id=10; SymbolId=0; Type=LIMIT; Side=SELL; Price=2529; StopPrice=0; Quantity=500; ExecutedQuantity=0; LeavesQuantity=500; GTC) with price 2550 and quantity 150 Add level: Level(Type=ASK; Price=2529; TotalVolume=350; HiddenVolume=0; VisibleVolume=350; Orders=1) - Top of the book! Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=3; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=1) - Top of the book! Update order: Order(Id=9999; SymbolId=0; Type=LIMIT; Side=SELL; Price=2554; StopPrice=0; Quantity=150; ExecutedQuantity=0; LeavesQuantity=150; GTC) Add level: Level(Type=ASK; Price=2554; TotalVolume=150; HiddenVolume=0; VisibleVolume=150; Orders=1) Update order book: OrderBook(Symbol=Symbol(Id=0; Name="test "); Bids=4; Asks=4; BuyStop=0; SellStop=0; TrailingBuyStop=0; TrailingSellStop=0)

No memory leaks detected. Visual Leak Detector is now exiting.

chronoxor commented 1 year ago

The issue is not reproducing on my side. Could you please check if it still actual on your side?