// Function to fetch crypto data from Binance
async function downloadCryptoData(symbol, timeframe, limit) {
const binance = new ccxt.binance();
const ohlcv = await binance.fetchOHLCV(symbol, timeframe, undefined, limit);
const data = ohlcv.map(item => ({
timestamp: new Date(item[0]),
open: item[1],
high: item[2],
low: item[3],
close: item[4],
volume: item[5]
}));
return data;
}
// Function to check signal
function checkSignal(data) {
let signal = null;
if (data.sma8 > data.sma13 && data.sma5 > data.sma8) {
signal = 'BUY';
} else if (data.sma8 < data.sma13 && data.sma5 < data.sma8) {
signal = 'SELL';
}
return signal;
}
// Function to calculate stop-loss and take-profit prices
function calculateStopLossTakeProfit(entryPrice, stopLossPercent,
takeProfitPercent, isBuy) {
let stopLossPrice, takeProfitPrice;
if (isBuy) {
stopLossPrice = entryPrice * (1 - stopLossPercent);
takeProfitPrice = entryPrice * (1 + takeProfitPercent);
} else {
stopLossPrice = entryPrice * (1 + stopLossPercent);
takeProfitPrice = entryPrice * (1 - takeProfitPercent);
}
return { stopLossPrice, takeProfitPrice };
}
// Function to process signals
async function generateSignals() {
const symbols = ['BTC/USDT:USDT', 'ETH/USDT:USDT','BNB/USDT:USDT',
'JASMY/USDT:USDT','ONDO/USDT', 'BOME/USDT:USDT','SUPER/USDT:USDT','XAI/USDT:USDT',
'AI/USDT:USDT', 'PYTH/USDT:USDT','ARB/USDT:USDT',
'MEME/USDT:USDT','1000BONK/USDT:USDT','WLD/USDT:USDT', 'OGN/USDT:USDT',
'STRK/USDT:USDT','RONIN/USDT:USDT','SUPER/USDT:USDT',
'PIXEL/USDT:USDT','SOL/USDT:USDT','ALT/USDT:USDT','1000FLOKI/USDT:USDT','FLM/
USDT:USDT','SLP/USDT:USDT',
'1000XEC/USDT:USDT', '1000SHIB/USDT:USDT'];
const timeframe = '15m'; // Change this to your desired timeframe
const limit = 200; // Number of data points to retrieve (adjust as needed)
const stopLossPercentage = 0.02; // 2% stop-loss
const takeProfitPercentage = 0.05; // 5% take-profit
const signalsDiv = document.getElementById('signals');
signalsDiv.innerHTML = '';
for (const symbol of symbols) {
try {
// Download data for the current symbol and timeframe
const cryptoData = await downloadCryptoData(symbol, timeframe, limit);
// Calculate moving averages
const sma5 = calculateSMA(cryptoData.map(item => item.close), 5);
const sma8 = calculateSMA(cryptoData.map(item => item.close), 8);
const sma13 = calculateSMA(cryptoData.map(item => item.close), 13);
// Get the most recent data point
const latestData = cryptoData[cryptoData.length - 1];
// Check for buy and sell signals based on the most recent data point
const currentSignal = checkSignal({ sma5, sma8, sma13 });
// Print the signal with stop-loss and take-profit prices if it exists
if (currentSignal) {
const entryPrice = latestData.close;
const { stopLossPrice, takeProfitPrice } =
calculateStopLossTakeProfit(entryPrice, stopLossPercentage, takeProfitPercentage,
currentSignal === 'BUY');
const signalDiv = document.createElement('div');
signalDiv.classList.add('signal', currentSignal.toLowerCase());
signalDiv.innerHTML = `
<p>Symbol: ${symbol}</p>
<p>Signal: ${currentSignal}</p>
<p>Entry Price: $${entryPrice}</p>
<p>Stop Loss: $${stopLossPrice.toFixed(5)}</p>
<p>Take Profit: $${takeProfitPrice.toFixed(5)}</p>
`;
signalsDiv.appendChild(signalDiv);
} else {
const signalDiv = document.createElement('div');
signalDiv.classList.add('signal', 'no-signal');
signalDiv.textContent = `No signal for ${symbol} at the moment.`;
signalsDiv.appendChild(signalDiv);
}
} catch (error) {
console.error(`Error processing ${symbol}:`, error.message);
}
}
}
// Function to clear signals and VWAP results
function clearSignals() {
const signalsDiv = document.getElementById('signals');
signalsDiv.innerHTML = '';
const vwapResults = document.getElementById('vwapResults');
vwapResults.innerHTML = '';
}
// Function to calculate Simple Moving Average (SMA)
function calculateSMA(data, period) {
if (data.length < period) return null;
const sum = data.slice(-period).reduce((acc, val) => acc + val, 0);
return sum / period;
}
// Function to fetch crypto data from Binance and calculate VWAP
async function calculateVWAP() {
const symbols = ['BTC/USDT:USDT', 'ETH/USDT:USDT',
'SUPER/USDT:USDT','XAI/USDT:USDT', 'AI/USDT:USDT',
'PYTH/USDT:USDT','ARB/USDT:USDT',
'MEME/USDT:USDT','1000BONK/USDT:USDT','WLD/USDT:USDT', 'OGN/USDT:USDT',
'STRK/USDT:USDT','RONIN/USDT:USDT',
'PIXEL/USDT:USDT','SOL/USDT:USDT','ALT/USDT:USDT','1000FLOKI/USDT:USDT','FLM/
USDT:USDT','SLP/USDT:USDT'];
const timeframe = '15m'; // Adjust as needed
const limit = 60; // Fetch last 30 candlesticks
const vwapResults = document.getElementById('vwapResults');
vwapResults.innerHTML = '';
for (const symbol of symbols) {
try {
const binance = new ccxt.binance();
const ohlcv = await binance.fetchOHLCV(symbol, timeframe, undefined,
limit);
const df = ohlcv.map(item => ({
timestamp: new Date(item[0]),
open: item[1],
high: item[2],
low: item[3],
close: item[4],
volume: item[5]
}));
const short_ema_period = 20;
const long_ema_period = 50;
const vwap_period = 20;
const stop_loss_percent = 2;
const take_profit_percent = 3;
const calculateEMA = (data, period) => {
return data.reduce((acc, val) => acc + val.close, 0) / period;
};
const short_ema = calculateEMA(df, short_ema_period);
const long_ema = calculateEMA(df, long_ema_period);
const vwap = df.reduce((acc, val) => acc + (val.volume * val.close), 0)
/
df.reduce((acc, val) => acc + val.volume, 0);
// Calculate stop loss and take profit levels
const current_price = df[df.length - 1].close;
let signal;
let take_profit_price;
let stop_loss_price;
if (short_ema < long_ema && current_price > vwap) {
take_profit_price = current_price * (1 - take_profit_percent /
100);
stop_loss_price = current_price * (1 + stop_loss_percent / 100);
signal = `Signal for ${symbol}: Sell at price ${current_price}`;
} else if (short_ema > long_ema && current_price < vwap) {
take_profit_price = current_price * (1 + take_profit_percent /
100);
stop_loss_price = current_price * (1 - stop_loss_percent / 100);
signal = `Signal for ${symbol}: Buy at price ${current_price}`;
} else {
signal = `No signal for buy or sell for ${symbol}`;
}
const resultDiv = document.createElement('div');
resultDiv.classList.add('vwap-result');
resultDiv.innerHTML = `
<p>${signal}</p>
<p>Take Profit Signal: ${take_profit_price.toFixed(5)}</p>
<p>Stop Loss Signal: ${stop_loss_price.toFixed(5)}</p>
`;
vwapResults.appendChild(resultDiv);
} catch (error) {
console.error(`Error processing ${symbol}:`, error.message);
}
}
}