Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 3
0
def trailNearClose( trade, bar, initStop, stop ):
    global recentExtremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2                    # trail by x atr
    TIME_CUT_OFF         = ( 14, 54 )           # Trail only after cut off time    
    isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
    
    # Another Wider Trail earlier
    '''
    if( not isTime ):
        TRAIL_ATR_MULTIPLIER = 5
        TIME_CUT_OFF         = ( 14, 29 )
        isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
    '''

    if( 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  ) 
        #print(  trade.Market, bar.Index,  stop, "------", recentExtremePrice, trailAtr[bar.Index] )

    return stop
Exemplo n.º 4
0
def trailNearClose(trade, bar, initStop, stop):
    global recentExtremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2  # trail by x atr
    TIME_CUT_OFF = (14, 54)  # Trail only after cut off time
    isTime = isTimeTriggered(bar, TIME_CUT_OFF)

    # Another Wider Trail earlier
    '''
    if( not isTime ):
        TRAIL_ATR_MULTIPLIER = 5
        TIME_CUT_OFF         = ( 14, 29 )
        isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
    '''

    if (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)
        #print(  trade.Market, bar.Index,  stop, "------", recentExtremePrice, trailAtr[bar.Index] )

    return stop
Exemplo n.º 5
0
def trailOnSpike( trade, bar, initStop, stop ):
    global extremePrice, trailAtr, ss
        
    TRAIL_ATR_MULTIPLIER = 2    
    extremePrice         = mechTM.getExtremePrice( trade, bar, extremePrice )
    spike                = ss[bar.Index]
    
    if( (trade.isLong and spike >= 5)  or ( not trade.isLong and spike <= -5) ):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
        stop     = mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 
    
    return stop
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 8
0
def trailOnSpike(trade, bar, initStop, stop):
    global extremePrice, trailAtr, ss

    TRAIL_ATR_MULTIPLIER = 2
    extremePrice = mechTM.getExtremePrice(trade, bar, extremePrice)
    spike = ss[bar.Index]

    if ((trade.isLong and spike >= 5) or (not trade.isLong and spike <= -5)):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop
Exemplo n.º 9
0
def timeStopTrailNearClose( trade, bar, initStop, stop ):
    global extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2                    # trail by x atr    
    TIME_CUT_OFF         = ( 14,54 )            # Trail only after cut off time
    
    extremePrice         = mechTM.getExtremePrice( trade, bar, extremePrice )
    isTime               = isTimeTriggered( bar, TIME_CUT_OFF )
        
    # Time Stop. Only hold if price within 2 ATR of extreme and trail
    if( isTime  ):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER        
        stop     = mechTM.getTrailPrice( trade, bar, initStop, extremePrice, distance  ) 

    return stop
Exemplo n.º 10
0
def timeStopTrailNearClose(trade, bar, initStop, stop):
    global extremePrice, trailAtr

    TRAIL_ATR_MULTIPLIER = 2  # trail by x atr
    TIME_CUT_OFF = (14, 54)  # Trail only after cut off time

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

    # Time Stop. Only hold if price within 2 ATR of extreme and trail
    if (isTime):
        distance = trailAtr[bar.Index] * TRAIL_ATR_MULTIPLIER
        stop = mechTM.getTrailPrice(trade, bar, initStop, extremePrice,
                                    distance)

    return stop
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo n.º 15
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
Exemplo n.º 16
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