Stuck on If Loops

To be clear, I have read the manual, and I still dont understand.

I am trying to write a code snippet that launches an external python file when a Buy or Sell signal occur.

I tried initially using

If (Close > Close MA(Close,200)
    ShellExecute("PowerShell.exe","py createOrder.py","C:\\Program Files\\AmiBroker\\#####\\Python",0);

Now, when I use
ShellExecute("PowerShell.exe","py createOrder.py","C:\Program Files\AmiBroker\#####\Python",0); by itself, I do execute the file. I just want this line of code to be executed only if a Buy or Sell condition is met.

Now I understand why that doesn't work as both conditions are arrays.
The documentation here suggests that we can use loops for this. I was thrown the same error, error 6. So, then I assigned the 2 conditions to a variable

Buy = C < MA(C,200) ;
Sell = 0;
ExRem(Buy,Sell);

condition1 = ValueWhen(Close > MA(Close,200),MA(Close,200),1);
condition2 = ValueWhen(Close > MA(Close,200),Close,1); 


for (i = 0; i<BarCount; i+0)
{
	if (condition1 > condition2)
	ShellExecute("PowerShell.exe","py createOrder.py","C:\\Program Files\\AmiBroker\\B##############\\Python",0);
}	

But again, I get error 6.

All I want to achieve, is to execute a file on a buy, or a sell condition being met. How should I achieve this? (I have tried the internetpostrequest function and I cannot get that to work with Binance, and I've abandoned that in my system, and in cany case, if I did need to do internetpost request, it would stillbe code to be executed upon certain conditions being met).

Thanks for help.

fix the increment

homework: how to access values at array index. you are still not iterating over the index

Thank for that.
I put the i+0 at the end of

for (i = 0; i<BarCount; i+0)

as I did not want it to loop to the next bar and place a second order. I wanted it only to reference one bar (during the testing phase)
My understanding is that if I write

for( i = 1; i < BarCount; i++ )

then the code would run from the first bar in the data set, all the way throught to the last bar int he data set, and launch the shellexecute code for each of those?

i+0 is worse.
If the FOR loop doesn't terminate, how will the code stop? You will go into an endless loop.
the correct syntax is i++ or i=i+2 or something like that.

You still didnt find in the Manual how to access specific values in the array. Do that first.

Thanks for that. I appreciate the guidance.
Are you referring to this article?
If not, do you happen to know off hand which function might I use for that?
Thanks

yes, right. See the last part of using [ ]

1 Like

Ah, ok, thanks. I'll read it in more detail this evening when I get home from work. Thanks for the pointer.

Ah, fine. It works! I understand it now. I didn't really understand it before. I knew what an array was but I'd only really given this article a cursory glance.

The code below launches my python buy script if close is > 200MA, but does NOT launch it if close is <200 MA, (provided the market I am trading on the timeframe I am trading is currently >200MA, which is exactly what I am after. Thanks)

condition = Close > MA(Close,200);
Buy = IIf(condition,1,0);
Sell = 0;
ExRem(Buy,Sell);
for( i = 1; i < BarCount; i++ )
{
	if (Buy[i] == 1 )
		{	
		ShellExecute("PowerShell.exe","py createOrder.py","C:\\Program Files\\AmiBroker###########\Python",0);
		}
}

Nope, I thought I understood, but I don't. My code, when I run it via an analysis, gets stuck in a loop until I interrupt it and continues to fire off multiple orders at the exchange.
Do I need to change

for( i = 1; i < BarCount; i++ )

so that it runs once, on the current bar, then exits, or do I need to do something else?

You do not need loop.

Entire Knowledge base is mandatory read for all newbies before coming to forum.

3 Likes