|
import requests |
|
from sklearn.neural_network import MLPClassifier |
|
from sklearn.model_selection import train_test_split |
|
import numpy as np |
|
import pandas as pd |
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup |
|
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler |
|
import logging |
|
from selenium import webdriver |
|
import os |
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
BINANCE_API_KEY = os.getenv('https://api.binance.com/api/v3/ticker/price?symbol={symbol}') |
|
ALPHA_VANTAGE_API_KEY = os.getenv('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo') |
|
IEX_CLOUD_API_KEY = os.getenv('https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_KEY}') |
|
FOREX_COM_API_KEY = os.getenv('https://api.forex.com/v1/quotes/{pair}?api_key={FOREX_COM_API_KEY}') |
|
|
|
|
|
BINOMO_USERNAME = os.getenv('BINOMO_USERNAME') |
|
BINOMO_PASSWORD = os.getenv('BINOMO_PASSWORD') |
|
BINOMO_URL = 'https://binomo.com/' |
|
|
|
|
|
def get_crypto_price_binance(symbol): |
|
url = f'https://api.binance.com/api/v3/ticker/price?symbol={symbol}' |
|
headers = {'X-MBX-APIKEY': BINANCE_API_KEY} |
|
response = requests.get(url, headers=headers) |
|
data = response.json() |
|
return float(data['price']) |
|
|
|
def get_stock_price_yahoo(symbol): |
|
url = f'https://query1.finance.yahoo.com/v7/finance/quote?symbols={symbol}' |
|
response = requests.get(url) |
|
data = response.json() |
|
return float(data['quoteResponse']['result'][0]['regularMarketPrice']) |
|
|
|
def get_crypto_price_alpha_vantage(symbol): |
|
url = f'https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency={symbol[:3]}&to_currency={symbol[3:]}&apikey={ALPHA_VANTAGE_API_KEY}' |
|
response = requests.get(url) |
|
data = response.json() |
|
return float(data['Realtime Currency Exchange Rate']['5. Exchange Rate']) |
|
|
|
def get_stock_price_iex(symbol): |
|
url = f'https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_KEY}' |
|
response = requests.get(url) |
|
data = response.json() |
|
return float(data['latestPrice']) |
|
|
|
def get_forex_price_forex_com(pair): |
|
url = f'https://api.forex.com/v1/quotes/{pair}?api_key={FOREX_COM_API_KEY}' |
|
response = requests.get(url) |
|
data = response.json() |
|
return float(data['price']) |
|
|
|
|
|
def fetch_historical_data_yahoo(symbol, start_date, end_date): |
|
url = f'https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?period1={start_date}&period2={end_date}&interval=1d' |
|
response = requests.get(url) |
|
data = response.json() |
|
timestamps = data['chart']['result'][0]['timestamp'] |
|
close_prices = data['chart']['result'][0]['indicators']['quote'][0]['close'] |
|
return pd.DataFrame({'timestamp': timestamps, 'close': close_prices}) |
|
|
|
|
|
def prepare_data(): |
|
|
|
crypto_symbol = 'BTC-USD' |
|
stock_symbol = 'AAPL' |
|
start_date = '1714521600' |
|
end_date = '1715990400' |
|
|
|
crypto_data = fetch_historical_data_yahoo(crypto_symbol, start_date, end_date) |
|
stock_data = fetch_historical_data_yahoo(stock_symbol, start_date, end_date) |
|
|
|
|
|
merged_data = pd.merge(crypto_data, stock_data, on='timestamp', suffixes=('_crypto', '_stock')) |
|
X = merged_data[['close_crypto', 'close_stock']].values |
|
y = (merged_data['close_crypto'].shift(-1) > merged_data['close_crypto']).astype(int).values[:-1] |
|
|
|
X = X[:-1] |
|
return X, y |
|
|
|
|
|
X, y = prepare_data() |
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
|
|
model = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000) |
|
model.fit(X_train, y_train) |
|
|
|
|
|
def predict_trade_action(data): |
|
return model.predict(data) |
|
|
|
|
|
def start(update, context): |
|
keyboard = [ |
|
[InlineKeyboardButton("Real Account", callback_data='real_account')], |
|
[InlineKeyboardButton("Demo Account", callback_data='demo_account')], |
|
[InlineKeyboardButton("Check Balance", callback_data='check_balance')], |
|
[InlineKeyboardButton("Trade Options", callback_data='trade_options')] |
|
] |
|
reply_markup = InlineKeyboardMarkup(keyboard) |
|
update.message.reply_text('Choose an option:', reply_markup=reply_markup) |
|
|
|
def button(update, context): |
|
query = update.callback_query |
|
query.answer() |
|
|
|
if query.data == 'real_account': |
|
context.user_data['account'] = 'real' |
|
query.edit_message_text(text="Switched to Real Account") |
|
elif query.data == 'demo_account': |
|
context.user_data['account'] = 'demo' |
|
query.edit_message_text(text="Switched to Demo Account") |
|
elif query.data == 'check_balance': |
|
balance = check_binomo_balance(context.user_data.get('account', 'demo')) |
|
query.edit_message_text(text=f"Current Balance: {balance}") |
|
elif query.data == 'trade_options': |
|
keyboard = [ |
|
[InlineKeyboardButton("Buy", callback_data='buy')], |
|
[InlineKeyboardButton("Sell", callback_data='sell')], |
|
[InlineKeyboardButton("Change Currency Pair", callback_data='change_currency_pair')] |
|
] |
|
reply_markup = InlineKeyboardMarkup(keyboard) |
|
query.edit_message_text(text='Choose a trade option:', reply_markup=reply_markup) |
|
elif query.data in ['buy', 'sell']: |
|
place_trade_on_binomo(query.data) |
|
query.edit_message_text(text=f"Placed a {query.data} order.") |
|
elif query.data == 'change_currency_pair': |
|
query.edit_message_text(text='Please enter the currency pair (e.g., BTCUSDT):') |
|
|
|
|
|
def check_binomo_balance(account_type): |
|
|
|
if account_type == 'real': |
|
return 1000.0 |
|
else: |
|
return 50000.0 |
|
|
|
|
|
def place_trade_on_binomo(action): |
|
driver = webdriver.Chrome() |
|
driver.get(BINOMO_URL) |
|
|
|
|
|
driver.find_element_by_id('username').send_keys(BINOMO_USERNAME) |
|
driver.find_element_by_id('password').send_keys(BINOMO_PASSWORD) |
|
driver.find_element_by_id('login-button').click() |
|
|
|
|
|
|
|
if action == 'buy': |
|
|
|
pass |
|
else: |
|
|
|
pass |
|
|
|
driver.quit() |
|
|
|
|
|
def main(): |
|
|
|
updater = Updater("YOUR_TELEGRAM_BOT_TOKEN", use_context=True) |
|
dp = updater.dispatcher |
|
|
|
|
|
dp.add_handler(CommandHandler("start", start)) |
|
dp.add_handler(CallbackQueryHandler(button)) |
|
|
|
|
|
updater.start_polling() |
|
updater.idle() |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|