Beispiel #1
0
def back_test(df,
              start_date='0',
              date_delta=60,
              norm_type='character',
              quota_index=0,
              lost=1.0):
    if start_date:
        end_date = date_add(start_date, date_delta)
    else:
        end_date = '9'
    if end_date > today():
        return []
    df_dif = get_quota(df)[quota_index]
    df_dif_norm = get_normlized(df_dif)
    df = pd.concat([df.set_index('date')['close'], df_dif_norm['sar']],
                   axis=1).dropna(how='any')
    close = df['close']
    r = get_trade_chance(df_dif, close, norm_type, start_date, end_date, lost)
    if r:
        rate, date_this, buy_price, buy_date, sell_price, sell_date, start_date_open, end_date_open = r
    else:
        return []
    for s_type in sell_price:
        rate[s_type] = {}
        rate[s_type]['profit'] = {}
        rate[s_type]['profit']['total'] = 1.0
        rate[s_type]['trade'] = {}
        for i in range(len(buy_price[s_type])):
            try:
                #rate[s_type]['profit']['total'] = rate[s_type]['profit']['total'] * (sell_price[s_type][i] * (1 - 0.002) / buy_price[s_type][i])
                rate[s_type]['profit']['total'] = rate[s_type]['profit'][
                    'total'] * (sell_price[s_type][i] *
                                (1 - 0.002) / buy_price[s_type][i]) * (
                                    (sell_price[s_type][i]) *
                                    (1 - 0.002) / buy_price[s_type][i + 1])
                rate[s_type]['profit'][buy_date[s_type]
                                       [i]] = rate[s_type]['profit']['total']
                rate[s_type]['trade'][buy_date[s_type][i]] = [
                    buy_date[s_type][i], buy_price[s_type][i],
                    sell_date[s_type][i], sell_price[s_type][i]
                ]
            except Exception, e:
                if len(buy_price[s_type]) == len(sell_price[s_type]):
                    rate[s_type]['profit']['total'] = rate[s_type]['profit'][
                        'total'] * (end_date_open *
                                    (1 - 0.002) / sell_price[s_type][i])
                else:
                    rate[s_type]['profit']['total'] = rate[s_type]['profit'][
                        'total'] * (end_date_open *
                                    (1 - 0.002) / buy_price[s_type][i])
                rate[s_type]['profit'][date_this] = rate[s_type]['profit'][
                    'total']
                rate[s_type]['trade'][date_this] = [
                    buy_date[s_type][i], buy_price[s_type][i], 'lastday',
                    end_date_open
                ]
Beispiel #2
0
#coding:utf-8
import pandas as pd
from commfunction import today, date_add

df = pd.read_csv('./cluedetail.csv')

td = today()
td = date_add(td, -2)
thisweek_start = date_add(td, -8)
thisweek_end = date_add(td, -2)
lastweek_start = date_add(thisweek_start, -7)
lastweek_end = date_add(thisweek_end, -7)
lastmonth_start = date_add(thisweek_start, -31)
lastmonth_end = date_add(thisweek_end, -31)

df_thisweek = df[(df['date'] >= thisweek_start)
                 & (df['date'] <= thisweek_end)].groupby(['city']).count()
df_thisweek['date'] = thisweek_start + '~' + thisweek_end
df_lastweek = df[(df['date'] >= lastweek_start)
                 & (df['date'] <= lastweek_end)].groupby(['city']).count()
df_lastweek['date'] = lastweek_start + '~' + lastweek_end
df_lastmonth = df[(df['date'] >= lastmonth_start)
                  & (df['date'] <= lastmonth_end)].groupby(['city']).count()
df_lastmonth['date'] = lastmonth_start + '~' + lastmonth_end
df_thismonth = df[(df['date'] >= lastmonth_start)
                  & (df['date'] <= lastmonth_end)].groupby(['city']).count()
df_thismonth['date'] = lastmonth_start + '~' + lastmonth_end

df_all = pd.concat([df_thisweek, df_lastweek, df_lastmonth],
                   axis=0,
                   join='inner')
Beispiel #3
0
#coding:utf-8
from sqlalchemy import create_engine
import requests
from sqlalchemy.orm import sessionmaker
import time, datetime, commfunction
from random import random
import pandas as pd
import numpy as np
#exit()
stocks = open('stocks.txt', 'r').readlines()
stocks = [code.replace('\n', '').replace('\r', '') for code in stocks]
today = commfunction.today('%Y_%m_%d').replace('_0', '_')

DB_CONNECT_STRING = 'sqlite:///stock_US_data.db'
engine = create_engine(DB_CONNECT_STRING, echo=False)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()


def get_origin_ustock_datas(code, today):
    time.sleep(random() * 20)
    code = code.lower()
    url = 'http://stock.finance.sina.com.cn/usstock/api/jsonp_v2.php/var%20_' + code + today + '=/US_MinKService.getDailyK?symbol=' + code + '&_=' + today + '&___qn=3'
    contents = requests.get(url).text[:-2].split('=(')[1]
    try:
        df = pd.DataFrame(eval(contents))
        df.rename(columns={
            'o': 'open',
            'c': 'close',
            'h': 'high',
            'l': 'low',
Beispiel #4
0
#coding:utf-8
from sqlalchemy import create_engine
import tushare as ts
from sqlalchemy.orm import sessionmaker
import time, datetime
from commfunction import today, date_add

fw = open('stock_date.txt', 'w')
today_date = date_add(str(today()), -1)

DB_CONNECT_STRING = 'sqlite:///stock_CN_data.db'
engine = create_engine(DB_CONNECT_STRING, echo=False)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()

codeDatas = session.execute(
    'select * from publish_date where code="002230";').fetchall()

for data in codeDatas[:1]:
    code, time_to_market = data
    print code, time_to_market
    try:
        lastdate = session.execute('select date from day_k_data where code="' +
                                   code +
                                   '" order by date desc limit 1;').first()[0]
    except Exception, e:
        #print e
        try:
            lastdate = date_add(str(time_to_market), 0, '%Y%m%d')
        except:
            lastdate = date_add(str(today_date), -1095, '%Y-%m-%d')
Beispiel #5
0
#!/usr/bin/python
#coding:utf-8
import requests, time
import BeautifulSoup
import sys
import datetime
from random import random
import pandas as pd
from commfunction import today, date_add, date_today_delta
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import matplotlib.pyplot as plt

date_today = today()
record = 'greeks\t' + date_today + '\n'

stocks = open('stocks.txt', 'r').readlines()
stocks = [code.replace('\n', '').replace('\r', '') for code in stocks]

DB_CONNECT_STRING = 'sqlite:///stock_option_data.db'
engine = create_engine(DB_CONNECT_STRING, echo=False)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()


def get_origin_option_datas(url, datasAll=[]):
    time.sleep(random() * 20)
    content = BeautifulSoup.BeautifulSoup(requests.get(url).text)
    nextpage = content.findAll('a', {'id': 'quotes_content_left_lb_NextPage'})
    datasAll.append(
        content.findAll('div',
Beispiel #6
0
def downloadFenshi(stockNum):
    imageUrl = 'http://image.sinajs.cn/newchart/v5/usstock/wap/min_daily/226/' + stockNum + '.gif'
    print imageUrl
    return requests.get(imageUrl, 'fenshi/' + stockNum + today() + '.gif')
Beispiel #7
0
#!/usr/bin/python
#coding:utf-8
import requests,time
import BeautifulSoup
import sys
import datetime
from random import random
import pandas as pd
from commfunction import today,date_add,date_today_delta
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

date_today = date_add(today(),-1)
record = 'option_price\t'+date_today+'\n'

try:
    t = open('t.txt','r').readlines()
    if record in t:
        exit()
except Exception,e:
    pass
t = open('t.txt','a')
t.write(record)
t.close()

DB_CONNECT_STRING = 'sqlite:///stock_option_data.db'
engine = create_engine(DB_CONNECT_STRING,echo=False)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()

stocks = open('stocks.txt','r').readlines()