code: import numpy as np
  from filter import movingaverage
  
  def gentrends(x, window=1/3.0, charts=True):
      """
      Returns a Pandas dataframe with support and resistance lines.
  
      :param x: One-dimensional data set
      :param window: How long the trendlines should be. If window < 1, then it
                     will be taken as a percentage of the size of the data
      :param charts: Boolean value saying whether to print chart to screen
  
      """
      import numpy as np
      import pandas.io.data as pd
  
      x = np.array(x)
  
      if window < 1:
          window = int(window * len(x))
  
      max1 = np.where(x == max(x))[0][0]  # find the index of the abs max
      min1 = np.where(x == min(x))[0][0]  # find the index of the abs min
  
      # First the max
      if max1 + window > len(x):
          max2 = max(x[0:(max1 - window)])
      else:
          max2 = max(x[(max1 + window):])
  
      # Now the min
      if min1 - window < 0:
          min2 = min(x[(min1 + window):])
      else:
          min2 = min(x[0:(min1 - window)])
  
      # Now find the indices of the secondary extrema
      max2 = np.where(x == max2)[0][0]  # find the index of the 2nd max
      min2 = np.where(x == min2)[0][0]  # find the index of the 2nd min
  
      # Create & extend the lines
      maxslope = (x[max1] - x[max2]) / (max1 - max2)  # slope between max points
      minslope = (x[min1] - x[min2]) / (min1 - min2)  # slope between min points
      a_max = x[max1] - (maxslope * max1)  # y-intercept for max trendline
      a_min = x[min1] - (minslope * min1)  # y-intercept for min trendline
      b_max = x[max1] + (maxslope * (len(x) - max1))  # extend to last data pt
      b_min = x[min1] + (minslope * (len(x) - min1))  # extend to last data point
      maxline = np.linspace(a_max, b_max, len(x))  # Y values between max's
      minline = np.linspace(a_min, b_min, len(x))  # Y values between min's
  
      # OUTPUT
      trends = np.transpose(np.array((x, maxline, minline)))
      trends = pd.DataFrame(trends, index=np.arange(0, len(x)),
                            columns=['Data', 'Max Line', 'Min Line'])
  
      if charts is True:
          from matplotlib.pyplot import plot, grid, show, figure
          figure()
          plot(trends)
          grid()
          show()
  
      return trends, maxslope, minslope
  
  def segtrends(x, segments=2, charts=True, window=7):
      """
      Turn minitrends to iterative process more easily adaptable to
      implementation in simple trading systems; allows backtesting functionality.
  
      :param x: One-dimensional data set
      :param window: How long the trendlines should be. If window < 1, then it
                     will be taken as a percentage of the size of the data
      :param charts: Boolean value saying whether to print chart to screen
      """
  
      import numpy as np
      y = np.array(x)
      n=len(y)
      movy = movingaverage(y, window)
      # Implement trendlines and Find the indexes of these maxima in the data
      segments = int(segments)
      maxima = np.ones(segments)
      minima = np.ones(segments) 
      x_maxima = np.ones(segments)
      x_minima = np.ones(segments)
      segsize = int(len(y)/segments)
      for i in range(1, segments+1):
          ind2 = i*segsize
          ind1 = ind2 - segsize
          seg = y[ind1:ind2]
          maxima[i-1] = max(seg)
          minima[i-1] = min(seg)
          x_maxima[i-1] = ind1 + (np.where(seg == maxima[i-1])[0][0])
          x_minima[i-1] = ind1 + (np.where(seg == minima[i-1])[0][0])
  
      if charts:
          import matplotlib.pyplot as plt
          plt.plot(y)
          plt.grid(True)
  
      for i in range(0, segments-1):
          maxslope = (maxima[i+1] - maxima[i]) / (x_maxima[i+1] - x_maxima[i])
          a_max = maxima[i] - (maxslope * x_maxima[i])
          b_max = maxima[i] + (maxslope * (len(y) - x_maxima[i]))
          maxline = np.linspace(a_max, b_max, len(y))
  
          minslope = (minima[i+1] - minima[i]) / (x_minima[i+1] - x_minima[i])
          a_min = minima[i] - (minslope * x_minima[i])
          b_min = minima[i] + (minslope * (len(y) - x_minima[i]))
          minline = np.linspace(a_min, b_min, len(y))
  
          if charts:
              #plt.plot(maxline, 'g')
              #plt.plot(minline, 'r')
              pass
  
      if charts:
          plt.plot(range(n), movy, 'b')
          plt.plot(x_maxima, maxima, 'g')
          plt.plot(x_minima, minima, 'r')
          plt.show()
  
      # OUTPUT
      return x_maxima, maxima, x_minima, minima
  
  def minitrends(x, window=20, charts=True):
      """
      Turn minitrends to iterative process more easily adaptable to
      implementation in simple trading systems; allows backtesting functionality.
  
      :param x: One-dimensional data set
      :param window: How long the trendlines should be. If window < 1, then it
                     will be taken as a percentage of the size of the data
      :param charts: Boolean value saying whether to print chart to screen
      """
  
      import numpy as np
  
      y = np.array(x)
      if window < 1:  # if window is given as fraction of data length
          window = float(window)
          window = int(window * len(y))
      x = np.arange(0, len(y))
      dy = y[window:] - y[:-window]
      crit = dy[:-1] * dy[1:] < 0
  
      # Find whether max's or min's
      maxi = (y[x[crit]] - y[x[crit] + window] > 0) & \
             (y[x[crit]] - y[x[crit] - window] > 0) * 1
      mini = (y[x[crit]] - y[x[crit] + window] < 0) & \
             (y[x[crit]] - y[x[crit] - window] < 0) * 1
      maxi = maxi.astype(float)
      mini = mini.astype(float)
      maxi[maxi == 0] = np.nan
      mini[mini == 0] = np.nan
      xmax = x[crit] * maxi
      xmax = xmax[~np.isnan(xmax)]
      xmax = xmax.astype(int)
      xmin = x[crit] * mini
      xmin = xmin[~np.isnan(xmin)]
      xmin = xmin.astype(int)
  
      # See if better max or min in region
      yMax = np.array([])
      xMax = np.array([])
      for i in xmax:
          indx = np.where(xmax == i)[0][0] + 1
          try:
              Y = y[i:xmax[indx]]
              yMax = np.append(yMax, Y.max())
              xMax = np.append(xMax, np.where(y == yMax[-1])[0][0])
          except:
              pass
      yMin = np.array([])
      xMin = np.array([])
      for i in xmin:
          indx = np.where(xmin == i)[0][0] + 1
          try:
              Y = y[i:xmin[indx]]
              yMin = np.append(yMin, Y.min())
              xMin = np.append(xMin, np.where(y == yMin[-1])[0][0])
          except:
              pass
      if y[-1] > yMax[-1]:
          yMax = np.append(yMax, y[-1])
          xMax = np.append(xMax, x[-1])
      if y[0] not in yMax:
          yMax = np.insert(yMax, 0, y[0])
          xMax = np.insert(xMax, 0, x[0])
      if y[-1] < yMin[-1]:
          yMin = np.append(yMin, y[-1])
          xMin = np.append(xMin, x[-1])
      if y[0] not in yMin:
          yMin = np.insert(yMin, 0, y[0])
          xMin = np.insert(xMin, 0, x[0])
  
      # Plot results if desired
      if charts is True:
          from matplotlib.pyplot import plot, show, grid
          plot(x, y)
          plot(xMax, yMax, '-o')
          plot(xMin, yMin, '-o')
          grid(True)
          show()
      # Return arrays of critical points
      return xMax, yMax, xMin, yMin
  
  def iterlines(x, window=30, charts=True):
      """
      Turn minitrends to iterative process more easily adaptable to
      implementation in simple trading systems; allows backtesting functionality.
  
      :param x: One-dimensional data set
      :param window: How long the trendlines should be. If window < 1, then it
                     will be taken as a percentage of the size of the data
      :param charts: Boolean value saying whether to print chart to screen
      """
  
      import numpy as np
  
      x = np.array(x)
      n = len(x)
      if window < 1:
          window = int(window * n)
      sigs = np.zeros(n, dtype=float)
  
      i = window
      while i != n:
          if x[i] > max(x[i-window:i]): sigs[i] = 1
          elif x[i] < min(x[i-window:i]): sigs[i] = -1
          i += 1
  
      xmin = np.where(sigs == -1.0)[0]
      xmax = np.where(sigs == 1.0)[0]
      ymin = x[xmin]
      ymax = x[xmax]
      if charts is True:
          from matplotlib.pyplot import plot, grid, show
          plot(x)
          plot(xmin, ymin, 'ro')
          plot(xmax, ymax, 'go')
          grid(True)
          show()
  
      return sigs
   import tensorflow as tf
  
  from src.nn_utils.general import exp_mask_for_high_rank, mask_for_high_rank
  from src.nn_utils.integration_func import directional_attention_with_dense
  from src.nn_utils.nn import bn_dense_layer, linear
  
  
  def bi_directional_simple_block_attention(
          rep_tensor, rep_mask, block_len=5, scope=None,
          keep_prob=1., is_train=None, wd=0., activation='elu', hn=None):
      with tf.variable_scope(scope or 'bi_directional_simple_block_attn'):
  
          fw_attn_res = simple_block_attention(
              rep_tensor, rep_mask, block_len, "forward_attn", "forward",
              keep_prob, is_train, wd, activation, hn)
          bw_attn_res = simple_block_attention(
              rep_tensor, rep_mask, block_len, "backward_attn", "backward",
              keep_prob, is_train, wd, activation, hn)
          attn_res = tf.concat([fw_attn_res, bw_attn_res], -1)
          return attn_res
  
  
  def simple_block_attention(
          rep_tensor, rep_mask, block_len=5, scope=None, direction=None,
          keep_prob=1., is_train=None, wd=0., activation='elu', hn=None):
      assert direction is not None
  
      def scaled_tanh(x, scale=5.):
          return scale * tf.nn.tanh(1. / scale * x)
  
      bs, sl, vec = tf.shape(rep_tensor)[0], tf.shape(rep_tensor)[1], tf.shape(rep_tensor)[2]
      ivec = hn or rep_tensor.get_shape().as_list()[2]
      input_dim = rep_tensor.get_shape().as_list()[2]
      with tf.variable_scope(scope or 'block_simple'):
          # @1. split sequence
          with tf.variable_scope('split_seq'):
              block_num = tf.cast(tf.ceil(tf.divide(tf.cast(sl, tf.float32), tf.cast(block_len, tf.float32))), tf.int32)
              comp_len = block_num * block_len - sl
  
              rep_tensor_comp = tf.concat([rep_tensor, tf.zeros([bs, comp_len, input_dim], tf.float32)], 1)
              rep_mask_comp = tf.concat([rep_mask, tf.cast(tf.zeros([bs, comp_len], tf.int32), tf.bool)], 1)
  
              rep_tensor_split = tf.reshape(rep_tensor_comp, [bs, block_num, block_len, input_dim])  # bs,bn,bl,d
              rep_mask_split = tf.reshape(rep_mask_comp, [bs, block_num, block_len])  # bs,bn,bl
  
              # non-linear
              rep_map = bn_dense_layer(rep_tensor_split, ivec, True, 0., 'bn_dense_map', activation,
                                       False, wd, keep_prob, is_train)  # bs,bn,bl,vec
              rep_map_tile = tf.tile(tf.expand_dims(rep_map, 2), [1, 1, block_len, 1, 1])  # bs,bn,bl,bl,vec
              # rep_map_dp = dropout(rep_map, keep_prob, is_train)
              bn = block_num
              bl = block_len
  
          with tf.variable_scope('self_attention'):
              # @2.self-attention in block
              # mask generation
              sl_indices = tf.range(block_len, dtype=tf.int32)
              sl_col, sl_row = tf.meshgrid(sl_indices, sl_indices)
              if direction == 'forward':
                  direct_mask = tf.greater(sl_row, sl_col)  # bl,bl
              else:
                  direct_mask = tf.greater(sl_col, sl_row)  # bl,bl
              direct_mask_tile = tf.tile(
                  tf.expand_dims(tf.expand_dims(direct_mask, 0), 0), [bs, bn, 1, 1])  # bs,bn,bl,bl
              rep_mask_tile_1 = tf.tile(tf.expand_dims(rep_mask_split, 2), [1, 1, bl, 1])  # bs,bn,bl,bl
              rep_mask_tile_2 = tf.tile(tf.expand_dims(rep_mask_split, 3), [1, 1, 1, bl])  # bs,bn,bl,bl
              rep_mask_tile = tf.logical_and(rep_mask_tile_1, rep_mask_tile_2)
              attn_mask = tf.logical_and(direct_mask_tile, rep_mask_tile, name='attn_mask')  # bs,bn,bl,bl
  
              # attention
              f_bias = tf.get_variable('f_bias', [ivec], tf.float32, tf.constant_initializer(0.))
              dependent_head = linear(
                  rep_map, 2 * ivec, False, 0., 'linear_dependent_head', False, wd, keep_prob, is_train)  # bs,bn,bl,2vec
              dependent, head = tf.split(dependent_head, 2, 3)
              dependent_etd = tf.expand_dims(dependent, 2)  # bs,bn,1,bl,vec
              head_etd = tf.expand_dims(head, 3)  # bs,bn,bl,1,vec
              logits = scaled_tanh(dependent_etd + head_etd + f_bias, 5.0)  # bs,bn,bl,bl,vec
              logits_masked = exp_mask_for_high_rank(logits, attn_mask)
              attn_score = tf.nn.softmax(logits_masked, 3)  # bs,bn,bl,bl,vec
              attn_score = mask_for_high_rank(attn_score, attn_mask)  # bs,bn,bl,bl,vec
              self_attn_result = tf.reduce_sum(attn_score * rep_map_tile, 3)  # bs,bn,bl,vec
  
          with tf.variable_scope('source2token_self_attn'):
              inter_block_logits = bn_dense_layer(self_attn_result, ivec, True, 0., 'bn_dense_map', 'linear',
                                                  False, wd, keep_prob, is_train)  # bs,bn,bl,vec
              inter_block_logits_masked = exp_mask_for_high_rank(inter_block_logits, rep_mask_split)  # bs,bn,bl,vec
              inter_block_soft = tf.nn.softmax(inter_block_logits_masked, 2)  # bs,bn,bl,vec
              inter_block_attn_output = tf.reduce_sum(self_attn_result * inter_block_soft, 2)  # bs,bn,vec
  
          with tf.variable_scope('self_attn_inter_block'):
              inter_block_attn_output_mask = tf.cast(tf.ones([bs, bn], tf.int32), tf.bool)
              block_ct_res = directional_attention_with_dense(
                  inter_block_attn_output, inter_block_attn_output_mask, direction, 'disa',
                  keep_prob, is_train, wd, activation
              )  # [bs,bn,vec]
  
              block_ct_res_tile = tf.tile(tf.expand_dims(block_ct_res, 2), [1, 1, bl, 1])#[bs,bn,vec]->[bs,bn,bl,vec]
  
          with tf.variable_scope('combination'):
              # input:1.rep_map[bs,bn,bl,vec]; 2.self_attn_result[bs,bn,bl,vec]; 3.rnn_res_tile[bs,bn,bl,vec]
              rep_tensor_with_ct = tf.concat([rep_map, self_attn_result, block_ct_res_tile], -1)  # [bs,bn,bl,3vec]
              new_context_and_gate = linear(rep_tensor_with_ct, 2 * ivec, True, 0., 'linear_new_context_and_gate',
                                            False, wd, keep_prob, is_train)  # [bs,bn,bl,2vec]
              new_context, gate = tf.split(new_context_and_gate, 2, 3)  # bs,bn,bl,vec
              if activation == "relu":
                  new_context_act = tf.nn.relu(new_context)
              elif activation == "elu":
                  new_context_act = tf.nn.elu(new_context)
              elif activation == "linear":
                  new_context_act = tf.identity(new_context)
              else:
                  raise RuntimeError
              gate_sig = tf.nn.sigmoid(gate)
              combination_res = gate_sig * new_context_act + (1 - gate_sig) * rep_map  # bs,bn,bl,vec
  
          with tf.variable_scope('restore_original_length'):
              combination_res_reshape = tf.reshape(combination_res, [bs, bn * bl, ivec])  # bs,bn*bl,vec
              output = combination_res_reshape[:, :sl, :]
              return output
apis: numpy.ones numpy.append numpy.zeros matplotlib.pyplot.figure matplotlib.pyplot.grid numpy.insert matplotlib.pyplot.show numpy.isnan numpy.array matplotlib.pyplot.plot numpy.where
apis: tensorflow.nn.tanh tensorflow.reshape tensorflow.ones tensorflow.variable_scope tensorflow.logical_and tensorflow.concat tensorflow.identity tensorflow.nn.softmax tensorflow.reduce_sum tensorflow.split tensorflow.greater tensorflow.meshgrid tensorflow.constant_initializer tensorflow.shape tensorflow.expand_dims tensorflow.nn.elu tensorflow.cast tensorflow.nn.sigmoid tensorflow.zeros tensorflow.range tensorflow.nn.relu