% Sample Matlab callback for IB - used in conjunction with the IBMatlab function
%
% Usage example:
%    IBMatlab('Action','Query', ..., 'GenericTicklist','236', 'QuotesNumber',2, ...
%             'CallbackTickPrice',@IBMatlab_CallbackTickGeneric)
%
% See: http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/java/java_ewrapper_methods.htm
% ^^^  http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/java/tickgeneric.htm
%      http://www.interactivebrokers.com/php/apiguide/interoperability/activex_other/activexevents.htm#tickGeneric
%
% Using Generic Ticks in IB:
% http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/tables/generic_tick_types.htm
%
% Note: According to IB, "Generic Tick Tags cannot be specified if you elect to use the Snapshot market data subscription"
% ^^^^  This means that we must use the streaming-quotes mechanism => QuotesNumber>1
%
% Generic tick type values:
% http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm#apiguide/api/tick_values.htm%23XREF_tick_values_generic_tick
% http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/tables/tick_types.htm
% http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/tables/generic_tick_types.htm
%
% Using Shortable info in IB:
% http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/tables/using_the_shortable_tick.htm
%
% The full list of shortable stocks in IB (could change from time to time):
% http://investors.interactivebrokers.com/en/p.php?f=shortableStocks&p=s&ib_entity=ir
% http://investors.interactivebrokers.com/en/trading/ViewShortableStocks.php?cntry=usa&tag=United%20States&ib_entity=llc&ln= (=USA only, change cntry= for other countries)
function IBMatlab_CallbackTickGeneric(hObject, eventData, varargin)  %#ok unused

    % Only check the shortable tick type =46, according to 
    % http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm#apiguide/api/tick_values.htm%23XREF_tick_values_generic_tick
    if eventData.field == 46  % 46=Shortable

        % Get this event's tickerId (=orderId as returned from the original IBMatlab command)
        tickerId = eventData.tickerId;

        % Get the corresponding shortable value
        shortableValue = eventData.generic;

        % Now check whether the security is shortable or not
        title = sprintf('Shortable info for request %d', tickerId);
        if (shortableValue > 2.5)      % 3.0
            msgbox('There are at least 1000 shares available for a short sale', title, 'help');
        elseif (shortableValue > 1.5)  % 2.0
            msgbox('This contract will be available for short sale if shares can be located', title, 'warn');
        elseif (shortableValue > 0.5)  % 1.0
            msgbox('Not available for short sale', title, 'warn');
        else
            msgbox(sprintf('Unknown shortable value: %g',shortableValue), title, 'error');
        end
    end  % if shortable tickType

end  % IBMatlab_CallbackTickGeneric
