哪种数据结构和算法用于管理滑动止损系统?

huangapple go评论63阅读模式
英文:

Which data structure and algorithm used to manage a trailing stop loss system?

问题

抱歉给您带来不便,我想知道用于管理追踪止损系统的数据结构和算法是什么?
如果我们有一个实施追踪止损方法的系统,用于处理大量不同初始价格和价格振幅的用户订单,我们需要使用哪种数据结构来高效管理它?到目前为止,我考虑使用Map(key, value)数据结构。
其中key是初始价格,value是具有该价格的订单的List,但这种方式不能处理价格振幅,导致这种方式无效。
感谢并感激您所有的回复!

追踪止损的工作原理

英文:

Sorry for inconvenient, I wonder what data structure and algorithm used to manage a trailing stop loss system ?
If we have have a system which implements trailing stop loss method, to handle a large number of user orders with different initial prices and price amplitudes, we need to use which data structure to manage it efficiently ? What I am thinking so far is we use a Map(key, value) data structure.
Which the key is the initial price and value is a List of ordered with that price but that way can not handle the price amplitudes, lead to this way is useless.
Thanks and grateful for all of your replies !

How trailing stop loss work

答案1

得分: -2

一个可用于管理跟踪止损系统的数据结构是优先队列,也称为堆。优先队列是一种存储具有关联优先级的元素并允许高效检索具有最高(或最低)优先级元素的数据结构。

在跟踪止损系统的背景下,优先队列中的每个元素代表正在跟踪的股票或资产,其优先级由跟踪止损值确定。跟踪止损是一个动态值,会随着资产价格的变化而调整。

以下是如何使用优先队列来管理跟踪止损系统的方法:

  1. 定义数据结构:优先队列中的每个元素应包含以下信息:

    • 股票/资产符号或标识符
    • 股票/资产的当前价格
    • 股票/资产的跟踪止损值
  2. 初始化优先队列:创建一个空的优先队列。

  3. 插入股票/资产:每当向系统中添加新的股票/资产时,根据预定的公式或策略计算其初始跟踪止损值。创建包含必要信息的元素,并根据其跟踪止损值将其插入优先队列。具有最高跟踪止损值的元素将位于优先队列的前面。

  4. 更新跟踪止损值:随着股票/资产价格的变化,您需要相应地更新它们的跟踪止损值。从优先队列中检索具有最高优先级(最高跟踪止损值)的元素。根据更新后的价格计算新的跟踪止损值并更新优先队列中的元素。如果新的跟踪止损值高于下一个最高优先级元素的跟踪止损值,则调整元素的位置以反映新的优先级。

  5. 触发止损:每当股票/资产的价格达到或低于其跟踪止损值时,您可以触发该股票/资产的止损操作。

使用优先队列可以让您高效地跟踪和管理跟踪止损系统中的多个股票/资产。最关键的元素(最高跟踪止损值)始终位于队列的前面,因此很容易根据其优先级确定需要关注哪些股票/资产。

英文:

One data structure that can be used to manage a trailing stop loss system is a priority queue, also known as a heap. A priority queue is a data structure that stores elements with associated priorities and allows efficient retrieval of the element with the highest (or lowest) priority.

In the context of a trailing stop loss system, each element in the priority queue represents a stock or an asset being tracked, and the priority is determined by the trailing stop loss value. The trailing stop loss is a dynamic value that adjusts as the price of the asset changes.

Here's how you can use a priority queue to manage a trailing stop loss system:

  1. Define the data structure: Each element in the priority queue should contain the following information:

    • Stock/asset symbol or identifier
    • Current price of the stock/asset
    • Trailing stop loss value for the stock/asset
  2. Initialize the priority queue: Create an empty priority queue.

  3. Inserting stocks/assets: Whenever a new stock/asset is added to the system, calculate its initial trailing stop loss value based on a predefined formula or strategy. Create an element with the necessary information and insert it into the priority queue based on its trailing stop loss value. The element with the highest trailing stop loss value will be at the front of the priority queue.

  4. Updating trailing stop loss values: As the prices of the stocks/assets change, you need to update their trailing stop loss values accordingly. Retrieve the element with the highest priority (highest trailing stop loss value) from the priority queue. Calculate the new trailing stop loss value based on the updated price and update the element in the priority queue. If the new trailing stop loss value is higher than the trailing stop loss value of the next highest-priority element, adjust the position of the element in the priority queue to reflect the new priority.

  5. Triggering stop loss: Whenever the price of a stock/asset reaches or falls below its trailing stop loss value, you can trigger the stop loss action for that stock/asset.

Using a priority queue allows you to efficiently track and manage multiple stocks/assets in a trailing stop loss system. The most critical element (highest trailing stop loss value) is always readily accessible at the front of the queue, making it easy to identify which stocks/assets need attention based on their priority.

huangapple
  • 本文由 发表于 2023年6月22日 17:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定