Skip to content

Commit 1e1658a

Browse files
author
yves
committed
Final code versions from Session 3.
1 parent 4e50921 commit 1e1658a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

03_session/deploy.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# deploy.sh: local deploy or remote install helper
5+
if [[ "${1-}" == "--remote" ]]; then
6+
# remote installation steps
7+
echo "=== Remote setup on $(hostname) ==="
8+
# ensure we run inside the deployment folder
9+
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
10+
cd "$SCRIPT_DIR"
11+
sudo apt-get update
12+
sudo apt-get install -y python3 python3-venv python3-pip git
13+
python3 -m venv venv
14+
source venv/bin/activate
15+
pip install --upgrade pip
16+
pip install pandas numpy python-dateutil v20
17+
pip install git+https://github.com/yhilpisch/tpqoa.git
18+
echo "Remote setup complete."
19+
exit 0
20+
fi
21+
22+
if [[ $# -lt 1 ]]; then
23+
echo "Usage: $0 user@host"
24+
exit 1
25+
fi
26+
27+
SERVER=$1
28+
REMOTE_DIR=~/mr_trading
29+
echo "Copying scripts and credentials to $SERVER:$REMOTE_DIR ..."
30+
ssh "$SERVER" "mkdir -p $REMOTE_DIR"
31+
scp \
32+
mr_trading.py tpqoa.py oanda.cfg oanda_instruments.json deploy.sh \
33+
"$SERVER":$REMOTE_DIR/
34+
35+
echo "Running remote setup..."
36+
ssh "$SERVER" "bash $REMOTE_DIR/deploy.sh --remote"
37+
38+
read -p "Start live trading on $SERVER now? [y/N] " resp
39+
if [[ "$resp" =~ ^[Yy] ]]; then
40+
ssh "$SERVER" "cd $REMOTE_DIR && source venv/bin/activate && python mr_trading.py --config oanda.cfg --instrument DE30_EUR"
41+
else
42+
echo "To start later on $SERVER, run:"
43+
echo " ssh $SERVER"
44+
echo " cd $REMOTE_DIR && source venv/bin/activate && python mr_trading.py --config oanda.cfg --instrument DE30_EUR"
45+
fi

03_session/mr_trading.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@
88
"""
99

1010
import argparse
11+
import logging
1112

1213
import numpy as np
1314
import pandas as pd
1415

1516
from tpqoa import tpqoa
1617

18+
# Set up simple trade logging
19+
logging.basicConfig(
20+
filename='trade.log',
21+
level=logging.INFO,
22+
format='%(asctime)s - %(levelname)s - %(message)s'
23+
)
24+
1725

1826
def main():
1927
p = argparse.ArgumentParser(description='Live MR trading (3s bars)')
@@ -80,19 +88,23 @@ def on_tick(inst, t, bid, ask):
8088
api.create_order(args.instrument, 1)
8189
position = 1
8290
print(f"{last.name} LONG @ {price:.2f}")
91+
logging.info(f"ENTRY LONG time={last.name} price={price:.2f}")
8392
elif price > last['ub'] and last['rsi'] > 70:
8493
api.create_order(args.instrument, -1)
8594
position = -1
8695
print(f"{last.name} SHORT @ {price:.2f}")
96+
logging.info(f"ENTRY SHORT time={last.name} price={price:.2f}")
8797
# exit logic: simple mean reversion to middle band
8898
elif position == 1 and price >= last['mb']:
8999
api.create_order(args.instrument, -1)
90100
position = 0
91101
print(f"{last.name} EXIT LONG @ {price:.2f}")
102+
logging.info(f"EXIT LONG time={last.name} price={price:.2f}")
92103
elif position == -1 and price <= last['mb']:
93104
api.create_order(args.instrument, 1)
94105
position = 0
95106
print(f"{last.name} EXIT SHORT @ {price:.2f}")
107+
logging.info(f"EXIT SHORT time={last.name} price={price:.2f}")
96108

97109
# start streaming ticks
98110
print("Collecting 3-second bars...", end='\n')

0 commit comments

Comments
 (0)