Created
April 17, 2019 01:21
-
-
Save djflux/4c6173037e40fdb089955edbd06669f2 to your computer and use it in GitHub Desktop.
Test of various gdax-trading-toolkit modules and libs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as GTT from "gdax-trading-toolkit"; | |
import { Big } from "gdax-trading-toolkit/build/src/lib/types"; | |
import { TickerMessage, Trader, TraderConfig } from "gdax-trading-toolkit/build/src/core"; | |
import { LiveOrder } from "gdax-trading-toolkit/build/src/lib"; | |
import { GDAXExchangeAPI } from "gdax-trading-toolkit/build/src/exchanges/gdax/GDAXExchangeAPI"; | |
import { GDAXConfig } from "gdax-trading-toolkit/build/src/exchanges/gdax/GDAXInterfaces"; | |
import { GDAX_WS_FEED, GDAXFeed, GDAXFeedConfig } from "gdax-trading-toolkit/build/src/exchanges"; | |
import { GDAX_API_URL } from "gdax-trading-toolkit/build/src/exchanges/gdax/GDAXExchangeAPI"; | |
import { | |
CancelOrderRequestMessage, | |
ErrorMessage, | |
PlaceOrderMessage, | |
StreamMessage, | |
TradeExecutedMessage, | |
TradeFinalizedMessage | |
} from "gdax-trading-toolkit/build/src/core/Messages"; | |
import { StaticCommandSet } from "gdax-trading-toolkit/build/src/lib"; | |
import Limiter from "gdax-trading-toolkit/build/src/core/RateLimiter"; | |
const logger = GTT.Factories.ConsoleLoggerFactory(); | |
const auth = { | |
key: process.env.GDAX_KEY, | |
secret: process.env.GDAX_SECRET, | |
passphrase: process.env.GDAX_PASSPHRASE | |
}; | |
const wsUrl = 'wss://ws-feed-public.sandbox.pro.coinbase.com'; | |
const apiUrl = 'https://api-public.sandbox.pro.coinbase.com'; | |
const gdaxConfig: GDAXConfig = { | |
logger: logger, | |
apiUrl: process.env.GDAX_API_URL || 'https://api-public.sandbox.pro.coinbase.com', | |
auth: { | |
key: process.env.GDAX_KEY, | |
secret: process.env.GDAX_SECRET, | |
passphrase: process.env.GDAX_PASSPHRASE | |
} | |
}; | |
/** | |
* Remember to set GDAX_KEY, GDAX_SECRET and GDAX_PASSPHRASE envars to allow trading | |
*/ | |
const gdaxTrade = new GDAXExchangeAPI(gdaxConfig); | |
const product = 'BTC-USD'; | |
const [base, quote] = product.split('-'); | |
// const gdaxAPI = GTT.Factories.GDAX.DefaultAPI(logger); | |
const options: GDAXFeedConfig = { | |
logger: logger, | |
auth: { key: null, secret: null, passphrase: null }, // use public feed | |
channels: ['ticker'], | |
wsUrl: process.env.GDAX_WS_URL || 'wss://ws-feed-public.sandbox.pro.coinbase.com', | |
apiUrl: process.env.GDAX_API_URL || 'https://api-public.sandbox.pro.coinbase.com' | |
}; | |
const order: PlaceOrderMessage = { | |
type: 'placeOrder', | |
time: null, | |
productId: product, | |
orderType: 'limit', | |
side: 'buy', | |
size: '0.001', | |
price: '0.02' | |
}; | |
const orderMessages: StreamMessage[] = [ | |
{ | |
type: 'placeOrder', | |
productId: product, | |
side: 'buy', | |
orderType: 'limit', | |
size: '0.001', | |
price: '1.00' | |
} as PlaceOrderMessage | |
]; | |
gdaxTrade.placeOrder(order).then((result: LiveOrder) => { | |
logger.log('info', 'Order executed', `Order to ${order.side} ${order.size} ${base} placed. Result: ${result.status}`); | |
logger.log('debug', 'DEBUG', JSON.stringify(order)); | |
}); | |
GTT.Factories.GDAX.getSubscribedFeeds({ auth: auth, logger: logger, wsUrl: wsUrl, apiUrl: apiUrl }, [product]).then((feed: GDAXFeed) => { | |
GTT.Core.createTickerTrigger(feed, product, false) | |
.setAction((ticker: TickerMessage) => { | |
logger.log('debug', GTT.utils.printTicker(ticker, 3)); | |
}); | |
const traderConfig: TraderConfig = { | |
logger: logger, | |
productId: product, | |
exchangeAPI: feed.authenticatedAPI, | |
fitOrders: true | |
}; | |
const trader = new Trader(traderConfig); | |
const orders = new StaticCommandSet(orderMessages, false); | |
// We use a limiter to play each order once every 2 seconds. | |
const limiter = new Limiter(1, 500); | |
// We'll play the orders through the limiter, so connect them up | |
orders.pipe(limiter); | |
// We can only pipe one stream into the trader, so we can't pipe both the GDAX feed as well as our trading commands. | |
// We can pipe one, and then use the event mechanism to handle the other. In this demo we'll pipe the message feed | |
// to trader, | |
feed.pipe(trader); | |
// .. and execute the trade messages as they come out of the limiter. | |
limiter.on('data', (msg: StreamMessage) => { | |
logger.log('info', 'Execute following order: ', JSON.stringify(msg)); | |
trader.executeMessage(msg); | |
}); | |
logger.log('info', 'Placing order with trader object: ', JSON.stringify(order)); | |
trader.placeOrder(order); | |
// We're basically done. Now set up listeners to log the trades as they happen | |
trader.on('Trader.order-placed', (msg: LiveOrder) => { | |
logger.log('info', 'Order placed', JSON.stringify(msg)); | |
}); | |
trader.on('Trader.trade-executed', (msg: TradeExecutedMessage) => { | |
logger.log('info', 'Trade executed', JSON.stringify(msg)); | |
}); | |
trader.on('Trader.trade-finalized', (msg: TradeFinalizedMessage) => { | |
logger.log('info', 'Order complete', JSON.stringify(msg)); | |
}); | |
trader.on('Trader.my-orders-cancelled', (ids: string[]) => { | |
logger.log('info', `${ids.length} orders cancelled`); | |
}); | |
trader.on('Trader.place-order-failed', (err: ErrorMessage) => { | |
logger.log('error', 'Order placement failed', err); | |
}); | |
trader.on('error', (err: Error) => { | |
logger.log('error', 'An error occurred', err); | |
}); | |
limiter.on('end', () => { | |
// Wait a second to allow final order to settle | |
setTimeout(() => { | |
console.log('..and we are done. Final Trader state:'); | |
console.log(JSON.stringify(trader.state())); | |
process.exit(0); | |
// trader.cancelMyOrders().catch((err: Error) => { | |
// logger.log('error', 'Error cancelling orders', err); | |
// }); | |
}, 5000); | |
}); | |
// Send the orders once the feed has initialised | |
feed.once('snapshot', () => { | |
logger.log('info','Im here'); | |
orders.send(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment