How do I get a Function to run inside a program?

1 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2021-2-11
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.

请先登录,再进行评论。

采纳的回答

Allan Munro
Allan Munro 2021-3-4
Hi there, thanks for the info... where do I put in the program?
Anywhere?
  9 个评论
Walter Roberson
Walter Roberson 2021-3-6
unfortunately I do not have access to that broker facility. Have you asked Yair?
Allan Munro
Allan Munro 2021-3-6
I sent Yair a note yesterday. I am sure he knows what is going on.
The variable name issue is very confusing...

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by