-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market.py
67 lines (55 loc) · 1.71 KB
/
Market.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
from OrderManeger import Orders
class Market:
def __init__(self,marketName,marketData):
self.activeOrders=list()
self.marketName=marketName
self.marketTick=0
self.simulationEnded=False
self.data = marketData
self.dataLen=len(marketData)-1
def orderCreate(self,action,lot,stopLoss,takeProfit):
orderID=self.marketTick
order=Orders(self.getCurrentMarketData(),action,lot,stopLoss,takeProfit,orderID)
self.activeOrders.append(order)
return orderID
def getCurrentMarketData(self):
return self.data[self.marketTick]
def getTotalMarketData(self):
return self.data
def closeOrder(self,orderID):
moneyDiff=0
currentValue=self.getCurrentMarketData()
for order in self.activeOrders:
if orderID==order.orderID:
moneyDiff=moneyDiff + order.closeOrder(currentValue)
self.deleteClosedOrder(order)
return moneyDiff
def closeAllOrders(self):
moneyDiff=0
currentValue=self.getCurrentMarketData()
for order in self.activeOrders:
moneyDiff=moneyDiff + order.closeOrder(currentValue)
self.deleteClosedOrder(order)
return moneyDiff
def inceraseSelfTick(self):
if self.marketTick<self.dataLen:
self.marketTick=self.marketTick+1
else:
self.endSimulation()
def marketTickReturnResult(self):
totalMoneyDiff=0
currentValue=self.getCurrentMarketData()
for order in self.activeOrders:
moneyDiff=order.checkIfReadyToClose(currentValue)
if moneyDiff is not None:
self.deleteClosedOrder(order)
totalMoneyDiff=totalMoneyDiff+moneyDiff
self.inceraseSelfTick()
if self.simulationEnded==False:
return totalMoneyDiff
else:
return 0
def endSimulation(self):
self.simulationEnded=True
def deleteClosedOrder(self,order):
self.activeOrders.remove(order)