How to change variable size(s) with timers without causing errors?

1 visualización (últimos 30 días)
BuMatlab
BuMatlab el 1 de Oct. de 2019
Comentada: Guillaume el 1 de Oct. de 2019
Hello -
I am trying to run a simple timer function which runs every 10 seconds to monitor position(s) in the stock market. More specifically, I have created a variable called tradeFlag, which initializes as an empty variable ([]) and takes the value 1 if a trade is executed. What I am trying to do is prevent another trade from being executed by using simple if statements, like below:
tradeFlag = [];
function timer(hObj, eventdata, tradeFlag)
if isempty(tradeFlag) & % a market event happens
% submit order
assignin('base','tradeFlag',1)
end
end
However when I run this function, I either get a "Not enough input arguments error" or or an else block will not be executed. Is there a better way to do this? What am I missing here?
Many thanks!!
  3 comentarios
BuMatlab
BuMatlab el 1 de Oct. de 2019
Hi Kumar -
Thanks for answering. My question can probably be answered without uploading the full code. My question is simply will the timer function handle, and will the code block in the timer function respond to, changing input variable size and value?
Guillaume
Guillaume el 1 de Oct. de 2019
In order to understand the not enough input argument error, we would need the full text of the error message and at the very least the code thar creates the timer (and its properties if it's done on several lines).
"or an else block will not be executed" For that we would need to see the full if...else block.
"What I am trying to do is prevent another trade from being executed" What does a trade being executed actually mean in terms of code?

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 1 de Oct. de 2019
It depends on how you set the timer object's TimerFcn property.
Rather than depending on a variable in the base workspace, based on the limited description of what you're trying to do I would probably store the data in the UserData property of the timer object (which is passed into its TimerFcn as the first input.)
  2 comentarios
BuMatlab
BuMatlab el 1 de Oct. de 2019
Editada: Guillaume el 1 de Oct. de 2019
Here is how I set the timer function properties:
% set timer to run stopLoss
tmrStopLoss = timer('ExecutionMode','FixedRate','Period',10,'TimerFcn',...
{@stopLoss, ACCOUNTS, faGroups, stopLossAmounts, symbolsToIgnore, tradeFlag, stopLossFlag});
startat(tmrStopLoss,year(today),month(today),day(today),06,31,10);
How would I go about updating the UserData property?
Thank you!
Guillaume
Guillaume el 1 de Oct. de 2019
The callback that you show above doesn't match at all the function in your question.

Iniciar sesión para comentar.

Categorías

Más información sobre Transaction Cost Analysis en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by