Unable to understand the following line of code : use of dist = 1.5*ATR(10); and inside for loop it is used as an array - algorithmic-trading

Here dist is a variable but inside the for loop its used as an array in afl programming
dist = 1.5*ATR(10);
for(i = 0;i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "Buy\n#" + C[ i ], i, L[ i ]-dist[i], colorGreen );
if( Sell[i] ) PlotText( "Sell\n#" + C[ i ], i, H[ i ]+dist[i], colorRed,
colorYellow );
}

ATR in amibroker to Calculates Average True Range indicator
more about atr
ATR function return an array.
dist = 1.5*ATR(10);
this line of code will return an array with each element multiply by 1.5

Related

Calculate and list how many different 4 digit numbers can be done with the numbers 0 to 9?

There is something I want to learn.
Let's say we have some single digit numbers.
Example: 1-2-3-4-5-6-7-8-9-0
Example II: 1-2-4-6-0
And with these numbers, we want to get 4-digit numbers that are different from each other.
And we want to print them as lists.
Result:
4676
4236
1247
1236
....
Is it possible to do this?
You can write and run a macro like this:
// retrieve the selected text
str = document.selection.Text;
// check the input string format. The input must be something like: "1-2-4-6-0"
if( str.length == 0 ) {
alert( "Select the input string" );
Quit();
}
for( i = 0; i < str.length; ++i ) {
c = str.substr( i, 1 );
if( i % 2 == 0 ) {
if( c < '0' || c > '9' ) {
alert( "not digit" );
Quit();
}
}
else {
if( c != '-' ) {
alert( "not separated by '-'" );
Quit();
}
}
}
var arr = new Array();
j = 0;
for( i = 0; i < str.length; ++i ) {
if( i % 2 == 0 ) {
c = str.substr( i, 1 );
arr[j++] = c;
}
}
if( arr.length < 4 ) {
alert( "Input string should contain at least 4 digits" );
Quit();
}
// list all 4-digit combinations
len = arr.length;
str = "";
for( i = 0; i < len; ++i ) {
for( j = 0; j < len; ++j ) {
for( k = 0; k < len; ++k ) {
for( l = 0; l < len; ++l ) {
str += arr[i] + arr[j] + arr[k] + arr[l] + "\r\n";
}
}
}
}
// write the list in a new document
editor.EnableTab = true;
editor.NewFile();
document.write( str );
To run this, save this code as, for instance, GenCombinations.jsee, and then select this file from Select... in the Macros menu. Finally, select Run GenCombinations.jsee in the Macros menu after selecting an input string.

Optimised EmEditor Macro to return Min/Max column lengths on large delimited data

I currently have large delimited data sets and I need to return the min\max lengths for each column.
I'm currently using the following code in Emeditor v20.3, which works great, but am wondering if there is a quicker way, particularly when there are million of lines of data and hundreds of columns (and this code is slow).
Any quicker approaches or ideas would that could be wrapped into a javascript macro would be much appreciated.
for( col = colStart; col <= MaxCol; col++ ) {
sTitle = document.GetCell( 1, col, eeCellIncludeNone );
min = -1;
max = 0;
for( line = document.HeadingLines + 1; line < MaxLines; line++ ) {
str = document.GetCell( line, col, eeCellIncludeQuotesAndDelimiter );
if( min == -1 || min > str.length ) {
min = str.length;
}
if( max < str.length ) {
max = str.length;
}
}
OutputBar.writeln( col + min + " " + max + " " + sTitle);
}
Please update EmEditor to 20.3.906 or later, and run this macro:
colStart = 1;
MaxCol = document.GetColumns();
document.selection.EndOfDocument();
yLastLine = document.selection.GetActivePointY( eePosCellLogical );
min = -1;
max = 0;
for( col = colStart; col <= MaxCol; col++ ) {
sTitle = document.GetCell( 1, col, eeCellIncludeNone );
document.selection.SetActivePoint( eePosCellLogical, col, 1 );
editor.ExecuteCommandByID( 4064 ); // Find Empty or Shortest Cell
y = document.selection.GetActivePointY( eePosCellLogical );
if( y < yLastLine ) { // check if not the last empty line
str = document.GetCell( y, col, eeCellIncludeQuotes );
min = str.length;
}
else { // if the last empty line
document.selection.SetActivePoint( eePosCellLogical, col, 1 );
editor.ExecuteCommandByID( 4050 ); // Find Non-empty Shortest Cell
y = document.selection.GetActivePointY( eePosCellLogical );
str = document.GetCell( y, col, eeCellIncludeQuotes );
min = str.length;
}
document.selection.SetActivePoint( eePosCellLogical, col, 1 );
editor.ExecuteCommandByID( 4049 ); // Find Longest Cell
y = document.selection.GetActivePointY( eePosCellLogical );
str = document.GetCell( y, col, eeCellIncludeQuotes );
max = str.length;
OutputBar.writeln( col + " : " + min + " " + max + " " + sTitle);
}

Time complexity of this algorithm? (Big O)

I am trying to figure out the time complexity of this code, as a part of my assignment. Here is how the code looks like:
public int solvePuzzle( )
{
searchAlg = BINARY_SEARCH ? new BinarySearch() : new LinearSearch();
int matches = 0;
for( int r = 0; r < rows; r++ )
for( int c = 0; c < columns; c++ )
for( int rd = -1; rd <= 1; rd++ )
for( int cd = -1; cd <= 1; cd++ )
if( rd != 0 || cd != 0 )
matches += solveDirection( r, c, rd, cd );
searchAlg.printStatistics();
return matches;
}
This method either uses a Binary Search or Linear Search.
My assignment ask me to find the time complexity of this in T(M,N) = O(?) where M is the size of a sorted dictionary which will be search using either linear of binary search, and N is the size of the "puzzle" (char [][]) where both array(Rows and Columns = N = same size).
This part matches += solveDirection( r, c, rd, cd ); uses either binary/linear search to search through a sorted array.
So far, this is what I've come up with.
The Time complexity of Binary search is Log M
The Time complexity of Linear Seach is M
The Time complexity of the first two for-loop's are N each.
But what is the Time complexity of the 3rd and 4th loop, and what is T(M,N) equal to?
Is it O(3) for the 3r of 4th loop? Does it mean that T(M,N) = O(M * N * N * 3 * 3)/O(logM * N * N * 3 * 3) ?
Any help would de appreciated.
EDIT: the code for solveDirection():
private int solveDirection( int baseRow, int baseCol, int rowDelta, int colDelta )
{
String charSequence = "";
int numMatches = 0;
int searchResult;
charSequence += theBoard[ baseRow ][ baseCol ];
for( int i = baseRow + rowDelta, j = baseCol + colDelta;
i >= 0 && j >= 0 && i < rows && j < columns;
i += rowDelta, j += colDelta )
{
charSequence += theBoard[ i ][ j ];
if ( charSequence.length() > maxWordLength )
break;
searchResult = searchAlg.search( theWords, charSequence );
if( searchResult == theWords.length ) { // corrected by UH 2007-05-02
// either linear searched failed or binary search failed because charSequence
// is larger than the largest word in theWords
if ( searchAlg instanceof BinarySearch )
break; // binary search failed and it makes no sense to extend charSequence any further
else
continue; // linear search failed but an extension of charSequence may succeed
}
// precondition: 0 <= searchResult < theWords.length
// At this point one, and only one, of three conditions holds:
// 1. Linear search succeeded
// 2. Binary search succeded
// 3. Binary search failed at the insertion point for charSequence,
// which means that theWords[ searchResult ] is the least element greater than charSequence
if( PREFIX_TESTING && ! theWords[ searchResult ].startsWith( charSequence ) )
break;
if( theWords[ searchResult ].equals( charSequence ) ) {
// if( theWords[ searchResult ].length( ) < 2 )
// continue;
numMatches++;
if ( PRINT_WORDS )
System.out.println( "Found " + charSequence + " at " +
baseRow + " " + baseCol + " to " + i + " " + j );
}
}
return numMatches;
}
You’re on the right track.
However, the key insight is that O(k) = O(1) for any constant k (= independent of the size of the input). As such, your O(N·N·3·3) is the same as O(N·N). And this result needs to be multiplied with the search, which you’ve done correctly.

Need Helping returning indices max sequence algorithm

I have the following algorithm which returns the sum of the greatest subsequence in an array. The greatest sum is in either in the left half, right half, or in the middle of the array(divide and conquer)... I can return the indices if it is through the middle. But if the subsequence is in the left or right half of the array, I can't figure out what they would be(recursion a bit confusing). I can define the indice's variables outside of the method if needed. Here is the method:
private static int maxSumRec( int [ ] a, int left, int right )
{
int maxLeftBorderSum = 0, maxRightBorderSum = 0;
int leftBorderSum = 0, rightBorderSum = 0;
int center = ( left + right ) / 2;
if( left == right ) // Base case
return a[ left ] > 0 ? a[ left ] : 0;
int maxLeftSum = maxSumRec( a, left, center );
int maxRightSum = maxSumRec( a, center + 1, right );
for( int i = center; i >= left; i-- )
{
leftBorderSum += a[ i ];
if( leftBorderSum > maxLeftBorderSum )
maxLeftBorderSum = leftBorderSum;
}
for( int i = center + 1; i <= right; i++ )
{
rightBorderSum += a[ i ];
if( rightBorderSum > maxRightBorderSum )
maxRightBorderSum = rightBorderSum;
}
return max3( maxLeftSum, maxRightSum,
maxLeftBorderSum + maxRightBorderSum );
}

Longest palindrome prefix

How to find the longest palindrome prefix of a string in O(n)?
Use a rolling hash. If a is your string, let ha[x] be the hash of the first x chars in a computed from left to right and let hr[x] be the hash of the first x characters in s computed from right to left. You're interested in the last position i for which hf[i] = hb[i].
Code in C (use two hashes for each direction to avoid false positives):
int match = n - 1;
int ha1 = 0, ha2 = 0, hr1 = 0, hr2 = 0;
int m1 = 1, m2 = 1;
for ( int i = 0; a[i]; ++i )
{
ha1 = (ha1 + m1*a[i]) % mod1;
ha2 = (ha2 + m2*a[i]) % mod2;
hr1 = (a[i] + base1*hr1) % mod1;
hr2 = (a[i] + base2*hr2) % mod2;
m1 *= base1, m1 %= mod1;
m2 *= base2, m2 %= mod2;
if ( ha1 == hr1 && ha2 == hr2 )
match = i;
}
Solution for a more general problem, not prefix but sub-string, in O(n) :
http://www.akalin.cx/2007/11/28/finding-the-longest-palindromic-substring-in-linear-time/
Second result on google for "longest palindrome prefix"....
Or solution using suffix-trees :
http://www.allisons.org/ll/AlgDS/Tree/Suffix/
Using z-algorithm (https://codeforces.com/blog/entry/3107). Suppose s is the given string of length m. Code:
string rev="",str=s;
int m=s.size(),longestPalindromicPrefix=1;
if(m==0 || m==1) longestPalindromicPrefix=m;
for(int i=m-1;i>=0;i--)
rev+=s[i];
s+='#';
s+=rev;
int n=s.size(),z[n+4],l=0,r=0;
for(int i=1;i<n;i++){
if(i>r){
l=r=i;
while(r<n && s[r-l]==s[r])
r++;
z[i]=r-l,r--;
}
else{
int k=i-l;
if(z[k]<r-i+1)
z[i]=z[k];
else{
l=i;
while(r<n && s[r-l]==s[r])
r++;
z[i]=r-l,r--;
}
}
}
for(int i=m+1;i<n;i++){
if(2*z[i]>=2*m-i && z[i]>longestPalindromicPrefix)
longestPalindromicPrefix=z[i];
}

Resources