1
Fork 0
crypto_bot_training/Session_05/bots/Turtle.py
2025-06-26 15:46:49 +02:00

84 lines
No EOL
2.6 KiB
Python

'''
Turtle Strategy Overview:
- Works on multiple timeframes: 1m, 5m, 15m, 1h, 4h
- Enter LONG when 55-bar high is broken with upward momentum (limit order at bid)
- Enter SHORT when 55-bar low is broken with downward momentum (limit order at bid)
- Only trades during market hours: 9:30 AM - 4:00 PM EST, Monday-Friday
- Exit before 4:00 PM on Fridays if still in position
Exit Conditions:
- Take Profit: 0.2% gain from entry price
- Stop Loss: 2x ATR (Average True Range) from entry price
WARNING: Do not run without thorough backtesting and understanding!
'''
import ccxt, time, schedule
from Functions.functions import *
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',
}
})
timeframe = '1m'
symbol = 'ETHUSDT'
size = 1
params = {'timeInForce': 'GTC'}
take_profit_percent = 0.2
def bot():
if in_timeframe():
position_info, in_position, long = get_position(exchange, symbol)
candles = get_candle_df(exchange, symbol, timeframe, limit=55)
ticker = exchange.fetch_ticker(symbol)
bid = ticker['bid']
open_price = ticker['open']
if not in_position:
min_price = candles['low'].min()
max_price = candles['high'].max()
if bid <= min_price and open_price > min_price:
exchange.create_limit_sell_order(symbol, size, bid, params)
elif bid >= max_price and open_price < max_price:
exchange.create_limit_buy_order(symbol, size, bid, params)
elif in_position:
calc_atr(candles)
entry_price = float(position_info['entryPrice'])
atr = candles['ATR'].iloc[-1]
if long:
take_profit_price = entry_price * (1 + (take_profit_percent / 100))
stop_loss_price = entry_price - (atr * 2)
else:
take_profit_price = entry_price * (1 - (take_profit_percent / 100))
stop_loss_price = entry_price + (atr * 2)
if hit_target(bid, take_profit_price, stop_loss_price, long):
close_position(exchange, symbol)
elif end_of_trading_week():
close_position(exchange, symbol)
schedule.every(60).seconds.do(bot)
while True:
try:
schedule.run_pending()
except Exception as e:
print(f'ERROR RUNNING BOT: {e}. Sleeping 30 seconds...')
time.sleep(30)