-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimpleQuantTransition.py
More file actions
116 lines (90 loc) · 3.89 KB
/
Copy pathsimpleQuantTransition.py
File metadata and controls
116 lines (90 loc) · 3.89 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
import json
import logging
import pandas as pd
import sys
from simpleQuantConstants import *
from simpleQuantStrategyManager import SimpleQuantStrategyManager
from simpleQuantEvaluator import SimpleQuantEvaluator
from common.simpleQuantLogger import SimpleQuantLogger
class SimpleQuantTransition:
def __init__(self, jsonString):
self.name = jsonString['name']
self.strategyName = jsonString['strategyName']
self.object = jsonString['object']
self.duration = jsonString['duration']
self.id = jsonString['id']
self.objectData = pd.DataFrame()
self.strategy = SimpleQuantStrategyManager().getStrategyInstance(self.strategyName)()
self.loopIndex = 0
self.cashHistory= []
self.positionHistory = []
self.marketValue = []
def __call__(self):
# use transition id as topic
logger = SimpleQuantLogger(self.name, '127.0.0.1:4321')
logger.info('{name} is running {strategyName}'.format(name=self.name, strategyName=self.strategyName))
self.runStrategy()
evaluator = SimpleQuantEvaluator()
evaluator.performEvaluate(self)
report = evaluator.getReport()
logger.info('{name} is running {strategyName}....Done'.format(name=self.name, strategyName=self.strategyName))
return self.objectData.to_json(), json.dumps(report)
def getTransitionName(self):
return self.name
def getTransitionStrategyName(self):
return self.strategyName
def getTransitionObject(self):
return self.object
def getTransitionDuration(self):
return self.duration
def setTransitionObjectData(self, objectData):
self.objectData = objectData
def runStrategy(self):
self.initial()
decisionList = []
for currentData in self.objectData.itertuples(name='FeedData'):
decision = self.strategy.loop(currentData, decisionList)
self.performDecision(decision)
decisionList.append(decision)
self.loopIndex += 1
self.end()
def initial(self):
self.strategy.tearUp()
self.cashHistory.append(100000)
self.positionHistory.append(0)
def performDecision(self, decision):
# use next open price as deal price
price = self.getDealPrice()
# no available price
if price == 0:
decision.volumn = 0
currentCash = self.cashHistory[self.loopIndex]
currentPosition = self.positionHistory[self.loopIndex]
# market value depends on current cash and current object value
# current cash and current position was calcualted in previous loop
# for example, in day n, if decision was made for bid/ask,
# the cash and position wouldn't change till day n+1, because the decision was made by
# today's close price, that's means transaction can't happend till next day.
marketValue = currentCash + currentPosition * self.objectData.loc[self.loopIndex, 'close']
if decision.action == BID:
requiredCash = price * decision.volumn
if requiredCash < currentCash:
currentCash -= requiredCash
currentPosition += decision.volumn
elif decision.action == ASK:
if currentPosition - decision.volumn >=0:
gainCash = price * decision.volumn
currentCash += gainCash
if price != 0:
self.cashHistory.append(currentCash)
self.positionHistory.append(currentPosition)
self.marketValue.append(marketValue)
def getDealPrice(self):
priceIndex = self.loopIndex + 1
if priceIndex >= self.objectData.shape[0]:
return 0
else:
return self.objectData.loc[priceIndex, 'open']
def end(self):
self.strategy.tearDown()
self.objectData = self.objectData.assign(marketValue = self.marketValue)