Beispiel #1
0
 def _passed_to(self, datei, point, last_pm=0):
     '''
     return (True/False:If passed, Int:-1=out to down, 0=on, 1=out to up)
     '''
     ret = (False, 0)
     if point <= 0:
         return ret
     
     (open, high, low, close, volume) = self._get_prices(datei, 2)
     tp = (high[1]+low[1]+close[1])/3
     
     boxsize = kf.get_bollinger_boxsize(tp)
     
     cmp = tp - point
     
     if abs(cmp) < boxsize:
         return ret
     
     if max(high) >= point and min(low) <= point:
         if cmp > 0 and last_pm <= 0:
             return (True, 1)
         elif cmp < 0 and last_pm >= 0:
             return (True, -1)
         else:
             return ret
     else:
         if cmp > 0:
             return (False, 1)
         elif cmp < 0:
             return (False, -1)
         else:
             return ret
Beispiel #2
0
 def _get_nearbys(self, price, points):
     points.insert(0, price)
     order = np.argsort(points)
     
     bef = []
     aft = []
     boxsize = kf.get_bollinger_boxsize(price)
     for j in range(order):
         i = order[j]
         if i == 0:
             if j > 0:
                 bef = [order[j-1]]
                 k=j-2
                 while k>=0:
                     if points[order[k]] >= bef - boxsize:
                         bef.append(order[k])
                     k -= 1
             if j < len(order):
                 aft = order[j+1]
                 k=j+2
                 while k<len(order):
                     if points[order[k]] <= aft + boxsize:
                         aft.append(order[k])
                     k += 1
     return bef, aft
 def _passed_to(self, datei, point):
     '''
     return (True/False:If passed, Int:-1=out to down, 0=on, 1=out to up)
     '''
     (open, high, low, close) = self._get_prices(datei, 2)
     tp = (high[1]+low[1]+close[1])/3
     
     boxsize = kf.get_bollinger_boxsize(point)
     
     cpm = tp - point
     
     if abs(cpm) < boxsize:
         return (True, 0)
     
     if max(high) >= point and min(low) <= point:
         if cmp > 0:
             return (True, 1)
         elif cmp < 0:
             return (True, -1)
         else:
             return (True, 0)
     else:
         if cmp > 0:
             return (False, 1)
         elif cmp < 0:
             return (False, -1)
         else:
             return (False, 0)
 def _get_nearest_line_idxs(self, datei, line_points, exclude_idxs, trend):
     (open, high, low, close) = self._get_prices(datei, 1)
     tp = (high+low+close)/3
     
     line_points.append(tp)
     tp_idx = len(line_points)-1
     
     order = np.argsort(line_points)
     
     tp_order = -1
     ex_order = [-1]*len(exclude_idxs)
     for i in range(len(order)):
         if order[i] == tp_idx:
             tp_order = i
         for j in range(len(exclude_idxs)):
             exi = exclude_idxs[j]
             if order[i] == exi:
                 ex_order[j] = i
                 
     boxsize = kf.get_bollinger_boxsize(tp)
     nearest_line_idxs = []
     if tp_order > max(ex_order):
         if trend == "up":
             if tp_order == len(order)-1:
                 return [-1]
             else:
                 for i in range(tp_order+1,len(order)):
                     if len(nearest_line_idxs) == 0:
                         nearest_line_idxs = [order[i]]
                     else:
                         curr_point_idx = order[i]
                         pre_point_idx = nearest_line_idxs[-1]
                         if line_points[curr_point_idx] - boxsize <= line_points[pre_point_idx]:
                             nearest_line_idxs.append(curr_point_idx)
         else:
             return []
     
     if tp_order < min(ex_order):
         if trend == "down":
             if tp_order == 0:
                 return [-1]
             else:
                 r = range(tp_order).sort(reverse=True)
                 for i in r:
                     if len(nearest_line_idxs) == 0:
                         nearest_line_idxs = [order[i]]
                     else:
                         curr_point_idx = order[i]
                         pre_point_idx = nearest_line_idxs[-1]
                         if line_points[curr_point_idx] + boxsize >= line_points[pre_point_idx]:
                             nearest_line_idxs.append(curr_point_idx)
         else:
             return []
     return nearest_line_idxs
 def _get_touched_line_idxs(self, datei, line_points):
     (open, high, low, close) = self._get_prices(datei, 2)
     h = max(high)
     l = min(low)
     
     touched_line_idxs = []
     price = (high[-1]+low[-1]+close[-1])/3
     boxsize = kf.get_bollinger_boxsize(price)
     
     i = 0
     for i in range(line_points):
         lp = line_points[i]
         if h+boxsize >= lp and l-boxsize <= lp:
             touched_line_idxs.append(i)
     
     return touched_line_idxs
 def _get_trend_lines(self, joints, indexes, pmmask):
     j = 5
     trend_lines = {}
     for i in range(5, len(indexes)):
         if pmmask[i] != pmmask[i-2] or pmmask[i] != pmmask[i-4]:
             continue
         
         df = indexes[i]-indexes[i-4]
         if df != 0:
             slope = (joints[i]-joints[i-4])*1.0/df
         else:
             slope = 0
         const = joints[i-4]-slope*indexes[i-4]
         midrp = joints[i-2]
         midlp = self._eq(indexes[i-2], slope, const)
         boxsize = kf.get_bollinger_boxsize(midrp)
         if midlp - boxsize <= midrp and midlp + boxsize >= midrp:
             for d in range(indexes[i], indexes[i] + indexes[i]-indexes[i-4]+1):
                 if trend_lines.has_key(d) == False:
                     trend_lines[d] = []
                 trend_lines[d].append(self._eq(d, slope, const))
         j += 1
     self.trend_lines = trend_lines