Exemplo n.º 1
0
	def create_stddev(self, num_history_pts):
		self.output_table_name = self.output_table_name.replace("TREND", "POINT")
		self.output_table_name = self.output_table_name.replace("CANDLE", "POINT")
		self.output_table_name = self.output_table_name + self.STDDEV + "_" + str(num_history_pts)

		if self.to_save:
			##if already exists, drop it first and then recreate
			if DBManager.exists_table(self.output_table_name):
				DBManager.drop_table(self.output_table_name)
			
			pt = PointTable(self.output_table_name)
			pt.save()

		orig_pt_array = self.input_points
		stddev_pt_array = []

		for i, pt in enumerate(orig_pt_array):
			if i < num_history_pts: ##don't calculate stddev for first points since there is not enough history available
				pass
			else:
				date = pt.date
				stddev = StandardDeviation.simple(orig_pt_array[i-num_history_pts + 1: i+ 1])
				stddev_pt = Point(self.output_table_name, date, stddev)
				stddev_pt_array.append(stddev_pt)
		
		if self.to_save:
			self.save_pts(stddev_pt_array)

		##if CandleTable.TEMP in self.input_point_table_name:
		##	DBManager.drop_table(self.input_point_table_name)
		
		return stddev_pt_array
Exemplo n.º 2
0
	def create_oscil(self, period):
		self.output_table_name = self.output_table_name.replace("TREND", "POINT")
		self.output_table_name = self.output_table_name.replace("CANDLE", "POINT")
		self.output_table_name = self.output_table_name + self.OSCIL + "_" + str(period)
		
		if self.to_save:
			##if already exists, drop it first and then recreate
			if DBManager.exists_table(self.output_table_name):
				DBManager.drop_table(self.output_table_name)
			
			pt = PointTable(self.output_table_name)
			pt.save()
		
		oscil_pt_array = []

		for i, pt in enumerate(self.input_points):
			if i < (period-1): ##don't calculate stddev for first points since there is not enough history available
				pass
			else:
				date = pt.date
				oscil_value = pt.value - self.input_points[i-period].value
				oscil_pt = Point(self.output_table_name, date, oscil_value)
				oscil_pt_array.append(oscil_pt)
		
		
		if self.to_save:
			self.save_pts(oscil_pt_array)
		
		return oscil_pt_array
Exemplo n.º 3
0
	def create_moving_avg_simple(self, num_history_pts):
		self.output_table_name = self.output_table_name.replace("TREND", "POINT")
		self.output_table_name = self.output_table_name.replace("CANDLE", "POINT")
		self.output_table_name = self.output_table_name + self.SIMPLE_AVG + "_" + str(num_history_pts)
		
		if self.to_save:
			##if already exists, drop it first and then recreate
			if DBManager.exists_table(self.output_table_name):
				DBManager.drop_table(self.output_table_name)
			
			pt = PointTable(self.output_table_name)
			pt.save()
		
		points = self.input_points 
		mv = MovingAverage(self.output_table_name, points)
		pt_array = mv.simple(num_history_pts)
		
		if self.to_save:
			self.save_pts(pt_array)
		
		## possible delete the temporary point table created from candle
		##if CandleTable.TEMP in self.input_point_table_name:
		##	DBManager.drop_table(self.input_point_table_name)
		
		return pt_array
Exemplo n.º 4
0
    def decide(self, time, bits):
        cur_date = self.cur_traded_candles[time].date  ##current date

        ## if cur_date is too early to have valid trend data return no operation
        if not self.is_valid_date(cur_date):
            return Operation(Operation.NONE_OP, 0)

        trend_date_present = cur_date - self.DELAY * self.DAY  ##date to lookup in trend table
        trend_date_past = trend_date_present - self.DAY  ##date to lookup in trend table

        past_short_val = PointTable.lookup_date(self.avg_table_name_short,
                                                trend_date_past).value
        past_long_val = PointTable.lookup_date(self.avg_table_name_long,
                                               trend_date_past).value
        present_short_val = PointTable.lookup_date(self.avg_table_name_short,
                                                   trend_date_present).value
        present_long_val = PointTable.lookup_date(self.avg_table_name_long,
                                                  trend_date_present).value
        ##detect intersection between the long and short trend tables
        if present_short_val >= present_long_val:
            if past_short_val >= past_long_val:  ## no intersection
                return Operation(Operation.NONE_OP, 0)
            elif past_short_val < past_long_val:  ## intersection with short term spike
                if self.is_valid_buy(cur_date):
                    return Operation(Operation.BUY_OP, self.BUY_AMOUNT)
                else:
                    return Operation(Operation.NONE_OP, 0)
        elif present_short_val <= present_long_val:
            if past_short_val <= past_long_val:  ##no intersection
                return Operation(Operation.NONE_OP, 0)
            elif past_short_val > past_long_val:  ## intersection with short term trough
                return Operation(Operation.SELL_OP, bits)
Exemplo n.º 5
0
    def create_bb(self, type):
        bb_table_name = self.middle_avg_table_name + "_" + type

        ##if already exists, drop it first and then recreate
        if DBManager.exists_table(bb_table_name):
            DBManager.drop_table(bb_table_name)

        bb_pt_table = PointTable(bb_table_name)
        bb_pt_table.save()

        for i, avg in enumerate(self.avg_pts):
            if i < self.avg_period:
                pass
            else:
                date = avg.date
                if type == self.LOW:
                    value = avg.value - (
                        self.bb_factor *
                        self.stddev_pts[i - self.avg_period].value)
                elif type == self.HIGH:
                    value = avg.value + (
                        self.bb_factor *
                        self.stddev_pts[i - self.avg_period].value)

                new_pt = Point(bb_table_name, date, value)
                new_pt.save()
        dbm = DBManager.get_instance()
        dbm.save_and_close()
        return bb_table_name
Exemplo n.º 6
0
 def is_valid_date(self, date):
     earliest_date = PointTable.lookup(self.avg_table_name_short, 1).date
     latest_date = PointTable.get_last(self.avg_table_name_short).date
     if (date - (self.DELAY + 1) * self.DAY) < earliest_date:
         return False
     else:
         if date > latest_date:
             return False
         else:
             return True
Exemplo n.º 7
0
    def __init__(self, ct_name):
        self.ct_name = ct_name
        self.candles = CandleTable.get_candle_array(ct_name)

        self.pt_name_close = CandleTable.to_point_table(ct_name, Candle.CLOSE)
        self.points_close = PointTable.get_point_array(self.pt_name_close)

        self.pt_name_volume = CandleTable.to_point_table(
            ct_name, Candle.VOLUME)
        self.points_volume = PointTable.get_point_array(self.pt_name_volume)
Exemplo n.º 8
0
    def to_point_table(trend_table_name):
        pt_name = TrendTable.TEMP + "_" + trend_table_name

        ##if already exists, drop it first and then recreate
        if DBManager.exists_table(pt_name):
            DBManager.drop_table(pt_name)

        pt = PointTable(pt_name)
        pt.save()
        trends = TrendTable.get_trend_array(trend_table_name)
        for t in trends:
            p = Point(pt_name, t.date, t.hits)
            p.save()
        return pt_name
Exemplo n.º 9
0
    def decide(self, time, bits):
        cur_timestamp = self.cur_traded_candles[time].date
        trend_timestamp = cur_timestamp - self.DELAY * self.DAY
        cur_date = timestamp_to_date(cur_timestamp)

        if cur_date != self.last_date:
            self.last_date = cur_date

            try:
                past_avg = PointTable.lookup_date(self.avg_table_name,
                                                  trend_timestamp).value
                print(("Number of views: ", past_avg))
            except:
                return Operation(Operation.NONE_OP, 0)

            ## ask for user input
            response = raw_input("Please enter trade: ")
            if response == self.BUY_CMD:
                return Operation(Operation.BUY_OP, self.BUY_AMOUNT)
            elif response == self.SELL_CMD:
                return Operation(Operation.SELL_OP, bits)
            else:
                return Operation(Operation.NONE_OP, 0)
        else:
            return Operation(Operation.NONE_OP, 0)
Exemplo n.º 10
0
	def to_point_table(candle_table_name, attribute):
		pt_name = CandleTable.TEMP + "_" + candle_table_name
		
		##if already exists, drop it first and then recreate
		if DBManager.exists_table(pt_name):
			DBManager.drop_table(pt_name)
		
		pt = PointTable(pt_name)
		pt.save()
		candles = CandleTable.get_candle_array(candle_table_name)
		for c in candles:
			p = Point(pt_name, c.date, getattr(c, attribute))
			p.save()
		dbm = DBManager.get_instance()
		dbm.save_and_close()
		return pt_name
Exemplo n.º 11
0
	def setup(self):
		if self.POINT in self.table_name:
			self.input_points = PointTable.to_point_array(self.table_name)
		elif self.TREND in self.table_name:
			self.input_points = TrendTable.to_point_array(self.table_name)
		else:
			self.input_points = CandleTable.to_point_array(self.table_name, "close")

		self.output_table_name = self.table_name.replace(self.CANDLE, self.POINT)
Exemplo n.º 12
0
	def create_roc(self):
		self.output_table_name += self.SIMPLE_ROC
		pt_name = self.table_name
		
		##if already exists, drop it first and then recreate
		if DBManager.exists_table(self.output_table_name):
			DBManager.drop_table(self.output_table_name)
		
		pt = PointTable(self.output_table_name)
		pt.save()

		points = self.input_points 
		r = ROCCalculator(self.output_table_name, points)
		pt_array = r.simple()
		self.save_pts(pt_array)
		
		## possible delete the temporary point table created from candle
		##if CandleTable.TEMP in self.input_point_table_name:
		##	DBManager.drop_table(self.input_point_table_name)
		
		return self.output_table_name
Exemplo n.º 13
0
	def create_moving_avg_exp(self):
		self.output_table_name += self.EXP_AVG
		
		pt_name = self.table_name
		
		if DBManager.exists_table(self.output_table_name):
			DBManager.drop_table(self.output_table_name)
		
		pt = PointTable(self.output_table_name)
		pt.save()

		points = self.input_points 
		mv = MovingAverage(self.output_table_name, points)
		pt_array = mv.exponential()
		self.save_pts(pt_array)
		
		## possible delete the temporary point table created from candle
		##if CandleTable.TEMP in self.input_point_table_name:
		##	DBManager.drop_table(self.input_point_table_name)
		
		return self.output_table_name
Exemplo n.º 14
0
    def calc_vol_tables(self):
        if self.calc_stats:
            self.strat_stat_array = []
            self.ss_tn = StratStatTable.calc_name(self.table_name, self.NAME)
            if DBManager.exists_table(self.ss_tn):
                DBManager.drop_table(self.ss_tn)
            sst = StratStatTable(self.ss_tn)
            sst.save()

        pt_name = CandleTable.to_point_table(self.table_name, "volume")
        pt_close = CandleTable.to_point_table(self.table_name, "close")
        self.points = PointTable.get_point_array(pt_name)
        self.points_close = PointTable.get_point_array(pt_close)
        pt_name_1 = "TEMP1"
        mv1 = MovingAverage(pt_name_1, self.points)
        pt_name_2 = "TEMP2"
        mv2 = MovingAverage(pt_name_2, self.points)
        self.vol_pts_short = mv1.simple(self.VOL_PERIOD_SHORT)
        self.vol_pts_long = mv2.simple(self.VOL_PERIOD_LONG)

        ##pp = PointPopulator(self.table_name)
        ##self.stddev_pts_short = pp.create_stddev(self.STDDEV_PERIOD_SHORT)
        ##self.stddev_pts_long = pp.create_stddev(self.STDDEV_PERIOD_LONG)
        DBManager.drop_matching_tables("TEMP")