Usuário
Buy $1000 worth of Bitcoin every day while price is below $50k. Sell 50% at $75k and 50% at $100k
CODY - Crypto Strategy Assistant
Buy $1000 worth of Bitcoin every day while price is below $50k. Sell 50% at $75k and 50% at $100k
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:
Here's the Python code for the Quadency bot framework:
def initialize(self, state, context, args):
state['lastPurchase'] = None
state['btcHolding'] = 0.0
def run_iteration(self, state, context, args):
inputs = {
"symbol": args.params['pair'],
"buyThreshold": 50000,
"sellThreshold1": 75000,
"sellThreshold2": 100000,
"dailyInvestment": 1000
}
current_time = context.date()
price = self.exchange.fetch_ticker(inputs['symbol'])['last']
# Buy BTC if the price is below the threshold and it's been a day since last purchase
if price < inputs['buyThreshold'] and (state['lastPurchase'] is None or current_time - state['lastPurchase'] >= timedelta(days=1)):
baseAmount = inputs['dailyInvestment'] / price
order = self.exchange.create_order(type='market', side='buy', symbol=inputs['symbol'], amount=baseAmount)
log.info(f"Purchased {baseAmount} BTC")
state['btcHolding'] += baseAmount
state['lastPurchase'] = current_time
# Sell 50% of BTC holdings at each threshold
if state['btcHolding'] > 0:
if price >= inputs['sellThreshold1']:
sellAmount = state['btcHolding'] * 0.5
order = self.exchange.create_order(type='market', side='sell', symbol=inputs['symbol'], amount=sellAmount)
log.info(f"Sold 50% of holdings at {inputs['sellThreshold1']}")
state['btcHolding'] -= sellAmount
elif price >= inputs['sellThreshold2']:
sellAmount = state['btcHolding']
order = self.exchange.create_order(type='market', side='sell', symbol=inputs['symbol'], amount=sellAmount)
log.info(f"Sold remaining holdings at {inputs['sellThreshold2']}")
state['btcHolding'] = 0
To use this bot on Quadency:
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.