import React from 'react'; import Grid from '@material-ui/core/Grid'; import Box from '@material-ui/core/Box'; import Button from '@material-ui/core/Button'; import Typography from '@material-ui/core/Typography'; import TextField from '@material-ui/core/TextField'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormHelperText from '@material-ui/core/FormHelperText'; import InputLabel from '@material-ui/core/InputLabel'; import FormControl from '@material-ui/core/FormControl'; import Checkbox from '@material-ui/core/Checkbox'; import Select from '@material-ui/core/Select'; import MenuItem from '@material-ui/core/MenuItem'; import Alert from '@material-ui/lab/Alert'; import {addSession, testSessionConnection} from '../api/bbgo'; import {makeStyles} from '@material-ui/core/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 [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 Type { 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.
{ setApiKey(event.target.value) resetTestResponse() }} /> { 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 }
); }