119 lines
No EOL
3.7 KiB
Python
119 lines
No EOL
3.7 KiB
Python
'''
|
|
Engulfing Candle Strategy Overview:
|
|
|
|
Pattern Recognition:
|
|
- Identifies engulfing candle patterns on 15-minute timeframe
|
|
- BULLISH engulfing: Current bid > previous candle close
|
|
- BEARISH engulfing: Current bid < previous candle close
|
|
|
|
Entry Conditions:
|
|
- LONG: Bid > last close AND bid > 20-period SMA (trend confirmation)
|
|
- SHORT: Bid < last close AND bid < 20-period SMA (trend confirmation)
|
|
- Uses dual confirmation: price action + trend direction
|
|
|
|
Order Management:
|
|
- Entry orders placed at 0.1% offset from current bid/ask
|
|
- Cancels all existing orders before placing new ones
|
|
- 2-minute pause after order placement
|
|
|
|
Exit Conditions:
|
|
- Profit target: 9% gain
|
|
- Stop loss: 8% loss
|
|
- Managed through external PnL function
|
|
|
|
Technical Setup:
|
|
- Timeframe: 15 minutes
|
|
- SMA period: 20
|
|
- Combines reversal pattern with trend following for higher probability trades
|
|
|
|
WARNING: Do not run without thorough backtesting and understanding!
|
|
'''
|
|
|
|
import ccxt
|
|
import time, schedule
|
|
import Functions.funcs as func
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
api_key = os.getenv('BINANCE_API_KEY')
|
|
api_secret = os.getenv('BINANCE_SECRET_KEY')
|
|
|
|
exchange = ccxt.binance({
|
|
'apiKey': api_key,
|
|
'secret': api_secret,
|
|
'enableRateLimit': True,
|
|
'options': {
|
|
'defaultType': 'future',
|
|
}
|
|
})
|
|
|
|
symbol = 'BTCUSDT'
|
|
pos_size = 1
|
|
target = 9
|
|
max_loss = -8
|
|
params = {'timeInForce': 'GTC'}
|
|
|
|
def bot():
|
|
# Check PnL and manage existing positions
|
|
func.pnl_close(symbol, target, max_loss)
|
|
|
|
# Get market data and indicators
|
|
timeframe = '15m'
|
|
limit = 97
|
|
sma = 20
|
|
df = func.df_sma(symbol, timeframe, limit, sma)
|
|
|
|
# Get current prices
|
|
ask, bid = func.ask_bid(symbol)
|
|
|
|
# Get position info
|
|
_, in_position, current_size, _ = func.open_positions(symbol)
|
|
current_size = int(current_size) if current_size else 0
|
|
|
|
# Get technical levels
|
|
sma_20_15m = df.iloc[-1][f'sma{sma}_{timeframe}']
|
|
last_close = df.iloc[-1]['close']
|
|
|
|
print(f'Position: {in_position} | Size: {current_size} | Bid: {bid:.2f} | SMA: {sma_20_15m:.2f} | Last Close: {last_close:.2f}')
|
|
|
|
if not in_position:
|
|
# BULLISH engulfing: bid above last close + above SMA (uptrend)
|
|
if bid > last_close and bid > sma_20_15m:
|
|
print('BULLISH engulfing pattern detected - Opening LONG')
|
|
exchange.cancel_all_orders(symbol)
|
|
|
|
# Refresh prices and place order
|
|
ask, bid = func.ask_bid(symbol)
|
|
order_price = bid * 0.999 # 0.1% below bid
|
|
exchange.create_limit_buy_order(symbol, pos_size, order_price, params)
|
|
|
|
print(f'BUY order placed at {order_price:.2f} - Sleeping 2 minutes')
|
|
time.sleep(120)
|
|
|
|
# BEARISH engulfing: bid below last close + below SMA (downtrend)
|
|
elif bid < last_close and bid < sma_20_15m:
|
|
print('BEARISH engulfing pattern detected - Opening SHORT')
|
|
exchange.cancel_all_orders(symbol)
|
|
|
|
# Refresh prices and place order
|
|
ask, bid = func.ask_bid(symbol)
|
|
order_price = ask * 1.001 # 0.1% above ask
|
|
exchange.create_limit_sell_order(symbol, pos_size, order_price, params)
|
|
|
|
print(f'SELL order placed at {order_price:.2f} - Sleeping 2 minutes')
|
|
time.sleep(120)
|
|
|
|
else:
|
|
print('No engulfing pattern or trend confirmation - No trade')
|
|
else:
|
|
print('Already in position - Monitoring...')
|
|
|
|
schedule.every(28).seconds.do(bot)
|
|
|
|
while True:
|
|
try:
|
|
schedule.run_pending()
|
|
except Exception as e:
|
|
print(f'Error: {e} - Sleeping 30 seconds')
|
|
time.sleep(30) |