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