Exemple #1
0
 def if_buy(self, context):
     data = context.db.select_data_by_number(context.asset_code, 70,
                                             context.date)
     ohlc = OHLCVD(data)
     ohlc.add_ma(60)
     ohlc.add_ma(10)
     data_frame = ohlc.get_dataframe()
     ma60_value = data_frame.ma60.values[-1]
     low = data_frame.low.values[-1]
     price = data_frame.close.values[-1]
     # print 'ma60 value:', ma60_value
     # print "current price :", price
     rate = (ma60_value - low)/ma60_value
     # self.logger.info("ma60 value :" + str(ma60_value) + " current price "+
     #                  str(price)+" rate is :" + str(rate))
     # print "this is the rate of the ma60 minus low", rate
     if (ma60_value - low) / ma60_value >= 0.1:
         order = context.account.create_buy_order(name=context.asset_code,
                                                  price=price,
                                                  context=context
                                                  )
         if order.number > 0:
             return order
         else:
             self.logger.info("can not create order")
             return None
     return None
Exemple #2
0
 def if_buy(self, context):
     data = context.db.select_data_by_number(context.asset_code, 70,
                                             context.date)
     ohlc = OHLCVD(data)
     ohlc.add_ma(60)
     ohlc.add_ma(10)
     data_frame = ohlc.get_dataframe()
     ma60_value = data_frame.ma60.values[-1]
     low = data_frame.low.values[-1]
     price = data_frame.close.values[-1]
     # print 'ma60 value:', ma60_value
     # print "current price :", price
     rate = (ma60_value - low) / ma60_value
     # self.logger.info("ma60 value :" + str(ma60_value) + " current price "+
     #                  str(price)+" rate is :" + str(rate))
     # print "this is the rate of the ma60 minus low", rate
     if (ma60_value - low) / ma60_value >= 0.1:
         order = context.account.create_buy_order(name=context.asset_code,
                                                  price=price,
                                                  context=context)
         if order.number > 0:
             return order
         else:
             self.logger.info("can not create order")
             return None
     return None
Exemple #3
0
    def test_ohlc_add_all_feature(self):
        data = pd.read_csv('d:/stock/new-data/SH600741.TXT', sep='\t')
        ohlc = OHLCVD(data.values)
        ohlc.add_all_ta_feature()
        data = ohlc.get_dataframe()
        result = data.replace(np.nan, -1.0).replace(np.inf, -2.0).replace(-np.inf, -3.0)


        sql_line = '''insert into stock_with_feature values('sh600741', '%s', %s )'''%(result.values[1, 0],
                                                                                       ','.join([str(x) for x in result.values[1, 1:]]), )

        print sql_line
        db = MySQLUtils('root', '1988', 'stock', 'stock_with_feature')

        db.execute_sql(sql_line)
        self.assertEqual(ohlc.data.shape[1], 149)
Exemple #4
0
    def test_ohlc_add_all_feature(self):
        data = pd.read_csv('d:/stock/new-data/SH600741.TXT', sep='\t')
        ohlc = OHLCVD(data.values)
        ohlc.add_all_ta_feature()
        data = ohlc.get_dataframe()
        result = data.replace(np.nan,
                              -1.0).replace(np.inf,
                                            -2.0).replace(-np.inf, -3.0)

        sql_line = '''insert into stock_with_feature values('sh600741', '%s', %s )''' % (
            result.values[1, 0],
            ','.join([str(x) for x in result.values[1, 1:]]),
        )

        print sql_line
        db = MySQLUtils('root', '1988', 'stock', 'stock_with_feature')

        db.execute_sql(sql_line)
        self.assertEqual(ohlc.data.shape[1], 149)
Exemple #5
0
 def if_buy(self, context):
     data = context.db.select_data_by_number(context.asset_code, 1,
                                             context.date)
     ohlc = OHLCVD(data)
     data = ohlc.get_dataframe()
     low_value = data.low.values[-1]
     if data.open.values[-1] > data.close.values[-1]:
         min_value = data.close.values[-1]
     else:
         min_value = data.open.values[-1]
     if (min_value - low_value) / data.close.values[-1] > 0.05:
         order = context.account.create_buy_order(context.asset_code,
                                                  price=data.close.values[-1],
                                                  context=context)
         if order is not None and order.number > 0:
             return order
         else:
             self.logger.info("can not create order")
             return None
     return None
Exemple #6
0
 def if_buy(self, context):
     data = context.db.select_data_by_number(context.asset_code, 1,
                                             context.date)
     ohlc = OHLCVD(data)
     data = ohlc.get_dataframe()
     low_value = data.low.values[-1]
     if data.open.values[-1] > data.close.values[-1]:
         min_value = data.close.values[-1]
     else:
         min_value = data.open.values[-1]
     if (min_value - low_value) / data.close.values[-1] > 0.05:
         order = context.account.create_buy_order(
             context.asset_code,
             price=data.close.values[-1],
             context=context)
         if order is not None and order.number > 0:
             return order
         else:
             self.logger.info("can not create order")
             return None
     return None
Exemple #7
0
def prepare_data(db, stock_list, begin, end):
    results = []
    labels = []
    for stock in stock_list:
        tmp_result = db.get_array(stock, begin=begin, end=end)
        print stock
        if tmp_result.shape[0] > 100:
            ohlc = OHLCVD(data=tmp_result)
            ohlc.add_macd()
            ohlc.add_fall_days(5)
            ohlc.add_raise_day(5)
            ohlc.add_raise_day(3)
            ohlc.add_ma(60)
            # ohlc.add_rsi_feature()
            ohlc.add_jump_empty_down()
            ohlc.normalize()
            results.append(ohlc.data)
            label = ohlc.future_return(5)
            labels.append(label)
        print len(results)
        print len(labels)
    return results
Exemple #8
0
            # ohlc.add_rsi_feature()
            ohlc.add_jump_empty_down()
            ohlc.normalize()
            results.append(ohlc.data)
            label = ohlc.future_return(5)
            labels.append(label)
        print len(results)
        print len(labels)
    return results


if __name__ == '__main__':
    db = MySQLUtils('root', '1988', 'stock', 'stock_with_feature')
    import pandas as pd
    data = pd.read_csv('d:/stock/new-data/sh600741.txt', sep='\t')
    result = OHLCVD(data.values)
    result.add_all_ta_feature()
    result = result.data_frame.replace(np.nan, -1.0).replace(np.inf, -2.0).replace(-np.inf, -3.0)
    db_name = "stock_with_feature"
    db.create_feature_db(db_name)
    db.insert_feature_data(db_name, result.values, 'sh600741')
    # result = db.get_array('sh600741', begin='2007-10-10', end='2015-12-12')
    # ohlc = OHLCVD(data=result)
    # ohlc.add_macd()
    # ohlc.add_fall_days(5)
    # ohlc.add_raise_day(5)
    # ohlc.add_raise_day(3)
    # ohlc.add_ma(60)
    # ohlc.add_jump_empty_down()
    # ohlc.normalize()
    #
Exemple #9
0
# coding=utf-8
__author__ = 'squall'
import numpy as np

from backtest.utils.mysql import MySQLUtils
from data.ohlc import OHLCVD

if __name__ == '__main__':
    db = MySQLUtils('root', '1988', 'stock', 'stock')
    result = db.get_array('sh600741', begin='2007-10-10', end='2015-12-12')
    ohlc = OHLCVD(data=result)
    print result
    print ohlc.get_dataframe()
    ohlc.add_macd()
    ohlc.add_ma(60)
    print ohlc.get_array().shape
    print ohlc.get_dataframe()
    vol_values = ohlc.data_frame.volume.values
    kernel = np.ones(20)*0.05
    result = np.convolve(kernel, vol_values, mode='valid')
    # plt.plot(result)
    # plt.show()
Exemple #10
0
 def get_ohlc(self, id, begin=None, end=None):
     result = self.select_data(id, begin=begin, end=end)
     result = OHLCVD(result)
     return result
Exemple #11
0
    def test_something(self):
        db = MySQLUtils('root', '1988', 'test', 'stock')
        data = db.get_array('sh600741', begin='2015-01-01', end='2016-01-01')
        ohlc = OHLCVD(data)
        ohlc.add_macd()
        ohlc.add_ma(10)
        ohlc.add_rsi_feature()
        ohlc.add_raise_day(5)
        ohlc.add_raise_day(10)
        ohlc.add_recent_down_v_turn()
        data = ohlc.get_array()

        print data.shape[1]
Exemple #12
0
def prepare_data(db, stock_list, begin, end):
    results = []
    labels = []
    for stock in stock_list:
        tmp_result = db.get_array(stock, begin=begin, end=end)
        print stock
        if tmp_result.shape[0] > 100:
            ohlc = OHLCVD(data=tmp_result)
            ohlc.add_macd()
            ohlc.add_fall_days(5)
            ohlc.add_raise_day(5)
            ohlc.add_raise_day(3)
            ohlc.add_ma(60)
            # ohlc.add_rsi_feature()
            ohlc.add_jump_empty_down()
            ohlc.normalize()
            results.append(ohlc.data)
            label = ohlc.future_return(5)
            labels.append(label)
        print len(results)
        print len(labels)
    return results
Exemple #13
0
            # ohlc.add_rsi_feature()
            ohlc.add_jump_empty_down()
            ohlc.normalize()
            results.append(ohlc.data)
            label = ohlc.future_return(5)
            labels.append(label)
        print len(results)
        print len(labels)
    return results


if __name__ == '__main__':
    db = MySQLUtils('root', '1988', 'stock', 'stock_with_feature')
    import pandas as pd
    data = pd.read_csv('d:/stock/new-data/sh600741.txt', sep='\t')
    result = OHLCVD(data.values)
    result.add_all_ta_feature()
    result = result.data_frame.replace(np.nan, -1.0).replace(np.inf,
                                                             -2.0).replace(
                                                                 -np.inf, -3.0)
    db_name = "stock_with_feature"
    db.create_feature_db(db_name)
    db.insert_feature_data(db_name, result.values, 'sh600741')
    # result = db.get_array('sh600741', begin='2007-10-10', end='2015-12-12')
    # ohlc = OHLCVD(data=result)
    # ohlc.add_macd()
    # ohlc.add_fall_days(5)
    # ohlc.add_raise_day(5)
    # ohlc.add_raise_day(3)
    # ohlc.add_ma(60)
    # ohlc.add_jump_empty_down()