-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrategy.py
143 lines (116 loc) · 5 KB
/
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import pandas as pd
import numpy as np
import datetime as dt
import os
from toCsv import generateFileName
def vwapStrategy(feed, weightage):
buySum=0
sellSum=0
t1 = min(1, ( abs(feed['vwap'] -feed['currentPrice'])/(.03*feed['vwap']) ) )
if feed['currentPrice'] > feed['vwap'] and feed['lastStrategyPrice']< feed['vwap']:
buySum+= t1*weightage['vwap']
elif feed['currentPrice'] < feed['vwap'] and feed['lastStrategyPrice']> feed['vwap']:
sellSum+= t1*weightage['vwap']
return buySum,sellSum
def emaStrategy(feed,x, weightage):
buySum=0
sellSum=0
if feed['currentPrice'] > feed['ema'+str(x)] and feed['lastStrategyPrice'] < feed['ema'+str(x)]:
buySum+=weightage['ema']/4
elif feed['currentPrice'] < feed['ema'+str(x)] and feed['lastStrategyPrice'] > feed['ema'+str(x)]:
sellSum+=weightage['ema']/4
elif feed['currentPrice'] > feed['ema'+str(x)]:
buySum+=weightage['ema']/8
else:
sellSum+=weightage['ema']/8
return buySum,sellSum
def pivotStrategy(feed, weightage):
arr= [feed['s3'], feed['s2'], feed['s1'], feed['pivotpoint'], feed['r1'], feed['r2'], feed['r3']]
buySum=0
sellSum=0
if feed['currentPrice'] > feed['currentResistance'] and feed['lastStrategyPrice']< feed['currentResistance']:
buySum+=weightage['piv']
feed['currentSupport']= feed['currentResistance']
try:
feed['currentResistance']= arr[arr.index(feed['currentResistance']) +1]
except:
################################################
feed['currentResistance']= feed['currentResistance']*1.02
################################################
elif feed['currentPrice'] < feed['currentSupport'] and feed['lastStrategyPrice']> feed['currentSupport']:
sellSum+=weightage['piv']
feed['currentResistance']= feed['currentSupport']
try:
feed['currentSupport']= arr[arr.index(feed['currentSupport']) -1]
except:
################################################
feed['currentSupport']= feed['currentSupport']*0.98
################################################
return buySum,sellSum
def strategy(feed):
# upar wali sari strategies ka mix + don't return anything: either print the call or add a row to orders.csv file.
# Check its presence before reading
# print('input: ',feed)
if dt.datetime.now().hour >9 or dt.datetime.now().minute > 30: # 9:30 se pehle koi trade nahi leni
if feed['lastStrategyPrice']== 'NA':
feed['lastStrategyPrice']= feed['currentPrice']
# # read csv
# df= pd.read_csv(os.path.join(folder, fileName))
# # df mai sab kuchh hoga jo strategy lagane ke liye chahiye
filename = 'orders.csv'
folder = os.path.join(os.getcwd(),'daySummary')
##############
weightage = {
'vwap':.2,
'ema': .4,
'piv': .4
}
#############
print('8.2.1')
buySum = 0
sellSum = 0
# print('8.2.2')
vwapS = vwapStrategy(feed, weightage)
# print('8.2.3')
emaS9 = emaStrategy(feed,9, weightage)
# print('8.2.4')
emaS13 = emaStrategy(feed, 13, weightage)
# print('8.2.5')
emaS26 = emaStrategy(feed, 26, weightage)
# print('8.2.6')
emaS50 = emaStrategy(feed, 50, weightage)
pivotS = pivotStrategy(feed, weightage)
buySum = vwapS[0]+emaS50[0]+emaS26[0]+emaS13[0]+emaS9[0]+pivotS[0]
sellSum = vwapS[1]+emaS50[1]+emaS26[1]+emaS13[1]+emaS9[1]+pivotS[1]
print('diff', abs(buySum - sellSum))
#############################
if abs(buySum - sellSum) > .15:
##############################
print('reached')
dct = {
'orderPrice':feed['currentPrice'],
'symbol': feed['symbol']
}
if buySum > sellSum:
dct['order'] = 'BUY'
dct['stopLoss'] = 0.995*feed['currentPrice']
dct['targetPrice'] = 1.007*feed['currentPrice']
else:
dct['order'] = 'SELL'
dct['stopLoss'] = 1.005*feed['currentPrice']
dct['targetPrice'] = 0.993*feed['currentPrice']
dct['timeStamp'] = dt.datetime.now()
if filename in os.listdir(folder):
print('appending row')
x = pd.read_csv(os.path.join(folder,filename))
x = x.append(dct,ignore_index=True)
x.to_csv(os.path.join(folder,filename),index=False)
print('appended row')
else:
x = pd.DataFrame(columns=list(dct.keys()))
row = [dct[i] for i in dct]
x.loc[0]=row
x.to_csv(os.path.join(folder,filename),index=False)
# finally lastStrategyPrice ko update krdo
feed['lastStrategyPrice']= feed['currentPrice']
print('.', end= ' ')