-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlean_hogs_strategy.py
59 lines (44 loc) · 1.44 KB
/
lean_hogs_strategy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from ib_insync import *
ib = IB()
ib.connect()
def place_order(direction, qty, df, tp, sl):
bracket_order = ib.bracketOrder(
direction,
qty,
limitPrice=df.close.iloc[-1],
takeProfitPrice=tp,
stopLossPrice=sl,
)
for ord in bracket_order:
ib.placeOrder(contract, ord)
def on_new_bar(bars: BarDataList, has_new_bar: bool):
if has_new_bar:
df = util.df(data)
sma = df.close.tail(50).mean()
std_dev = df.close.tail(50).std() * 3
# Check if we are in a trade
if contract not in [i.contract for i in ib.positions()]:
# We are not in a trade - Look for a signal
if df.close.iloc[-1] > sma + std_dev:
# Trading more than 3 standard deviations above average - SELL
place_order('SELL', 1, df, sma, sma + std_dev * 2)
elif df.close.iloc[-1] < sma - std_dev:
# Trading more than 3 standard deviations below average - BUY
place_order('BUY', 1, df, sma, sma - std_dev * 2)
# Create Contract
contract = ContFuture('HE', 'GLOBEX')
ib.qualifyContracts(contract)
# Request Streaming bars
data = ib.reqHistoricalData(
contract,
endDateTime='',
durationStr='2 D',
barSizeSetting='15 mins',
whatToShow='MIDPOINT',
useRTH=True,
keepUpToDate=True,
)
# Set callback function for streaming bars
data.updateEvent += on_new_bar
# Run infinitely
ib.run()