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

조회 수: 2 (최근 30일)
Allan Munro
Allan Munro 2021년 2월 11일
댓글: Allan Munro 2021년 3월 6일
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by