Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 7
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()