@makhar88, as I wrote, the main goal of the previous post was to evaluate Chat-GPT's ability to spit out "correct" code. (By the way, when I provided documentation and some examples, I only had to request one change since the generated code included an "append" function on the matrix that doesn't exist...).
Below is something similar, without using the CBT, selecting # tickers to discard randomly (which seems closer to your goal).
I left the _TRACEF() functions to help understand the code.
// https://forum.amibroker.com/t/skip-random-symbols-from-watchlist/39544
#pragma maxthreads 1
maxToSkip = Param( "How many tickers to skip", 1, 0, 10, 1 );
wlnum = GetOption( "FilterIncludeWatchlist" );
watchlist = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
symbolCount = StrCount( watchlist, "," ) + 1;
// maxpos = symbolCount - maxToSkip;
maxpos = 12 - maxToSkip;
SetOption( "InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
RoundLotSize = 1;
SetOption( "AllowPositionShrinking", True );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 1 );
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = Open;
PositionScore = RSI();
dt = DateTime();
// relevant code to define tickers to skip
if( Status( "stocknum" ) == 0 )
{
_TRACEF( "Selected watchlist (%g) - %g symbols: %s", wlNum, symbolCount, watchlist );
StaticVarRemove( "Stocks_IDs_*" );
m = Matrix( symbolCount, 1, 0 );
// Fill the matrix and set some static variables
for( i = 0; i < symbolCount; i++ )
{
symbol = StrExtract( watchlist, i, ',' );
m[i][0] = i + 1; // add one since we will use negative numbers to indicate what to skip and -0 is the same as 0...
_TRACEF( "%g/%g - %s (ID: %g)", i + 1, symbolCount, symbol, m[i][0] );
StaticVarSet( "Stocks_IDs_" + symbol, i + 1 );
}
_TRACEF( "Matrix IDs: %s", MxToString( m ) );
// Shuffle the matrix
for( j = 0; j < symbolCount; j++ )
{
rndIdx = floor( mtRandom() * symbolCount ); // Generate random index
_TRACEF( "Random indexID: %g", rndIdx );
temp = m[j][0];
m[j][0] = m[rndIdx][0];
m[rndIdx][0] = temp; // Swap values
}
_TRACEF( "Shuffled matrix IDs: %s", MxToString( m ) );
for( j = 0; j < maxToSkip; j++ )
{
_TRACEF( "Will skip: [%s]", StrExtract( watchlist, m[j][0] - 1 ) );
m[j][0] = -m[j][0]; // flag as selected to be ignored - negate the ID
}
_TRACEF( "Shuffled ID with flags: %s", MxToString( m ) );
// Save the matrix
StaticVarSet( "Stocks_IDs_Matrix", m );
}
//// A FAKE SYSTEM - Replace with your rules
// Generate random signals using MA crossover
ShortMA = MA( Close, 20 );
LongMA = MA( Close, 50 );
// Entry signals
BuySignal = Cross( ShortMA, LongMA );
SellSignal = Cross( LongMA, ShortMA );
// Skipping the random selected tickers
symbol = Name();
symbolID = StaticVarGet( "Stocks_IDs_" + symbol );
canTrade = True;
_TRACEF( "Processing [%s] - ID: %g", symbol, symbolID );
// Retrieve the saved shuffled matrix
m = StaticVarGet( "Stocks_IDs_matrix" );
for( j = 0; j < symbolCount ; j++ )
{
mID = m[j][0];
if( mID == -symbolID )
{
_TRACEF( "----------------> Skipping trades for [%s] - ID: %g == %g", symbol, symbolID, mID );
canTrade = False;
break;
}
else if( mID == symbolID )
{
_TRACEF( "Ok to trade [%s] - ID: %g == %g", symbol, symbolID, mID );
break;
}
}
// Rest of code...
Buy = canTrade and BuySignal;
Sell = canTrade and SellSignal;
When done with debugging, remove/comment out the #pragma