def test_get_price_change(self): """ Make sure the price change is calculated correctly.""" # Copied a row from EUR_USD raw file. row = ['2006-07-20', '1.26278', '1.26953', '1.26234', '1.269'] + \ ['1.26293', '1.27015', '1.26249', '1.27', '13111'] price_change = util.get_price_change(row, 10000) # Check the calculation. self.assertEqual(price_change, '60.7') return
def transform_row(row, next_row, pip_factor): """ Return the transformed row with features and target variable. Args: row: list of Strings. A row from the raw data file representing today's candle. next_row: list of Strings. The row representing tomorrow's candle. pip_factor: int. The multiplier for calculating pip from price. Returns: data_point: list of floats. Combine the features and the target. """ features = list_to_features(row, pip_factor) target = util.get_price_change(next_row, pip_factor) data_point = features + [target] return data_point
def dry_run(self, pred, **kwargs): """ Do a dry run of strategy Euler as if the strategy was put in place. Return the day-to-day balance during the run, and produce a report on the profitability of this strategy over the given period. Args: pred: np.vector. Predicted daily price changes for days in the last part of the test data. kwargs: named arguments, including: print_result: boolean. Whether to print dry run report. export_plot: string. Name of the plot to be saved. Returns: balance: np.array. Accumulated profit/loss of every day. """ # Write report title. report = "\nDry run report: {0}\n\n".format(self.instrument) report += str(self.params) + '\n' report += '=' * 80 + '\n' # Initialize the balance. test_size = pred.size balance = np.zeros_like(pred) # Now go through the actual daily candles and figure out actual PL. data = self.test_data[-test_size:] for i in range(len(data)): # Fetch the row. row = data[i] # Fetch the predicted and actual price change. predicted = pred[i] actual = util.get_price_change(row, self.pip_factor) # Figure out the action we take and the units. units = self.parse_units(predicted) controls = self.parse_controls() # Calculate profit/loss for this day. p_l = util.get_profit_loss(row, units, **controls) # Update daily profit/loss balance[i] = balance[i - 1] + p_l # Add to the report. row[0] is date. report += util.format_row(row[0], units, predicted, actual, p_l) # Add final total profit/loss to the report. report += "Total profit/loss: {0}".format(balance[-1]) # Export the graphs if asked. if 'export_plot' in kwargs: # Do some plots. common.plot(balance, kwargs['export_plot']) # Print the report if asked. if 'print_result' in kwargs and kwargs['print_result']: print(report) return balance