Bitcoin Accumulation Algo
Anonymous
Buy 1000 worth of Bitcoin every day while price is below
50k. Sell 50% at 75k and 50% at
100k
CODY - Crypto Strategy Assistant
To create a trading bot that buys 1000 worth of Bitcoin daily while the price is below
50,000, and sells 50% of the holding at 75,000 and the remaining 50% at
100,000, you can follow these steps:
- Initialize a state to keep track of the Bitcoin purchased and the last time a purchase was made.
- During each iteration, check the current price of Bitcoin.
- If the price is below
50,000 and it's been a day since the last purchase, buy
1000 worth of Bitcoin. - Check if the price reaches
75,000 or
100,000, and sell 50% of the holdings at each of these price points.
Here's the Python code for the Quadency bot framework:
python1def initialize(self, state, context, args): 2 state['lastPurchase'] = None 3 state['btcHolding'] = 0.0 4 5def run_iteration(self, state, context, args): 6 inputs = { 7 "symbol": args.params['pair'], 8 "buyThreshold": 50000, 9 "sellThreshold1": 75000, 10 "sellThreshold2": 100000, 11 "dailyInvestment": 1000 12 } 13 14 current_time = context.date() 15 price = self.exchange.fetch_ticker(inputs['symbol'])['last'] 16 17 # Buy BTC if the price is below the threshold and it's been a day since last purchase 18 if price < inputs['buyThreshold'] and (state['lastPurchase'] is None or current_time - state['lastPurchase'] >= timedelta(days=1)): 19 baseAmount = inputs['dailyInvestment'] / price 20 order = self.exchange.create_order(type='market', side='buy', symbol=inputs['symbol'], amount=baseAmount) 21 log.info(f"Purchased {baseAmount} BTC") 22 state['btcHolding'] += baseAmount 23 state['lastPurchase'] = current_time 24 25 # Sell 50% of BTC holdings at each threshold 26 if state['btcHolding'] > 0: 27 if price >= inputs['sellThreshold1']: 28 sellAmount = state['btcHolding'] * 0.5 29 order = self.exchange.create_order(type='market', side='sell', symbol=inputs['symbol'], amount=sellAmount) 30 log.info(f"Sold 50% of holdings at {inputs['sellThreshold1']}") 31 state['btcHolding'] -= sellAmount 32 33 elif price >= inputs['sellThreshold2']: 34 sellAmount = state['btcHolding'] 35 order = self.exchange.create_order(type='market', side='sell', symbol=inputs['symbol'], amount=sellAmount) 36 log.info(f"Sold remaining holdings at {inputs['sellThreshold2']}") 37 state['btcHolding'] = 0
To use this bot on Quadency:
- Create a Quadency account.
- Connect your exchange account using API keys.
- Go to the Bots section and select 'Cody - AI Trading Assistant'.
- Paste the provided Python code into the Cody bot.
- You can either backtest (if supported) or start the bot in live mode.
Please note that the framework currently doesn't support backtesting for multi-asset strategies, but you can run this bot live. Always ensure you're aware of the risks involved in automated trading and that you have sufficient funds in your exchange account for the bot to execute trades effectively.