Skip to content

Commit

Permalink
Revert last commit, accidentally changed Gemfile
Browse files Browse the repository at this point in the history
This reverts commit 0fb23cf.
  • Loading branch information
SinaKhalili committed Oct 11, 2024
1 parent 0fb23cf commit 788f12d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 175 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ruby '>= 3'
ruby '>= 2.6'
source 'https://rubygems.org'

# Middleman
Expand All @@ -8,6 +8,6 @@ gem 'middleman-autoprefixer', '~> 3.0'
gem 'middleman-sprockets', '~> 4.1'
gem 'rouge', '~> 3.21'
gem 'redcarpet', '~> 3.6.0'
gem 'nokogiri', '~> 1.16.1'
gem 'nokogiri', '~> 1.13.3'
gem 'sass'
gem 'webrick'
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ GEM
middleman-syntax (3.3.0)
middleman-core (>= 3.2)
rouge (~> 3.2)
mini_portile2 (2.8.0)
minitest (5.17.0)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
padrino-helpers (0.15.2)
i18n (>= 0.6.7, < 2)
Expand All @@ -91,7 +93,7 @@ GEM
parallel (1.22.1)
parslet (2.0.0)
public_suffix (5.0.1)
racc (1.8.1)
racc (1.6.0)
rack (2.2.6.2)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
Expand Down Expand Up @@ -130,14 +132,14 @@ DEPENDENCIES
middleman-autoprefixer (~> 3.0)
middleman-sprockets (~> 4.1)
middleman-syntax (~> 3.2)
nokogiri (~> 1.16.1)
nokogiri (~> 1.13.3)
redcarpet (~> 3.6.0)
rouge (~> 3.21)
sass
webrick

RUBY VERSION
ruby 3.3.5p100
ruby 2.7.2p137

BUNDLED WITH
2.2.22
171 changes: 2 additions & 169 deletions source/includes/_historicaldata.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,180 +40,13 @@ mainnet-beta: `https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/prog
| candleResolution | | 1M |


## Example: Trades for market
```python
import requests
import csv
from io import StringIO

URL_PREFIX = 'https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH'

market_symbol = 'SOL-PERP'
year = '2024'
month = '01'
day = '01'
response = requests.get(f'{URL_PREFIX}/market/{market_symbol}/tradeRecords/{year}/{year}{month}{day}')
response.raise_for_status()

csv_data = StringIO(response.text)
reader = csv.reader(csv_data)
for row in reader:
print(row)
```

```typescript
import { parse } from 'csv-parse/sync';

const URL_PREFIX = 'https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';

async function fetchAndParseCSV(marketSymbol: string, year: string, month: string, day: string): Promise<void> {
const url = `${URL_PREFIX}/market/${marketSymbol}/tradeRecords/${year}/${year}${month}${day}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const csvText = await response.text();
const records = parse(csvText, {
skip_empty_lines: true
});

records.forEach((row: string[]) => {
console.log(row);
});
} catch (error) {
console.error('Error fetching or parsing CSV:', error);
}
}
## Examples

async function main() {
const marketSymbol = 'SOL-PERP';
const year = '2024';
const month = '01';
const day = '01';

await fetchAndParseCSV(marketSymbol, year, month, day);
}

main().catch(error => console.error('An error occurred:', error));
```

Get historical trades on `SOL-PERP` for a given date (ex. 2024-01-01):
Get historical trades on `SOL-PERP` for August 5, 2023:
```
https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH/market/SOL-PERP/tradeRecords/2024/20240101
```


## Example: Trades for date range
```python
import requests
import csv
from io import StringIO
from datetime import date, timedelta

URL_PREFIX = 'https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH'

def get_trades_for_range(account_key, start_date, end_date):
all_trades = []
current_date = start_date
while current_date <= end_date:
year = current_date.year
month = current_date.month
day = current_date.day
url = f"{URL_PREFIX}/user/{account_key}/tradeRecords/{year}/{year}{month:02}{day:02}"
response = requests.get(url)
response.raise_for_status()

csv_data = StringIO(response.text)
reader = csv.reader(csv_data)
for row in reader:
all_trades.append(row)

current_date += timedelta(days=1)

return all_trades


# Example usage
account_key = "<Some Account Key>"
start_date = date(2024, 1, 24)
end_date = date(2024, 1, 26)

trades = get_trades_for_range(account_key, start_date, end_date)
```

```typescript
import { parse } from 'csv-parse/sync';

const URL_PREFIX = 'https://drift-historical-data-v2.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';

interface Trade {
// Define the structure of a trade record here
// For example:
// date: string;
// amount: number;
// price: number;
[key: string]: any;
}

async function getTradesForRange(accountKey: string, startDate: Date, endDate: Date): Promise<Trade[]> {
const allTrades: Trade[] = [];
let currentDate = new Date(startDate);

while (currentDate <= endDate) {
const year = currentDate.getFullYear();
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const day = currentDate.getDate().toString().padStart(2, '0');

const url = `${URL_PREFIX}/user/${accountKey}/tradeRecords/${year}/${year}${month}${day}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const csvText = await response.text();
const records = parse(csvText, {
skip_empty_lines: true,
columns: true
});

allTrades.push(...records);
} catch (error) {
console.error(`Error fetching or parsing CSV for ${year}-${month}-${day}:`, error);
}

currentDate.setDate(currentDate.getDate() + 1);
}

return allTrades;
}

async function main() {
const accountKey = "<Some Account Key>";
const startDate = new Date(2024, 0, 24); // Note: month is 0-indexed in JavaScript
const endDate = new Date(2024, 0, 26);

try {
const trades = await getTradesForRange(accountKey, startDate, endDate);
console.log(`Total trades fetched: ${trades.length}`);
console.log('First few trades:', trades.slice(0, 5));
} catch (error) {
console.error('An error occurred:', error);
}
}

main();
```

> Note: To speed this up, you could download the data in parallel (Promise.all or asyncio).
We can write a script to download all the data, one by one, for a given account in a given date range.


## Records Columns

Below are definitions of the columns in each record type.
Expand Down

0 comments on commit 788f12d

Please sign in to comment.