| Conditions | 6 |
| Total Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """trade.Occurrence |
||
| 44 | def update_holder(self, holder): |
||
| 45 | """Udpate the Holder state according to the occurrence. |
||
| 46 | |||
| 47 | This implementation is a example of how a Occurrence object |
||
| 48 | can update the Holder state; this method should be overriden |
||
| 49 | by classes that inherit from the Occurrence class. |
||
| 50 | |||
| 51 | This sample implementation simply update the quantity and the average |
||
| 52 | price of the Subject in the Holder's possession every time objects |
||
| 53 | from this class are passed to Holder.trade(). |
||
| 54 | |||
| 55 | This sample implementation considers the following signature for |
||
| 56 | the Holder.state dict: |
||
| 57 | |||
| 58 | .. code:: python |
||
| 59 | |||
| 60 | { |
||
| 61 | "SUBJECT SYMBOL": { |
||
| 62 | "quantity": 0, |
||
| 63 | "value": 0 |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | And the following signature for the Occurrance.details dict: |
||
| 68 | |||
| 69 | .. code:: python |
||
| 70 | |||
| 71 | { |
||
| 72 | "quantity": 0, |
||
| 73 | "value": 0 |
||
| 74 | } |
||
| 75 | """ |
||
| 76 | |||
| 77 | subject_symbol = self.subject.symbol |
||
| 78 | |||
| 79 | # If the Holder already have a state regarding this Subject, |
||
| 80 | # update that state |
||
| 81 | if subject_symbol in holder.state: |
||
| 82 | |||
| 83 | # If the Holder have zero units of this subject, the average |
||
| 84 | # value paid/received for the subject is the value of the trade itself |
||
| 85 | if not holder.state[subject_symbol]['quantity']: |
||
| 86 | holder.state[subject_symbol]['value'] = self.details['value'] |
||
| 87 | |||
| 88 | # If the Holder owns units of this subject then the average value |
||
| 89 | # paid/received for the subject may need to be updated with |
||
| 90 | # this occurrence details |
||
| 91 | |||
| 92 | # If the occurrence have the same sign as the quantity in the Holder |
||
| 93 | # state, a new average value needs to be calculated for the subject |
||
| 94 | elif same_sign( |
||
| 95 | holder.state[subject_symbol]['quantity'], |
||
| 96 | self.details['quantity']): |
||
| 97 | holder.state[subject_symbol]['value'] = average_price( |
||
| 98 | holder.state[subject_symbol]['quantity'], |
||
| 99 | holder.state[subject_symbol]['value'], |
||
| 100 | self.details['quantity'], |
||
| 101 | self.details['value'] |
||
| 102 | ) |
||
| 103 | |||
| 104 | # If the occurrence does not have the same sign of the quantity in the |
||
| 105 | # Holder state, then do other stuff. |
||
| 106 | # A trade app would normally implement some sort of profit/loss logic |
||
| 107 | # here. |
||
| 108 | # This sample implementation only checks if the average value |
||
| 109 | # of the subject needs to be updated and then update it as needed. |
||
| 110 | else: |
||
| 111 | if same_sign( |
||
| 112 | self.details['quantity'], |
||
| 113 | holder.state[subject_symbol]['quantity'] + self.details['quantity']): |
||
| 114 | holder.state[subject_symbol]['value'] = self.details['value'] |
||
| 115 | |||
| 116 | # Update the quantity of the subject in the Holder's posession |
||
| 117 | holder.state[subject_symbol]['quantity'] += self.details['quantity'] |
||
| 118 | |||
| 119 | # If the Holder don't have a state with this occurrence's Subject, |
||
| 120 | # then register this occurrence as the first state of the Subject |
||
| 121 | # in the Holder's possession |
||
| 122 | else: |
||
| 123 | holder.state[subject_symbol] = { |
||
| 124 | 'quantity': self.details['quantity'], |
||
| 125 | 'value': self.details['value'] |
||
| 126 | } |
||
| 127 | |||
| 128 | # If the Holder knows about this Subject but don't have any unit |
||
| 129 | # of it, the paid value of the subject in the Holder state should |
||
| 130 | # be zero. |
||
| 131 | if not holder.state[subject_symbol]['quantity']: |
||
| 132 | holder.state[subject_symbol]['value'] = 0 |
||
| 133 | |||
| 142 |