Sunday, 8 December 2019

quantopian trading volume

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = get_pricing('SPY', start_date='2015-6-1', end_date='2015-7-1', fields='volume')
x = np.arange(len(data))
print data.values
print data.index
plt.bar(x, data.values)

tick_num = 12
tick_step = float(len(data))/tick_num
x_tick = np.arange(0, len(data), tick_step)
x_label = []
for i in range(0, tick_num):
    x_label.append(data.index[int(x_tick[i])].date())

print x_tick
print x_label
 
plt.xticks(x_tick, x_label)
plt.xticks(rotation=-90)
plt.ylabel('Volume (USD)');

[  7.63173340e+07   7.56557330e+07   7.05195340e+07   1.07026621e+08
   8.29844740e+07   7.15034260e+07   7.82032850e+07   1.01090613e+08
   5.82412800e+07   8.38160790e+07   9.04484390e+07   6.10451090e+07
   9.68857910e+07   1.11951254e+08   7.89226310e+07   5.74651720e+07
   4.89672890e+07   6.57530870e+07   6.33106560e+07   7.72355920e+07
   1.41736435e+08   1.27227662e+08   9.46635640e+07]
DatetimeIndex(['2015-06-01', '2015-06-02', '2015-06-03', '2015-06-04',
               '2015-06-05', '2015-06-08', '2015-06-09', '2015-06-10',
               '2015-06-11', '2015-06-12', '2015-06-15', '2015-06-16',
               '2015-06-17', '2015-06-18', '2015-06-19', '2015-06-22',
               '2015-06-23', '2015-06-24', '2015-06-25', '2015-06-26',
               '2015-06-29', '2015-06-30', '2015-07-01'],
              dtype='datetime64[ns, UTC]', freq='C')
[  0.           1.91666667   3.83333333   5.75         7.66666667
   9.58333333  11.5         13.41666667  15.33333333  17.25        19.16666667
  21.08333333]
[datetime.date(2015, 6, 1), datetime.date(2015, 6, 2), datetime.date(2015, 6, 4), datetime.date(2015, 6, 8), datetime.date(2015, 6, 10), datetime.date(2015, 6, 12), datetime.date(2015, 6, 16), datetime.date(2015, 6, 18), datetime.date(2015, 6, 22), datetime.date(2015, 6, 24), datetime.date(2015, 6, 26), datetime.date(2015, 6, 30)]

daily trading volume graph over a month

data = get_pricing('SPY', start_date='2015-6-1', end_date='2017-7-1', fields='volume')

daily trading volume over 2 years

data = get_pricing('SPY', start_date='2015-6-1', end_date='2015-6-2', frequency='minute', fields='volume')
...
for i in range(0, tick_num):
    x_label.append(data.index[int(x_tick[i])])

[ 1130598.   860265.   466341.   305119.   420599.   434213.   456097.
   485796.   321990.   256441.   276184.   193212.   286944.   167065.
   108485.   197818.   422527.   593151.   479281.   214867.   601252.
   268215.   570393.   303907.   262587.   203801.   176336.   654809.
   242458.   227793.   854860.   557071.  1116683.   310044.   356430.
   ...
   100768.   366257.    81084.   184404.    86525.   167199.   196828.
   111246.   203550.   299601.   147401.   122207.   235673.   216496.
   278106.   352249.  1112440.   418138.   273468.   270791.   439510.
   424709.   117943.   172694.   181214.   317756.   765112.   316917.
   522166.   798810.  1502124.]
DatetimeIndex(['2015-06-01 13:31:00+00:00', '2015-06-01 13:32:00+00:00',
               '2015-06-01 13:33:00+00:00', '2015-06-01 13:34:00+00:00',
               '2015-06-01 13:35:00+00:00', '2015-06-01 13:36:00+00:00',
               '2015-06-01 13:37:00+00:00', '2015-06-01 13:38:00+00:00',
               '2015-06-01 13:39:00+00:00', '2015-06-01 13:40:00+00:00',
               ...
               '2015-06-02 19:51:00+00:00', '2015-06-02 19:52:00+00:00',
               '2015-06-02 19:53:00+00:00', '2015-06-02 19:54:00+00:00',
               '2015-06-02 19:55:00+00:00', '2015-06-02 19:56:00+00:00',
               '2015-06-02 19:57:00+00:00', '2015-06-02 19:58:00+00:00',
               '2015-06-02 19:59:00+00:00', '2015-06-02 20:00:00+00:00'],
              dtype='datetime64[ns, UTC]', length=780, freq=None)
[   0.   65.  130.  195.  260.  325.  390.  455.  520.  585.  650.  715.]
[Timestamp('2015-06-01 13:31:00+0000', tz='UTC'), Timestamp('2015-06-01 14:36:00+0000', tz='UTC'), Timestamp('2015-06-01 15:41:00+0000', tz='UTC'), Timestamp('2015-06-01 16:46:00+0000', tz='UTC'), Timestamp('2015-06-01 17:51:00+0000', tz='UTC'), Timestamp('2015-06-01 18:56:00+0000', tz='UTC'), Timestamp('2015-06-02 13:31:00+0000', tz='UTC'), Timestamp('2015-06-02 14:36:00+0000', tz='UTC'), Timestamp('2015-06-02 15:41:00+0000', tz='UTC'), Timestamp('2015-06-02 16:46:00+0000', tz='UTC'), Timestamp('2015-06-02 17:51:00+0000', tz='UTC'), Timestamp('2015-06-02 18:56:00+0000', tz='UTC')]

minute trading volume over 2 days
data = get_pricing('SPY', start_date='2015-6-1', end_date='2015-6-2', frequency='minute', fields='volume')
data = data.resample('60T').sum()

[ 11360751.  18557297.   7024886.   7016271.   5862379.   7315033.
  17311150.   1869567.        nan        nan        nan        nan
        nan        nan        nan        nan        nan        nan
        nan        nan        nan        nan        nan        nan
  16010783.  14301804.   5977053.  10200373.   4987498.   8257317.
  14418781.   1502124.]
DatetimeIndex(['2015-06-01 13:00:00+00:00', '2015-06-01 14:00:00+00:00',
               '2015-06-01 15:00:00+00:00', '2015-06-01 16:00:00+00:00',
               '2015-06-01 17:00:00+00:00', '2015-06-01 18:00:00+00:00',
               '2015-06-01 19:00:00+00:00', '2015-06-01 20:00:00+00:00',
               '2015-06-01 21:00:00+00:00', '2015-06-01 22:00:00+00:00',
               '2015-06-01 23:00:00+00:00', '2015-06-02 00:00:00+00:00',
               '2015-06-02 01:00:00+00:00', '2015-06-02 02:00:00+00:00',
               '2015-06-02 03:00:00+00:00', '2015-06-02 04:00:00+00:00',
               '2015-06-02 05:00:00+00:00', '2015-06-02 06:00:00+00:00',
               '2015-06-02 07:00:00+00:00', '2015-06-02 08:00:00+00:00',
               '2015-06-02 09:00:00+00:00', '2015-06-02 10:00:00+00:00',
               '2015-06-02 11:00:00+00:00', '2015-06-02 12:00:00+00:00',
               '2015-06-02 13:00:00+00:00', '2015-06-02 14:00:00+00:00',
               '2015-06-02 15:00:00+00:00', '2015-06-02 16:00:00+00:00',
               '2015-06-02 17:00:00+00:00', '2015-06-02 18:00:00+00:00',
               '2015-06-02 19:00:00+00:00', '2015-06-02 20:00:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='60T')
[  0.           2.66666667   5.33333333   8.          10.66666667
  13.33333333  16.          18.66666667  21.33333333  24.          26.66666667
  29.33333333]
[Timestamp('2015-06-01 13:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-01 15:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-01 18:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-01 21:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-01 23:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 02:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 05:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 07:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 10:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 13:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 15:00:00+0000', tz='UTC', offset='60T'), Timestamp('2015-06-02 18:00:00+0000', tz='UTC', offset='60T')]

hourly trading volume in 2 days

reference:
https://www.quantopian.com/lectures/introduction-to-volume-slippage-and-liquidity
https://pythonspot.com/matplotlib-bar-chart/
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html

Saturday, 7 December 2019

matplotlib area fill

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

x = np.linspace(-5,5,100)
#normal distribution, mean =0, deviation = 1
y = stats.norm.pdf(x,0,1)
plt.plot(x,y)

plt.fill_between(x, y, color='yellow')

fill between normal distribution and x axis
y2 = x**2
plt.plot(x,y2)

plt.fill_between(x, y, y2, color='yellow')

plt.ylim(0, 0.4)
fill between normal distribution and parabola

plt.fill_between(x, y, y2, where=y>y2, color='yellow')
filter out outside region

reference:
https://www.quantopian.com/lectures/confidence-intervals
https://www.science-emergence.com/Articles/How-to-fill-an-area-in-matplotlib-/
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/fill_between_demo.html

Friday, 6 December 2019

哪些国外经历回国可能会大幅降薪, 哪些海外工作经历回国高薪?

第一, 中国比国外还领先的行业。

比如说,有同学在日本留学,毕业之后在日本的电商网站工作,工作一段时间之后想回国,觉得自己有在海外的工作经验,所以找工作开价很高,但是一直没能找到如愿的工作。实际上,中国的互联网,电商行业已经处于全球领先的地位,国内并不缺乏相关的人才,所以在海外的电商经验,并不能成为回国找相关工作的助推器。还有人在新加坡做APP项目运营,新加坡的APP水准是远落后中国,这样的经历回国后,如果直接按照汇率定价很高,都比较难找到理想工作。

第二, 还有一些中外行业差别巨大的工作,国外经历用处不大。

比如税务、银行 。举个例子,有同学在国外读会计专业,在国外的会计事务所有税务经验,回国想做金融,但是工资要求和国外持平或者更高,这一点实际很难做到。每个国家的税法都是不同的。

关于会计岗位,中国不缺初级、中级财务人才。如果国外比较底层的经验,不太可能空降成为高薪的财务经理等职位。特别是在澳大利亚学习会计的同学,有着相当大的比例,尤其需要注意这一点,并不是有了国外的普通会计经验,就可以回国拿高薪,除非你已经做到CFO的级别,那很缺。更何况国内的会计已经是趋于饱和,想找一个懂中国会计法和税法的熟手并不是难事。

银行属于极度完善和流水作业的行业,越发达的银行业,流水线程度越高。如果只是在国外银行某个环节搭把手干活。回来想拿到很高的薪水,非常难。甚至找个特别满意的工作都比较难。

第三, 国外有而中国无的职位,回国很难找工作。

有同学在英国留学,学习会计金融,在英国的银行有外汇清算的工作经验。但是回国很久都找不到工作。原因在于,英国有很开放的外汇交易,所以需要外汇清算这个岗位,但是中国目前的汇市并不开放,能够提供外汇清算的岗位少之又少,所以找不到相关工作是必然。欧洲作为工业革命的起源地,在很多行业都遵循着行业流水线化的原则,经过多年的发展,很多成熟行业的工作已经流水化作业,每个人负责的部分是有限的一小块。但是在中国,部分行业的发展还没有成熟到流水线化,有些在欧洲有的岗位,在中国未必能找得到。

第四,技术类的基础岗。

因为华人在国外,要找个高起点的工作比较难。所以绝大部分工作经验在5年以内的国外经验都还处于边缘岗位,工作经验对比国内同龄人逊色一大截。

有的同学在国外做了一年数据分析,或者国外做了一年四大审计,希望回国找个2-3万的工作。这个是比较难的。

因为数据分析本身是一个入门性质的岗位,一方面中国还没发达到基础入门岗给2-3万,另一方面,中国数据方面人才真心不缺!只会比国外的好!

简历君,见过有人在美国咨询公司做了5年数据分析回国。一开始定位年薪60-70万。最后找了七八个月工作都没着落。朋友给推荐了去BAT,结果面试官一问,“啊,你在国外做的这个很基础啊,我们应届生都可以做。我们做得这个是人工智能水准了,你胜任不了。”最终市场给了1.5万月薪。

而四大审计,本身审计就不是什么高薪高难度工作。中国审计行业发展几十年了,多牛的人都一大把。哪个国家四大回来的人都一堆。所以国外四大回来,二十年前会有个比较不错的薪水。放在21世纪,可能要小小失望一把。

哪些海外工作经历回国高薪呢?

通用技术型工作是可以在国外镀金的,比如医生,金融量化交易、国外做过国际大企业总部品牌营销,美国娱乐业,硅谷高科技公司重要项目负责人。此类工作属于通用型的工作,并不会因为国家不同而有太大差别,而且国外技术比中国更成熟。当然,一样,要国外的高级人才回来。这一类的工作,不仅回国会比较吃香,就算是想留在国外也是比较好找工作的,薪水也普遍不错。

Thursday, 5 December 2019

quantopian curve fit gaussian + polynomial

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

def expfit(x, a, b, c, d, e, f, g):
    return  a*np.exp(-(b*x*x+c*x+d))+e*x*x+f*x+g

x = np.linspace(1,1+len(price),len(price))

params, params_covariance = curve_fit(expfit, x, price.values )

print params

y = expfit(x, *params)

f = pd.Series(y, index=price.index, name='exp fit')

pd.concat([price, f], axis=1).plot()

[  1.87976840e+03  -2.61667985e-06   6.40016840e-03   3.83225576e+00
  -1.06443204e-05   7.86290764e-02  -1.36885462e+01]

[  1.15483490e+01   1.10677384e-04  -1.16953358e-02  -1.34140290e+00
  -7.36267261e-06   3.04019894e-04   5.55989990e+01]

[  1.58855939e+02   9.57979344e-05  -1.48746514e-02   1.72812129e+00
  -4.63387042e-05   1.52436240e-01   3.93288443e+01]

def expfit(x, a, b, c, d, e, f, g, h):
    return  a*np.exp(-(b*x*x+c*x+d))+e*x*x+f*x+g+h*x*x*x

[  7.19650057e+02   2.88317903e-06   2.24186888e-03   7.27803145e-01
  -5.10066383e-04   8.24549494e-01  -2.58031805e+02   9.90353284e-08]

gaussian + 3rd order polynomial

[  9.86790120e-03   9.34621548e-05  -9.37662256e-03  -8.68029637e+00
  -5.06336033e-05   5.08207847e-02   4.03453234e+01   1.05790726e-08]


reference:
http://chuanshuoge2.blogspot.com/2019/12/quantopian-curve-fit-gaussian-linear.html

quantopian curve fit gaussian + linear

start = '2008-01-01'
end = '2018-01-01'
price = get_pricing('AAPL', fields='price', start_date=start, end_date=end)
price.plot()
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

def expfit(x, a, b, c, d, e, f):
    return  a*np.exp(-(b*x*x+c*x+d))+e*x+f

x = np.linspace(1,1+len(price),len(price))

params, params_covariance = curve_fit(expfit, x, price.values )

print params

y = expfit(x, *params)

f = pd.Series(y, index=price.index, name='exp fit')

pd.concat([price, f], axis=1).plot()

[  7.84216333e+01   5.57230815e-05  -7.13434301e-03   1.45807761e+00
   5.84734404e-02  -4.80640750e+00]


[  8.44865621e+00   1.29652892e-04  -1.37545875e-02  -1.42288153e+00
   -1.96774039e-02   6.58394726e+01]

reference:
https://chuanshuoge2.blogspot.com/2019/12/quantopian-curve-fit-gaussian.html

quantopian curve fit gaussian

start = '2008-01-01'
end = '2018-01-01'
price = get_pricing('XOM', fields='price', start_date=start, end_date=end)
price.plot()
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

#Gaussian formula  
def expfit(x, a, b, c, d, e, f):
    return  a*np.exp(-(b*x*x+c*x+d))+e

x = np.linspace(1,1+len(price),len(price))

params, params_covariance = curve_fit(expfit, x, price.values )

print params

y = expfit(x, *params)

f = pd.Series(y, index=price.index, name='exp fit')

pd.concat([price, f], axis=1).plot()

[ -4.26776636e-01   3.41090632e-06  -3.42580712e-03  -3.33515628e+00
   8.08866791e+01   1.00000000e+00]
reference:
https://en.wikipedia.org/wiki/Gaussian_function
http://chuanshuoge2.blogspot.com/2019/12/python-curve-fitting.html
http://chuanshuoge2.blogspot.com/2019/12/quantopian-curve-fitting-log.html