示例#1
0
def bet_size_dynamic(current_pos, max_pos, market_price, forecast_price, cal_divergence=10, cal_bet_size=0.95,
                     func='sigmoid'):
    """
    Calculates the bet sizes, target position, and limit price as the market price and forecast price fluctuate.
    The current position, maximum position, market price, and forecast price can be passed as separate pandas.Series
    (with a common index), as individual numbers, or a combination thereof. If any one of the aforementioned arguments
    is a pandas.Series, the other arguments will be broadcast to a pandas.Series of the same length and index.

    :param current_pos: (pandas.Series, int) Current position.
    :param max_pos: (pandas.Series, int) Maximum position
    :param market_price: (pandas.Series, float) Market price.
    :param forecast_price: (pandas.Series, float) Forecast price.
    :param cal_divergence: (float) The divergence to use in calibration.
    :param cal_bet_size: (float) The bet size to use in calibration.
    :param func: (string) Function to use for dynamic calculation. Valid options are: 'sigmoid', 'power'.
    :return: (pandas.DataFrame) Bet size (bet_size), target position (t_pos), and limit price (l_p).
    """
    # Create a dictionary of bet size variables for easier handling.
    d_vars = {'pos': current_pos, 'max_pos': max_pos, 'm_p': market_price, 'f': forecast_price}
    events_0 = confirm_and_cast_to_df(d_vars)

    # Calibrate w.
    w_param = get_w(cal_divergence, cal_bet_size, func)
    # Compute the target bet position.
    events_0['t_pos'] = events_0.apply(lambda x: get_target_pos(w_param, x.f, x.m_p, x.max_pos, func), axis=1)
    # Compute the break even limit price.
    events_0['l_p'] = events_0.apply(lambda x: limit_price(x.t_pos, x.pos, x.f, w_param, x.max_pos, func), axis=1)
    # Compute the bet size.
    events_0['bet_size'] = events_0.apply(lambda x: bet_size(w_param, x.f-x.m_p, func), axis=1)

    return events_0[['bet_size', 't_pos', 'l_p']]
示例#2
0
 def test_bet_size_dynamic_default(self):
     """
     Tests for successful execution using the default arguments of 'bet_size_dynamic', which are:
      average_active = False
      step_size = 0.0
     """
     # Setup the test DataFrame.
     dates_test = np.array([
         dt.datetime(2000, 1, 1) + i * dt.timedelta(days=1)
         for i in range(5)
     ])
     events_test = pd.DataFrame(data=[[25, 55, 75.50, 80.00],
                                      [35, 55, 76.90, 75.00],
                                      [45, 55, 74.10, 72.50],
                                      [40, 55, 67.75, 65.00],
                                      [30, 55, 62.00, 70.80]],
                                columns=['pos', 'max_pos', 'm_p', 'f'],
                                index=dates_test)
     # Calculate results.
     d_events = {col: events_test[col] for col in list(events_test.columns)}
     events_results = confirm_and_cast_to_df(d_events)
     w_param = get_w(10, 0.95, 'sigmoid')
     events_results['t_pos'] = events_results.apply(
         lambda row: get_target_pos(w_param, row.f, row.m_p, row.max_pos,
                                    'sigmoid'),
         axis=1)
     events_results['l_p'] = events_results.apply(lambda row: limit_price(
         row.t_pos, row.pos, row.f, w_param, row.max_pos, 'sigmoid'),
                                                  axis=1)
     events_results['bet_size'] = events_results.apply(
         lambda row: bet_size(w_param, row.f - row.m_p, 'sigmoid'), axis=1)
     df_result = events_results[['bet_size', 't_pos', 'l_p']]
     # Evaluate.
     self.assertTrue(
         df_result.equals(
             bet_size_dynamic(events_test['pos'], events_test['max_pos'],
                              events_test['m_p'], events_test['f'])))