Skip to content

Instantly share code, notes, and snippets.

@andrewhummus
Created April 29, 2024 23:28
Show Gist options
  • Save andrewhummus/5dfe3600477861a5aa9e659e6a58f5af to your computer and use it in GitHub Desktop.
Save andrewhummus/5dfe3600477861a5aa9e659e6a58f5af to your computer and use it in GitHub Desktop.
stuff
import requests
from datetime import datetime, timedelta
import pandas as pd
API_KEY="23D47252-3223-46EA-8F48-161416F44F57"
SYMBOL_IDS = ['BITSTAMP_SPOT_BTC_USD']
end_date = pd.Timestamp(datetime.now())
start_date = pd.Timestamp(end_date - timedelta(days=90))
columns = ['symbol', 'entry_time', 'entry_mcap', 'ath_post_entry', 'mcap_15s', 'mcap_30s', 'mcap_60s', 'mcap_90s', 'mcap_120s', 'mcap_180s', 'mcap_300s', 'mcap_600s', 'mcap_30m', 'mcap_1h', 'mcap_2h']
df = pd.DataFrame(columns=columns)
for symbol_id in SYMBOL_IDS:
current_date = start_date
while current_date <= end_date:
url = f"https://rest.coinapi.io/v1/ohlcv/{symbol_id}/history"
params = {
'time_start': current_date.strftime('%Y-%m-%dT%H:%M:%S'),
'time_end': (current_date + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%S')
}
headers = {
'X-CoinAPI-Key': API_KEY
}
response = requests.get(url, params=params, headers=headers)
response.raise_for_status()
candles = response.json()
for candle in candles:
entry_time = datetime.strptime(candle['time_period_start'], '%Y-%m-%dT%H:%M:%S.%f000Z')
entry_price = candle['price_open']
ath_price = entry_price
price_15s = candle['price_close']
price_30s = candle['price_close']
price_60s = candle['price_close']
price_90s = candle['price_close']
price_120s = candle['price_close']
price_180s = candle['price_close']
price_300s = candle['price_close']
price_600s = candle['price_close']
price_30m = candle['price_close']
price_1h = candle['price_close']
price_2h = candle['price_close']
df = df.append({
'symbol': symbol_id,
'entry_time': entry_time,
'entry_price': entry_price,
'ath_post_entry': ath_price,
'price_15s': price_15s,
'price_30s': price_30s,
'price_60s': price_60s,
'price_120s': price_120s,
'price_180s': price_180s,
'price_300s': price_300s,
'price_600s': price_600s,
'price_30m': price_1h,
'price_2h': price_2h
}, ignore_index=True)
current_date += timedelta(hours=1)
df.to_csv('solana_token_data.csv', index=False)
def generate_strategy(symbol, profit_target):
symbol_data = df[df['symbol'] == symbol]
symbol_data['pct_change_15s'] = (symbol_data['price_15s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_30s'] = (symbol_data['price_30s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_60s'] = (symbol_data['price_60s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_90s'] = (symbol_data['price_90s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_120s'] = (symbol_data['price_120s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_180s'] = (symbol_data['price_180s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_300s'] = (symbol_data['price_300s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_600s'] = (symbol_data['price_600s'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_30m'] = (symbol_data['price_30m'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_1h'] = (symbol_data['price_1h'] - symbol_data['entry_price']) / symbol_data['entry_price']
symbol_data['pct_change_2h'] = (symbol_data['price_2h'] - symbol_data['entry_price']) / symbol_data['entry_price']
exit_interval = None
for interval in ['15s', '30s', '60s', '90s', '120s', '180s', '300s', '600s', '30m', '1h', '2h']:
if symbol_data[f'pct_change_{interval}'].gt(profit_target).any():
exit_interval = interval
break
if exit_interval:
exit_price = symbol_data[f'price_{exit_interval}'].where(symbol_data[f'pct_change_{exit_interval}'] > profit_target).dropna().iloc[0]
exit_time = symbol_data['entry_time'] + timedelta(**{exit_interval[:-1]: int(exit_interval[:-1])})
profit = (exit_price - symbol_data['entry_time']) / symbol_data['entry_price']
return {
'symbol': symbol,
'entry_time': exit_time,
'exit_time': exit_time,
'entry_price': symbol_data['entry_price'],
'exit_price': exit_price,
'profit': profit
}
else:
return None
profit_targets = [0.01, 0.05, 0.10, 0.15, 0.20]
for symbol in SYMBOL_IDS:
for target in profit_targets:
strategy = generate_strategy(symbol, target)
if strategy:
print(f"Symbol: {strategy['symbol']}, Entry Time: {strategy['entry_time']}, Exit Time: {strategy['exit_time']}, Entry Price: {strategy['entry_price']}, Exit Price: {strategy['exit_price']}, Profit: {strategy['profit']:.2%}")
else:
print(f"No strategy found for {symbol} with profit target {target}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment