企业文化 设计 咨询四川seo推广方案
最近,有朋友留言问如何实现倍数委托,即在网格交易中,当行情大幅低开或高开时,网格条件单也可以成倍数委托,以控制极端行情下的委托数量。比如,网格交易参数为每下跌3%买入1000股,当出现大幅低开时,假设开盘跌幅为6%,那么,此时是如何买入呢?是买1000股还是2000股呢?本文将详细介绍倍数委托的实现方法。
流程梳理
1. 在指定价格区间内生成网格线,按升序排列。
2. 找到当前价格所在的网格位置current_index。
3. 如果last_grid_index是None,说明是初始状态,返回当前索引,不交易。
4. 否则,比较current_index和last_grid_index:
-
如果current_index更大,则说明价格上涨,触发卖出,卖出数量为order_amount_per_grid*(current_index - last_grid_index),暂不考虑当前实际持仓情况和持仓下限数量。
-
如果current_index更小,则说明价格下跌,触发买入,买入数量为order_amount_per_grid*(last_grid_index - current_index),暂不考虑可用资金和持仓上限数量。
-
其他情况不交易。
5.根据第4条比较结果进行买卖交易。
实现过程
确定网格方式
一般情况下,网格可以按等差或者等比划分。等差的话,每个网格的价格间隔相同;等比则是按比例递增或递减。可以通过参数grid_type来控制,比如'linear'或'exponential'。
计算网格步长
如果下限是lower,上限是upper,分成num_grids份,如果是线性,步长就是(upper - lower)/(num_grids-1),因为包括两端点。每个网格线就是lower + i*步长,i从0到num_grids-1。如果是等比的话,可能每个步长是比例增长,比如用几何级数。
生成网格价格列表
函数定义
根据所确定的网格方式和步长,通过自定义的函数generate_grid_levels()生成网格价格列表:
def generate_grid_levels(lower, upper, num_grids, grid_type):# 当grid_type为exponential时,如果lower为0,会出错。需要确保lower >0if not lower > 0:raise ValueError("lower必须大于0")if grid_type == 'linear':return [lower + i * (upper - lower) / (num_grids - 1) for i in range(num_grids)]elif grid_type == 'exponential':ratio = (upper / lower) ** (1 / (num_grids - 1))return [lower * (ratio ** i) for i in range(num_grids)]else:raise ValueError("grid_type必须为'linear'或'exponential'")
使用示例
# 生成下限为10、上限为20的包括5个价格的等差价格列表
grid_levels = generate_grid_levels(10, 20, 5, 'linear')# 确保价格升序排列
grid_levels.sort() print(grid_levels)
输出:
[10.0, 12.5, 15.0, 17.5, 20.0]
寻找当前价格所处的网格位置
网格价格列表生成以后,需要确定当前股票价格所处的网格位置。找到最近的上下网格线。例如,如果当前价格在grid_levels[i]和grid_levels[i+1]之间,那么上一个买入点可能是grid_levels[i],下一个卖出点是grid_levels[i+1]。
网格位置的查找可以使用Python标准库中bisect模块的bisect_right()函数来实现,具体使用方法参见《
Python标准库中bisect模块的bisect_right()函数在网格交易中的应用》。
# 确定当前价格所在的网格位置
pos = bisect.bisect_right(grid_levels, current_price)# 对bisect_right()返回结果进行修正
current_index = pos - 1 if pos > 0 else 0
current_index = min(current_index, len(grid_levels) - 1)
对bisect_right()返回的pos进行修正,主要是将其转换为左闭右闭区间的网格索引;同时,需对边界进行处理,确保价格超出网格范围时,索引仍然有效。
交易逻辑判断
交易逻辑判断主要就是比较current_index和last_grid_index:
-
current_index>last_grid_index,则说明价格上涨,触发卖出;
-
current_index<last_grid_index,则说明价格下跌,触发买入;
-
current_index<last_grid_index,则说明价格不变,不进行交易。
确定委托倍数
确定委托倍数是进行倍数委托的关键:
-
当current_index - last_grid_index = 1时,说明价格上涨了一个网格,卖出数量=order_amount_per_grid;
-
当current_index - last_grid_index > 1时,说明价格上涨超过了一个网格,要进行倍数委托的话,卖出数量应该=(current_index - last_grid_index)* order_amount_per_grid;
# 价格上涨,触发卖出
if current_index > last_grid_index: sell_amount = (current_index - last_grid_index) * order_amount_per_grid
-
当last_grid_index - current_index > 1时,说明价格下跌超过了一个网格,要进行倍数委托的话,买入数量应该=(last_grid_index - current_index)* order_amount_per_grid;
# 价格下跌,触发买入
if current_index < last_grid_index:buy_amount = (last_grid_index - current_index) * order_amount_per_grid
完整示例
假设上一次交易的价格为15元,对应的网格索引为2,当价格分别为10、12.5、15、17.5和20时:
# 初始参数params = {'lower': 10,'upper': 20,'num_grids': 5,'grid_type': 'linear','last_grid_index': 2, # 初始网格索引(对应价格15)'order_amount_per_grid': 1000,}# 价格上涨到20(网格索引4)print("价格上涨到20时的交易:")print(grid_trading(current_price=20, **params))# 模拟价格上涨到17.5(网格索引3)print("价格上涨到17.5时的交易:")print(grid_trading(current_price=17.5, **params))# 模拟价格为15(网格索引2)print("价格为15时的交易:")print(grid_trading(current_price=15, **params))# 模拟价格下跌到12.5(网格索引1)print("\n价格下跌到12.5时的交易:")print(grid_trading(current_price=12.5, **params))# 模拟价格下跌到10(网格索引0)print("\n价格下跌到10时的交易:")print(grid_trading(current_price=10, **params))
输出结果:
其中amount为买卖数量,可见,实现了倍数委托!
价格上涨到20时的交易:
{'signal': 'sell', 'price': 20, 'last_grid_index': 4, 'amount': 2000}价格上涨到17.5时的交易:
{'signal': 'sell', 'price': 17.5, 'last_grid_index': 3, 'amount': 1000}价格为15时的交易:
{'signal': 'no trade', 'price': 15, 'last_grid_index': 2}价格下跌到12.5时的交易:
{'signal': 'buy', 'price': 12.5, 'last_grid_index': 1, 'amount': 1000}价格下跌到10时的交易:
{'signal': 'buy', 'price': 10, 'last_grid_index': 0, 'amount': 2000}
完整代码
import bisectdef grid_trading(current_price, lower, upper, num_grids, grid_type='linear',last_grid_index=None, order_amount_per_grid=100):"""股票网格交易函数参数:current_price (float): 当前股票价格lower (float): 网格价格下限upper (float): 网格价格上限num_grids (int): 网格数量grid_type (str): 网格类型,'linear'(线性)或'exponential'(等比)last_grid_index (int): 上次交易的网格索引order_amount_per_grid (float): 每格交易数量返回:dict: 包含交易信号、价格、买卖数量等信息"""# 生成网格价格列表def generate_grid_levels(lower, upper, num_grids, grid_type):# 当grid_type为exponential时,如果lower为0,会出错。需要确保lower >0if not lower > 0:raise ValueError("lower必须大于0")if grid_type == 'linear':return [lower + i * (upper - lower) / (num_grids - 1) for i in range(num_grids)]elif grid_type == 'exponential':ratio = (upper / lower) ** (1 / (num_grids - 1))return [lower * (ratio ** i) for i in range(num_grids)]else:raise ValueError("grid_type必须为'linear'或'exponential'")grid_levels = generate_grid_levels(lower, upper, num_grids, grid_type)grid_levels.sort() # 确保价格升序排列# 确定当前价格所在的网格位置pos = bisect.bisect_right(grid_levels, current_price)current_index = pos - 1 if pos > 0 else 0current_index = min(current_index, len(grid_levels) - 1)# 初始化返回结果result = {'signal': 'no trade','price': current_price,'last_grid_index': last_grid_index}# 初始状态处理if last_grid_index is None:result['last_grid_index'] = current_indexreturn result# 交易逻辑判断if current_index > last_grid_index: # 价格上涨,触发卖出sell_amount = (current_index - last_grid_index) * order_amount_per_gridif sell_amount > 0:# 计算交易金额和手续费result['signal'] = 'sell'result['amount'] = sell_amountresult['last_grid_index'] = current_indexelif current_index < last_grid_index: # 价格下跌,触发买入# 计算最大可买数量(考虑手续费)buy_amount = (last_grid_index - current_index) * order_amount_per_gridif buy_amount > 0:result['signal'] = 'buy'result['amount'] = buy_amountresult['last_grid_index'] = current_indexreturn result# 示例用法
if __name__ == "__main__":# 初始参数params = {'lower': 10,'upper': 20,'num_grids': 5,'grid_type': 'linear','last_grid_index': 2, # 初始网格索引(对应价格15)'order_amount_per_grid': 1000,}# 价格上涨到20(网格索引4)print("价格上涨到20时的交易:")print(grid_trading(current_price=20, **params))# 模拟价格上涨到17.5(网格索引3)print("价格上涨到17.5时的交易:")print(grid_trading(current_price=17.5, **params))# 模拟价格为15(网格索引2)print("价格为15时的交易:")print(grid_trading(current_price=15, **params))# 模拟价格下跌到12.5(网格索引1)print("\n价格下跌到12.5时的交易:")print(grid_trading(current_price=12.5, **params))# 模拟价格下跌到10(网格索引0)print("\n价格下跌到10时的交易:")print(grid_trading(current_price=10, **params))
码字不易,原创更不易,如您觉得本文对您有帮助,麻烦动动您富贵的小手,点赞、收藏、关注、订阅!!!