def q_to_r_Qh(params, substep, state_history, prev_state, policy_input):
    """
    This function calculates and returns Q after a trade where delta_Q is the amount being sold into the pool
    """
    delta_Q = policy_input['q_sold'] #amount of Q being sold by the user
    Q = prev_state['Q']
    
    # JS July 9, 2021: compute threshold for reserve availability
    threshold = Q + delta_Q

    if delta_Q == 0 or threshold < 0:
        return ('Q', Q)
    else:
        return ('Q', Q + delta_Q)