added files
This commit is contained in:
130
src/alpaca.ts
Normal file
130
src/alpaca.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import ws from 'ws';
|
||||
|
||||
|
||||
const paper_credentials = {
|
||||
key : 'PKCSK7Z6WGNF6QDEKEG3',
|
||||
secret : '89JxFVaJ14BVa1xVzDL6gIZETsFHYl1cnseEVJog'
|
||||
}
|
||||
const live_livecredentials = {
|
||||
key : '', //not yet
|
||||
secret : ''
|
||||
}
|
||||
|
||||
|
||||
export class Alpaca {
|
||||
private credentials: {key: string, secret: string};
|
||||
private api_url : string;
|
||||
constructor(live = false) {
|
||||
if (live) {
|
||||
this.credentials = live_livecredentials;
|
||||
this.api_url = ''//
|
||||
throw new Error("not doing live yet");
|
||||
}
|
||||
else {
|
||||
this.credentials = paper_credentials;
|
||||
this.api_url = 'https://paper-api.alpaca.markets';
|
||||
}
|
||||
}
|
||||
|
||||
private get(url: string) {
|
||||
//probably options
|
||||
return fetch(this.api_url + url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'APCA-API-KEY-ID': this.credentials.key,
|
||||
'APCA-API-SECRET-KEY': this.credentials.secret
|
||||
}
|
||||
});
|
||||
}
|
||||
private data(url: string) {
|
||||
return fetch( url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'APCA-API-KEY-ID': this.credentials.key,
|
||||
'APCA-API-SECRET-KEY': this.credentials.secret
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getAccount() {
|
||||
const response = await this.get('/v2/account');
|
||||
return await response.json();
|
||||
}
|
||||
public async getPositions() {
|
||||
const response = await this.get('/v2/positions');
|
||||
return await response.json();
|
||||
}
|
||||
public async getOrders() {
|
||||
const response = await this.get('/v2/orders');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getAssets() {
|
||||
const response = await this.get('/v2/assets');
|
||||
return await response.json();
|
||||
}
|
||||
public async getAsset(symbol: string) {
|
||||
const response = await this.get('/v2/assets/' + symbol);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getWatchlists() {
|
||||
const response = await this.get('/v2/watchlists');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getCalendar() {
|
||||
const response = await this.get('/v2/calendar');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getClock() {
|
||||
const response = await this.get('/v2/clock');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getAccountConfigurations() {
|
||||
const response = await this.get('/v2/account/configurations');
|
||||
return await response.json();
|
||||
}
|
||||
public async getAccountActivities() {
|
||||
const response = await this.get('/v2/account/activities');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getPortfolioHistory() {
|
||||
const response = await this.get('/v2/account/portfolio/history');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getOrdersByPath() {
|
||||
const response = await this.get('/v2/orders');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getOrderByPath() {
|
||||
const response = await this.get('/v2/orders');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getOrderByClientOrderId() {
|
||||
const response = await this.get('/v2/orders');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getOrderByID() {
|
||||
const response = await this.get('/v2/orders');
|
||||
return await response.json();
|
||||
}
|
||||
public async getLatestQuote(symbol: string) {{
|
||||
const response = await this.data(`https://data.alpaca.markets/v2/stocks/${symbol}/quotes/latest`);
|
||||
return response.json();
|
||||
|
||||
}}
|
||||
public async getLatestTrades(symbols: string[]) {
|
||||
console.log(symbols.length)
|
||||
console.log(symbols.join(','))
|
||||
const response = await this.data('https://data.alpaca.markets/v2/stocks/trades/latest?symbols=' + symbols.join(','));
|
||||
return response.json();
|
||||
}
|
||||
}
|
||||
61
src/index.ts
Normal file
61
src/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Alpaca } from "./alpaca";
|
||||
|
||||
const alpaca = new Alpaca(false);
|
||||
|
||||
async function printAsset(symbol: string) {
|
||||
const asset = await alpaca.getAsset(symbol);
|
||||
if (asset && asset.fractionable)
|
||||
console.log(symbol + ' is fractional')
|
||||
else
|
||||
console.log(symbol + ' is not fractional')
|
||||
}
|
||||
async function accountBalance() {
|
||||
const account = await alpaca.getAccount();
|
||||
return account.cash;
|
||||
|
||||
}
|
||||
async function waitForNextOpen() {
|
||||
const clock = await alpaca.getClock();
|
||||
return wait(new Date(clock.next_open).valueOf() - new Date().valueOf());
|
||||
}
|
||||
function wait(ms :number) {
|
||||
return new Promise((resolve) => {
|
||||
console.log('would wait: ' + ms + 'ms');
|
||||
resolve('')
|
||||
//setTimeout(resolve, ms);
|
||||
});
|
||||
|
||||
}
|
||||
async function runDay() {
|
||||
await waitForNextOpen();
|
||||
console.log('run the day');
|
||||
await printAsset('AAPL');
|
||||
await printAsset('TQQQ');
|
||||
await printAsset('SQQQ');
|
||||
|
||||
console.log(await accountBalance());
|
||||
let stocks = [
|
||||
'AAPL',
|
||||
'TQQQ',
|
||||
'SQQQ'
|
||||
]
|
||||
console.log(await alpaca.getLatestTrades(stocks))
|
||||
//console.log(await alpaca.getLatestQuote('AAPL'));
|
||||
}
|
||||
async function main() {
|
||||
await runDay();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//run main
|
||||
main().then(
|
||||
() => {
|
||||
console.log("done")
|
||||
}
|
||||
).catch(
|
||||
(e) => console.log('Error: ', e)
|
||||
).finally(
|
||||
() => process.exit(0)
|
||||
);
|
||||
Reference in New Issue
Block a user