def __init__(self, price, vol=Value('100%'), **kvargs): self.vol = Value(vol) self.price = PriceHelper.parse_price(price) self.id = kvargs.get('id') self.date = kvargs.get('date') self.status = OrderStatus( kvargs.get('status', OrderStatus.NEW.name).lower()) self.sl = float(kvargs.get('sl', 0)) self.smart = self.s2b(kvargs.get('smart', None)) self.parent_smart = kvargs.get('parent_smart', None) self.best_price = float(kvargs.get('best_price', 0))
def __init__(self, initial_target, type=Type.FIXED.name, val=DEFAULT_TRAILING_SL_VALUE, zone_entry=DEFAULT_ZONE_ENTRY, limit_price_threshold=DEFAULT_LIMIT_PRICE, last_stoploss=0, **kvargs): self.type = StopLossSettings.Type(type.lower()) self.val = Value(kvargs.get('threshold', val)) self.limit_price_threshold = Value(limit_price_threshold) self.zone_entry = Value(zone_entry) self.initial_target = StopLossTarget(**initial_target) self.last_stoploss = float(last_stoploss)
def __init__(self, targets=None, side=None, sl_threshold=None, is_entry=True, smart=False, **kvargs): if 'threshold' in kvargs: sl_threshold = kvargs.get('threshold') self.sl_threshold = Value( sl_threshold ) if sl_threshold else EntryExitSettings.DEFAULT_THRESHOLD self.side = Side(side.lower()) if side else None self.smart = smart self.is_entry = is_entry self.targets: [Target] = [] # As an alternative to specifiying array of targets if 'target' in kvargs: self.targets.append( self._create_target(kvargs.get('target'), is_entry)) if targets: self.set_targets( [self._create_target(t, is_entry) for t in targets])
def serializable_dict(self): # d = dict(self.__dict__) d = OrderedDict() d['type'] = self.type if self.is_trailing() and self.val != Value( StopLossSettings.DEFAULT_TRAILING_SL_VALUE): d['threshold'] = self.val if self.last_stoploss: d['last_stoploss'] = self.format_float(self.last_stoploss) if self.limit_price_threshold != Value( StopLossSettings.DEFAULT_LIMIT_PRICE): d['limit_price_threshold'] = self.limit_price_threshold if self.zone_entry != Value(StopLossSettings.DEFAULT_ZONE_ENTRY): d['zone_entry'] = self.zone_entry d['initial_target'] = self.initial_target return d
def __init__(self, is_buy, price, sl_threshold=Value("1%"), logger: Logger = None, best_price=0): super().__init__(logger) self.is_buy = is_buy self.target_price = None self.initialized = False self.sl_threshold = sl_threshold self.best_pullback_limit_price = best_price self.target_zone_touched = False if self.best_pullback_limit_price == 0 else True self.init_price(price)
def create_price_helper(cls, price_str): s = str(price_str).strip().lower() if PriceHelper.is_float_price(s): return PriceHelper(True, float(s), None, None) token = s operand = None val = None if s.startswith(PriceHelper.CURR_PRICE_TOKEN): token = PriceHelper.CURR_PRICE_TOKEN s = s[len(PriceHelper.CURR_PRICE_TOKEN):] if len(s) > 0 and s[0] in ['+', '-']: operand = s[0] s = s[1:] if len(s) > 0: val = Value(s) return PriceHelper(False, token, operand, val)
class EntryExitSettings(CustomSerializable): DEFAULT_THRESHOLD = Value("1%") def __init__(self, targets=None, side=None, sl_threshold=None, is_entry=True, smart=False, **kvargs): if 'threshold' in kvargs: sl_threshold = kvargs.get('threshold') self.sl_threshold = Value( sl_threshold ) if sl_threshold else EntryExitSettings.DEFAULT_THRESHOLD self.side = Side(side.lower()) if side else None self.smart = smart self.is_entry = is_entry self.targets: [Target] = [] # As an alternative to specifiying array of targets if 'target' in kvargs: self.targets.append( self._create_target(kvargs.get('target'), is_entry)) if targets: self.set_targets( [self._create_target(t, is_entry) for t in targets]) def set_targets(self, targets): self.targets.extend(targets) self.targets.sort(key=lambda t: (not t.is_completed(), t.price), reverse=self.side.is_buy()) def get_completed_targets(self) -> List[Target]: return [t for t in self.targets if t.is_completed()] def get_all_smart_targets(self) -> List[Target]: return [t for t in self.targets if t.is_smart()] def get_all_incomplete_smart_targets(self) -> List[Target]: return [ t for t in self.targets if t.is_smart() and not t.is_completed() ] def is_completed(self): return all(t.is_completed() for t in self.targets) def _create_target(self, t, is_entry): return EntryTarget( **t, parent_smart=self.smart) if is_entry else ExitTarget( **t, parent_smart=self.smart) def serializable_dict(self): # d = dict(self.__dict__) d = OrderedDict() # it is set automatically at Trade._init_entry_exit method if self.is_entry and self.side: d['side'] = self.side if self.smart is not None: d['smart'] = self.smart if self.sl_threshold and (self.sl_threshold != EntryExitSettings.DEFAULT_THRESHOLD): d['threshold'] = self.sl_threshold if self.targets: d['targets'] = self.targets return d def is_entry(self): return self.is_entry def is_exit(self): return not self.is_entry
class Target(CustomSerializable): def __init__(self, price, vol='100%', **kvargs): self.vol = Value(vol) self.price = PriceHelper.parse_price(price) self.id = kvargs.get('id') self.date = kvargs.get('date') self.status = OrderStatus( kvargs.get('status', OrderStatus.NEW.name).lower()) self.sl = float(kvargs.get('sl', 0)) self.smart = self.s2b(kvargs.get('smart', None)) self.parent_smart = kvargs.get('parent_smart', None) self.best_price = float(kvargs.get('best_price', 0)) cv = kvargs.get('calculated_volume', None) self.calculated_volume = float(cv) if cv else None def s2b(self, s): return Utils.s2b(s) def is_completed(self): return self.status.is_completed() def is_new(self): return self.status.is_new() def is_active(self): return self.status.is_active() def has_id(self): return self.id is not None # def set_completed(self, date_str=datetime.now().replace(microsecond=0).isoformat(' ')): def set_completed(self, id=None, date=datetime.now()): self.status = OrderStatus.COMPLETED self.date = date if id: self.id = id def set_canceled(self): self.status = OrderStatus.NEW self.id = None self.calculated_volume = None def set_active(self, id=None): self.status = OrderStatus.ACTIVE if id: self.id = id def has_custom_stop(self): return self.sl != 0 def custom_stop(self): return self.sl def is_stoploss_target(self): return False def is_exit_target(self): return False def is_entry_target(self): return False def is_smart(self): if self.parent_smart is not None: if self.smart is not None: return self.smart return self.parent_smart return False if self.smart is None else self.smart def __str__(self): desc = ('{}:{:.08f}@{}; Smart:{}' if PriceHelper.is_float_price( self.price) else '{}:{}@{}{}').format(self.__class__.__name__, self.price, self.vol, self.is_smart()) if self.calculated_volume and self.vol.is_rel(): desc += '; Abs Vol:{:.08f}'.format(self.calculated_volume) desc += ' ;Stoploss:{}'.format(self.sl) desc += ' ;Status:{}'.format(self.status) desc += ' ;ID:{}'.format(self.id) return desc # '(abs vol: {:.08f})'.format(self.calculated_volume) if self.vol.is_rel() and self.calculated_volume else '' def __repr__(self): return self.__str__() def serializable_dict(self): d = OrderedDict() if not self.status.is_new(): d['status'] = self.status if self.id: d['id'] = self.id if self.date: d['date'] = self.date if PriceHelper.is_float_price(self.price): d['price'] = self.format_float(self.price) else: d['price'] = self.price d['vol'] = self.vol if self.smart is not None: d['smart'] = self.smart if self.sl != 0: d['sl'] = self.format_float(self.sl) if self.best_price > 0: d['best_price'] = self.format_float(self.best_price) if self.calculated_volume: d['calculated_volume'] = self.format_float(self.calculated_volume) return d