def manage(candle, fb, symbol, all_trades, params): trades = [t for t in all_trades if t.symbol == symbol and t.is_open] for trade in trades: trade.update_trade(candle) if not trade.is_closed: if trade.direction == 'BUY': tp_base = sum([d['high'] for d in trade.data])/len(trade.data) trade.takeprofit = tp_base*params.get('tp_koef', 2.1) elif trade.direction == 'SELL': tp_base = sum([d['low'] for d in trade.data])/len(trade.data) trade.takeprofit = tp_base/params.get('tp_koef', 2.1) # FIA — low profit - good winrate if params.get('use_FIA', False): fia_dmin = params.get('fia_dmin', 5) fia_dmax = params.get('fia_dmax', 15) fia_treshold = params.get('fia_treshold', 0.1) if trade.days > fia_dmin and trade.days < fia_dmax and (trade.profit/trade.days)/trade.open_price*100 < fia_treshold and trade.profit > 0: trade.close_trade(candle, candle.close_price, 'FIA') #BREAKEVEN if not trade.is_closed and params.get('use_BREAKEVEN', False): if trade.direction == 'BUY' and trade.stoploss < trade.open_price and candle.low_price > trade.open_price: trade.stoploss = candle.low_price if trade.direction == 'SELL' and trade.stoploss > trade.open_price and candle.high_price < trade.open_price: trade.stoploss = candle.high_price #FORCE TAKE PROFIT if not trade.is_closed and params.get('use_FTP', False): if (trade.profit/trade.days)/trade.open_price > params.get('FTP', 0.01): trade.close_trade(candle, candle.close_price, 'FTP') # PULL TO HAMMER/DOJI/SHOOTING STAR pull = False if not trade.is_closed: if candle.is_hammer(): if params.get('use_PTH', False): pth = params.get('pth_mix', 0.25) if trade.direction == 'BUY': nsl = trade.stoploss*pth+candle.low_price*(1-pth) elif trade.direction == 'SELL': nsl = trade.stoploss*pth+candle.high_price*(1-pth) pull = True if candle.is_shooting_star(): if params.get('use_PTSS', False): ptss = params.get('ptss_mix', 0.25) if trade.direction == 'BUY': nsl = trade.stoploss*ptss+candle.low_price*(1-ptss) elif trade.direction == 'SELL': nsl = trade.stoploss*ptss+candle.high_price*(1-ptss) pull = True if candle.is_doji(): if params.get('use_PTDJ', False): ptdj = params.get('ptdj_mix', 0.25) if trade.direction == 'BUY': nsl = trade.stoploss*ptdj+candle.low_price*(1-ptdj) if trade.direction == 'SELL': nsl = trade.stoploss*ptdj+candle.high_price*(1-ptdj) pull = True if fb.pointer > 5 and params.get('use_PTTF', False): f = fb.last(symbol, 5, figure=True) if f.is_top_fractal(): ptf = params.get('pttf_mix', 0.25) if trade.direction == 'BUY': nsl = trade.stoploss*ptf+candle.low_price*(1-ptf) elif trade.direction == 'SELL': nsl = trade.stoploss*ptf+candle.high_price*(1-ptf) pull = True if fb.pointer > 5 and params.get('use_PTBF', False): f = fb.last(symbol, 5, figure=True) if f.is_bottom_fractal(): ptf = params.get('ptbf_mix', 0.25) if trade.direction == 'BUY': nsl = trade.stoploss*ptf+candle.low_price*(1-ptf) elif trade.direction == 'SELL': nsl = trade.stoploss*ptf+candle.high_price*(1-ptf) pull = True if params.get('use_PTC2', False): ptf = params.get('ptc2_mix', 0.25) if CCI(fb.last(symbol, 2)) < CCI(fb.last(symbol,2, -1)): if trade.direction == 'BUY': nsl = trade.stoploss*ptf+candle.low_price*(1-ptf) pull = True elif CCI(fb.last(symbol, 2)) > CCI(fb.last(symbol, 2, -1)): if trade.direction == 'SELL': nsl = trade.stoploss*ptf+candle.high_price*(1-ptf) pull = True if pull: if (nsl > trade.stoploss and trade.direction == 'BUY') or (nsl < trade.stoploss and trade.direction == 'SELL'): trade.stoploss = nsl pull = False
def open(candle, fb, symbol, trades, params): trade = None allowed_to_buy = False allowed_to_sell = False has_buy_signal = False has_sell_signal = False open_reason = None if True: # TAIL if params.get('open_TAIL', False): bs = 0.01 if candle.body_size() == 0 else candle.body_size() if candle.low_tail()/bs > 0.2: has_buy_signal = True open_reason = 'TAIL_BUY' elif candle.high_tail()/bs > 0.2: has_sell_signal = True open_reason = 'TAIL_SELL' if fb.pointer > 5: # BREAKUP if params.get('open_BREAK', False): f = fb.last(symbol, 5, figure=True) if f.is_breakup(): has_buy_signal = True open_reason = 'BREAKUP_BUY' if f.is_breakdown(): has_sell_signal = True open_reason = 'BREAKDOWN_SELL' #HAMMER if params.get('open_HAMMER', False): f = fb.last(symbol, 3, figure=True) if f.summary().is_hammer() or f.summary(last=2).is_hammer(): has_buy_signal = True open_reason = 'HAMMER_BUY' elif f.summary().is_shooting_star() or f.summary(last=2).is_shooting_star(): has_sell_signal = True open_reason = "S_STAR_SELL" #FRACTAL if params.get('open_FRACTAL', False): f = fb.last(symbol, 5, figure=True) if f.is_bottom_fractal(): has_buy_signal = True open_reason = 'FRAC_BUY' elif f.is_top_fractal(): has_sell_signal = True open_reason = 'FRAC_SELL' if params.get('open_C2', False): if CCI(fb.last(symbol, 2)) > CCI(fb.last(symbol, 2, -1)): has_buy_signal = True open_reason = 'C2_BUY' elif CCI(fb.last(symbol, 2)) > CCI(fb.last(symbol, 2, -1)): has_buy_signal = True open_reason = 'C2_SELL' filter_passed = True if params.get('use_FILTERS', False): filter_passed = False max_per = params.get('f_max_per', 250) th = params.get('f_max_th', 0.8) if fb.pointer > max_per: m = max(bar['high'] for bar in fb.last(symbol, max_per)) if candle.close_price > m*th: filter_passed = True if filter_passed and (has_buy_signal or has_sell_signal): tp_value = candle.close_price*params.get('rel_tp_k', 0.2) if has_buy_signal: #if ts['open_long'] > ts['open_short'] or ts['open'] == 0: allowed_to_buy = True # else: # if ts['open_profit'] > -0.5: # PARAMS! # if params.get('use_FLIP', False): # close_all(trades, candle, 'FLIP') # allowed_to_sell = True if has_sell_signal: # if ts['open_long'] < ts['open_short'] or ts['open'] == 0: allowed_to_sell = True # else: # if ts['open_profit'] > -0.5: # if params.get('use_FLIP', False): # close_all(trades, candle, 'FLIP') # allowed_to_buy = True if allowed_to_buy and allowed_to_sell: allowed_to_buy = False allowed_to_sell = False if allowed_to_buy: trade = Trade() trade.open_trade(symbol,'BUY', candle, candle.close_price, candle.low_price*params.get('init_sl_k', 0.98), candle.close_price + tp_value, open_reason) if allowed_to_sell and params.get('trade_short', False): trade = Trade() trade.open_trade(symbol,'SELL', candle, candle.close_price, candle.high_price*(2-params.get('init_sl_k', 0.98)), candle.close_price - tp_value, open_reason) return trade
ssum += this.close_price - this.open_price delta = this.close_price - prev.close_price #delta = this.close_price - this.open_price if prev.is_bullish(): white = white + delta if prev.is_bearish(): black += delta if prev.is_doji(): doji += delta if prev.is_hammer(): hammer += delta if prev.is_shooting_star(): ss += delta if prev.is_shooting_star() and prev.is_bearish(): bs += delta cond = CCI(chart.last(14, -1)) < 0 if CCI(chart.last(2, -1)) > CCI(chart.last(2, -2)) and CCI( chart.last(2, -2)) < CCI(chart.last(2, -3)) and cond: ccup += delta ccupc += 1 print(ccup) chart.next() print("B: %r, W: %r, D:%r, H:%r, S:%r, CCUP:%r" % (black, white, doji, hammer, ss, ccup)) print(ccupc)
def open(cc, f, symbol, trades, params): trade = None #ts = trade_stats(trades) allowed_to_buy = False has_buy_signal = False open_reason = None if True: # TAIL if params.get('open_TAIL', False): bs = 0.01 if cc.body_size() == 0 else cc.body_size() if cc.low_tail() / bs > 0.2: has_buy_signal = True open_reason = 'TAIL_BUY' if f.pointer > 50: # BREAKUP if params.get('open_BREAK', False): ff = f.last(symbol, 5, figure=True) if ff.is_breakup(): has_buy_signal = True open_reason = 'BREAKUP_BUY' #HAMMER if params.get('open_HAMMER', False): ff = f.last(symbol, 3, figure=True) if ff.summary().is_hammer() or ff.summary(last=2).is_hammer(): has_buy_signal = True open_reason = 'HAMMER_BUY' #DOUBLE HAMMER if params.get('open_DOUBLE_HAMMER', False): pf = params.get('dh_fast', 2) ps = params.get('dh_slow', 20) if f.last(symbol, pf, figure=True).summary().is_hammer() and f.last( symbol, ps, figure=True).summary().is_hammer(): has_buy_signal = True open_reason = 'DOUBLE_HAMMER' #FRACTAL if params.get('open_FRACTAL', False): ff = f.last(symbol, 5, figure=True) if ff.is_bottom_fractal(): has_buy_signal = True open_reason = 'FRAC_BUY' #C2 if params.get('open_C2', False): if CCI(f.last(symbol, 2)) > CCI(f.last(symbol, 2, -1)): has_buy_signal = True open_reason = 'C2_BUY' passed_filters = [] if params.get('use_HIGH_FILTER', False): high_filter_passed = 0 max_per = params.get('hf_max_per', 250) th = params.get('hf_max_th', 0.8) if f.pointer > max_per: m = max(bar['high'] for bar in f.last(symbol, max_per)) if cc.close_price > m * th: high_filter_passed = 1 passed_filters.append(high_filter_passed) if params.get('use_CCI_FILTER', False): cci_filter_passed = 0 per = params.get('cci_f_per', 14) if CCI(f.last(symbol, per)) > CCI(f.last(symbol, per, -1)): cci_filter_passed = 1 passed_filters.append(cci_filter_passed) # if params.get('use_COS_FILTER', False): # cos_filter_passed = 0 # per = params.get('cos_f_per', 14) # if CCI(f.last(symbol, per)) > params.get('cos_f_val', 50): # cos_filter_passed = 1 # passed_filters.append(cos_filter_passed) if params.get('use_SMA_FILTER', False): sma_filter_passed = 0 per1 = params.get('sma_f_per_1', 12) # 5 - 15 per2 = params.get('sma_f_per_2', 24) # 16 - 30 per3 = params.get('sma_f_per_3', 50) # 31 - 50 if SMA(f.last(symbol, per1)) > SMA(f.last(symbol, per2)) > SMA( f.last(symbol, per3)): sma_filter_passed = 1 passed_filters.append(sma_filter_passed) all_filters_passed = sum(passed_filters) == len(passed_filters) if all_filters_passed and has_buy_signal: tp_value = cc.close_price * params.get('rel_tp_k', 0.2) if has_buy_signal: allowed_to_buy = True if allowed_to_buy: trade = Trade() trade.open_trade(symbol, 'BUY', cc, cc.close_price, cc.low_price * params.get('init_sl_k', 0.98), cc.close_price + tp_value, open_reason) return trade
def manage(cc, f, symbol, all_trades, params): trades = [t for t in all_trades if t.symbol == symbol and t.is_open] for trade in trades: trade.update_trade(cc) if not trade.is_closed: tp_base = sum([d['high'] for d in trade.data]) / len(trade.data) tp_base = sum([d['high'] for d in trade.data]) / len(trade.data) trade.takeprofit = tp_base * params.get('tp_koef', 2.1) # FIA — low profit - good winrate if not trade.is_closed and params.get('use_FIA', False): fia_dmin = params.get('fia_dmin', 5) fia_dmax = params.get('fia_dmax', 15) fia_treshold = params.get('fia_treshold', 0.1) if trade.days > fia_dmin and trade.days < fia_dmax and ( trade.profit / trade.days ) / trade.open_price * 100 < fia_treshold and trade.profit > 0: trade.close_trade(cc, cc.close_price, 'FIA') #BREAKEVEN if not trade.is_closed and params.get('use_BREAKEVEN', False): if trade.stoploss < trade.open_price and cc.low_price > trade.open_price: trade.stoploss = cc.low_price #FORCE TAKE PROFIT if not trade.is_closed and params.get('use_FTP', False): if (trade.profit / trade.days) / trade.open_price > params.get( 'FTP', 0.01): trade.close_trade(cc, cc.close_price, 'FTP') # PULL TO HAMMER/DOJI/SHOOTING STAR pull = False if not trade.is_closed: if cc.is_hammer(): if params.get('use_PTH', False): pth = params.get('pth_mix', 0.25) nsl = trade.stoploss * pth + cc.low_price * (1 - pth) pull = True if cc.is_shooting_star(): if params.get('use_PTSS', False): ptss = params.get('ptss_mix', 0.25) nsl = trade.stoploss * ptss + cc.low_price * (1 - ptss) pull = True if cc.is_doji(): if params.get('use_PTDJ', False): ptdj = params.get('ptdj_mix', 0.25) nsl = trade.stoploss * ptdj + cc.low_price * (1 - ptdj) pull = True if f.pointer > 5 and params.get('use_PTTF', False): ff = f.last(symbol, 5, figure=True) if ff.is_top_fractal(): ptf = params.get('pttf_mix', 0.25) nsl = trade.stoploss * ptf + cc.low_price * (1 - ptf) pull = True if f.pointer > 5 and params.get('use_PTBF', False): ff = f.last(symbol, 5, figure=True) if ff.is_bottom_fractal(): ptf = params.get('ptbf_mix', 0.25) nsl = trade.stoploss * ptf + cc.low_price * (1 - ptf) pull = True # if f.pointer > 10 and params.get('use_PTP', False): # if f.last(symbol, 10, figure=True).is_power_growth: # ptf = params.get('ptp_mix', 0.5) # nsl = trade.stoploss*ptf+cc.low_price*(1-ptf) # pull = True if params.get('use_PTC2', False): ptf = params.get('ptc2_mix', 0.25) if CCI(f.last(symbol, 2)) < CCI(f.last(symbol, 2, -1)): nsl = trade.stoploss * ptf + cc.low_price * (1 - ptf) pull = True if pull: if nsl > trade.stoploss: trade.stoploss = nsl pull = False
def open(cc, c, trades, params): trade = None #ts = trade_stats(trades) allowed_to_buy = False has_buy_signal = False open_reason = None if True: #ts['open'] <= params.get('max_pos', 50): # TAIL if params.get('open_TAIL', False): bs = 0.01 if cc.body_size() == 0 else cc.body_size() if cc.low_tail()/bs > 0.2: has_buy_signal = True open_reason = 'TAIL_BUY' if c.pointer > 5: # BREAKUP if params.get('open_BREAK', False): f = c.last(5, figure=True) if f.is_breakup(): has_buy_signal = True open_reason = 'BREAKUP_BUY' #HAMMER if params.get('open_HAMMER', False): f = c.last(3, figure=True) if f.summary().is_hammer() or f.summary(last=2).is_hammer(): has_buy_signal = True open_reason = 'HAMMER_BUY' #DOUBLE HAMMER if params.get('open_DOUBLE_HAMMER', False): pf = params.get('dh_fast', 2) ps = params.get('dh_slow', 20) if c.last(pf, figure=True).summary().is_hammer() and c.last(ps, figure=True).summary().is_hammer(): has_buy_signal = True open_reason = 'DOUBLE_HAMMER' #FRACTAL if params.get('open_FRACTAL', False): f = c.last(5, figure=True) if f.is_bottom_fractal(): has_buy_signal = True open_reason = 'FRAC_BUY' if params.get('open_C2', False): if CCI(c.last(2)) > CCI(c.last(2, -1)): has_buy_signal = True open_reason = 'C2_BUY' passed_filters = [] if params.get('use_HIGH_FILTER', False): high_filter_passed = 0 max_per = params.get('hf_max_per', 250) th = params.get('hf_max_th', 0.8) if c.pointer > max_per: m = max(bar['high'] for bar in c.last(max_per)) if cc.close_price > m*th: high_filter_passed = 1 passed_filters.append(high_filter_passed) if params.get('use_CCI_FILTER', False): cci_filter_passed = 0 per = params.get('cci_f_per', 14) if CCI(c.last(per)) > CCI(c.last(per, -1)): cci_filter_passed = 1 passed_filters.append(cci_filter_passed) all_filters_passed = sum(passed_filters) == len(passed_filters) if all_filters_passed and has_buy_signal: tp_value = cc.close_price*params.get('rel_tp_k', 0.2) if has_buy_signal: #if ts['open_long'] > ts['open_short'] or ts['open'] == 0: allowed_to_buy = True if allowed_to_buy: trade = Trade() trade.open_trade(c.symbol, 'BUY', cc, cc.close_price, cc.low_price*params.get('init_sl_k', 0.98), cc.close_price + tp_value, open_reason) return trade