file_plot

#!/usr/bin/env python
# coding: utf-8

from datetime import datetime
from datetime import timedelta


global g_flg_rise, g_flg_buy, g_timer_day, g_price_buy, g_price_sale, g_days_diff
global g_date_buy, g_date_sale, g_date_buy_read, g_price_buy_read, g_price_sale_read
global g_list_days, g_cnt_win, g_cnt_lost
global g_high_max, g_total


g_list_days = []
g_cnt_win = 0
g_cnt_lost = 0
g_price_sale_read = 0
g_price_sale = 0
g_date_buy = "00-00-00"
g_date_sale = "00-00-00"
g_days_diff = 0



def day_diff(begin, end):
    d1 = datetime.strptime(begin, "%Y-%m-%d")
    d2 = datetime.strptime(end, "%Y-%m-%d")
    k = (d2 - d1).days
    #print k,begin, end, "---------------------"
    return k



def float_2p(p):
    k = '%.2f' % p
    #print type(k),type(p)
    return float(k)




def judge_rise(price):
    if (0 == price):
        return 0.00
    price *= 1.1
    price += 0.005
    return float_2p(price)


def judge_down(price):
    if (0 == price):
        return 0.00
    price *= 0.9
    price += 0.005
    return float_2p(price)

'''
def plot(quotes):
    date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0
    flg_rise, high_max, flg_buy, days, cnt_win, cnt_lost = 0, 0, 0, 0, 0, 0
    total = 100
    total_old = 0

    for q in quotes:
        date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4])
        #if (date == "2015-07-21"):
        #    print date, open, high, low, close, q
        rise = judge_rise(close_old)
        #print rise, close, close_old
        if (0 == flg_buy):
            if (close == rise):
                #print "hi", rise, close, close_old, date
                flg_rise += 1
                high_max = high
            else:
                if (2 < flg_rise):
                    if (high_max <= high):
                        high_max = high
                        #print "3 dan"

                    else:
                        flg_buy = 1
                        days = 10
                else:
                    flg_rise = 0
        elif (1 == flg_buy):
            if (0 >= days):
                #print "days die"
                flg_rise, flg_buy, = 0, 0
            if (high_max < high):
                buy = high_max + 0.01
                flg_buy = 2
                #print "buy"

            days -= 1
        elif (2 == flg_buy):
            if (high_max <= high):
                high_max = high
            if (0.9 >= close / buy):
                total_old = total
                total =  total_old * close / buy
                print buy, high_max, close, total_old, total, date, "lost----------------------------------------------------------"
                flg_rise, flg_buy, = 0, 0
                cnt_lost += 1

            else:
                if (0.9 >= close / high_max or buy - close < 0.5):
                    total_old = total
                    total = total_old * close / buy
                    print buy, high_max, close, total_old, total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
                    flg_rise, flg_buy, = 0, 0
                    cnt_win += 1

        else:
            print "else "
            flg_rise, flg_buy, = 0, 0
        date_old, open_old, high_old, low_old, close_old = date, open, high, low, close
    print total, cnt_win, cnt_lost
    return total
        #print date, open, high, low, close
'''





def plot_buy(code, open, high, low, close, close_old, date):
    global g_flg_rise, g_high_max, g_flg_buy, g_timer_day, g_price_buy
    global g_date_buy, g_date_buy_read, g_price_buy_read

    if (0 == g_flg_buy):
        rise = judge_rise(close_old)
        if (close == rise):
            g_flg_rise += 1
            g_high_max = high
        else:
            if (2 < g_flg_rise):
                if (g_high_max <= high):
                    g_high_max = high
                    # print "read buy"
                else:
                    g_flg_buy = 1
                    g_timer_day = 10
            else:
                g_flg_rise = 0
                # print g_flg_buy, g_timer_day
    else:
        if (0 >= g_timer_day):
            # print "g_timer_day die"
            g_flg_rise, g_flg_buy, = 0, 0
        if (g_high_max < high):
            g_price_buy = g_high_max
            g_flg_rise, g_flg_buy, = 0, 0
            g_date_buy = date

            # print "buy"

            return 1
        g_timer_day -= 1
        # print g_flg_buy, g_timer_day,"g_price_buy"

    return 0


def plot_buy2(code, open, high, low, close, close_old, date):
    global g_flg_rise, g_high_max, g_flg_buy, g_timer_day, g_price_buy
    global g_date_buy

    rise = judge_rise(close_old)
    if (close == rise):
        g_flg_rise += 1
        g_high_max = high
    else:
        if (2 < g_flg_rise):
            if (g_high_max <= high):
                g_high_max = high
            g_price_buy = open + 0.1
            g_flg_rise = 0
            g_date_buy = date
            # print "buy"
            return 1
        else:
            g_flg_rise = 0

    return 0


def plot_sale(code, open, high, low, close, close_old, date):
    global g_high_max, g_cnt_win, g_cnt_lost, g_price_buy
    global g_total
    global g_date_buy, g_date_sale

    if (high == low):
        return 1

    if (g_high_max <= high):
        g_high_max = high
    if (0.9 >= close / g_price_buy):
        total_old = g_total
        g_total = total_old * close / g_price_buy
        print g_price_buy, g_high_max, close, total_old, g_total, date, "lost----------------------------------------------------------"
        g_cnt_lost += 1
        return 2
    else:
        gain = g_price_buy / close
        if (1.3 > gain):
            line = 0.8
        elif (1.2 > gain):
            line = 0.85
        else:
            line = 0.9
        if (line >= close / g_high_max or 1.05 > gain or 2 > close - g_price_buy):
            total_old = g_total
            g_total = total_old * close / g_price_buy
            print g_price_buy, g_high_max, close, total_old, g_total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
            g_cnt_win += 1
            # g_date_buy = ""
            g_date_sale = date
            return 2

    return 1


def read_buy_rate(rate, date, close, g_high_max):
    global g_date_buy_read, g_price_buy_read
    if (rate + 0.1 > close / g_high_max):
        g_date_buy_read = date
        g_price_buy_read = g_high_max * rate


def plot_buy3(code, open, high, low, close, close_old, date):
    global g_high_max, g_price_buy, g_date_buy, g_price_sale
    global g_date_buy_read, g_price_buy_read, g_price_sale_read

  
        #print "buy", date, g_price_buy
        return 1

    return 0



def plot_sale3(code, open, high, low, close, close_old, date):
    global g_cnt_win, g_price_buy, g_price_sale, g_days_diff
    global g_total, g_list_days
    global g_date_buy, g_date_sale, g_price_sale_read

    rise = judge_rise(close_old)
    if (close == rise):
        return 1
   
        code_clinch['high_max'] = float_2p(g_high_max)

        #print "----days", g_list_days
        g_list_days.append(code_clinch)

        g_date_buy = "00-00-00"
        g_price_buy = 0.00
        g_days_diff += code_clinch['day_diff']

        return 2

    return 1


dict_g_buy_sale_plot = {"plot_buy": plot_buy, "plot_buy2": plot_buy2, "plot_sale": plot_sale, "plot_buy3": plot_buy3,
                        "plot_sale3": plot_sale3}


def calculate_g_buy_sale(code, open, high, low, close, close_old, date, symbol):
    return dict_g_buy_sale_plot.get(symbol)(code, open, high, low, close, close_old, date)


def plot(code, quotes):
    global g_flg_rise, g_high_max, g_flg_buy, g_timer_day, g_cnt_win, g_cnt_lost, g_price_buy
    g_flg_rise, g_high_max, g_flg_buy, g_timer_day, g_cnt_win, g_cnt_lost, g_price_buy = 0, 0, 0, 0, 0, 0, 0
    global g_total
    g_total = 100
    global g_dict_total

    date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0
    flg_buy = 0

    for q in quotes:
        date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4])
        # print flg_buy
        if (0 == flg_buy):
            flg_buy = calculate_g_buy_sale(code, open, high, low, close, close_old, date, "plot_buy3")
        elif (1 == flg_buy):
            flg_buy = calculate_g_buy_sale(code, open, high, low, close, close_old, date, "plot_sale3")
        if (2 == flg_buy):
            g_flg_rise, g_flg_buy, = 0, 0
            flg_buy = 0
        date_old, open_old, high_old, low_old, close_old = date, open, high, low, close

    print g_total, g_cnt_win, g_cnt_lost


    g_dict_total['total'] = float_2p(g_total)
    g_dict_total['cnt_win'] = g_cnt_win
    g_dict_total['cnt_lost'] = g_cnt_lost
    #print g_dict_total ,"--------------------------------------------"
    return g_dict_total


def plot2(code, quotes):
    print "222"

def plot3(code, quotes):
    global g_high_max, g_flg_buy, g_cnt_win, g_cnt_lost, g_price_buy, g_date_buy, price_sale_read
    g_high_max, g_flg_buy, g_cnt_win, g_cnt_lost, g_price_buy = 0, 0, 0, 0, 0
    global g_total
    g_total = 100
    global g_dict_total, g_list_days

    date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0
    flg_buy = 0
    g_list_days = []
    g_days_diff = 0
    #print "---------------p3 enter----------------"
    for q in quotes:
        date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4])
        # print flg_buy
        if (0 == flg_buy):
            flg_buy = calculate_g_buy_sale(code, open, high, low, close, close_old, date, "plot_buy3")
        elif (1 == flg_buy):
            flg_buy = calculate_g_buy_sale(code, open, high, low, close, close_old, date, "plot_sale3")
        if (2 == flg_buy):
            flg_buy = 0
            g_flg_buy = 0

        date_old, open_old, high_old, low_old, close_old = date, open, high, low, close
    #print "---------------p3 bye----------------"
    print code, g_total, g_cnt_win, g_cnt_lost, g_date_buy, g_price_buy
    statistics = dict(code="",total=0, cnt_win=0, cnt_lost=0, days_diff=0,  days_diff_ave=0, date_buy="", price_buy=0.00, date_sale="", price_sale=0.00,
                      date_buy_read="", price_buy_read=0.00, close=0.00, rate=0.00, price_sale_read = 0.00, high_max=0.00)

    statistics['code'] = code
    statistics['total'] = float_2p(g_total)
    statistics['cnt_win'] = g_cnt_win
    statistics['cnt_lost'] = g_cnt_lost
    statistics['date_buy'] = g_date_buy
    statistics['price_buy'] = float_2p(g_price_buy)
    statistics['date_sale'] = g_date_sale
    statistics['price_sale'] = float_2p(g_price_sale)
    statistics['date_buy_read'] = g_date_buy_read
    statistics['price_buy_read'] = float_2p(g_price_buy_read)
    statistics['close'] = close_old
    statistics['rate'] = 1.0
    if (0 < g_price_buy):
        statistics['rate'] = float_2p(close_old / g_price_buy)
    statistics['price_sale_read'] = float_2p(g_price_sale_read)
    statistics['high_max'] = g_high_max
    statistics['days_diff'] = g_days_diff
    statistics['days_diff_ave'] = 0
    if (0 < g_cnt_win):
        statistics['days_diff_ave'] = g_days_diff / g_cnt_win

    #print g_list_days
    #print statistics ,"--------------------------------------------"
    return statistics, g_list_days

calculateDict = {"plot": plot, "plot2": plot2, "plot3": plot3}


def calculate(code, x, symbol):
    return calculateDict.get(symbol)(code, x)
 
class c_statistics(object):
    def __init__(self, code, total, cnt_win, cnt_lost, days_diff,  days_diff_ave, date_buy, price_buy, date_sale,
                 price_sale, date_buy_read, price_buy_read, close, rate1, price_sale_read, high_max):
        self.code = code
        self.total = total
        self.cnt_win = cnt_win
        self.cnt_lost = cnt_lost
        self.days_diff = days_diff
        self.days_diff_ave = days_diff_ave
        self.date_buy = date_buy
        self.price_buy = price_buy
        self.date_sale = date_sale
        self.price_sale = price_sale
        self.date_buy_read = date_buy_read
        self.price_buy_read = price_buy_read
        self.close = close
        self.rate = rate1
        self.price_sale_read = price_sale_read
        self.high_max = high_max


if (0 < g_price_buy):
    rate1 = float_2p(close_old / g_price_buy)
else:
    rate1 = 1.0
if (0 < g_cnt_win):
    days_diff_ave = g_days_diff / g_cnt_win
else:
    days_diff_ave = 0
statistics = c_statistics(code=code,total=float_2p(g_total), cnt_win=g_cnt_win, cnt_lost=g_cnt_lost, days_diff=g_days_diff,  days_diff_ave=days_diff_ave, date_buy=g_date_buy, price_buy=float_2p(g_price_buy), date_sale=g_date_sale, price_sale=float_2p(g_price_sale),
                      date_buy_read=g_date_buy_read, price_buy_read=float_2p(g_price_buy_read), close=close_old, rate1=rate1, price_sale_read = float_2p(g_price_sale_read), high_max=g_high_max)




class c_clinch(object):
    def __init__(self, code, total, date_buy, price_buy, date_sale, price_sale, rate1, cnt_win, high_max):
        self.code = code
        self.total = total
        self.date_buy = date_buy
        self.price_buy = price_buy
        self.date_sale = date_sale
        self.price_sale = price_sale
        self.rate = rate1
        self.cnt_win = cnt_win
        self.high_max = high_max
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值