select item,value
from tfinance_yahoo
where corp = 'AAPL' AND period = '201806' AND item in ('totalassets','totalLiab')
select item
case when item='totalAssets' then vlaue else 0 end,
value
from tfinance_yahoo
where corp = 'AAPL' AND period = '201806' AND item in ('totalassets','totalLiab')
select item,
case when item='totalAssets' then value else 0 end,
case when item = 'totalLiab' then value else 0 end,
value
from tfinance_yahoo
where corp = 'AAPL' AND period = '201806' AND item in('totalassets','totalLiab')
select
sum(case when item='totalAssets' then value else 0 end),
sum(case when item = 'totalLiab' then value else 0 end)
from tfinance_yahoo
where corp = 'AAPL' AND period = '201806' AND item in('totalassets','totalLiab')
select corp,
sum(case when item = 'totalliab' then value else 0 end)/sum(case when item='totalassets' then value else 0 end)
from tfinance_yahoo
where period = '201806' AND item in('totalAssets','totalLiab')
group by corp
select corp,
sum(case when item = 'totalliab' then value else 0 end)/sum(case when item='totalassets' then value else 0 end)
from tfinance_yahoo
where period = '201806' AND item in('totalAssets','totalLiab')
group by corp
order by sum(case when item = 'totalliab' then value else 0 end)/sum(case when item='totalassets' then value else 0 end)
desc
select 选取数据条目
from 数据集
where 索引条目
group by 分组
order by 排序,降序
desc 升序
group by corp
having sum(case when item = 'totalliab' then value else 0 end)/sum(case when item='totalassets' then value else 0 end)>0.5
SQL + python与交易系统
select * from wdi_data where country_code = 'CHN'
select code,sum(close),count(*),sum(close)/count(*)
from stock_data
group by code
#Z = 1.2X1 + 1.4X2 + 3.3X3 + 0.6X4 + 0.99X5
#X1=营运资本/资产总额
#X2=留存收益/资产总额
#X3=息税前利润/资产总额
#X4=股东权益的市场价值总额/负债总额
#X5=销售收入/资产总额
select a.corp,a.x1,a.x2,a.x3,a.x4,a.x5,
1.2*a.x1 + 1.4*a.x2 + 3.3*a.x3 +0.6*a.x4+0.99*a.x5 as z
from(select corp,
((sum(case when item = 'totalCurrentAssets' then value else 0 end)-sum(case when item = 'totalCurrentLiabilities' then value else 0 end))/sum(case when item = 'totalassets' then value else 0 end)) as x1,
(sum(case when item = 'retainedearnings' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x2,
(sum(case when item = 'ebit' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x3,
(sum(case when item = 'totalstockholderequity' then value else 0 end)/sum(case when item = 'totalLiab' then value else 0 end)) as x4,
(sum(case when item = 'totalRevenue' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x5
from tfinance_yahoo
where period = '201806'AND item in('totalAssets','totalLiab','totalRevenue','ebit' ,'totalCurrentAssets','totalCurrentLiabilities','retainedearnings','totalstockholderequity')
group by corp
)a
group by a.corp
select a.corp,a.x1,a.x2,a.x3,b.close*a.commonstocks/a.totalLiab as x4,a.x5,
1.2*a.x1 + 1.4*a.x2 + 3.3*a.x3 +0.6*b.close*a.commonstocks/a.totalLiab+0.99*a.x5 as z
from(select corp,
((sum(case when item = 'totalCurrentAssets' then value else 0 end)-sum(case when item = 'totalCurrentLiabilities' then value else 0 end))/sum(case when item = 'totalassets' then value else 0 end)) as x1,
(sum(case when item = 'retainedearnings' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x2,
(sum(case when item = 'ebit' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x3,
(sum(case when item = 'totalRevenue' then value else 0 end)/sum(case when item = 'totalassets' then value else 0 end)) as x5,
sum(case when item = 'commonstock' then value else 0 end) as commonstocks,
sum(case when item = 'totalLiab' then value else 0 end) as totalLiab
from tfinance_yahoo
where period = '201806'AND item in('totalAssets','totalLiab','totalRevenue','ebit' ,'totalCurrentAssets','totalCurrentLiabilities','retainedearnings','commonstock')
group by corp
)a,
(select b_1.close as close
from
(
select close
from stock_data
where code = 'AAPL'
order by time
desc
)b_1
limit 1
)b
海龟们都是期货交易者,海龟们只选择有一定交易量流动性高的市场。这里我选择日指数数据CSI300.INDX,一方面是为了更好与基准比较,另一方面也是因为该标的可以不用担心流动性的问题。
头寸规模是海龟交易系统最重要的部分之一。
海龟交易法则根据一个市场的绝对波动幅度来调整头寸规模,也就是将头寸的绝对波动幅度进行了标准化。比如,投资标的的价值波动性较强时,可以减少持有量,相反,当它的价值波动性较弱时候,可以增加持有量。总而言之,市场的波动性与头寸规模可以相互抵消。
海龟用一个被称为N的概念来表示某个市场根本的波动性,它表示单个交易日某个特定市场所造成的价格波动的平均范围,它同时也涵盖了开盘价的缺口。其实这个所谓的N,就是我们平常所熟悉的ATR,关于ATR的介绍,可以戳AVERAGE TRUE RANGE.
以下为计算公式:
TR=Max(H-L,H-PDC,PDC-L)
其中:
TR=真实波幅
H=当日最高价
L=当日最低价
PDC=前一日收盘价
N(即ATR)的计算公式如下(其实就是前面计算所得TR的20日移动平均):
N=(19*PDN+TR)/20
其中:
PDN=前一日N值
TR=当日的真实波动幅度
有了N之后,下一步可以计算绝对波动幅度,也就是用根本的市场价格波动性(用N值定义)表示的价值量波动性。
绝对波动幅度=N*合约每一点所代表的价值
最后,海龟按照我们所称的单位(Units)建立头寸。使1N代表帐户净值的1%。 波幅调整后的头寸单位为:
头寸规模单位=账户的1%/市场的绝对波动幅度
可以看出,使用N作为市场波动标准化的度量并以此作为开仓量及持仓量的依据,其背后的资金管理含义是,即便当日投资标的跌幅达到N(ATR)的水平,当日的损失都能控制在1%的总资产水平内。 即便当日投资标的跌幅达到N(ATR)的水平,当日的损失都能控制在1%的总资产水平内。 即便当日投资标的跌幅达到N(ATR)的水平,当日的损失都能控制在1%的总资产水平内。
海龟的入市规则有两个系统,我们可以根据自己的意愿决定将净值配置在何种系统上。
系统一————以20日突破为基础的偏短线系统
突破定义为超过前20日的最高价或者最低价 海龟总是在日间突破发生时进行交易,而不会等到收盘或次日开盘。
系统二————以55日突破为基础的较简单的长线系统
只要有一个信号显示价格超过了前55日的最高价和最低价就建立头寸。
海龟交易系统不是一有突破信号就全仓介入,而是根据最新市场价格变化进行逐步建仓。
海龟在价格突破时只建立一个单位的头寸,在建立头寸后根据前面指令的实际成交价为基础以每突破0.5N的间隔进行加仓。
例如:
黄金:N=2.5
55日突破=310
增加的第一个单位310.00
第二个单位310.00+1/2个2.5即311.25
第三个单位311.25+1/2个2.5即312.50
第四个单位312.50+1/2个2.5即313.75
海龟被告知在接受入市信号时要非常连续,因为一年中的大部分利润可能仅仅来自两三次大赢利。
对大多数人来说,始终抱着亏损的交易终究会反转的愿望比干脆退出亏损头寸并承认交易失败要容易得多。长期看,不会止损的交易是不会成功的。在你建立头寸之前,你需要预先确定退出的点位。如果市场波动触及你的价位,你就必须每一次毫无例外的退出。在这一立场上摇摆不定最终会导致灾难。(画外音:前段时间大家应该体会比较深刻吧。)
止损标准
海龟以头寸风险为基础设置止损,任何一笔交易不能出现2%以上的风险,因为价格波动1N表示1%的账户净值,容许风险为2%的最大止损就是价格波动2N,为了保证全部头寸的风险最小,如果另外增加了单位,前面单位的止损需提高0.5N。
例如:
原油:N=1.255 日突破=28.30
第一单位 28.30 25.90
艰难的离市
对于大多数交易员,海龟离市规则是系统法则中唯一最难的部分。等待10或20新低出现通常意味着眼睁睁瞅着20%,40%甚至100%的利润化为泡影。
海龟交易法则对于系统一系统二有着不同的离市标准:
系统一
离市对于多头头寸为10日最低价,对于空头头寸为10日最高价。如果价格波动于头寸背离至10日突破头寸中所有单位都会退出
系统二
离市对于多头头寸为20日最低价,对于空头头寸为20日最高价,如果价格波动与头寸背离至20日突破头寸中所有单位都会退出
海龟入市时一般不会设置离市止损价,但会在日间盯着价格,一旦价格穿过离市突破价,就开始打电话下离市指令。
PYTHON代码
import pandas as pd
import pymysql
from datetime import datetime
from datetime import timedelta
#变量数据
cash0 = 10000#资金量
date_1 = '2017-11-01'#初始日期
p_1 = datetime.strptime(date_1,"%Y-%m-%d")
p_2 = p_1 - timedelta(days = 55)
p_3 = p_1 - timedelta(days = 20)
date_2 = p_2.strftime('%Y-%m-%d')
date_3 = p_3.strftime('%Y-%m-%d')
#链接数据库
con = pymysql.connect(host = '47.97.205.89',port = 3306,user = '#', passwd = '#',db = 'quantity',use_unicode = True, charset = "utf8")
cursor = con.cursor()
#############################
########海龟交易法则#########
#############################
#回测标的股/股票池
code = 'AAPL'
#提取前55日均价&提取前20日最低价
df = pd.read_sql("select close from stock_data where code='"+code+"'and TradingDay>='"+date_2+"'and TradingDay<'"+date_1+"'",con)
max_ = df['close'].max()
df = pd.read_sql("select close from stock_data where code='"+code+"'and TradingDay>='"+date_3+"'and TradingDay<'"+date_1+"'",con)
min_ = df['close'].min()
cash = cash0#开仓前资金
stocknum = 0#持股量
buynum = 0
sellnum = 0
remain = 0
cursor.execute("select close from stock_data where code='"+code+"' and TradingDay>='"+date_1+"' order by Time")
for row in cursor.fetchall():
price = row[0] #获取此分钟的收盘价格
#开仓(超过前55日买入),每次全仓操作
if (price > max_ and stocknum==0) :
stocknum = int(cash/price)
cash = cash - stocknum*price
buynum = buynum + 1
remain = price
#离市*低于前20日最低价卖出
elif price < min_ and stocknum!=0:
cash = cash + stocknum*price
stocknum = 0
sellnum = sellnum + 1
#动态调整最低最高价
if price>max_:max_ = price
if price<min_:min_ = price
print ('现金:'+str(cash)+',股票:'+str(stocknum)+',股价:'+str(remain)+',收益:'+str(100*((cash+stocknum*remain)-cash0)/cash0)+'%')
print (' 买入次数:'+str(buynum)+' 卖出次数:'+str(sellnum))
print(max_)
print(min_)
cursor.close()
con.close()
现金:9146.239929182,股票:0,股价:169.75,收益:-8.537600708179998%
买入次数:1 卖出次数:1
180.080001831
150.610397339
#强调数据仓库的作用
#DW数据仓库
#DM数据集市
#创建另外一张表,将结果放进去
#tabe为实体,需要及时更新,速度较快,只有管理员可以创建
#create table vifinance_yahoo as
#view为虚拟,自动更新,速度较慢
create view vifinance_yahoo as
select corp,sum(case when item= 'totalliab' then value else 0 end)/sum(case when item= 'totalassets' then value else 0 end) as aaa
from tfinance_yahoo
group by corp
#市值调取
select tfinance_yahoo.value,stock_list from tfinance_yahoo left jon stock_list on
tfinance_yahoo.corp=jon stock_list.code