Beispiel #1
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
    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
	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
		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_data2(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_data2(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._slope.append(self._now_middle_value)
		slope1 = self.get_slope(120)
		slope2 = self.get_slope(360)
		slope3 = self.get_slope(600)
		self._slope_tick = (slope3 + slope2 + slope1)/3
		
		# self._ris_data = bf.get_rsi_data(self._rsi_array,self._rsi_period)
		# self._now_sd_val =bf.get_sd_data(self._now_md_price[TIME], 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]

		self._diff_volume_array.append(diff_volume)
		self._diff_open_interest_array.append(diff_interest)
		self._diff_turn_over_array.append(diff_turnover)
		# 直接就是diff_interest
		# ema_diff_volume = bf.get_ema_data_2(self._diff_volume_array,self._diff_period)
		if self._now_bar_num > self._limit_bar_num:
			ema_diff_volume = bf.get_sum(self._diff_volume_array,self._limit_bar_num)
			ema_diff_openinterest = bf.get_sum(self._diff_open_interest_array,self._limit_bar_num)
			ema_diff_turnonver = bf.get_sum(self._diff_turn_over_array,self._limit_bar_num)
			self._now_bar_num = 1
			self._bar_max_lastprice = self._now_md_price[LASTPRICE]
			self._bar_min_lastprice = self._now_md_price[LASTPRICE]
		else:
			ema_diff_volume = bf.get_sum(self._diff_volume_array,self._now_bar_num)
			ema_diff_openinterest = bf.get_sum(self._diff_open_interest_array,self._now_bar_num)
			ema_diff_turnonver = bf.get_sum(self._diff_turn_over_array,self._now_bar_num)
			self._now_bar_num +=1
			if self._bar_max_lastprice ==0 or self._bar_max_lastprice < self._now_md_price[LASTPRICE]:
				self._bar_max_lastprice = self._now_md_price[LASTPRICE]
			if self._bar_min_lastprice ==0 or self._bar_min_lastprice > self._now_md_price[LASTPRICE]:
				self._bar_min_lastprice = self._now_md_price[LASTPRICE]

		if diff_volume ==0:
			spread =50
			self._diff_spread_array.append(spread)
		else:

			avg_price = float(diff_turnover)/diff_volume/self._multiple
			# avg_price = float(ema_diff_turnonver)/ema_diff_volume/self._multiple
			# if lastprice > self._now_middle_value:
			# if self._pre_md_price[ASKPRICE1] != self._pre_md_price[BIDPRICE1]:
			# 注意,现在算的只是和买一价的位置关系。
			spread = 100*(avg_price - self._pre_md_price[BIDPRICE1])/(self._pre_md_price[ASKPRICE1] - self._pre_md_price[BIDPRICE1])
			# spread = 100*(avg_price - self._now_md_price[BIDPRICE1])/(self._now_md_price[ASKPRICE1] - self._now_md_price[BIDPRICE1])
			# if self._bar_max_lastprice == self._bar_min_lastprice:
			# 	spread = 50
			# else:
			# 	spread = 100*(avg_price - self._bar_min_lastprice)/(self._bar_max_lastprice - self._bar_min_lastprice)
			self._diff_spread_array.append(spread)
			# spread = bf.get_weighted_mean(self._diff_spread_array,self._diff_volume_array,self._diff_period)
		# 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 self._now_volume_tick < self._volume_period:
			self._now_volume_tick +=1
			self._sum_volume += diff_volume
		else:
			self._now_volume_tick =1
			self._sum_volume = diff_volume
			self._pre_sum_volume = self._sum_volume

		tmp_beishu =0 
		if self._pre_sum_volume != 1 and self._pre_sum_volume != 0:
			tmp_beishu = self._sum_volume/self._pre_sum_volume