forked from algo2t/alphatrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zstreaming_data.py
85 lines (68 loc) · 2.92 KB
/
zstreaming_data.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import statistics
import datetime
import config
import json
from time import sleep
# pip install https://github.com/algo2t/alphatrade
from alphatrade import AlphaTrade, LiveFeedType
sas = AlphaTrade(login_id=config.login_id, password=config.password,
twofa=config.TOTP, access_token=config.access_token, master_contracts_to_download=['MCX', 'NFO'])
ins_scrip = sas.get_instrument_by_symbol('MCX', 'NATURALGAS AUG FUT')
print(ins_scrip)
ltp = 0.0
socket_opened = False
count_s = 0
count_b = 0
def event_handler_quote_update(message):
global ltp
# ltp = message['ltp']
# tick = json.loads(message, indent=1)
print(f'ticks :: {message}')
def open_callback():
global socket_opened
socket_opened = True
def run_strategy():
global ltp
minute_close = []
sas.start_websocket(subscribe_callback=event_handler_quote_update,
socket_open_callback=open_callback,
run_in_background=True)
while (socket_opened is False): # wait till socket open & then subscribe
pass
sas.subscribe(ins_scrip, LiveFeedType.COMPACT)
# sas.subscribe(ins_scrip, LiveFeedType.MARKET_DATA)
# sas.subscribe(ins_scrip, LiveFeedType.SNAPQUOTE)
# sas.subscribe(ins_scrip, LiveFeedType.FULL_SNAPQUOTE)
print("Script Start Time :: " + str(datetime.datetime.now()))
while True:
print('current price is :: '+str(ltp))
if datetime.datetime.now().second in [0, 30]:
# NOTE This is just an example to stop script without using `control + c` Keyboard Interrupt
# It checks whether the stop.txt has word stop
# This check is done every 30 seconds
stop_script = open('zstop.txt', 'r').read().strip()
print(stop_script + " time :: " + str(datetime.datetime.now()))
if (stop_script == 'stop'):
print('exiting script')
break
# LTP is stored in the minute_close array and then mean is found on close to get SMA_5 and SMA_20
if (datetime.datetime.now().second == 0):
minute_close.append(ltp)
print('current price is :: '+str(ltp))
if (len(minute_close) > 20):
sma_5 = statistics.mean(minute_close[-5:])
sma_20 = statistics.mean(minute_close[-20:])
print('current price is :: '+str(ltp))
if (sma_5 > sma_20) and (status != 'bought'):
# buy_signal(ins_scrip)
print('buy signal crossover :: ' + str(count_b))
status = 'bought'
count_b = count_b+1
elif (sma_5 < sma_20) and (status != 'sold'):
# sell_signal(ins_scrip)
print('sell signal crossover :: ' + str(counts))
status = 'sold'
counts = counts + 1
sleep(1)
sleep(0.2) # sleep for 200ms
run_strategy()