Example #1
0
def trailOnProfit5(trade, bar, initStop, stop):
    global recentExtremePrice, extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2
    MIN_X = 1.5
    TIME_CUT_OFF = (14, 54)  # Trail only after cut off time

    extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)
    isTime = isTimeTriggered(bar, TIME_CUT_OFF)

    if (mechTM.currentTradeStatus(trade, initStop, bar) >= MIN_X):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)
    elif (isTime):
        if (recentExtremePrice is None):
            recentExtremePrice = bar.H if trade.isLong else bar.L  # Use first bar after trigger as initial extreme
        else:
            recentExtremePrice = mechTM.getExtremePrice(
                trade, bar, recentExtremePrice)

        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, recentExtremePrice,
                                    distance)
    else:
        newStop = donchianLow[bar.Index] if trade.isLong else donchianHigh[
            bar.Index]
        if isStopValid(trade, initStop, newStop):
            stop = newStop

    return stop
Example #2
0
def trailOnProfit5( trade, bar, initStop, stop ):
    global recentExtremePrice, extremePrice, trailAtr
        
    TRAIL_ATR_MULTIPLIER = 2
    MIN_X                = 1.5
    TIME_CUT_OFF         = ( 14,54 )             # Trail only after cut off time

    extremePrice         = mechTM.getExtremePrice( trade, bar, extremePrice )
    isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
    
    if(  mechTM.currentTradeStatus( trade, initStop, bar ) >= MIN_X  ): 
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
        stop     = mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  )
    elif( isTime ):
        if(  recentExtremePrice is None ):
            recentExtremePrice = bar.H if trade.isLong else bar.L                            # Use first bar after trigger as initial extreme
        else :
            recentExtremePrice = mechTM.getExtremePrice( trade, bar, recentExtremePrice )

        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop     = mechTM.getTrailPrice( trade, bar, initStop, recentExtremePrice, distance  )
    else:
        newStop = donchianLow[ bar.Index ]  if trade.isLong  else donchianHigh[ bar.Index ]        
        if isStopValid( trade, initStop, newStop ):
            stop = newStop

    return stop
Example #3
0
def timeStop( trade, bar, initStop ):
    global barCount
    
    barCount += 1
    
    if( barCount >=20 and mechTM.currentTradeStatus( trade, initStop, bar ) < 0.1 ):
        return mechTM.markClosedAtCurrentPrice( trade, bar )
    else:
        return False
Example #4
0
def timeStop(trade, bar, initStop):
    global barCount

    barCount += 1

    if (barCount >= 20
            and mechTM.currentTradeStatus(trade, initStop, bar) < 0.1):
        return mechTM.markClosedAtCurrentPrice(trade, bar)
    else:
        return False
Example #5
0
def donchianTrailTimeStop( trade, bar, initStop, stop ):
    global barCount
    
    barCount += 1
    
    TIME_LIMIT = 20
    MIN_X      = 0

    if( barCount >= TIME_LIMIT   and   mechTM.currentTradeStatus( trade, initStop, bar ) < MIN_X   ):
        stop = donchianTrail( trade, bar, initStop, stop )
    
    return stop
Example #6
0
def trailOnProfit( trade, bar, initStop, stop ):
    global extremePrice, trailAtr
        
    TRAIL_ATR_MULTIPLIER = 2
    MIN_X                = 1.5
    extremePrice         = mechTM.getExtremePrice( trade, bar, extremePrice )
    
    if(  mechTM.currentTradeStatus( trade, initStop, bar ) >= MIN_X  ):        
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
        stop     =  mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 
    
    return stop
Example #7
0
def trailOnProfit(trade, bar, initStop, stop):
    global extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2
    MIN_X = 1.5
    extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)

    if (mechTM.currentTradeStatus(trade, initStop, bar) >= MIN_X):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop
Example #8
0
def donchianTrailTimeStop(trade, bar, initStop, stop):
    global barCount

    barCount += 1

    TIME_LIMIT = 20
    MIN_X = 0

    if (barCount >= TIME_LIMIT
            and mechTM.currentTradeStatus(trade, initStop, bar) < MIN_X):
        stop = donchianTrail(trade, bar, initStop, stop)

    return stop
Example #9
0
def trailOnProfit3( trade, bar, initStop, stop ):
    global extremePrice, trailAtr
        
    TRAIL_ATR_MULTIPLIER = 2
    MIN_X                = 1.5
    TIME_CUT_OFF         = ( 14,54 )             # Trail only after cut off time

    extremePrice         = mechTM.getExtremePrice( trade, bar, extremePrice )
    isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
    
    #isTime = False
    
    if(  isTime or mechTM.currentTradeStatus( trade, initStop, bar ) >= MIN_X  ):        
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
        stop     = mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 
    
    return stop
Example #10
0
def trailOnTimeStop( trade, bar, initStop, stop ):
    global barCount, extremePrice, trailAtr
    
    TRAIL_ATR_MULTIPLIER = 2
    MIN_X                = 0
    BAR_TIME_LIMIT       = 20

    barCount  += 1    
    
    extremePrice  = mechTM.getExtremePrice( trade, bar, extremePrice ) 
    
    if( barCount >= BAR_TIME_LIMIT and mechTM.currentTradeStatus( trade, initStop, bar ) < MIN_X  ):
        # TODO try by ignoring BO Bar, ie once time limit is up, take last bar as extreme
        #extremePrice  = mechTM.getExtremePrice( trade, bar, extremePrice )      #  Not updated until Timestop => Trails behind recent extreme (or BO Bar whichever is further) after timestop is active
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop     = mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 
    
    return stop
Example #11
0
def trailOnProfit3(trade, bar, initStop, stop):
    global extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2
    MIN_X = 1.5
    TIME_CUT_OFF = (14, 54)  # Trail only after cut off time

    extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)
    isTime = isTimeTriggered(bar, TIME_CUT_OFF)

    #isTime = False

    if (isTime or mechTM.currentTradeStatus(trade, initStop, bar) >= MIN_X):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop
Example #12
0
def trailOnTimeStop(trade, bar, initStop, stop):
    global barCount, extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2
    MIN_X = 0
    BAR_TIME_LIMIT = 20

    barCount += 1

    extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)

    if (barCount >= BAR_TIME_LIMIT
            and mechTM.currentTradeStatus(trade, initStop, bar) < MIN_X):
        # TODO try by ignoring BO Bar, ie once time limit is up, take last bar as extreme
        #extremePrice  = mechTM.getExtremePrice( trade, bar, extremePrice )      #  Not updated until Timestop => Trails behind recent extreme (or BO Bar whichever is further) after timestop is active
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop
Example #13
0
def trailOnProfit2( trade, bar, initStop, stop ):
    global extremePrice, trailAtr
        
    TRAIL_ATR_MULTIPLIER = 2
    MIN_X                = 1.5

    
    isPinBar = ( bar.H == bar.L )        
    if( not isPinBar ) :
        if trade.isLong:
            isPinBar = (bar.H-bar.C) / (bar.H-bar.L) > 0.75
        else:
            isPinBar = (bar.C-bar.L) / (bar.H-bar.L) > 0.75
    
    if( not isPinBar ):
        extremePrice = mechTM.getExtremePrice( trade, bar, extremePrice )

    if(  mechTM.currentTradeStatus( trade, initStop, bar ) >= MIN_X  ):
            distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
            return  mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 
    
    return stop
Example #14
0
def trailOnProfit2(trade, bar, initStop, stop):
    global extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2
    MIN_X = 1.5

    isPinBar = (bar.H == bar.L)
    if (not isPinBar):
        if trade.isLong:
            isPinBar = (bar.H - bar.C) / (bar.H - bar.L) > 0.75
        else:
            isPinBar = (bar.C - bar.L) / (bar.H - bar.L) > 0.75

    if (not isPinBar):
        extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)

    if (mechTM.currentTradeStatus(trade, initStop, bar) >= MIN_X):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        return mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop