![](https://crypto4nerd.com/wp-content/uploads/2024/03/17f3vYYnu1nSNA6MhXUUb7g-1024x576.png)
Saving data of multiple cryptocurrencies from TradingView to local system to do backtesting and hyper-optimization on the machine learning models
# !pip install --upgrade --no-cache-dir git+https://github.com/rongardF/tvdatafeed.gitimport os
import json
import asyncio
from datetime import datetime, timedelta
import pandas as pd
from tvDatafeed import TvDatafeed, Interval
# Initialize TvDatafeed object
# username = 'YourTradingViewUsername'
# password = 'YourTradingViewPassword'
# tv = TvDatafeed(username, password)
tv = TvDatafeed()
# List of symbols
data = [
"BTCUSDT.P", "GMTUSDT.P", "ETHUSDT.P", "MTLUSDT.P", "NEARUSDT.P", "SOLUSDT.P", "OGNUSDT.P", "ZILUSDT.P", "APEUSDT.P", "XRPUSDT.P", "ADAUSDT.P", "AVAXUSDT.P", "KNCUSDT.P", "DOGEUSDT.P", "WAVESUSDT.P", "1000SHIBUSDT.P", "FTMUSDT.P", "BNBUSDT.P", "XMRUSDT.P", "DOTUSDT.P", "GALAUSDT.P", "MATICUSDT.P", "LRCUSDT.P", "RUNEUSDT.P", "AUDIOUSDT.P", "FILUSDT.P", "ETCUSDT.P", "EOSUSDT.P", "ZECUSDT.P", "AXSUSDT.P", "LTCUSDT.P", "SANDUSDT.P", "LINKUSDT.P", "SXPUSDT.P", "ATOMUSDT.P", "BCHUSDT.P", "PEOPLEUSDT.P", "MANAUSDT.P", "AAVEUSDT.P", "ALICEUSDT.P", "BNXUSDT.P", "KAVAUSDT.P", "CRVUSDT.P", "ONEUSDT.P", "VETUSDT.P", "THETAUSDT.P", "DYDXUSDT.P", "ICPUSDT.P", "ALGOUSDT.P", "SUSHIUSDT.P", "RENUSDT.P", "COMPUSDT.P", "XLMUSDT.P", "CHZUSDT.P", "TLMUSDT.P", "TRXUSDT.P", "XTZUSDT.P", "FTTUSDT.P", "IMXUSDT.P", "CELRUSDT.P", "WOOUSDT.P", "HNTUSDT.P", "EGLDUSDT.P", "ENJUSDT.P", "CELOUSDT.P", "BATUSDT.P", "KSMUSDT.P", "UNIUSDT.P", "ROSEUSDT.P", "BAKEUSDT.P", "RSRUSDT.P", "IOSTUSDT.P", "GRTUSDT.P", "DASHUSDT.P", "ALPHAUSDT.P", "FLOWUSDT.P", "OCEANUSDT.P", "DENTUSDT.P", "CHRUSDT.P", "OMGUSDT.P", "HOTUSDT.P", "LINAUSDT.P", "SRMUSDT.P", "COTIUSDT.P", "SKLUSDT.P", "NEOUSDT.P", "SNXUSDT.P", "ICXUSDT.P", "ARUSDT.P", "1INCHUSDT.P", "API3USDT.P", "ANKRUSDT.P", "DUSKUSDT.P", "REEFUSDT.P", "BALUSDT.P", "BANDUSDT.P", "ZRXUSDT.P", "C98USDT.P", "QTUMUSDT.P", "STORJUSDT.P", "IOTAUSDT.P", "ONTUSDT.P", "MASKUSDT.P", "GTCUSDT.P", "HBARUSDT.P", "MKRUSDT.P", "TOMOUSDT.P", "ENSUSDT.P", "ZENUSDT.P", "SFPUSDT.P", "CVCUSDT.P", "IOTXUSDT.P", "CTKUSDT.P", "FLMUSDT.P", "NKNUSDT.P", "YFIUSDT.P", "RLCUSDT.P", "BTSUSDT.P", "KLAYUSDT.P", "BELUSDT.P", "XEMUSDT.P", "ANTUSDT.P", "SCUSDT.P", "LITUSDT.P", "CTSIUSDT.P", "STMXUSDT.P", "UNFIUSDT.P", "RVNUSDT.P", "1000XECUSDT.P", "RAYUSDT.P", "BLZUSDT.P", "ATAUSDT.P", "ARPAUSDT.P", "DGBUSDT.P", "LPTUSDT.P", "TRBUSDT.P", "OPUSDT.P", "GALUSDT.P"
]
nest_asyncio.apply()
# Define data download function
async def download_data(symbol):
try:
data = tv.get_hist(symbol=symbol, exchange='BINANCE', interval=Interval.in_15_minute, n_bars=20000, extended_session=True)
if not data.empty:
# Convert Date objects to strings
# data['Date'] = data.index.date.astype(str)
# data['Time'] = data.index.time.astype(str)
data['date'] = data.index.astype(str) # Add a new column for timestamps
folder_name = "tradingview_crypto_assets_15m"
os.makedirs(folder_name, exist_ok=True)
# Replace "USDT.P" with "/USDT:USDT" in the file name
symbol_file_name = symbol.replace("USDT.P", "") + ".json"
file_name = os.path.join(folder_name, symbol_file_name)
# Convert DataFrame to dictionary
data_dict = data.to_dict(orient='records')
with open(file_name, "w") as file:
# Serialize dictionary to JSON
json.dump(data_dict, file)
print(f"Data for {symbol} downloaded and saved successfully.")
else:
print(f"No data available for {symbol}.")
except Exception as e:
print(f"Error occurred while downloading data for {symbol}: {e}")
# Define main function to run async download tasks
async def main():
tasks = [download_data(symbol) for symbol in data]
await asyncio.gather(*tasks)
# Run the main function
asyncio.run(main())
Backtesting Strategy with below function which has Specific Stop Loss, Take profit, leverage setup done:
# Define MyCandlesStrat_3 class
class MyCandlesStrat_3(Strategy):
sl_pct_l = best_params['BEST_STOP_LOSS_sl_pct_long']
tp_pct_l = best_params['BEST_TAKE_PROFIT_tp_pct_long']
limit_l = best_params['BEST_LIMIT_ORDER_limit_long']
sl_pct_s = best_params['BEST_STOP_LOSS_sl_pct_short']
tp_pct_s = best_params['BEST_TAKE_PROFIT_tp_pct_short']
limit_s = best_params['BEST_LIMIT_ORDER_limit_short']
margin_leverage = best_params['BEST_LEVERAGE_margin_leverage']# sl_pct_l = 0.025
# tp_pct_l = 0.025
# limit_l = 0.024
# sl_pct_s = 0.025
# tp_pct_s = 0.025
# limit_s = 0.024
# margin_leverage = 2
def init(self):
super().init()
self.signal1 = self.I(SIGNAL_3, self.data)
def next(self):
super().next()
# if self.position:
if (self.signal1 == 1):
# sl_pct = 0.005 # 2% stop-loss
# tp_pct = 0.005 # 5% take-profit
sl_price = self.data.Close[-1] * (1 - (self.sl_pct_l * 0.001))
tp_price = self.data.Close[-1] * (1 + (self.tp_pct_l * 0.001))
limit_price = tp_price * 0.996
self.position.is_long
self.buy(sl=sl_price, limit=limit_price, tp=tp_price)
elif (self.signal1 == 2):
# sl_pct = 0.005 # 2% stop-loss
# tp_pct = 0.005 # 5% take-profit
sl_price = self.data.Close[-1] * (1 + (self.sl_pct_s * 0.001))
tp_price = self.data.Close[-1] * (1 - (self.tp_pct_s * 0.001))
limit_price = sl_price * 0.996
self.position.is_short
self.sell(sl=sl_price, limit=limit_price, tp=tp_price)
# Run backtest
bt_3 = Backtest(df_ens, MyCandlesStrat_3, cash=100000, commission=.001, margin= (1/MyCandlesStrat_3.margin_leverage), exclusive_orders=False)
stat_3 = bt_3.run()
print("backtest one done at 226 line - ", stat_3)
# Optimization
def optimize_strategy():
# Optimization Params
optimizer = 'Sharpe Ratio'
stats = bt_3.optimize(
sl_pct_l = range(6,100, 2), # (5,10,15,20,25,30,40,50,75,100)
tp_pct_l = range(6,100, 2), # (0.005, 0.01, 0.015, 0.02, 0.025, 0.03, 0.04, 0.05, 0.075, 0.1)
# limit_l = (4,9,14,19,24,29,39,49,74,90),# (0.004, 0.009, 0.014, 0.019, 0.024, 0.029, 0.039, 0.049, 0.074, 0.09)
sl_pct_s = range(6,100, 2),
tp_pct_s = range(6,100, 2),
# limit_s = (4,9,14,19,24,29,39,49,74,90),
margin_leverage = range(1, 6),
constraint=lambda p: ( (p.sl_pct_l > (p.tp_pct_l + 4) ) and ((p.sl_pct_s) > (p.tp_pct_s + 4) )),
maximize = optimizer,
return_optimization=True,
method = 'skopt',
max_tries = 100 # 20% for 0.2 and 100% for 1.0, this applys when not using 'skopt' method, for 'skopt' number starts from 1 to 200 max epochs
)
# Extract the optimization results
best_params = {
'Optimizer': optimizer,
'OptimizerResult_Cross': stats[0][optimizer],
'BEST_STOP_LOSS_sl_pct_long': stats[1].x[0],
'BEST_TAKE_PROFIT_tp_pct_long': stats[1].x[1] ,
'BEST_LIMIT_ORDER_limit_long': stats[1].x[1] * 0.997,
'BEST_STOP_LOSS_sl_pct_short': stats[1].x[2] ,
'BEST_TAKE_PROFIT_tp_pct_short': stats[1].x[3] ,
'BEST_LIMIT_ORDER_limit_short': stats[1].x[3] * 0.997,
'BEST_LEVERAGE_margin_leverage': stats[1].x[4]}
return best_params
Custom Assets list auto saved after running HyperOptimization on machine Learning lead backtesting.
ππ₯ Unlock Your Crypto Trading Potential with Our Revolutionary Bot! π₯π
Calling all crypto enthusiasts, financial wizards, and machine learning mavens! ππ°
Are you ready to elevate your trading game to new heights? Introducing our cutting-edge trading bot, meticulously crafted to cater to the needs of the modern trader. Say goodbye to manual guesswork and hello to the future of automated trading powered by Machine Learning and Neural Networks!
π₯ Latest Updates β March 25th, 2024: π₯
β¨ Custom Asset Integration: Now, tailor your trading experience with custom assets, complete with personalized settings for take profit, stop loss, and leverage. Trade with confidence, your way!
β¨ Hyper-Tuned Asset Selection: Our bot does the heavy lifting for you, automatically identifying the best crypto assets for your ML/DL models through rigorous hyper-tuning and backtesting. Say hello to smarter trading decisions!
β¨ Multi-Asset Trading: Trade multiple assets simultaneously with ease. Take full control of your portfolio and diversify like never before!
β¨ USDT Value Trading: Introducing trading with specified USDT value for added flexibility and precision in your transactions.
β¨ Enhanced Performance: With improved error handling, threading, multiprocessing, GPU activation, and CPU core utilization, experience lightning-fast performance and seamless execution.
β¨ Neural Network Integration: Fine-tune your trading strategies with our Neural Network model, complete with preprocessing steps for optimal results.
β¨ Data Access Simplified: Say goodbye to hassles! Access data from TradingView effortlessly, no login credentials required.
β¨ Easy Setup: Follow our straightforward package installation instructions for a hassle-free setup process. Get started in minutes!
π‘ Description: Our trading bot revolutionizes the way you trade cryptocurrencies. Train, test, backtest, and deploy ML/DL models effortlessly across various assets and time frames. Customize to your heartβs content and unleash its power for live or paper trading. Experience seamless execution, comprehensive functionality, and detailed trading reports across multiple exchanges.
π Take the First Step Towards Trading Success: π
Unlock the full potential of your trading journey with our innovative bot. Hyper-optimize your models, run extensive tests, and dive into the world of automated trading with confidence. Need assistance or have suggestions? Weβre here for you every step of the way.
π Donβt miss out on this opportunity to supercharge your trading strategies and embark on a journey to financial freedom! π
π Click here to seize the opportunity:
Hereβs to your success in the thrilling world of crypto trading!
In conclusion, the emergence of our Python-powered trading bot represents a quantum leap forward in the realm of algorithmic trading. By addressing the limitations inherent in existing bots and leveraging the unparalleled capabilities of machine learning and neural networks, our bot offers a trans-formative solution for traders seeking to navigate the complexities of the cryptocurrency markets with confidence and precision. With its unparalleled flexibility, multi-asset trading capabilities, real-time decision-making prowess, and integration of cutting-edge technologies, our bot stands poised to revolutionize the way traders approach automated trading. Embrace the future of trading with our bot and unlock a world of possibilities in the ever-expanding realm of cryptocurrency trading.
Warm Regards,
Puranam Pradeep Picasso
Linkedin β https://www.linkedin.com/in/puranampradeeppicasso/
Patreon β https://patreon.com/pppicasso
Facebook β https://www.facebook.com/puranam.p.picasso/
Twitter β https://twitter.com/picasso_999