-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
positionsize.py
81 lines (57 loc) · 2.51 KB
/
positionsize.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
from decimal import Decimal
class PositionSize:
symbol = "USDJPY"
algorithm = None
bonus = 0
def __init__(self, algorithm, symbol, tenkan, kijun, senkou, period=15):
self.symbol = symbol
self.algorithm = algorithm
# Get POINT value for current asset
def get_point_value(self):
# lotSize = 1000
conversionRate = self.algorithm.Portfolio.Securities[self.symbol].QuoteCurrency.ConversionRate
# conversionRate = 1.31819(GBPUSD)
pointValue = self.get_lot_size() * conversionRate
# pointValue = 1318.19USD
return pointValue
# Get LOT size
def get_lot_size(self):
# E.g.: symbol = "EURGBP";
lotSize = self.algorithm.Portfolio.Securities[self.symbol].SymbolProperties.LotSize
return lotSize
def checkSLPosition(self, sl, type, data):
self.algorithm.Log("Check {0}".format(sl))
_bonus = self.bonus
_volume = Decimal(0.01) * self.get_wins_lose_multiplier()
position = self.get_point_value() * Decimal(sl)
lot = (self.algorithm.Portfolio.Cash * (_volume + Decimal(_bonus))) * self.get_lot_size() / position
orderQua = abs(self.algorithm.CalculateOrderQuantity(self.symbol, 38 * type))
#buiyng = self.algorithm.Portfolio.GetBuyingPower(self.symbol, OrderDirectiondirection = OrderDirection.Up)
if lot > orderQua:
return int(orderQua)
return int(lot)
# Get wins and loses multiplier from closed positions
def get_wins_lose_multiplier(self):
multiplier = 1
positionClosed = []
for position in self.positions:
if position.leave > 0:
positionClosed.append(position)
if len(positionClosed) > 0:
if positionClosed[-1].Result >= 0:
multiplier += 1
if len(positionClosed) > 1:
if positionClosed[-2].Result >= 0:
multiplier += 1
if len(positionClosed) > 2:
if positionClosed[-3].Result >= 0:
multiplier += 1
if positionClosed[-1].Result < 0:
multiplier -= 0.1
if len(positionClosed) > 1:
if positionClosed[-2].Result < 0:
multiplier -= 0.1
if len(positionClosed) > 2:
if positionClosed[-3].Result < 0:
multiplier -= 0.1
return multiplier