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

1 Ansicht (letzte 30 Tage)
Allan Munro
Allan Munro am 11 Feb. 2021
Kommentiert: Allan Munro am 6 Mär. 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 Kommentar
Star Strider
Star Strider am 11 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by