Skip to content

Commit

Permalink
added possibility for offset values in start_-/end_date arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
LordOfTheSnow committed Jun 24, 2022
1 parent 6d39d70 commit ace3e42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Use shorter values for the _duration_ and _shard duration_ if you want to. (The

```
python readdata.py SYMBOL --start_date YYYY-MM-DD --end_date YYYY-MM-DD --do_not_write
python readdata.py SYMBOL --start_date -2 --end_date -1 --do_not_write
```

_SYMBOL_ has to be a Nasdaq Data Link ticker symbol. You can find the symbol in the upper right corner of the webpage that is displaying that symbol's data.
Expand All @@ -85,11 +86,23 @@ for a help page

Options starting with a _-_ or _--_ are optional and can be omitted.

If `--start_date` and/or `--end_date` are omitted, the current day will be taken.
If `--start_date` and/or `--end_date` are omitted, the current day will be taken. If a negative offset (e.g. `-1`) is given, the current date minus that number of days will be taken (e.g. -1 day)

If `--do_not_write` is given, no values are written to the database

#### Run via periodically via cron

If you want to run this script periodically via cron, you can call the wrapper script **cronscript.sh** (rename the provided `cronscript.sh.example` to `cronscript.sh` so that local changes will not be overwritten with next git pull).

8. Set execute permissions for the script: `chmod ug+x cronscript.sh`
9. Call `crontab -e` to edit the cron table (crontab), e.g.:

```
# m h dom mon dow command
0 1 1 * * /home/pi/src/nasdaq/cronscript.sh
```
This will run the command at 01:00h local time on every first day of every month. Edit the path to the script to wherever you put it.
The script assumes you have a Python virtual environment created with `python3 -m venv venv` in the same directory where you checked out the repository. The script will activate the virtual environment, call the Python interpreter with the script and will deactivate the virtual environment then.

### Files and data created

Expand Down Expand Up @@ -120,4 +133,9 @@ In the current implementation, the timestamp will always be at 0:00 local time a

## History

* 21-Jun-2021: made repository public
* 24-Jun-2021
* added possibilty to use offsets for the `start_date` and `end_date` parameters. You can now use `--start_date -2` to set the start date to the current date - 2 days.
* added example file _cronscript.sh.example_ to run this script in a Python virtual environment via cron

* 21-Jun-2021
* made repository public
7 changes: 7 additions & 0 deletions cronscript.sh.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $SCRIPT_DIR
source venv/bin/activate
python readdata.py OPEC/ORB --start_date -1
python readdata.py BOE/XUDLERD --start_date -2
deactivate
31 changes: 20 additions & 11 deletions readdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import nasdaqdatalink
from dotenv import load_dotenv
from argparse import ArgumentParser
from datetime import datetime
from datetime import datetime, timedelta
from influx18 import *

def main():
Expand All @@ -32,9 +32,9 @@ def main():
parser.add_argument("symbol",
help="Ticker symbol (==Nasdaq Data Link Code), e.g. OPEC/ORB for OPEC Crude Oil)")
parser.add_argument("-sd", "--start_date",
help="start date for the values to be received. Format: 'YYYY-MM-DD'")
help="start date for the values to be received. Format: 'YYYY-MM-DD' or a negative offset (e.g. -1 = -1 day)")
parser.add_argument("-ed", "--end_date",
help="end date for the values to be received. Format: 'YYYY-MM-DD'")
help="end date for the values to be received. Format: 'YYYY-MM-DD' or a negative offset (e.g. -1 = -1 day)")
parser.add_argument("-dnw", "--do_not_write", action='store_true',
help="do not write values to database")

Expand All @@ -51,17 +51,26 @@ def main():
# get start and end date
# first get the current date
utcnow = datetime.utcnow()
start_date = utcnow.strftime("%Y-%m-%d")
end_date = utcnow.strftime("%Y-%m-%d")
data_link_log.debug(f"start_date (calculated): {start_date}")
data_link_log.debug(f"end_date (calculated): {end_date}")

if args.start_date:
start_date = args.start_date
data_link_log.debug(f"start_date (set): {start_date}")
if str(args.start_date).startswith("-"):
start_date_ts = utcnow + timedelta(days=int(args.start_date))
start_date = start_date_ts.strftime("%Y-%m-%d")
else:
start_date = args.start_date
else:
start_date = utcnow.strftime("%Y-%m-%d")
data_link_log.debug(f"start_date: {start_date}")

if args.end_date:
end_date = args.end_date
data_link_log.debug(f"end_date (set): {end_date}")
if str(args.end_date).startswith("-"):
end_date_ts = utcnow + timedelta(days=int(args.end_date))
end_date = end_date_ts.strftime("%Y-%m-%d")
else:
end_date = args.end_date
else:
end_date = utcnow.strftime("%Y-%m-%d")
data_link_log.debug(f"end_date: {end_date}")

# read the data
data = nasdaqdatalink.Dataset(args.symbol).data(params={ 'start_date':start_date, 'end_date':end_date})
Expand Down

0 comments on commit ace3e42

Please sign in to comment.