@prithvibhat the function I posted here works, and @mradtke provided you a better (and faster) alternative in which you directly manipulate the entire array values without a loop over the individual elements (the resulting array values should be the same if both functions process digits that have the same precision).
In any case, once you have fixed the user-defined function, it is the rest of your formula that you need to double check: you used "Short" instead of what probably should have been "Sell".
Here below is a revised formula (that probably it is not yet what you want) but at least shows, in the exploration, the values you are using/calculating (and also works in backtest mode - even if such "magic numbers" based system seems not to be particularly profitable)
function SumDigitsLoop( price )
{
local i, j;
SumOfDigits = 0; // will return an array
// you need to process the input "price" array
for( j = 0; j < BarCount; j++ )
{
strVal = StrReplace( NumToStr( price[j] ), ".", "" );
StrLength = StrLen( strVal );
for( i = 0; i < StrLength ; i++ )
{
SumOfDigits[j] = SumOfDigits[j] + StrToNum( StrMid( strVal, i, 1 ) );
}
}
return SumOfDigits;
}
// https://forum.amibroker.com/t/getting-some-of-digits-of-price/7033/5
function SumDigits( price, precision )
{
// Round the price to 'precision' places after the decimal, and get rid of the decimal point
remainingDigits = round( price * ( 10 ^ precision ) );
sumDigs = 0;
while( LastValue( Cum( remainingDigits ) ) > 0 )
{
// Add the right most digit to the sum, and then move the remaining digits over
sumDigs += remainingDigits % 10;
remainingDigits = floor( remainingDigits / 10 );
}
return sumDigs;
}
prevLow = Ref( L, -1 );
prevHigh = Ref( H, -1 );
prevLowDigitSum = SumDigitsLoop( prevLow );
prevHighDigitSum = SumDigitsLoop( prevHigh );
prevLowDigitSum2 = SumDigits( prevLow, 2 ); // uset for comparison
prevHighDigitSum2 = SumDigits( prevHigh, 2 );
// Backtest options
maxpos = 10; // maximum number of open positions
SetOption( "InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
Buy = ( ( prevLowDigitSum % 11 ) == 0 );
Sell = ( ( prevHighDigitSum % 11 ) == 0 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
// Exploration
Filter = 1; // Buy OR Sell ;
AddColumn( IIf( Buy, 1, Null ), "Buy", 1 );
AddColumn( IIf( Sell, 1, Null ), "Sell", 1 );
AddColumn( C, "Close" );
AddColumn( L, "Low" );
AddColumn( prevLow , "Pr.L" );
AddColumn( prevLowDigitSum, "Pr.L.D.Sum Loop" );
AddColumn( prevLowDigitSum2, "Pr.L.D.Sum" );
AddColumn( H, "High" );
AddColumn( prevHigh, "Pr.H" );
AddColumn( prevHighDigitSum, "Pr.H.D.Sum Loop" );
AddColumn( prevHighDigitSum2, "Pr.H.D.Sum" );
SetSortColumns( -2 );
Here is an exploration result:
To better understand if your logic is correct I suggest using a single instrument for your explorations so you can see/verify the previous days' values. (Set the "Apply to" field to "Current").