How do I get a Function to run inside a program?
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Allan Munro
 el 11 de Feb. de 2021
  
    
    
    
    
    Comentada: Allan Munro
 el 6 de Mzo. de 2021
            Hi, I am missing something in getting this function to run.  Functions are new to me.  This is a function in a bigger program of mine but it isn't executing.  Thanks for the help.
Allan
function Check_IB_Open_Positions
    fprintf('************************************** \n'); 
    fprintf('Checking IB Positions for Sell Signals \n');
    fprintf('************************************** \n'); 
    % 1. Check IB For any existing LONG Position and check for a close Signal
    % Get Stock Name
    % Get marketPrice
    % Get averagePrice
    % Get Quantity
    % 2. Check IQFEED for Latest Data and calculate the Stochastic
    %Perform Profit Check  If TRUE then complete 'Close Signal Check'
    openPositions = IBMatlab('action','symbol','portfolio'); pause(1)
    % Loop over all open positions (array of structs)
    for posIdx = 1 : length(openPositions)
        % Get the next open position
        thisPosition = openPositions(posIdx);  % thisPosition is a struct
        symbol = thisPosition.symbol;
        % Skip this position if it's not really open (position qty = 0)
        position = thisPosition.position;
        if position == 0, continue, end
        % Compare the position's reported market price and its average price.
        % If marketPrice > averageCost, proceed to CLOSE_LONG_POSITION_SIGNAL CONDITION
        marketPrice = thisPosition.marketPrice;
        averageCost = thisPosition.averageCost;
        closeLongPosition = marketPrice > averageCost;
        % If LongProfitCheck is TRUE the check stochastic for 'CLOSE_LONG_POSITION_SIGNAL'
        if closeLongPosition   % CLOSE_LONG_POSITION_SIGNAL CONDITION
            CloseLongStep4 = True;
            if CloseLongStep4
                % Closing Position at marketPrice +/- some extra percent
                % (depending on whether the position is long or short)
                factor = 1 + 0.0001*sign(position);
                Close_Limit_Price = round(marketPrice * factor, 2);
                %fprintf('******** CLOSING EXISTING LONG POSITION ******** \n');
                fprintf('%-5s => closing open position (%g) at $%.2f\n', symbol, position, Close_Limit_Price);
                IBMatlab('action','CLOSE', 'Symbol',symbol, 'type','LMT', 'LimitPrice',Close_Limit_Price);
            else
                % do nothing
            end
        end
    end
end
1 comentario
  Star Strider
      
      
 el 11 de Feb. de 2021
				No mention of how (or if) you are calling it.  
I would just call it in your calling script as: 
Check_IB_Open_Positions
and the see what it does.  
Note that: 
openPositions = IBMatlab('action','symbol','portfolio'); pause(1)
is sending ‘IBMatlab’ character vector arguments, not actual data of the action, symbol, and portfolio, (unless those are the data you want to send to it).  If you want to send it actual actions, symbols, and portfolios, those have to be arguments to ‘Check_IB_Open_Positions’ as well, since it will not pick them up from your workspace.  
See the documentation section on Function Basics for information on how functions work and how to call them.  
Respuesta aceptada
  Allan Munro
 el 4 de Mzo. de 2021
        9 comentarios
  Walter Roberson
      
      
 el 6 de Mzo. de 2021
				unfortunately I do not have access to that broker facility. Have you asked Yair?
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


