import React from 'react'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Typography from '@mui/material/Typography'; import TextField from '@mui/material/TextField'; import FormControlLabel from '@mui/material/FormControlLabel'; import FormHelperText from '@mui/material/FormHelperText'; import InputLabel from '@mui/material/InputLabel'; import FormControl from '@mui/material/FormControl'; import InputAdornment from '@mui/material/InputAdornment'; import IconButton from '@mui/material/IconButton'; import Checkbox from '@mui/material/Checkbox'; import Select from '@mui/material/Select'; import MenuItem from '@mui/material/MenuItem'; import FilledInput from '@mui/material/FilledInput'; import Alert from '@mui/lab/Alert'; import VisibilityOff from '@mui/icons/VisibilityOff'; import Visibility from '@mui/icons/Visibility'; import { addSession, testSessionConnection } from '../api/bbgo'; import { makeStyles } from '@mui/material/styles'; const useStyles = makeStyles((theme) => ({ formControl: { marginTop: theme.spacing(1), marginBottom: theme.spacing(1), minWidth: 120, }, buttons: { display: 'flex', justifyContent: 'flex-end', marginTop: theme.spacing(2), paddingTop: theme.spacing(2), paddingBottom: theme.spacing(2), '& > *': { marginLeft: theme.spacing(1), }, }, })); export default function AddExchangeSessionForm({ onBack, onAdded }) { const classes = useStyles(); const [exchangeType, setExchangeType] = React.useState('max'); const [customSessionName, setCustomSessionName] = React.useState(false); const [sessionName, setSessionName] = React.useState(exchangeType); const [testing, setTesting] = React.useState(false); const [testResponse, setTestResponse] = React.useState(null); const [response, setResponse] = React.useState(null); const [apiKey, setApiKey] = React.useState(''); const [apiSecret, setApiSecret] = React.useState(''); const [showApiKey, setShowApiKey] = React.useState(false); const [showApiSecret, setShowApiSecret] = React.useState(false); const [isMargin, setIsMargin] = React.useState(false); const [isIsolatedMargin, setIsIsolatedMargin] = React.useState(false); const [isolatedMarginSymbol, setIsolatedMarginSymbol] = React.useState(''); const resetTestResponse = () => { setTestResponse(null); }; const handleExchangeTypeChange = (event) => { setExchangeType(event.target.value); setSessionName(event.target.value); resetTestResponse(); }; const createSessionConfig = () => { return { name: sessionName, exchange: exchangeType, key: apiKey, secret: apiSecret, margin: isMargin, envVarPrefix: exchangeType.toUpperCase(), isolatedMargin: isIsolatedMargin, isolatedMarginSymbol: isolatedMarginSymbol, }; }; const handleAdd = (event) => { const payload = createSessionConfig(); addSession(payload, (response) => { setResponse(response); if (onAdded) { setTimeout(onAdded, 3000); } }).catch((error) => { console.error(error); setResponse(error.response); }); }; const handleTestConnection = (event) => { const payload = createSessionConfig(); setTesting(true); testSessionConnection(payload, (response) => { console.log(response); setTesting(false); setTestResponse(response); }).catch((error) => { console.error(error); setTesting(false); setTestResponse(error.response); }); }; return ( Add Exchange Session Exchange { setSessionName(event.target.value); }} value={sessionName} /> { setCustomSessionName(event.target.checked); }} value="1" /> } label="Custom exchange session name" /> By default, the session name will be the exchange type name, e.g.{' '} binance or max.
If you're using multiple exchange sessions, you might need to custom the session name.
This is for advanced users.
API Key { setShowApiKey(!showApiKey); }} onMouseDown={(event) => { event.preventDefault(); }} edge="end" > {showApiKey ? : } } onChange={(event) => { setApiKey(event.target.value); resetTestResponse(); }} /> API Secret { setShowApiSecret(!showApiSecret); }} onMouseDown={(event) => { event.preventDefault(); }} edge="end" > {showApiSecret ? : } } onChange={(event) => { setApiSecret(event.target.value); resetTestResponse(); }} /> {exchangeType === 'binance' ? ( { setIsMargin(event.target.checked); resetTestResponse(); }} value="1" /> } label="Use margin trading." /> This is only available for Binance. Please use the leverage at your own risk. { setIsIsolatedMargin(event.target.checked); resetTestResponse(); }} value="1" /> } label="Use isolated margin trading." /> This is only available for Binance. If this is set, you can only trade one symbol with one session. {isIsolatedMargin ? ( { setIsolatedMarginSymbol(event.target.value); resetTestResponse(); }} fullWidth required /> ) : null} ) : null}
{testResponse ? ( testResponse.error ? ( {testResponse.error} ) : testResponse.success ? ( Connection Test Succeeded ) : null ) : null} {response ? ( response.error ? ( {response.error} ) : response.success ? ( Exchange Session Added ) : null ) : null}
); }