日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

聚宽源码59

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聚宽源码59 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文策略源碼如下:
#次新+小市值+KAMA擇時 輪動

導入TA-Lib

import talib as tb
def initialize(context):
set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
set_option(‘use_real_price’, True)
g.buy_stock_count = 3
g.kama_days = 13
g.new_public_days = 150
g.return_radio = 0.999

g.days_counter = 0 g.buy_period =2# 運行函數 run_daily(market_end, time='after_close')

def handle_data(context, data):
hour = context.current_dt.hour
minute = context.current_dt.minute

if hour == 9 and minute == 59:if(g.days_counter%g.buy_period == 0):buy_stocks = select_stocks(context,data)adjust_position(context,data, buy_stocks)#每N分鐘檢查是否要賣 if minute%5 == 0 :for stock in context.portfolio.positions.keys():if(context.portfolio.positions[stock].closeable_amount > 0):single = get_kama_single(context,stock)if(single == -1):order_target_value(stock, 0)

def market_end(context):
g.days_counter = g.days_counter +1;

#過濾停牌 st
def filter_paused_and_st_stock(stock_list):
current_data = get_current_data()
return [stock for stock in stock_list if not current_data[stock].paused
and not current_data[stock].is_st and ‘ST’ not in current_data[stock].
name and ‘*’ not in current_data[stock].name and ‘退’ not in current_data[stock].name]

def filter_gem_stock(context, stock_list):
return [stock for stock in stock_list if stock[0:3] != ‘300’]

#篩選股票, 次新最小市值排序獲取前N個
def select_stocks(context,data):
q = query(valuation.code,
valuation.circulating_market_cap
).order_by(
valuation.circulating_market_cap.asc()
).filter(
valuation.circulating_market_cap <= 99
).limit(100)

df = get_fundamentals(q) stock_list = list(df['code']) stock_list = filter_paused_and_st_stock(stock_list) stock_list = filter_gem_stock(context, stock_list) blacklist = [] #新股篩選 tmpList = [] for stock in stock_list :#按交易日期算更合理?days_public=(context.current_dt.date() - get_security_info(stock).start_date).daysif days_public < g.new_public_days and days_public > 50:tmpList.append(stock) stock_list = tmpList #均線篩選 tmp_TimeSelect_List = [] for stock in stock_list :sigle = get_kama_single(context, stock)if(sigle >= 0):tmp_TimeSelect_List.append(stock) stock_list = tmp_TimeSelect_Listfilter_stocks = [] last_prices = history(1, '1m', 'close', security_list=stock_list) curr_data = get_current_data() for stock in stock_list:if last_prices[stock][-1] < curr_data[stock].high_limit:if last_prices[stock][-1] > curr_data[stock].low_limit:if stock not in blacklist:filter_stocks.append(stock) stock_list = filter_stocks stock_list = stock_list[:g.buy_stock_count] return stock_list;

def adjust_position(context,data, buy_stocks):
#賣
for stock in context.portfolio.positions.keys():
last_prices = history(1, ‘1m’, ‘close’, security_list=context.portfolio.positions.keys())
if stock not in buy_stocks:
curr_data = get_current_data()
if last_prices[stock][-1] < curr_data[stock].high_limit:
position = context.portfolio.positions[stock]
order_target_value(stock, 0)
#買
for stock in buy_stocks:
position_count = len(context.portfolio.positions)
if g.buy_stock_count > position_count:
value = context.portfolio.cash / (g.buy_stock_count - position_count)
if context.portfolio.positions[stock].total_amount == 0:
order_target_value(stock, value)

def get_kama_single(context,security):
period= g.kama_days46
close_long = get_price(security, end_date=context.current_dt, frequency=‘10m’, fields=[‘close’], count= period +246 )[‘close’].values;
#close_short = get_price(g.security, end_date=context.previous_date, frequency=‘1d’, fields=[‘close’], count= short_days+10 )[‘close’].values;
kama_long = tb.KAMA(close_long,timeperiod= period);
#kama_short = tb.KAMA(close_short,timeperiod= short_days);
# if( (kama_short[-1] > kama_long[-1]) & (kama_short[-2] <= kama_long[-2]) & (kama_short[-1] > kama_short[-2])):
# return 1
# if(( kama_short[-1] < kama_long[-1]) & (kama_short[-2] >= kama_long[-2]) & (kama_short[-1] < kama_short[-2])):
# return 0
#std = np.std(kama_long[long_days:]);

#sell if ((kama_long[-1] / kama_long[-2])< g.return_radio ): #or ((close_short[-1]/close_short[-2])<=0.93):return -1; # if ((close_long[-1] / close_long[-2])<= 0.97 ): #or ((close_short[-1]/close_short[-2])<=0.93): # return -1; #attribute_history if(kama_long[-1]/kama_long[-2] >1.01):return 1; # if kama_short[-1] < kama_long[-1] and kama_short[-2] > kama_long[-2]: # #sell # return 0; # if kama_short[-1] > kama_long[-1] and kama_short[-2] < kama_long[-2]: # #buy # return 1 return 0;

總結

以上是生活随笔為你收集整理的聚宽源码59的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。