import numpy as np
import pandas as pd
# 期货策略类,包括开仓、买入、止盈、止损方法与策略执行主函数
class StockStrategy:
df = None
open_offset_num = 5
buy_in_offset_num = 0
stop_win_offset_num = 0
stop_lose_num = 0
price_list = []
price_stop_lose = []
flag_buy_in = False
need_sell_out = False
stop_lose_rate = 3
# 初始化策略类StockStrategy
def __init__(self, df: pd.DataFrame) -> None:
self.df = df
# 设置止损模块,止损比例
def set_stop_lose_rate(self, slr) -> None:
self.stop_lose_rate = slr
# 设置偏移量: 开仓偏移、买入偏移和止盈偏移,如需修改止损量,需重写set_stop_lose_num(self, date)方法
def set_offset_num(self, oon: int, bion: int, swon: int) -> None:
if oon is not None:
self.open_offset_num = oon # 开仓偏移量
if bion is not None:
self.buy_in_offset_num = bion # 买入偏移量
if swon is not None:
self.stop_win_offset_num = swon # 卖出偏移量
# 返回买卖结果
def get_buy_sell_list(self) -> list:
return self.stock_strategy_main()
# 准备开仓模块
def strategy_open(self, i) -> bool:
df = self.df
if df['hl'][i] <= (df['支撑线'][i] + self.open_offset_num):
return True
else:
return False
# 买入策略模块
def strategy_buy_in(self, i) -> bool:
df = self.df
if df['hl'][i] >= (df['阻力线'][i] + self.buy_in_offset_num):
return True
else:
return False
# 止盈策略模块
def strategy_stop_win(self, i) -> bool:
df = self.df
if len(self.price_list) < 3: # 3天不到,观望
self.price_list.append(df['Low'][i])
return False
if np.mean(self.price_list) > df['Low'][i]: # 价格创新低等待
del (self.price_list[0])
self.price_list.append(df['Low'][i])
return False
if df['Low'][i] <= df['中界线'][i]: # 价格不创新低了,但没突破中线不止盈
self.price_list = []
return True
if df['Low'][i] <= (df['支撑线'][i] + self.stop_win_offset_num):
return True
else:
return False
# 止损策略模块
def strategy_stop_lose(self, i, last_buy_in) -> bool:
df = self.df
if (df['hl'][i] - last_buy_in) > (self.stop_lose_num * self.stop_lose_rate):
return True
else:
return False
# 设置买卖止损值
def set_stop_lose_num(self, i) -> int:
df = self.df
return df['阻力线'][i] - df['中界线'][i]
# 买卖框架主函数,[{'buy_date': buy_date,'buy_price':buy_price,'sell_date':sell_date,'sell_price':sell_price},{}...]
def stock_strategy_main(self) -> list:
df = self.df
last_buy_in_date = None
last_buy_in_price = 0
last_sell_out_date = None
last_sell_out_price = 0
buy_sell_dict = {}
buy_sell_list = []
for i in df.index[20:]:
if self.flag_buy_in: # 有没有条件买入做空,默认为没有买入条件False
if self.strategy_buy_in(i): # 最高价大于BOLL上端线了,买买买,空空空,不要怂,就是干
last_buy_in_date = i # 买入日期记录一下
last_buy_in_price = df['Close'][i] # 买入价格记录一下,后边好算盈亏
self.stop_lose_num = self.set_stop_lose_num(i) # 设好止损,以防踩坑上不来
self.flag_buy_in = False # 买入后,停止买入判断,以防买太多,死的惨
self.need_sell_out = True # 开启卖出状态
continue
if self.need_sell_out: # 是否需要卖出,买入了就需要卖出
# 能不能正常卖出,能的话,就准备继续买入,扩大战果
if self.strategy_stop_win(i): # 是否需要获利了结
last_sell_out_date = i # 记录卖出日期
last_sell_out_price = df['Close'][i] # 记录卖出价格
buy_sell_dict = {
'buy_date': last_buy_in_date,
'buy_price': last_buy_in_price,
'sell_date': last_sell_out_date,
'sell_price': last_sell_out_price
}
buy_sell_list.append(buy_sell_dict)
buy_sell_dict = {}
self.need_sell_out = False # 卖光啦,不需要再卖出啦
self.flag_buy_in = True # 空仓啦,可以准备再买点,发财,发财
continue
# 需要止损了,忍痛割爱,冷静一下,暂时不具备买入条件
elif self.strategy_stop_lose(i, last_buy_in_price):
last_sell_out_date = i # 记录卖出日期
last_sell_out_price = df['Close'][i] # 记录卖出价格
buy_sell_dict = {
'buy_date': last_buy_in_date,
'buy_price': last_buy_in_price,
'sell_date': last_sell_out_date,
'sell_price': last_sell_out_price
}
buy_sell_list.append(buy_sell_dict)
buy_sell_dict = {}
self.need_sell_out = False # 卖了,不需要再卖出啦
continue
# 没有需要卖出的商品,那就看看能不能准备买入,做点小买卖
if self.need_sell_out is False:
if self.strategy_open(i): # 空头还可以嘛,再上去我就做空
self.flag_buy_in = True # 开启买入做空模式
continue
return buy_sell_list
python量化空单交易BOLL线买入、3天不创新低止盈
最新推荐文章于 2024-07-12 14:38:12 发布