def trend_least_square(self, prices, diff_vals, cutoff, mult): diff = self.find_matches(diff_vals, cutoff, mult) temp_prices = self.match_indexes(diff, prices) inter, slope = Helper.least_square(temp_prices) return Straight(inter, slope, 0, self.sim_vars.analysisRange), diff
def run_algo(self, stock): finished_trades = [] print "Running SR: %s" % (stock,) splits = [] data = Helper.read_data_file(self.data_file_loc + stock + self.data_file_ext) # Not all stocks have split data try: splits = Helper.read_split_file(self.split_file_loc + stock + self.split_file_ext) except: None trades = [] short_buffer_zone = False long_buffer_zone = False for i in range(0, len(data) - self.sim_vars.analysisRange, self.sim_vars.stepSize): prices = data[i: i+self.sim_vars.analysisRange] for j in range(0, len(prices)): prices[j].index = j close = prices[-1].close cur_split = Split(1.0, 0) for split in splits: if split.timestamp < prices[-1].timestamp < (split.timestamp + 86400): cur_split = split # Check if we can exit trade finished_trades.extend(self.check_exit(trades, prices[-1], cur_split)) # Find least square line of all prices inter, slope = Helper.least_square(prices) mean_least_square = Straight(inter, slope, 0, self.sim_vars.analysisRange) y_vals = mean_least_square.y_vals diff_vals = [prices[z].close - y_vals[z] for z in range(0, len(prices))] # Generate least square trend lines (res_least_square, res_diff) = self.trend_least_square(prices, diff_vals, self.sim_vars.resCutoff, 1) (sup_least_square, sup_diff) = self.trend_least_square(prices, diff_vals, self.sim_vars.supCutoff, -1) res_val = res_least_square.y_vals[-1] sup_val = sup_least_square.y_vals[-1] # Generate range to enter trade max_long_buy_point = sup_val + (res_val-sup_val)*self.sim_vars.resMaxBuyPer/100 min_long_buy_point = sup_val + (res_val-sup_val)*self.sim_vars.resMinBuyPer/100 max_short_sell_point = res_val - (res_val-sup_val)*self.sim_vars.supMinBuyPer/100 min_short_sell_point = res_val - (res_val-sup_val)*self.sim_vars.supMaxBuyPer/100 if long_buffer_zone or short_buffer_zone: # Find sections of line that have matches pos_matches = [False, False, False] neg_matches = [False, False, False] for k in range(0, 3): for diff in res_diff: if (k+1)*(len(prices))//3 > diff.index > k*(len(prices))//3: pos_matches[k] = True for diff in sup_diff: if (k+1)*(len(prices))//3 > diff.index > k*(len(prices))//3: neg_matches[k] = True buy_point, sell_point, pot_buy, actual_type = Helper.trendType(res_least_square.slope, sup_least_square.slope, res_least_square.intercept, sup_least_square.intercept, self.sim_vars.analysisRange, self.sim_vars.supMinBuyPer/100.0, self.sim_vars.resMinBuyPer/100.0, close, self.sim_vars.analysisRange-1, self.sim_vars.analysisRange-1) #only consider buying when support matches in middle and at least one side long_pot_buy = pot_buy and (neg_matches[1] and (neg_matches[0] or neg_matches[2])) \ and (pos_matches[1] and (pos_matches[0] and pos_matches[2])) short_pot_buy = long_pot_buy if self.sim_vars.longStocks and long_buffer_zone and sell_point > close*(1.0 + self.sim_vars.minimumPercent/100.0) and long_pot_buy: if min_long_buy_point <= close <= max_long_buy_point: long_buffer_zone = False t = PlotTrade("long", prices[-1].timestamp, None, close, None, None, None, sell_point, close - close*self.sim_vars.stopLossPerc/100.0, prices, sup_least_square, res_least_square, stock) t.mean_line = mean_least_square trades.append(t) elif self.sim_vars.shortStocks and short_buffer_zone and buy_point < close/(1.0 + self.sim_vars.minimumPercent/100.0) and short_pot_buy: if min_short_sell_point <= close <= max_short_sell_point: short_buffer_zone = False t = PlotTrade("short", prices[-1].timestamp, None, close, None, None, None, buy_point, close + close*self.sim_vars.stopLossPerc/100, prices, sup_least_square, res_least_square, stock) t.mean_line = mean_least_square trades.append(t) # We only want to allow trades if we go below a buffer zone # Check if within buffer zone if sup_val + (res_val-sup_val)*(self.sim_vars.bufferPercent/100.0) >= close: long_buffer_zone = True elif close < min_long_buy_point and long_buffer_zone: long_buffer_zone = True else: long_buffer_zone = False if res_val - (res_val-sup_val)*(self.sim_vars.bufferPercent/100.0) <= close: short_buffer_zone = True elif close > max_short_sell_point and short_buffer_zone: short_buffer_zone = True else: short_buffer_zone = False return finished_trades