def get_md_data(self, md_array):
        # tranfer the string to float
        # md_array[LASTPRICE] = float(md_array[LASTPRICE])
        # md_array[VOLUME] = float(md_array[VOLUME])
        # md_array[OPENINTEREST] = float(md_array[OPENINTEREST])
        # md_array[TURNONER] = float(md_array[TURNONER])
        # md_array[BIDPRICE1] = float(md_array[BIDPRICE1])
        # md_array[ASKPRICE1] = float(md_array[ASKPRICE1])

        self._pre_md_price = self._now_md_price
        self._now_md_price = md_array

        lastprice = self._now_md_price[LASTPRICE]
        # self._lastprice_array.append(lastprice)
        # print lastprice
        if len(self._pre_md_price) == 0:
            # "the is the first line data"
            return

        if self._pre_ema_val_60 == 0:
            self._pre_ema_val_60 = lastprice
            self._pre_ema_val_5 = lastprice
            self._pre_ema_val_1 = lastprice
        self._now_middle_60 = bf.get_ema_data(lastprice, self._pre_ema_val_60,
                                              self._ema_period)
        self._now_middle_5 = bf.get_ema_data(lastprice, self._pre_ema_val_5,
                                             self._ema_period)
        self._now_middle_1 = bf.get_ema_data(lastprice, self._pre_ema_val_1,
                                             self._ema_period)

        self._sd_val = bf.get_sd_data(lastprice, self._lastprice_array,
                                      self._ema_period)
        self._rsi_val = bf.get_rsi_data(lastprice, self._lastprice_array,
                                        self._rsi_period)

        # print len(self._lastprice_array)
        hour = int(self._now_md_price[TIME].split(':')[0])
        if hour != self._current_hour and hour != 13 and hour != 21:
            # print "the hour is not  equal "
            self._current_hour = hour
            self._pre_ema_val_60 = self._now_middle_60
            self._lastprice_array.append(lastprice)
        if self._now_ema_tick_1 >= self._limit_ema_tick_1:
            self._now_ema_tick_1 = 0
            self._pre_ema_val_1 = self._now_middle_1
        else:
            self._now_ema_tick_1 += 1
        if self._now_ema_tick_5 >= self._limit_ema_tick_5:
            self._now_ema_tick_5 = 0
            self._pre_ema_val_5 = self._now_middle_5
        else:
            self._now_ema_tick_5 += 1

        diff_volume = self._now_md_price[VOLUME] - self._pre_md_price[VOLUME]
        diff_interest = self._now_md_price[OPENINTEREST] - self._pre_md_price[
            OPENINTEREST]
        diff_turnover = self._now_md_price[TURNONER] - self._pre_md_price[
            TURNONER]

        if diff_volume != 0 and (self._pre_md_price[ASKPRICE1] -
                                 self._pre_md_price[BIDPRICE1]) != 0:
            avg_price = float(diff_turnover) / diff_volume / self._multiple
            spread = 100 * (avg_price - self._pre_md_price[BIDPRICE1]) / (
                self._pre_md_price[ASKPRICE1] - self._pre_md_price[BIDPRICE1])
        else:
            spread = 50

        tmp_to_csv = [
            self._now_md_price[TIME], self._now_md_price[LASTPRICE],
            round(self._now_middle_60, 2),
            round(self._now_middle_5, 2),
            round(self._now_middle_1, 2),
            round(self._sd_val, 2),
            round(self._rsi_val, 2),
            round(diff_volume, 2),
            round(spread, 2)
        ]
        # print tmp_to_csv
        self._write_to_csv_data.append(tmp_to_csv)

        return True
Exemple #2
0
	def get_md_data(self,md_array):
		# tranfer the string to float
		md_array[LASTPRICE] = float(md_array[LASTPRICE])
		md_array[VOLUME] = float(md_array[VOLUME])
		md_array[OPENINTEREST] = float(md_array[OPENINTEREST])
		md_array[TURNONER] = float(md_array[TURNONER])
		md_array[BIDPRICE1] = float(md_array[BIDPRICE1])
		md_array[ASKPRICE1] = float(md_array[ASKPRICE1])


		self._pre_md_price = self._now_md_price
		self._now_md_price = md_array

		lastprice = self._now_md_price[LASTPRICE]
		# print lastprice

		if len(self._pre_md_price) ==0:
			return
			# self._rsi_array.append(0)
		else:
			# self._rsi_array.append(lastprice - self._pre_md_price[LASTPRICE])
			if self._now_bar_rsi_tick >= self._rsi_bar_period:
				# 表示已经到了一个bar的周期。
				tmpdiff = lastprice - self._pre_rsi_lastprice		
				self._pre_rsi_lastprice = lastprice
				self._now_bar_rsi_tick = 1
				self._ris_data =bf.get_rsi_data(tmpdiff,self._rsi_array,self._rsi_period)
				self._rsi_array.append(tmpdiff)
			else:
				self._now_bar_rsi_tick +=1
				tmpdiff = lastprice - self._pre_rsi_lastprice
				self._ris_data =bf.get_rsi_data(tmpdiff,self._rsi_array,self._rsi_period)
				# self._ris_data = 0

		
		if len(self._lastprice_array) < self._param_period:
			self._lastprice_array.append(lastprice)
			# this is we dont start the period.
			ema_period = len(self._lastprice_array)
			pre_ema_val = bf.get_ema_data(lastprice,self._pre_ema_val,ema_period)
			self._pre_ema_val = pre_ema_val
			# save the pre_ema_val and return
			if lastprice not in self._lastprice_map:
				self._lastprice_map[lastprice] =1
			else:
				self._lastprice_map[lastprice] +=1
			return True
		else:
			self._lastprice_array.append(lastprice)
			front_lastprice = self._lastprice_array[0]
			self._lastprice_array.pop(0)
			if front_lastprice != lastprice:
				if lastprice not in self._lastprice_map :
					self._lastprice_map[lastprice] = 1
				else:
					self._lastprice_map[lastprice] +=1

				self._lastprice_map[front_lastprice] -=1

		# start the judge
		if self._moving_theo =="EMA":
			self._now_middle_value = bf.get_ema_data(lastprice,self._pre_ema_val,self._param_period)
			self._pre_ema_val = self._now_middle_value
		else:
			self._now_middle_value = bf.get_ma_data(self._lastprice_array,self._param_period)
		
		self._now_sd_val =bf.get_sd_data_by_map(self._lastprice_map,self._param_period)	
		# self.f.write(str(self._now_md_price[TIME])+","+str(lastprice)+","+str(self._now_middle_value)+","+str(self._now_sd_val)+","+str(self._ris_data)+"\n")
		if self._now_md_price[LASTPRICE] > self._pre_md_price[LASTPRICE]:
			if self._series_lastprice >=0:
				self._series_lastprice +=1
			else:
				self._series_lastprice = 0
		elif self._now_md_price[LASTPRICE] < self._pre_md_price[LASTPRICE]:
			if self._series_lastprice <=0:
				self._series_lastprice -=1
			else:
				self._series_lastprice =0
		else:
			if self._series_lastprice >0:
				self._series_lastprice +=1
			elif self._series_lastprice <0:
				self._series_lastprice -=1
			else:
				self._series_lastprice =0
		# if self._direction ==LONG:
		# 	if self._now_md_price[LASTPRICE] >= self._pre_md_price[LASTPRICE]:
		# 		self._series_lastprice  = self._series_lastprice +1
		# 	else:
		# 		self._series_lastprice = 0
		# elif self._direction ==SHORT:
		# 	if self._now_md_price[LASTPRICE] <= self._pre_md_price[LASTPRICE]:
		# 		self._series_lastprice  = self._series_lastprice +1
		# 	else:
		# 		self._series_lastprice = 0
		# else:
		# 	return
		diff_volume = self._now_md_price[VOLUME] - self._pre_md_price[VOLUME]
		self._tmp_sum_diff_volume += diff_volume
		self._now_bar_volume_tick +=1
		if self._now_bar_volume_tick >= self._limit_bar_volume_tick:
			self._diff_volume_array.append(self._tmp_sum_diff_volume)
			self._tmp_sum_diff_volume = 0
			self._now_bar_volume_tick = 0

		open_time = self.is_trend_open_time()
		close_time = self.is_trend_close_time()
		# close_time = False
		
		if open_time and self._now_interest < self._limit_interest:
		# if open_time:
			self._now_interest +=1
			# print "we start to open"
			self._open_lastprice = self._now_md_price[LASTPRICE]
			self._max_profit = 0
			mesg= "the time of open: "+self._now_md_price[TIME] + ",the price: " + str(self._now_md_price[LASTPRICE])
			# mesg1 = "the diff volume: "+str(ema_diff_volume)+", the interest: " + str(ema_diff_openinerest) + ", the spread: "+ str(ema_spread)
			self._file.write(mesg+"\n")
			# self._file.write(mesg1+"\n")
			# print "the diff volume is:" + str(self._now_md_price[VOLUME] - self._pre_md_price[VOLUME])
		# elif close_time:
		elif close_time and self._now_interest >0:
			self._now_interest -=1
			# print "we need to close"
			if self._direction ==LONG:
				self._profit +=(self._now_md_price[LASTPRICE] - self._open_lastprice)
			elif self._direction ==SHORT:
				self._profit +=(self._open_lastprice - self._now_md_price[LASTPRICE])
			self._open_lastprice = 0
			self._max_profit = 0
			mesg= "the time of close:"+self._now_md_price[TIME] + ",the price: " + str(self._now_md_price[LASTPRICE])
			self._file.write(mesg+"\n")

		return True
	def get_md_data(self,md_array):
		# tranfer the string to float
		md_array[LASTPRICE] = float(md_array[LASTPRICE])
		md_array[VOLUME] = float(md_array[VOLUME])
		md_array[OPENINTEREST] = float(md_array[OPENINTEREST])
		md_array[TURNONER] = float(md_array[TURNONER])
		md_array[BIDPRICE1] = float(md_array[BIDPRICE1])
		md_array[ASKPRICE1] = float(md_array[ASKPRICE1])


		self._pre_md_price = self._now_md_price
		self._now_md_price = md_array

		lastprice = self._now_md_price[LASTPRICE]
		# self._lastprice_array.append(lastprice)
		# print lastprice
		if len(self._pre_md_price) ==0:
			# "the is the first line data"
			return

		# self._rsi_array.append(lastprice - self._pre_md_price[LASTPRICE])
		if self._now_bar_rsi_tick >= self._rsi_bar_period:
			# 表示已经到了一个bar的周期。
			tmpdiff = lastprice - self._pre_rsi_lastprice		
			self._pre_rsi_lastprice = lastprice
			self._now_bar_rsi_tick = 1
			self._ris_data =bf.get_rsi_data(tmpdiff,self._rsi_array,self._rsi_period)
			self._rsi_array.append(tmpdiff)
		else:
			self._now_bar_rsi_tick +=1
			tmpdiff = lastprice - self._pre_rsi_lastprice
			self._ris_data =bf.get_rsi_data(tmpdiff,self._rsi_array,self._rsi_period)
				# self._ris_data = 0

		self._lastprice_array.append(lastprice)
		if len(self._lastprice_array) <= self._param_period:
			# this is we dont start the period.
			# print  "the lastprice length is small: " +str(len(self._lastprice_array))
			ema_period = len(self._lastprice_array)
			pre_ema_val = bf.get_ema_data(lastprice,self._pre_ema_val,ema_period)
			self._pre_ema_val = pre_ema_val
			# save the pre_ema_val and return
			if lastprice not in self._lastprice_map:
				self._lastprice_map[lastprice] =1
			else:
				self._lastprice_map[lastprice] +=1
			return True

		front_lastprice = self._lastprice_array[0]
		self._lastprice_array.pop(0)
		if front_lastprice != lastprice:
			if lastprice not in self._lastprice_map :
				self._lastprice_map[lastprice] = 1
			else:
				self._lastprice_map[lastprice] +=1

			self._lastprice_map[front_lastprice] -=1
		# start the judge
		if self._moving_theo =="EMA":
			self._now_middle_value = bf.get_ema_data(lastprice,self._pre_ema_val,self._param_period)
			self._pre_ema_val = self._now_middle_value
		else:
			self._now_middle_value = bf.get_ma_data(self._lastprice_array,self._param_period)
		

		
		self._now_sd_val =bf.get_sd_data_by_map(self._lastprice_map,self._param_period)	

		diff_volume = self._now_md_price[VOLUME] - self._pre_md_price[VOLUME]
		diff_interest = self._now_md_price[OPENINTEREST] - self._pre_md_price[OPENINTEREST]
		diff_turnover = self._now_md_price[TURNONER] - self._pre_md_price[TURNONER]

		if diff_volume != 0 and (self._pre_md_price[ASKPRICE1] - self._pre_md_price[BIDPRICE1]) !=0:
			avg_price = float(diff_turnover)/diff_volume/self._multiple
			spread = 100*(avg_price - self._pre_md_price[BIDPRICE1])/(self._pre_md_price[ASKPRICE1] - self._pre_md_price[BIDPRICE1])
		else:
			spread = 50


		tmp_to_csv = [self._now_md_price[TIME],self._now_md_price[LASTPRICE],
					round(self._now_middle_value,2),round(self._now_sd_val,2),
					round(self._ris_data,2),
					round(diff_volume,2),round(diff_interest,2),round(spread,2)]
		# print tmp_to_csv
		self._write_to_csv_data.append(tmp_to_csv)

		return True