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

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 个评论

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.

请先登录,再进行评论。

 采纳的回答

Hi there, thanks for the info... where do I put in the program?
Anywhere?

9 个评论

Put
Check_IB_Open_Positions
at the top of your program, or at the stage in your program where you are prepared to have the Open Positions checked.
Thanks.
It seems I am getting another error now.
Missing input parameter: Did you forget the 'Action' parameter? in IBMatlab() (IBMatlab.p line 0)
Any idea what that means?
Thanks
Allan
'action' is a name/value pair. It must be followed by one of Buy, Sell, SShort, SLong, Close, Exercise, Lapse, Query, Cancel, Account, Portfolio, History, Realtime, Scanner, Contract, Fundamental, License, Version, Disconnect .
However you have
openPositions = IBMatlab('action','symbol','portfolio'); pause(1)
which tries to pass 'symbol' instead.
'symbol' is a different name/value pair, that must be followed by the name of a stock symbol. It is unlikely that there is a stock with a symbol named 'portfolio' .
Hi Walter,
That solved that problem! Thank you very much.
Now in running the program I am getting another error.
When I get the program running I am getting an error in my array (around line 500) that my cell array variable names are incorrect.
I am getting the symbols from IBMatlab, then I need to send them to IQML to collect the data. I am getting an error in symbol or (position) part of the function. Any ideas here?
Thanks
Allan
I would suspect that
Closedata = IQML('history', 'Symbol', ClosenumSymbols, 'MaxItems',numBars, 'Days',9, ...
'DataType','interval', 'IntervalSize',barSize, 'Timeout',180, ...
'MsgParsingLevel',1, 'Fields','High,Low,Close,Datenum', 'Debug',0, ...
'RaiseErrorMsgs',false, 'UseParallel',true);
should be
Closedata = IQML('history', 'Symbol', Closesymbols, 'MaxItems',numBars, 'Days',9, ...
'DataType','interval', 'IntervalSize',barSize, 'Timeout',180, ...
'MsgParsingLevel',1, 'Fields','High,Low,Close,Datenum', 'Debug',0, ...
'RaiseErrorMsgs',false, 'UseParallel',true);
It does not make sense to me to pass the number of symbols there instead of the symbols.
Hi Walter,
Yeah, I'm still getting errors.
What seems to be the trouble is the 'symbols' collected from the IB call are being collected in a format that the call IQML can not read.
There a few commands in the code to turn them into a gervarname and do a few other things, and to restucture the array. It is beyond my knowledge to fix this last issue.
I am using two programs for Matlab to run this program. IBMatlab which is a program to collect data from IB (Interactive Brokers) and another program , IQML-which collects data for stocks from IQFeed.
Everything in the program workes except for this last part. Where the program goes to IB to check for any open positions, then take those stock symbols and collect the last 11 bars of data from IQfeed, and if the logic says, sell the stock, it then places a sell order.
I have gone through all the documentation I have, and tried to replicate what was coded in the top part of the program which does a similar function, however I am getting stuck with a variety of errors after each fix on this section.
Thanks for your help though, your time was very generous.
Allan
Walter, taking out the or changing the Closesymbols part didn't work. There is something going on to change the stock names into better variable names used in the array.
I tried many variations to fix it, but lept getting a different error each go around. I am lost. : (
unfortunately I do not have access to that broker facility. Have you asked Yair?
I sent Yair a note yesterday. I am sure he knows what is going on.
The variable name issue is very confusing...

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by