コード例 #1
0
def is_time_to_exit(data,
                    funcs,
                    smas=var.default_smas,
                    emas=var.default_emas,
                    stop=1,
                    bought_at=0,
                    max_price=0,
                    count=-1):
    '''
    Detects when is time to exit trade.

    stop variable:
    0 -> no stop loss [DANGEROUS]
    1 -> regular stop loss
    2 -> trailing stop loss
    '''

    if count == 0: return True

    if stop == 1:
        if stop_loss(data.Last.iloc[-1], bought_at, percentage=10):
            return True
    elif stop == 2:
        if trailing_stop_loss(data.Last.iloc[-1], max_price, percentage=10):
            return True

    for func in funcs:
        if func(data, smas=smas, emas=emas):
            return True

    return False
コード例 #2
0
def is_time_to_exit(data,
                    funcs,
                    smas=var.default_smas,
                    emas=var.default_emas,
                    stop=2,
                    bought_at=0,
                    max_price=0,
                    count=-1):
    """
    Detects when is time to exit trade.

    stop variable:
    0 -> no stop loss [DANGEROUS]
    1 -> regular stop loss
    2 -> trailing stop loss
    """

    #if count == 0:
    #    return True

    if stop == 1:
        if stop_loss(data.Last.iloc[-1],
                     bought_at,
                     percentage=var.stop_loss_prcnt):
            log.debug(f"[FUNC] Stop-loss")
            return True
    elif stop == 2:
        if trailing_stop_loss(data.Last.iloc[-1],
                              max_price,
                              percentage=var.trailing_loss_prcnt):
            log.debug(f"[FUNC] Trailing stop-loss")
            return True

    for func in funcs:
        if func(data, smas=smas, emas=emas):
            log.debug(f"[FUNC] {func.__name__}")
            return True

    return False
コード例 #3
0
ファイル: test_aux.py プロジェクト: xuongrong86/algotrading
 def test_trailing_stop_loss(self):
     self.assertEqual(aux.trailing_stop_loss(100, 110, 11), False)
     self.assertEqual(aux.trailing_stop_loss(100, 110, 10), False)
     self.assertEqual(aux.trailing_stop_loss(100, 110, 9), True)