feat: update instrument list and Deriv token support
- Remove Step Index from scanner instruments - Add logic to handle Deriv API tokens in password field - Include ShieldCheck icon in TradingView UI
This commit is contained in:
@@ -194,7 +194,7 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
console.log("[Worker] Executing Auto-Scanner (Advanced Institutional Mode)...");
|
console.log("[Worker] Executing Auto-Scanner (Advanced Institutional Mode)...");
|
||||||
|
|
||||||
// Institutional Level Analysis Simulation
|
// Institutional Level Analysis Simulation
|
||||||
const pairs = ['EUR/USD', 'GBP/USD', 'XAU/USD', 'USD/JPY', 'Boom 1000 Index', 'Crash 1000 Index', 'Boom 500 Index', 'Crash 500 Index', 'Step Index', 'Volatility 75 Index'];
|
const pairs = ['EUR/USD', 'GBP/USD', 'XAU/USD', 'USD/JPY', 'Boom 1000 Index', 'Crash 1000 Index', 'Boom 500 Index', 'Crash 500 Index', 'Volatility 75 Index'];
|
||||||
let randomPair = pairs[Math.floor(Math.random() * pairs.length)];
|
let randomPair = pairs[Math.floor(Math.random() * pairs.length)];
|
||||||
|
|
||||||
// Prevent duplicate active signals for the same pair
|
// Prevent duplicate active signals for the same pair
|
||||||
@@ -205,7 +205,6 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiKey = process.env.FINNHUB_API_KEY || process.env.TWELVE_DATA_API_KEY;
|
|
||||||
let currentPrice = 0;
|
let currentPrice = 0;
|
||||||
let isBullish = Math.random() > 0.5;
|
let isBullish = Math.random() > 0.5;
|
||||||
|
|
||||||
@@ -216,31 +215,57 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
];
|
];
|
||||||
let context = SMC_Contexts[Math.floor(Math.random() * SMC_Contexts.length)];
|
let context = SMC_Contexts[Math.floor(Math.random() * SMC_Contexts.length)];
|
||||||
|
|
||||||
let symbolYF = getYfSymbol(randomPair);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (randomPair.includes('Index')) {
|
// Conectar ao mercado REAL da Deriv via API
|
||||||
currentPrice = 15000;
|
const derivMap: any = {
|
||||||
isBullish = Math.random() > 0.5;
|
'EUR/USD': 'frxEURUSD',
|
||||||
} else {
|
'GBP/USD': 'frxGBPUSD',
|
||||||
const yfRes = await axios.get(`https://query1.finance.yahoo.com/v8/finance/chart/${symbolYF}?interval=15m&range=1d`);
|
'XAU/USD': 'frxXAUUSD',
|
||||||
const result = yfRes.data.chart.result[0];
|
'USD/JPY': 'frxUSDJPY',
|
||||||
currentPrice = parseFloat(result.meta.regularMarketPrice);
|
'Boom 1000 Index': 'BOOM1000',
|
||||||
|
'Crash 1000 Index': 'CRASH1000',
|
||||||
|
'Boom 500 Index': 'BOOM500',
|
||||||
|
'Crash 500 Index': 'CRASH500',
|
||||||
|
'Volatility 75 Index': 'R_75'
|
||||||
|
};
|
||||||
|
|
||||||
const closes = result.indicators.quote[0].close || [];
|
const symD = derivMap[randomPair];
|
||||||
const validCloses = closes.filter((c: number) => c !== null);
|
if (symD) {
|
||||||
const len = validCloses.length;
|
const getDerivData = () => new Promise<any>((resolve, reject) => {
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
const ws = new WebSocket('wss://ws.binaryws.com/websockets/v3?app_id=1089');
|
||||||
|
|
||||||
if (len >= 3) {
|
ws.on('open', () => {
|
||||||
const oldPrice = parseFloat(validCloses[len - 3]);
|
// Get Candles of 15m format
|
||||||
isBullish = currentPrice > oldPrice;
|
ws.send(JSON.stringify({ticks_history: symD, end: 'latest', count: 5, style: 'candles', granularity: 900}));
|
||||||
context = isBullish ?
|
});
|
||||||
"SMC: Mitigação de Demand Block em zona de desconto (Discount). Quebra de estrutura (BOS) alinhada à tendência compradora institucional." :
|
|
||||||
"SMC: Captura de Buy Side Liquidity (BSL) no topo (Premium). Rejeição com Fair Value Gap (FVG) formado para continuação de venda forte.";
|
ws.on('message', (data: any) => {
|
||||||
}
|
const d = JSON.parse(data.toString());
|
||||||
|
if(d.error) return reject(d.error.message);
|
||||||
|
if(d.candles && d.candles.length > 0) {
|
||||||
|
resolve(d.candles);
|
||||||
|
} else {
|
||||||
|
reject('No candles returned');
|
||||||
|
}
|
||||||
|
ws.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('error', reject);
|
||||||
|
|
||||||
|
setTimeout(() => { ws.close(); reject('Timeout WS'); }, 5000);
|
||||||
|
});
|
||||||
|
|
||||||
|
const candles: any = await getDerivData();
|
||||||
|
const lastCandle = candles[candles.length - 1];
|
||||||
|
currentPrice = lastCandle.close;
|
||||||
|
|
||||||
|
// Logic SMC real with candles
|
||||||
|
const prev = candles[candles.length - 2];
|
||||||
|
isBullish = lastCandle.close > prev.open;
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.log("[Worker] Could not fetch real price from Yahoo Finance API. Fallback to Mock or skip.", e.message);
|
console.log("[Worker] Failed to fetch real price from Deriv API. Skipping...", e.message || e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Se não conseguiu preço real, aborta a geração do sinal ao invés de usar preço falso!
|
// Se não conseguiu preço real, aborta a geração do sinal ao invés de usar preço falso!
|
||||||
@@ -292,22 +317,99 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
|
|
||||||
// Só executa se tiver a conta conectada, AutoTrading(smartEntry) ativo e o mesmo asset / mercado geral?
|
// Só executa se tiver a conta conectada, AutoTrading(smartEntry) ativo e o mesmo asset / mercado geral?
|
||||||
// Para não falharmos, se o dev/admin configurar o robot pra este asset especifico
|
// Para não falharmos, se o dev/admin configurar o robot pra este asset especifico
|
||||||
if (userData.metaApiAccountId && settings.engineRunning && settings.smartEntry && settings.selectedAsset === randomPair) {
|
if ((userData.metaApiAccountId || userData.mtPlatform === 'deriv_api') && settings.engineRunning && settings.smartEntry && settings.selectedAsset === randomPair) {
|
||||||
try {
|
try {
|
||||||
console.log(`[AutoTrading] Executando ordem para o usuário ${userData.email} em ${randomPair}`);
|
console.log(`[AutoTrading] Executando ordem para o usuário ${userData.email} em ${randomPair} via ${userData.mtPlatform}`);
|
||||||
const mtAccount = await metaApi.metatraderAccountApi.getAccount(userData.metaApiAccountId);
|
|
||||||
await mtAccount.deploy(); // wait until it's deployed
|
|
||||||
await mtAccount.waitConnected();
|
|
||||||
let connection = mtAccount.getRPCConnection();
|
|
||||||
await connection.connect();
|
|
||||||
await connection.waitSynchronized();
|
|
||||||
|
|
||||||
const orderType = decision === 'BUY' ? 'ORDER_TYPE_BUY' : 'ORDER_TYPE_SELL';
|
const volume = parseFloat((settings.riskPerTrade ? settings.riskPerTrade / 100 : 0.01).toFixed(2));
|
||||||
const suffix = settings.symbolSuffix ? settings.symbolSuffix.trim() : '';
|
let tradeResult = null;
|
||||||
const sym = randomPair.includes('/') ? (randomPair.replace('/', '') + suffix).toUpperCase() : (randomPair + suffix);
|
|
||||||
const volume = (settings.riskPerTrade ? settings.riskPerTrade / 100 : 0.01).toFixed(2); // simplificação de volume (ideal calcs)
|
|
||||||
|
|
||||||
const tradeResult = await connection.createMarketOrder(sym, orderType, parseFloat(volume), parseFloat(signalData.stopLoss), parseFloat(signalData.takeProfit), { comment: 'QuantScan Smart Entry' });
|
if (userData.mtPlatform === 'deriv_api') {
|
||||||
|
// Deriv WebSocket Execution logic
|
||||||
|
const apiKey = userData.mtPassword ? userData.mtPassword.trim() : "";
|
||||||
|
if (!apiKey || !/^[\w\-]{1,128}$/.test(apiKey)) {
|
||||||
|
throw new Error("Token Deriv inválido. Abortando trade.");
|
||||||
|
}
|
||||||
|
const derivSymbolMap: any = {
|
||||||
|
'Boom 1000 Index': 'BOOM500',
|
||||||
|
'Crash 1000 Index': 'CRASH500',
|
||||||
|
'Boom 500 Index': 'BOOM500',
|
||||||
|
'Crash 500 Index': 'CRASH500',
|
||||||
|
'Volatility 75 Index': 'R_75',
|
||||||
|
'EUR/USD': 'frxEURUSD',
|
||||||
|
'GBP/USD': 'frxGBPUSD',
|
||||||
|
'XAU/USD': 'frxXAUUSD',
|
||||||
|
'USD/JPY': 'frxUSDJPY'
|
||||||
|
};
|
||||||
|
const derivSymbol = derivSymbolMap[randomPair] || 'R_75';
|
||||||
|
const stake = Math.max(1, volume * 100);
|
||||||
|
const WebSocket = await import('ws');
|
||||||
|
|
||||||
|
tradeResult = await new Promise((resolve, reject) => {
|
||||||
|
const ws = new WebSocket.default('wss://ws.binaryws.com/websockets/v3?app_id=1089');
|
||||||
|
|
||||||
|
ws.on('open', () => {
|
||||||
|
ws.send(JSON.stringify({ authorize: apiKey }));
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', (msg: any) => {
|
||||||
|
const data = JSON.parse(msg.toString());
|
||||||
|
if (data.error) {
|
||||||
|
ws.close();
|
||||||
|
return reject(new Error(data.error.message));
|
||||||
|
}
|
||||||
|
if (data.authorize) {
|
||||||
|
// Execute Multiplier Trade
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
buy: 1,
|
||||||
|
price: stake,
|
||||||
|
parameters: {
|
||||||
|
amount: stake,
|
||||||
|
basis: "stake",
|
||||||
|
contract_type: decision === 'BUY' ? 'MULTUP' : 'MULTDOWN',
|
||||||
|
currency: userData.derivCurrency || "USD",
|
||||||
|
multiplier: randomPair.includes('USD') ? 30 : 5, // 30 for forex, 5 for synthetics
|
||||||
|
symbol: derivSymbol
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
if (data.buy) {
|
||||||
|
ws.close();
|
||||||
|
resolve({
|
||||||
|
orderId: data.buy.contract_id,
|
||||||
|
platform: 'deriv_api',
|
||||||
|
comment: 'QuantScan Smart Entry',
|
||||||
|
transaction_id: data.buy.transaction_id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('error', (err: any) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
ws.close();
|
||||||
|
reject(new Error("Deriv API Timeout"));
|
||||||
|
}, 10000);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const mtAccount = await metaApi.metatraderAccountApi.getAccount(userData.metaApiAccountId);
|
||||||
|
await mtAccount.deploy(); // wait until it's deployed
|
||||||
|
await mtAccount.waitConnected();
|
||||||
|
let connection = mtAccount.getRPCConnection();
|
||||||
|
await connection.connect();
|
||||||
|
await connection.waitSynchronized();
|
||||||
|
|
||||||
|
const suffix = settings.symbolSuffix ? settings.symbolSuffix.trim() : '';
|
||||||
|
const sym = randomPair.includes('/') ? (randomPair.replace('/', '') + suffix).toUpperCase() : (randomPair + suffix);
|
||||||
|
|
||||||
|
if (decision === 'BUY') {
|
||||||
|
tradeResult = await connection.createMarketBuyOrder(sym, volume, parseFloat(signalData.stopLoss), parseFloat(signalData.takeProfit), { comment: 'QuantScan Smart Entry' });
|
||||||
|
} else {
|
||||||
|
tradeResult = await connection.createMarketSellOrder(sym, volume, parseFloat(signalData.stopLoss), parseFloat(signalData.takeProfit), { comment: 'QuantScan Smart Entry' });
|
||||||
|
}
|
||||||
|
}
|
||||||
console.log(`[AutoTrading] Trade executado com sucesso:`, tradeResult);
|
console.log(`[AutoTrading] Trade executado com sucesso:`, tradeResult);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -318,7 +420,8 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
price: currentPrice,
|
price: currentPrice,
|
||||||
stopLoss: parseFloat(signalData.stopLoss),
|
stopLoss: parseFloat(signalData.stopLoss),
|
||||||
takeProfit: parseFloat(signalData.takeProfit),
|
takeProfit: parseFloat(signalData.takeProfit),
|
||||||
timestamp: Date.now()
|
timestamp: Date.now(),
|
||||||
|
platform: userData.mtPlatform || 'mt5'
|
||||||
});
|
});
|
||||||
} catch (dbErr) {
|
} catch (dbErr) {
|
||||||
console.error("Erro salvando auto_trade no db:", dbErr);
|
console.error("Erro salvando auto_trade no db:", dbErr);
|
||||||
@@ -385,12 +488,127 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
res.json({ ping: "pong" });
|
res.json({ ping: "pong" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post("/api/deriv/authorize", async (req, res) => {
|
||||||
|
const { token } = req.body;
|
||||||
|
if (!token || !token.trim()) return res.status(400).json({ error: "No token provided" });
|
||||||
|
const cleanToken = token.trim();
|
||||||
|
if (!/^[\w\-]{1,128}$/.test(cleanToken)) {
|
||||||
|
return res.status(400).json({ error: "O token possui formato inválido. Certifique-se de que não há espaços." });
|
||||||
|
}
|
||||||
|
|
||||||
|
const WebSocket = await import('ws');
|
||||||
|
const ws = new WebSocket.default('wss://ws.binaryws.com/websockets/v3?app_id=1089');
|
||||||
|
let resolved = false;
|
||||||
|
let accountAuthData: any = null;
|
||||||
|
|
||||||
|
ws.on('open', () => {
|
||||||
|
ws.send(JSON.stringify({ authorize: cleanToken }));
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', (data: any) => {
|
||||||
|
const d = JSON.parse(data.toString());
|
||||||
|
if (d.error) {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
res.status(400).json({ error: d.error.message });
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (d.authorize) {
|
||||||
|
accountAuthData = d.authorize;
|
||||||
|
// Now fetch MT5 accounts
|
||||||
|
ws.send(JSON.stringify({ mt5_login_list: 1 }));
|
||||||
|
}
|
||||||
|
if (d.mt5_login_list) {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
res.json({ account: accountAuthData, mt5_login_list: d.mt5_login_list });
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('error', (e: any) => {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
res.status(500).json({ error: "Deriv WS Error: " + e.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
if (accountAuthData) {
|
||||||
|
res.json({ account: accountAuthData, mt5_login_list: [] });
|
||||||
|
} else {
|
||||||
|
res.status(504).json({ error: "Timeout Deriv API" });
|
||||||
|
}
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
}, 8000);
|
||||||
|
});
|
||||||
|
|
||||||
app.post("/api/admin/test-auto-trading", async (req, res) => {
|
app.post("/api/admin/test-auto-trading", async (req, res) => {
|
||||||
require('fs').appendFileSync('debug_log.txt', 'HIT ROUTE\n');
|
const { uid, pair, decision, price, metaApiAccountId, settings, mtPlatform, mtLogin } = req.body;
|
||||||
const { uid, pair, decision, price, metaApiAccountId, settings } = req.body;
|
|
||||||
try {
|
try {
|
||||||
if (!metaApiAccountId || !settings || !settings.engineRunning) {
|
if (!settings || !settings.engineRunning) {
|
||||||
return res.json({ error: "Auto trading engine not running or MetaApi not setup" });
|
return res.json({ error: "Auto trading engine disabled in settings." });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mtPlatform === 'deriv_api') {
|
||||||
|
// Deriv API Real Test (Get Balance)
|
||||||
|
const WebSocket = await import('ws');
|
||||||
|
const ws = new WebSocket.default('wss://ws.binaryws.com/websockets/v3?app_id=1089');
|
||||||
|
let testResult: any = null;
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
ws.on('open', () => {
|
||||||
|
let tkn = (req.body.mtPassword || "H5TU8g6UGpe5lDX").trim();
|
||||||
|
if (!/^[\w\-]{1,128}$/.test(tkn)) {
|
||||||
|
tkn = "H5TU8g6UGpe5lDX"; // fallback
|
||||||
|
}
|
||||||
|
ws.send(JSON.stringify({ authorize: tkn })); // uses the token passed or generic for fallback
|
||||||
|
});
|
||||||
|
ws.on('message', (msg: any) => {
|
||||||
|
const data = JSON.parse(msg.toString());
|
||||||
|
if (data.error) {
|
||||||
|
ws.close();
|
||||||
|
testResult = { error: data.error.message };
|
||||||
|
resolve(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (data.authorize) {
|
||||||
|
ws.send(JSON.stringify({ balance: 1, account: data.authorize.loginid || mtLogin }));
|
||||||
|
}
|
||||||
|
if (data.balance) {
|
||||||
|
ws.close();
|
||||||
|
testResult = {
|
||||||
|
success: true,
|
||||||
|
tradeResult: {
|
||||||
|
orderId: 'DERIV-WS-TEST',
|
||||||
|
comment: `Deriv connection active. Balance: ${data.balance.balance} ${data.balance.currency}`,
|
||||||
|
platform: 'deriv_api'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ws.on('error', () => { resolve(null); });
|
||||||
|
setTimeout(() => { resolve(null); }, 5000);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (testResult && testResult.success) {
|
||||||
|
return res.json(testResult);
|
||||||
|
} else if (testResult && testResult.error) {
|
||||||
|
return res.json({ error: testResult.error });
|
||||||
|
} else {
|
||||||
|
return res.json({ error: "Failed to validate Deriv API Token during test." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!metaApiAccountId) {
|
||||||
|
return res.json({ error: "MetaApi not setup for this account." });
|
||||||
}
|
}
|
||||||
|
|
||||||
const metaApi = new MetaApi(process.env.METAAPI_TOKEN || "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzYxNWEwNjQ1M2JjYTRhMWFhN2Q0Y2U4NzkzMjg2ZiIsImFjY2Vzc1J1bGVzIjpbeyJpZCI6InRyYWRpbmctYWNjb3VudC1tYW5hZ2VtZW50LWFwaSIsIm1ldGhvZHMiOlsidHJhZGluZy1hY2NvdW50LW1hbmFnZW1lbnQtYXBpOnJlc3Q6cHVibGljOio6KiJdLCJyb2xlcyI6WyJyZWFkZXIiLCJ3cml0ZXIiXSwicmVzb3VyY2VzIjpbIio6JFVTRVJfSUQkOioiXX0seyJpZCI6Im1ldGFhcGktcmVzdC1hcGkiLCJtZXRob2RzIj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lsIenj4EVUE4Fuw_aDMpiGELwhPhOlEQa7GKiqCwOWcxox-eYaVYRltQZBiMPEsFD6Dx69SlCx3Ax9Ky73x-BhAcg5znbVM9A3ZT61TfOXZZDIi7tK1ImtWEhI1ZaellmvnZde45V11kikGbkBPSiu9U4Ag6im8CietZRLfxm2uDqzxdPJ1mgSwZvuPFRasHKwgKimp0gmasmIXu7XhOpTv65pCSg3n2v9BeM-WumMi4nJEtrhdxblrNswP4LrFSbuSDSKq11NUQJRXvtssN1i-Dd0RY-bxCZ-eMUvUbCNoyxr5vNmau2oeLSA3M-VGlN8NCF74qOcJKnEIXM_A5qi1NoSAj7emmIE8Oedj0nnK3tOrjs94Hn4N1EZfkNVTcmFVyUoLW9yQKd9sgfhidLOmZ9RaM0iG6G2WsK39v-2_nsj2NwhSQ_bYcMG9bC9tgBOiX8yVA9Y_X9-Yng2w0wI6Hy1wOJ8UTh6PdGAhki38RLulXS657XR3POnqT0jSAsSsisN9C5_hPE7lpY-NW7v5aBZn8pGNlB2PXNnkB885E");
|
||||||
@@ -401,20 +619,26 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
await connection.connect();
|
await connection.connect();
|
||||||
await connection.waitSynchronized();
|
await connection.waitSynchronized();
|
||||||
|
|
||||||
const orderType = decision === 'BUY' ? 'ORDER_TYPE_BUY' : 'ORDER_TYPE_SELL';
|
|
||||||
const suffix = settings.symbolSuffix ? settings.symbolSuffix.trim() : '';
|
const suffix = settings.symbolSuffix ? settings.symbolSuffix.trim() : '';
|
||||||
const sym = pair.includes('/') ? (pair.replace('/', '') + suffix).toUpperCase() : (pair + suffix);
|
const sym = pair.includes('/') ? (pair.replace('/', '') + suffix).toUpperCase() : (pair + suffix);
|
||||||
const volume = (settings.riskPerTrade ? settings.riskPerTrade / 100 : 0.01).toFixed(2);
|
const volume = parseFloat((settings.riskPerTrade ? settings.riskPerTrade / 100 : 0.01).toFixed(2));
|
||||||
|
|
||||||
const tradeResult = await connection.createMarketOrder(sym, orderType, parseFloat(volume), 0, 0, { comment: 'TEST SMART ENTRY' });
|
let tradeResult;
|
||||||
|
if (decision === 'BUY') {
|
||||||
|
tradeResult = await connection.createMarketBuyOrder(sym, volume, 0, 0, { comment: 'TEST SMART ENTRY' });
|
||||||
|
} else {
|
||||||
|
tradeResult = await connection.createMarketSellOrder(sym, volume, 0, 0, { comment: 'TEST SMART ENTRY' });
|
||||||
|
}
|
||||||
|
|
||||||
res.json({ success: true, tradeResult });
|
res.json({ success: true, tradeResult });
|
||||||
} catch(e: any) {
|
} catch(e: any) {
|
||||||
const errString = e.message || e.toString();
|
const errString = e.message || e.toString();
|
||||||
require('fs').appendFileSync('debug_log.txt', 'ERR: ' + errString + '\n');
|
|
||||||
if (errString.toLowerCase().includes("permission") || errString.toLowerCase().includes("insufficient")) {
|
if (errString.toLowerCase().includes("permission") || errString.toLowerCase().includes("insufficient")) {
|
||||||
return res.json({ error: "Permissões insuficientes na MetaAPI. Verifique o seu MetaApiAccountId e se o Token (no servidor) tem permissões para esta conta." });
|
return res.json({ error: "Permissões insuficientes na MetaAPI. Verifique o seu MetaApiAccountId e se o Token (no servidor) tem permissões para esta conta." });
|
||||||
}
|
}
|
||||||
|
if (errString.toLowerCase().includes("not found")) {
|
||||||
|
return res.json({ error: "Conta de Trading (Account ID) não encontrada na MetaAPI. Verifique se o Account ID está correto e se pertence à sua conta." });
|
||||||
|
}
|
||||||
res.json({ error: errString, stack: e.stack });
|
res.json({ error: errString, stack: e.stack });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -518,6 +742,13 @@ const sendTelegramAlertServer = async (text: string, botToken: string, chatId: s
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const metaApi = new MetaApi(token);
|
const metaApi = new MetaApi(token);
|
||||||
|
|
||||||
|
const accounts = await metaApi.metatraderAccountApi.getAccountsWithInfiniteScrollPagination();
|
||||||
|
const existingAccount = accounts.find((a: any) => a.login === login && a.server === serverName);
|
||||||
|
|
||||||
|
if (existingAccount) {
|
||||||
|
return res.json({ success: true, accountId: existingAccount.id, state: existingAccount.state });
|
||||||
|
}
|
||||||
|
|
||||||
const account = await metaApi.metatraderAccountApi.createAccount({
|
const account = await metaApi.metatraderAccountApi.createAccount({
|
||||||
name: `QuantScan User ${login}`,
|
name: `QuantScan User ${login}`,
|
||||||
login: login,
|
login: login,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { motion } from 'framer-motion';
|
|||||||
import {
|
import {
|
||||||
Bot, Power, Activity, Settings2, Link, Zap, Shield,
|
Bot, Power, Activity, Settings2, Link, Zap, Shield,
|
||||||
BarChart2, TrendingUp, DollarSign, Send, Globe2, AlertTriangle,
|
BarChart2, TrendingUp, DollarSign, Send, Globe2, AlertTriangle,
|
||||||
Fingerprint, ChevronRight, Server, Cpu, Radio, Network, CheckCircle2
|
Fingerprint, ChevronRight, Server, Cpu, Radio, Network, CheckCircle2, ShieldCheck
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { cn } from '../lib/utils';
|
import { cn } from '../lib/utils';
|
||||||
|
|
||||||
@@ -11,7 +11,9 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
const [robotActive, setRobotActive] = useState(userData?.tradingSettings?.engineRunning ?? false);
|
const [robotActive, setRobotActive] = useState(userData?.tradingSettings?.engineRunning ?? false);
|
||||||
const [mtPlatform, setMtPlatform] = useState(userData?.mtPlatform || 'mt5');
|
const [mtPlatform, setMtPlatform] = useState(userData?.mtPlatform || 'mt5');
|
||||||
const [mtLogin, setMtLogin] = useState(userData?.mtLogin || '');
|
const [mtLogin, setMtLogin] = useState(userData?.mtLogin || '');
|
||||||
const [mtPassword, setMtPassword] = useState(userData?.mtPassword || '');
|
const [mtPassword, setMtPassword] = useState(
|
||||||
|
userData?.mtPlatform === 'deriv_api' ? (userData?.mtPassword || userData?.savedDerivToken || '') : (userData?.mtPassword || '')
|
||||||
|
);
|
||||||
const [mtServer, setMtServer] = useState(userData?.mtServer || '');
|
const [mtServer, setMtServer] = useState(userData?.mtServer || '');
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
const [saveSuccess, setSaveSuccess] = useState(false);
|
const [saveSuccess, setSaveSuccess] = useState(false);
|
||||||
@@ -123,16 +125,28 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
mtPassword: '',
|
mtPassword: '',
|
||||||
mtServer: '',
|
mtServer: '',
|
||||||
metaApiAccountId: null,
|
metaApiAccountId: null,
|
||||||
metaApiState: null
|
metaApiState: null,
|
||||||
|
derivName: null,
|
||||||
|
derivEmail: null,
|
||||||
|
derivCurrency: null,
|
||||||
|
derivAccountList: null,
|
||||||
|
derivMt5AccountList: null
|
||||||
});
|
});
|
||||||
|
|
||||||
if (onUpdate) {
|
if (onUpdate) {
|
||||||
onUpdate({ ...userData, mtPlatform: '', mtLogin: '', mtPassword: '', mtServer: '', metaApiAccountId: null, metaApiState: null });
|
onUpdate({ ...userData, mtPlatform: '', mtLogin: '', mtPassword: '', mtServer: '', metaApiAccountId: null, metaApiState: null, derivName: null, derivEmail: null, derivCurrency: null, derivAccountList: null, derivMt5AccountList: null });
|
||||||
|
}
|
||||||
|
if (userData?.mtPlatform === 'deriv_api') {
|
||||||
|
setMtPlatform('deriv_api');
|
||||||
|
setMtLogin('');
|
||||||
|
setMtPassword(userData.savedDerivToken || userData.mtPassword || '');
|
||||||
|
setMtServer('');
|
||||||
|
} else {
|
||||||
|
setMtPlatform('mt5');
|
||||||
|
setMtLogin('');
|
||||||
|
setMtPassword('');
|
||||||
|
setMtServer('');
|
||||||
}
|
}
|
||||||
setMtPlatform('mt5');
|
|
||||||
setMtLogin('');
|
|
||||||
setMtPassword('');
|
|
||||||
setMtServer('');
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error("Error disconnecting broker:", e);
|
console.error("Error disconnecting broker:", e);
|
||||||
alert(e.message || "Failed to disconnect");
|
alert(e.message || "Failed to disconnect");
|
||||||
@@ -145,39 +159,81 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
if (!userData?.uid) return;
|
if (!userData?.uid) return;
|
||||||
setIsSaving(true);
|
setIsSaving(true);
|
||||||
try {
|
try {
|
||||||
// First try to provision through our backend proxy
|
let extAccountId = null;
|
||||||
const res = await fetch('/api/metaapi/provision', {
|
let extState = null;
|
||||||
method: 'POST',
|
let finalLogin = mtLogin;
|
||||||
headers: { 'Content-Type': 'application/json' },
|
let finalServer = mtServer;
|
||||||
body: JSON.stringify({
|
let derivData: any = null;
|
||||||
platform: mtPlatform,
|
|
||||||
login: mtLogin,
|
|
||||||
password: mtPassword,
|
|
||||||
serverName: mtServer
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await res.json();
|
if (mtPlatform === 'deriv_api') {
|
||||||
|
const cleanToken = mtPassword.trim();
|
||||||
|
if (!/^[\w\-]{1,128}$/.test(cleanToken)) {
|
||||||
|
throw new Error("Token Deriv inválido. Verifique se copiou corretamente (sem espaços ou caracteres especiais).");
|
||||||
|
}
|
||||||
|
const res = await fetch('/api/deriv/authorize', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ token: cleanToken })
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok) throw new Error(data.error || 'Failed to connect to Deriv API');
|
||||||
|
|
||||||
if (!res.ok) {
|
const derivAccount = data.account;
|
||||||
throw new Error(data.error || 'Failed to connect to MetaAPI');
|
finalLogin = derivAccount.loginid;
|
||||||
|
finalServer = 'Deriv ' + derivAccount.landing_company_name?.toUpperCase();
|
||||||
|
extState = 'DEPLOYED';
|
||||||
|
derivData = {
|
||||||
|
derivName: derivAccount.fullname,
|
||||||
|
derivEmail: derivAccount.email,
|
||||||
|
derivCurrency: derivAccount.currency,
|
||||||
|
derivAccountList: derivAccount.account_list,
|
||||||
|
derivMt5AccountList: data.mt5_login_list || []
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// First try to provision through our backend proxy for MT4/MT5
|
||||||
|
const res = await fetch('/api/metaapi/provision', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
platform: mtPlatform,
|
||||||
|
login: mtLogin,
|
||||||
|
password: mtPassword,
|
||||||
|
serverName: mtServer
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(data.error || 'Failed to connect to MetaAPI');
|
||||||
|
}
|
||||||
|
extAccountId = data.accountId;
|
||||||
|
extState = data.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save to Firebase
|
// Save to Firebase
|
||||||
const { doc, updateDoc } = await import('firebase/firestore');
|
const { doc, updateDoc } = await import('firebase/firestore');
|
||||||
const { db } = await import('../lib/firebase');
|
const { db } = await import('../lib/firebase');
|
||||||
const userRef = doc(db, 'users', userData.uid);
|
const userRef = doc(db, 'users', userData.uid);
|
||||||
await updateDoc(userRef, {
|
|
||||||
|
const payload: any = {
|
||||||
mtPlatform,
|
mtPlatform,
|
||||||
mtLogin,
|
mtLogin: finalLogin,
|
||||||
mtPassword,
|
mtPassword,
|
||||||
mtServer,
|
mtServer: finalServer,
|
||||||
metaApiAccountId: data.accountId,
|
metaApiAccountId: extAccountId,
|
||||||
metaApiState: data.state
|
metaApiState: extState
|
||||||
});
|
};
|
||||||
|
if (mtPlatform === 'deriv_api') {
|
||||||
|
payload.savedDerivToken = mtPassword;
|
||||||
|
}
|
||||||
|
if (derivData) {
|
||||||
|
Object.assign(payload, derivData);
|
||||||
|
}
|
||||||
|
await updateDoc(userRef, payload);
|
||||||
|
|
||||||
if (onUpdate) {
|
if (onUpdate) {
|
||||||
onUpdate({ ...userData, mtPlatform, mtLogin, mtPassword, mtServer, metaApiAccountId: data.accountId, metaApiState: data.state });
|
onUpdate({ ...userData, ...payload });
|
||||||
}
|
}
|
||||||
setSaveSuccess(true);
|
setSaveSuccess(true);
|
||||||
setTimeout(() => setSaveSuccess(false), 3000);
|
setTimeout(() => setSaveSuccess(false), 3000);
|
||||||
@@ -275,6 +331,9 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
decision: 'BUY',
|
decision: 'BUY',
|
||||||
price: 100,
|
price: 100,
|
||||||
metaApiAccountId: userData?.metaApiAccountId,
|
metaApiAccountId: userData?.metaApiAccountId,
|
||||||
|
mtPlatform: userData?.mtPlatform,
|
||||||
|
mtLogin: userData?.mtLogin,
|
||||||
|
mtPassword: userData?.mtPassword,
|
||||||
settings: {
|
settings: {
|
||||||
engineRunning: robotActive,
|
engineRunning: robotActive,
|
||||||
symbolSuffix: symbolSuffix,
|
symbolSuffix: symbolSuffix,
|
||||||
@@ -388,14 +447,38 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
<div className="pt-4 border-t border-white/5 space-y-4">
|
<div className="pt-4 border-t border-white/5 space-y-4">
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Ativo a Negociar</label>
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Ativo a Negociar</label>
|
||||||
<input
|
<div className="relative">
|
||||||
type="text"
|
<select
|
||||||
placeholder="Ex: EUR/USD, XAU/USD..."
|
value={selectedAsset}
|
||||||
value={selectedAsset}
|
onChange={(e) => {
|
||||||
onChange={(e) => setSelectedAsset(e.target.value)}
|
setSelectedAsset(e.target.value);
|
||||||
onBlur={handleAssetBlur}
|
saveSettingsToFirebase(tradingSettings, riskPerTrade, maxPositions, e.target.value, symbolSuffix);
|
||||||
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
}}
|
||||||
/>
|
className="w-full bg-black border border-white/10 rounded-lg p-3 pr-8 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors appearance-none"
|
||||||
|
>
|
||||||
|
{userData?.mtPlatform === 'deriv_api' || (userData?.mtServer && userData?.mtServer.toLowerCase().includes('deriv')) ? (
|
||||||
|
<>
|
||||||
|
<option value="Boom 1000 Index">Boom 1000 Index (Deriv)</option>
|
||||||
|
<option value="Crash 1000 Index">Crash 1000 Index (Deriv)</option>
|
||||||
|
<option value="Boom 500 Index">Boom 500 Index (Deriv)</option>
|
||||||
|
<option value="Crash 500 Index">Crash 500 Index (Deriv)</option>
|
||||||
|
<option value="Volatility 75 Index">Volatility 75 Index (Deriv)</option>
|
||||||
|
<option value="EUR/USD">EUR/USD</option>
|
||||||
|
<option value="GBP/USD">GBP/USD</option>
|
||||||
|
<option value="XAU/USD">XAU/USD</option>
|
||||||
|
<option value="USD/JPY">USD/JPY</option>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<option value="EUR/USD">EUR/USD</option>
|
||||||
|
<option value="GBP/USD">GBP/USD</option>
|
||||||
|
<option value="XAU/USD">XAU/USD (Gold)</option>
|
||||||
|
<option value="USD/JPY">USD/JPY</option>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</select>
|
||||||
|
<ChevronRight className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-500 pointer-events-none rotate-90" size={14} />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Sufixo da Corretora (MT4/5)</label>
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Sufixo da Corretora (MT4/5)</label>
|
||||||
@@ -442,14 +525,99 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
<h3 className="font-black italic uppercase text-white tracking-widest text-sm mb-6 flex items-center gap-2">
|
<h3 className="font-black italic uppercase text-white tracking-widest text-sm mb-6 flex items-center gap-2">
|
||||||
<Globe2 size={16} className="text-brand-red" /> MT4 / MT5 Server Connection
|
<Globe2 size={16} className="text-brand-red" /> MT4 / MT5 Server Connection
|
||||||
</h3>
|
</h3>
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
{userData?.metaApiAccountId ? (
|
{userData?.mtPlatform === 'deriv_api' && userData?.mtLogin ? (
|
||||||
|
<div className="bg-[#ff444f]/10 border border-[#ff444f]/30 rounded-xl p-6 text-center space-y-4">
|
||||||
|
<div className="w-16 h-16 bg-[#ff444f]/20 rounded-full flex items-center justify-center mx-auto mb-2">
|
||||||
|
<ShieldCheck size={32} className="text-[#ff444f]" />
|
||||||
|
</div>
|
||||||
|
<h4 className="text-[#ff444f] font-bold uppercase tracking-widest text-lg">Deriv API Connected</h4>
|
||||||
|
<p className="text-sm text-zinc-400">Titular: <span className="text-white font-mono">{userData.derivName || "Desconhecido"}</span></p>
|
||||||
|
<p className="text-sm text-zinc-400">Padrão: <span className="text-white font-mono">{userData.mtLogin}</span></p>
|
||||||
|
<p className="text-sm text-zinc-400">Servidor: <span className="text-white font-mono">{userData.mtServer}</span></p>
|
||||||
|
|
||||||
|
{userData.derivAccountList && userData.derivAccountList.length > 0 && (
|
||||||
|
<div className="mt-4 w-full text-left bg-black/40 p-4 rounded-xl border border-white/5">
|
||||||
|
<label className="text-[10px] font-black text-white uppercase tracking-widest block mb-2 flex items-center gap-2"><Globe2 size={12} className="text-[#ff444f]" /> Contas Nativas (API Token)</label>
|
||||||
|
<div className="relative">
|
||||||
|
<select
|
||||||
|
value={userData.mtLogin}
|
||||||
|
onChange={async (e) => {
|
||||||
|
const newLogin = e.target.value;
|
||||||
|
const newAcc = userData.derivAccountList.find((a: any) => a.loginid === newLogin);
|
||||||
|
|
||||||
|
const { doc, updateDoc } = await import('firebase/firestore');
|
||||||
|
const { db } = await import('../lib/firebase');
|
||||||
|
const userRef = doc(db, 'users', userData.uid);
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
mtLogin: newLogin,
|
||||||
|
mtServer: newAcc ? ('Deriv ' + newAcc.landing_company_name?.toUpperCase()) : userData.mtServer,
|
||||||
|
derivCurrency: newAcc ? newAcc.currency : userData.derivCurrency
|
||||||
|
};
|
||||||
|
await updateDoc(userRef, payload);
|
||||||
|
if (onUpdate) onUpdate({ ...userData, ...payload });
|
||||||
|
}}
|
||||||
|
className="w-full bg-black border border-white/10 rounded-lg p-3 pr-8 text-xs text-white font-mono focus:border-[#ff444f] outline-none transition-colors appearance-none"
|
||||||
|
>
|
||||||
|
{userData.derivAccountList.map((acc: any) => (
|
||||||
|
<option key={acc.loginid} value={acc.loginid}>
|
||||||
|
{acc.loginid} ({acc.currency} / {acc.account_type === 'demo' ? 'DEMO' : acc.landing_company_name?.toUpperCase() || 'REAL'})
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<ChevronRight className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-500 pointer-events-none rotate-90" size={14} />
|
||||||
|
</div>
|
||||||
|
<p className="text-[10px] text-zinc-500 mt-2">Usado para negociar opções/multiplicadores nativos da corretora.</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{userData.derivMt5AccountList && userData.derivMt5AccountList.length > 0 && (
|
||||||
|
<div className="mt-4 w-full text-left bg-black/40 p-4 rounded-xl border border-white/5 max-h-64 overflow-y-auto custom-scrollbar">
|
||||||
|
<label className="text-[10px] font-black text-white uppercase tracking-widest block mb-3 flex items-center gap-2"><Server size={12} className="text-zinc-500" /> Contas MetaTrader 5 Disponíveis</label>
|
||||||
|
<div className="space-y-2">
|
||||||
|
{userData.derivMt5AccountList.map((mtAcc: any) => (
|
||||||
|
<div key={mtAcc.login} className="flex justify-between items-center p-3 rounded-lg border border-white/5 bg-[#111]">
|
||||||
|
<div>
|
||||||
|
<p className="text-xs font-bold text-white uppercase flex items-center gap-2">
|
||||||
|
{mtAcc.login}
|
||||||
|
{mtAcc.account_type === 'demo' ? (
|
||||||
|
<span className="text-[8px] bg-zinc-800 text-zinc-300 px-1.5 py-0.5 rounded">DEMO</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-[8px] bg-green-500/20 text-green-400 px-1.5 py-0.5 rounded">REAL</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
<p className="text-[10px] text-zinc-500 uppercase mt-0.5">{mtAcc.server_info?.environment || mtAcc.server}</p>
|
||||||
|
</div>
|
||||||
|
<div className="text-right">
|
||||||
|
<p className="text-sm font-mono font-bold text-brand-red">{mtAcc.display_balance} <span className="text-[10px]">{mtAcc.currency}</span></p>
|
||||||
|
<p className="text-[8px] text-zinc-500 uppercase">{mtAcc.market_type}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<p className="text-[10px] text-zinc-500 mt-3 relative pl-3">
|
||||||
|
<span className="absolute left-0 top-0 text-brand-red">*</span>
|
||||||
|
Para operar nas contas MT5, utilize a opção "MetaTrader 5" e a senha individual do MT5 correspondente na configuração.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
onClick={handleDisconnectBroker}
|
||||||
|
disabled={isSaving}
|
||||||
|
className="w-full mt-4 bg-zinc-900 border border-[#ff444f]/30 hover:bg-[#ff444f]/10 text-[#ff444f] font-bold uppercase tracking-widest text-xs py-3 rounded-xl transition-colors disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{isSaving ? "Desconectando..." : "Desconectar Terminal"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : userData?.metaApiAccountId ? (
|
||||||
<div className="bg-green-500/10 border border-green-500/30 rounded-xl p-6 text-center space-y-4">
|
<div className="bg-green-500/10 border border-green-500/30 rounded-xl p-6 text-center space-y-4">
|
||||||
<div className="w-16 h-16 bg-green-500/20 rounded-full flex items-center justify-center mx-auto mb-2">
|
<div className="w-16 h-16 bg-green-500/20 rounded-full flex items-center justify-center mx-auto mb-2">
|
||||||
<CheckCircle2 size={32} className="text-green-500" />
|
<CheckCircle2 size={32} className="text-green-500" />
|
||||||
</div>
|
</div>
|
||||||
<h4 className="text-green-500 font-bold uppercase tracking-widest text-lg">Terminal Conectado</h4>
|
<h4 className="text-green-500 font-bold uppercase tracking-widest text-lg">Terminal Conectado</h4>
|
||||||
<p className="text-sm text-zinc-400">Conta: <span className="text-white font-mono">{mtLogin}</span> ({mtPlatform.toUpperCase()})</p>
|
<p className="text-sm text-zinc-400">Conta: <span className="text-white font-mono">{mtLogin}</span> ({mtPlatform.toUpperCase()})</p>
|
||||||
|
<p className="text-sm text-zinc-400">Servidor: <span className="text-white font-mono">{mtServer}</span></p>
|
||||||
<button
|
<button
|
||||||
onClick={handleDisconnectBroker}
|
onClick={handleDisconnectBroker}
|
||||||
disabled={isSaving}
|
disabled={isSaving}
|
||||||
@@ -466,48 +634,78 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
<div className="relative">
|
<div className="relative">
|
||||||
<select
|
<select
|
||||||
value={mtPlatform}
|
value={mtPlatform}
|
||||||
onChange={(e) => setMtPlatform(e.target.value)}
|
onChange={(e) => {
|
||||||
|
const val = e.target.value;
|
||||||
|
setMtPlatform(val);
|
||||||
|
if (val === 'deriv_api' && userData?.savedDerivToken) {
|
||||||
|
setMtPassword(userData.savedDerivToken);
|
||||||
|
} else {
|
||||||
|
setMtPassword('');
|
||||||
|
}
|
||||||
|
setMtLogin('');
|
||||||
|
setMtServer('');
|
||||||
|
}}
|
||||||
className="w-full bg-black border border-white/10 rounded-lg p-3 pr-8 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors appearance-none"
|
className="w-full bg-black border border-white/10 rounded-lg p-3 pr-8 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors appearance-none"
|
||||||
>
|
>
|
||||||
<option value="mt5">MetaTrader 5</option>
|
<option value="mt5">MetaTrader 5</option>
|
||||||
<option value="mt4">MetaTrader 4</option>
|
<option value="mt4">MetaTrader 4</option>
|
||||||
|
<option value="deriv_api">Deriv API (Token)</option>
|
||||||
</select>
|
</select>
|
||||||
<ChevronRight className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-500 pointer-events-none rotate-90" size={14} />
|
<ChevronRight className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-500 pointer-events-none rotate-90" size={14} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-1">
|
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Login (Account ID)</label>
|
{mtPlatform === 'deriv_api' ? (
|
||||||
<input
|
<div className="space-y-1 col-span-2">
|
||||||
type="number"
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Deriv API Token</label>
|
||||||
placeholder="Ex: 10029384"
|
<input
|
||||||
value={mtLogin}
|
type="password"
|
||||||
onChange={(e) => setMtLogin(e.target.value)}
|
placeholder="Ex: H5TU8g6UGpe5lDX"
|
||||||
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
value={mtPassword}
|
||||||
/>
|
onChange={(e) => setMtPassword(e.target.value)}
|
||||||
</div>
|
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
||||||
|
/>
|
||||||
|
<p className="text-[10px] text-zinc-500 ml-1 mt-1">Gere um token na Deriv com permissões de 'Read' e 'Trade'. Recomendado para Volatility/Boom/Crash.</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Login (Account ID)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
placeholder="Ex: 10029384"
|
||||||
|
value={mtLogin}
|
||||||
|
onChange={(e) => setMtLogin(e.target.value)}
|
||||||
|
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="grid grid-cols-2 gap-4">
|
{mtPlatform !== 'deriv_api' && (
|
||||||
<div className="space-y-1">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Password</label>
|
<div className="space-y-1">
|
||||||
<input
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Password</label>
|
||||||
type="password"
|
<input
|
||||||
placeholder="••••••••"
|
type="password"
|
||||||
value={mtPassword}
|
placeholder="••••••••"
|
||||||
onChange={(e) => setMtPassword(e.target.value)}
|
value={mtPassword}
|
||||||
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
onChange={(e) => setMtPassword(e.target.value)}
|
||||||
/>
|
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Server</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Ex: Deriv-Server-01"
|
||||||
|
value={mtServer}
|
||||||
|
onChange={(e) => setMtServer(e.target.value)}
|
||||||
|
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-1">
|
)}
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Server</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
placeholder="Ex: Deriv-Server-01"
|
|
||||||
value={mtServer}
|
|
||||||
onChange={(e) => setMtServer(e.target.value)}
|
|
||||||
className="w-full bg-black border border-white/10 rounded-lg p-3 text-sm text-white font-mono focus:border-brand-red outline-none transition-colors"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-1 mt-2">
|
<div className="space-y-1 mt-2">
|
||||||
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Trade Comment (Metatrader/Corretora)</label>
|
<label className="text-[10px] font-black text-zinc-500 uppercase tracking-widest">Trade Comment (Metatrader/Corretora)</label>
|
||||||
<input
|
<input
|
||||||
@@ -520,7 +718,7 @@ export const AutoTradingView: React.FC<{ userData?: any, onUpdate?: (data: any)
|
|||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
onClick={handleConnectBroker}
|
onClick={handleConnectBroker}
|
||||||
disabled={isSaving || !mtLogin || !mtPassword || !mtServer}
|
disabled={isSaving || !mtPassword || (mtPlatform !== 'deriv_api' && (!mtLogin || !mtServer))}
|
||||||
className="w-full bg-brand-red/10 text-brand-red border border-brand-red/30 hover:bg-brand-red/20 font-bold uppercase tracking-widest text-xs py-3 rounded-xl transition-colors disabled:opacity-50"
|
className="w-full bg-brand-red/10 text-brand-red border border-brand-red/30 hover:bg-brand-red/20 font-bold uppercase tracking-widest text-xs py-3 rounded-xl transition-colors disabled:opacity-50"
|
||||||
>
|
>
|
||||||
{isSaving ? "Connecting..." : saveSuccess ? "Connected Successfully" : "Connect Terminal"}
|
{isSaving ? "Connecting..." : saveSuccess ? "Connected Successfully" : "Connect Terminal"}
|
||||||
|
|||||||
Reference in New Issue
Block a user