Iterating through a watchlist

I'm trying to build a system which runs a batch, runs a scan, adds the results to a watchlist, then fires a market order at the broker.

I have tried using various functions and I am unsure how to iterate through the watchlist (or even if I should be doing that.

I know I should trype some code here to display what I have done, but I'm embarrassed that I am just running orund in a vortex and have no idea how to achieve what I am trying to do.
Here's one of my many attempts.

tickers = Foreign("TestBuy", Name());
for (i = 0; i < BarCount; i++)
{
    ticker = StrExtract(tickers, i);
    printf("Ticker: %s", ticker);
    ibc.PlaceOrder(ticker + "-LSE-STK-GBP", "BUY", 1, "MKT", 0, 0, "DAY", False);
}

Igf anytone can point me in the right direction, or advise me something obvious I am missing, I'd be hugely grateful.

Your loop is running through the bars but you want it to run through the symbols. See the code on: AFL Function Reference - CATEGORYGETSYMBOLS

Also for auto trading, look for the articles posted on the old KB: AmiBroker Users' Knowledge Base

2 Likes

Thanks so much, Peter, that was helpful.
This works;

ibc = GetTradingInterface("IB");
if( ibc.IsConnected() )
{
	list = CategoryGetSymbols(categoryWatchlist,0,0);
	for( i = 0; ( symbol = StrExtract( list, i ) ) != ""; i++ )
	{

	ibc.PlaceOrder(symbol+"-LSE-STK-GBP", "BUY", 1, "MKT", 0, 0, "GTC", True );
	}
}

I note that the CategoryGetSymbols function takes a number for the watchlist. I tend to rename my watchlists to keep track of pending orders, open positions and which position is open in which position etc. I'm guessing I simply need to keep track of where that watchlist is in the order of watchlists. That's easy enough though. I'm happy with this solution, thanks.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.