-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgenerateSignals.py
More file actions
116 lines (91 loc) · 4.76 KB
/
generateSignals.py
File metadata and controls
116 lines (91 loc) · 4.76 KB
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
from orderManagement import OrderManagement
from decimal import Decimal
import math
class GenerateSignals():
def __init__(self):
# it means that the list will hold upto 30 minutes of data
self.indexLimitForLists = 30
self.incrementOverPeriodToCount = 0
self.limitToExitPosition = Decimal(80)
def run(self, mapOfData, positionStatus, orderManagementObject):
# changed it from the close price of previous trades to the best quotes
meanPriceFromQuotes = mapOfData["mean"]
tempMap = {"close": meanPriceFromQuotes}
mapOfSignals = self.generateSignal(tempMap)
signalToSell = mapOfSignals["sell"]
signalToBuy = mapOfSignals["buy"]
# stop exit if order is below average price
if positionStatus != None:
enteredPrice = Decimal(positionStatus['average_price'])
# shorting - if the price we entered is smaller then what we want to exit with then stop - we want sell high buy low
if positionStatus['direction'] == "sell":
if meanPriceFromQuotes > (enteredPrice):
# print("price is too high turn signalToBuy to false")
signalToBuy = False
# long - if the price we entered is larger then what we want to exit with then stop - we want sell high buy low
elif positionStatus['direction'] == "buy":
if meanPriceFromQuotes < (enteredPrice):
# print("price is too low turn signalToSell to false")
signalToSell = False
'''
Could be more simple but testing so keep separate other wise integrate with whats above if block
'''
# exit as soon as you surpass average price
if positionStatus != None:
enteredPrice = Decimal(positionStatus['average_price'])
if positionStatus['direction'] == "sell":
if meanPriceFromQuotes < (enteredPrice):
signalToBuy = True
elif positionStatus['direction'] == "buy":
if meanPriceFromQuotes > (enteredPrice):
signalToSell = True
# exit as soon as you surpass average price
"""
hints at possible loses
"""
# exit order quickly
if positionStatus != None:
enteredPrice = Decimal(positionStatus['average_price'])
# shorting
if positionStatus['direction'] == "sell":
if meanPriceFromQuotes > (enteredPrice + self.limitToExitPosition):
'''
if orders of the same position are still there then we do not send out a emergency signal
but RETURNS TRUE IF ORDER IS PRESENT
'''
signalToBuy = not self.checkIfAnyOrdersAreStillPresent(positionStatus, orderManagementObject)
if signalToBuy:
print("buy out, Lossing sell")
# long
elif positionStatus['direction'] == "buy":
if meanPriceFromQuotes < (enteredPrice - self.limitToExitPosition):
'''
RETURNS TRUE IF ORDER IS PRESENT
'''
signalToSell = not self.checkIfAnyOrdersAreStillPresent(positionStatus, orderManagementObject)
if signalToSell:
print("sell out, Lossing buy")
return {"sell": signalToSell, "buy": signalToBuy}
# checks if orders are present on our current position thus moving the average price in our favour
def checkIfAnyOrdersAreStillPresent(self, positionStatus, orderManagementObject):
listOfPresentOrders = orderManagementObject.getOpenOrders(indiceName="BTC-PERPETUAL")
# no orders present
if len(listOfPresentOrders) == 0: return False
if positionStatus != None:
# going short
if positionStatus['direction'] == "sell":
# returns true if order is present
return self.findItemByLoopingThroughPresentOrders(listOfPresentOrders, "sell")
elif positionStatus['direction'] == "buy":
return self.findItemByLoopingThroughPresentOrders(listOfPresentOrders, "buy")
# finds if we still have orders on the same side as the position as it could fill later on increase/decreasing our average price
def findItemByLoopingThroughPresentOrders(self, listOfOrders, side):
for order in listOfOrders:
if order["direction"] == side:
return True
return False
# this means we are always going to enter orders, as the signal is always true
def generateSignal(self, mapOfDataPrice):
buySignal = True
sellSignal = True
return {"sell": sellSignal, "buy": buySignal}