def __search_decisive_price(self, side: Side): percent = "" if side is Side.BUY: percent = "longCountPercent" if side is Side.SELL: percent = "shortCountPercent" if not self.current_position_book_is_exist( ) or not self.before_position_book_is_exist(): return None now_p_buckets = fx_lib.divide_buckets_up_and_down( self.current_position_book()["buckets"], self.current_price()) before_p_buckets = fx_lib.divide_buckets_up_and_down( self.before_position_book()["buckets"], self.current_price()) for i in range(self.params["target_range"]): # low now = now_p_buckets["low"][i][percent] before = before_p_buckets["low"][i][percent] position_growth_rate = self.calculate_position_growth_rate( now, before) if position_growth_rate >= self.params["entry_threshold"] \ and before > self.params["min_required_count_percent"]: return now_p_buckets["low"][i]["price"] # high now = now_p_buckets["high"][i][percent] before = before_p_buckets["high"][i][percent] position_growth_rate = self.calculate_position_growth_rate( now, before) if position_growth_rate >= self.params["entry_threshold"] \ and before > self.params["min_required_count_percent"]: return now_p_buckets["low"][i]["price"] return None
def export_csv(self, f_name: str): date_from = self.date_from header = ["date_time", "price", "limit_short", "stop_short", "limit_long", "stop_long", "gan_short", "loss_short", "gan_long", "loss_long"] records = [] while date_from <= self.date_to: record = [date_from, -1, -1, -1, -1, -1, -1, -1, -1, -1] timestamp = date_from.timestamp() if self.order_book_is_exist(timestamp): order_book = self.order_book(timestamp) record[1] = order_book["price"] buckets = fx_lib.divide_buckets_up_and_down(order_book["buckets"], order_book["price"]) limit_short = 0 stop_short = 0 limit_long = 0 stop_long = 0 for i in range(10): limit_short += buckets["high"][i]["shortCountPercent"] stop_short += buckets["low"][i]["shortCountPercent"] limit_long += buckets["low"][i]["longCountPercent"] stop_long += buckets["high"][i]["longCountPercent"] record[2] = limit_short record[3] = stop_short record[4] = limit_long record[5] = stop_long if self.position_book_is_exist(timestamp): position_book = self.position_book(timestamp) record[1] = position_book["price"] buckets = fx_lib.divide_buckets_up_and_down(position_book["buckets"], position_book["price"]) gan_short = 0 loss_short = 0 gan_long = 0 loss_long = 0 for i in range(10): gan_short += buckets["high"][i]["shortCountPercent"] loss_short += buckets["low"][i]["shortCountPercent"] gan_long += buckets["low"][i]["longCountPercent"] loss_long += buckets["high"][i]["longCountPercent"] record[6] = gan_short record[7] = loss_short record[8] = gan_long record[9] = loss_long records.append(record) date_from = date_from + timedelta(minutes=20) export_dir = "../result_data/book_csv/" os.makedirs(export_dir, exist_ok=True) pd.DataFrame(records, columns=header).to_csv(export_dir + f_name, index=False)
def create_new_orders_logic_type_b(instrument: Instrument, params: dict, buckets: list, current_price: float, target_range: int): buckets = fx_lib.divide_buckets_up_and_down(buckets, current_price) long = buckets["long"] short = buckets["short"] orders = [] if (len(long) <= target_range + len(params["stopOrders"])) or ( len(short) <= target_range + len(params["stopOrders"])): return orders # short order for i in range(target_range): activatable = True for j in range(len(params["stopOrders"])): so = params["stopOrders"][j] if short[i + j]["shortCountPercent"] < so: activatable = False break if activatable: enter_price = short[i]["price"] + pips_to_price( instrument, params["spreadAllowance"]) limit_price = enter_price - pips_to_price( instrument, params["profitDistance"]) stop_price = enter_price + pips_to_price( instrument, params["stopLossDistance"]) order = Order(instrument, Side.SELL, Type.MARKET_IF_TOUCHED, 1, enter_price, limit_price, stop_price) order.memo = short[i] order.memo2 = short[i + 1] order.memo3 = short[i + 2] orders.append(order) break # long orders for i in range(target_range): activatable = True for j in range(len(params["stopOrders"])): lo = params["stopOrders"][j] if long[i + j]["longCountPercent"] < lo: activatable = False break if activatable: enter_price = long[i]["price"] - pips_to_price( instrument, params["spreadAllowance"]) limit_price = enter_price + pips_to_price( instrument, params["profitDistance"]) stop_price = enter_price - pips_to_price( instrument, params["stopLossDistance"]) order = Order(instrument, Side.BUY, Type.MARKET_IF_TOUCHED, 1, enter_price, limit_price, stop_price) order.memo = long[i] order.memo2 = long[i + 1] order.memo3 = long[i + 2] orders.append(order) break return orders
def sum_sell_power(self): if self.high_volatility_ob(): return 0 now_buckets = fx_lib.divide_buckets_up_and_down( self.current_order_book()["buckets"], self.current_price()) sum_short = 0 for i in range(self.params["power_count_range"]): sum_short += now_buckets["low"][i]["shortCountPercent"] # / (i+1) return sum_short
def high_volatility_pb(self): now_buckets = fx_lib.divide_buckets_up_and_down( self.current_position_book()["buckets"], self.current_price()) if len(now_buckets["low"]) < 5 or len(now_buckets["high"]) < 5: return True low_distance = abs(self.current_price() - now_buckets["low"][0]["price"]) high_distance = abs(self.current_price() - now_buckets["high"][0]["price"]) return low_distance + high_distance > pips_to_price(self.instrument, 5)
def sum_sell_fuel(self): if self.high_volatility_pb(): return 0 now_buckets = fx_lib.divide_buckets_up_and_down( self.current_position_book()["buckets"], self.current_price()) sum_long = 0 invalid_range = 2 for i in range(invalid_range, self.params["fuel_count_range"]): sum_long += now_buckets["high"][i]["longCountPercent"] # / (i+1) return sum_long
def calculate_slope_of_trend(self): buckets = fx_lib.divide_buckets_up_and_down( self.before_position_book()["buckets"], self.current_price()) low = buckets["low"] high = buckets["high"] short_sum = 0 long_sum = 0 for i in range(self.params["target_range"]): short_sum += low[i]["shortCountPercent"] + high[i][ "shortCountPercent"] long_sum += low[i]["longCountPercent"] + high[i]["longCountPercent"] return (long_sum - short_sum) / (long_sum + short_sum)