ThinkOrSwim function equivalent in MQL - mql4

I have never heard of ThinkOrSwrim till yesterday when someone asked me to convert a ThinkOrSwim script to an MQL4 indicator.
A part of the code is as follows:
input length = 21;
input price = close;
input ATRs=1;
input trueRangeAverageType = AverageType.WILDERS;
def flag;
def EMA = ExpAverage(close, length);
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
I want to ask you to kindly check and let me know if my understanding is correct.
input ATRs=1; // This should be a multiplier for ATR, then I think I should give it a double
//type for more flexible control.
input trueRangeAverageType = AverageType.WILDERS;
//As far as I understood, wilders is the same as SMMA in MQL.
.
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
Here is the main piece of this code which I need your help with.
My understanding is as follows
ATRs ==>> Just a multiplier
I think the rest of this line is calculating the ATR, right?
If so, then I can see that I cannot simply convert this to iATR (in mql), because we are not able to choose MA Methode of ATR in mql4.
Then I think first I have to put the "True Range" of each bar in an array and then use this array as a price source to get the averages.
MQL4:
for(int i = 0; i < rates_total; i++)
{
data[i] = iATR(_Symbol, TF_1, 1, i);
}
for(int i = 0; i < limit; i++)
{
ExtBuffer[i] = iMAOnArray(data, 0, Inplenght, 0, InpMAMethod, i);
}
If I'm in the right way yet, Then I think the iATR period has to be 1, to have the TrueRange of each bar and not the average of the TrueRanges.
And then have the variable length (from thinkOrSwim inputs) as the period parameter for iMAOnArray.
I would appreciate any help with it.
Regards
Edit:
I forgot to ask you something,
why should the programmer who wrote this thinkscript code call this variable shift1?

Related

writing to flash memory dspic33e

I have some questions regarding the flash memory with a dspic33ep512mu810.
I'm aware of how it should be done:
set all the register for address, latches, etc. Then do the sequence to start the write procedure or call the builtins function.
But I find that there is some small difference between what I'm experiencing and what is in the DOC.
when writing the flash in WORD mode. In the DOC it is pretty straightforward. Following is the example code in the DOC
int varWord1L = 0xXXXX;
int varWord1H = 0x00XX;
int varWord2L = 0xXXXX;
int varWord2H = 0x00XX;
int TargetWriteAddressL; // bits<15:0>
int TargetWriteAddressH; // bits<22:16>
NVMCON = 0x4001; // Set WREN and word program mode
TBLPAG = 0xFA; // write latch upper address
NVMADR = TargetWriteAddressL; // set target write address
NVMADRU = TargetWriteAddressH;
__builtin_tblwtl(0,varWord1L); // load write latches
__builtin_tblwth(0,varWord1H);
__builtin_tblwtl(0x2,varWord2L);
__builtin_tblwth(0x2,varWord2H);
__builtin_disi(5); // Disable interrupts for NVM unlock sequence
__builtin_write_NVM(); // initiate write
while(NVMCONbits.WR == 1);
But that code doesn't work depending on the address where I want to write. I found a fix to write one WORD but I can't write 2 WORD where I want. I store everything in the aux memory so the upper address(NVMADRU) is always 0x7F for me. The NVMADR is the address I can change. What I'm seeing is that if the address where I want to write modulo 4 is not 0 then I have to put my value in the 2 last latches, otherwise I have to put the value in the first latches.
If address modulo 4 is not zero, it doesn't work like the doc code(above). The value that will be at the address will be what is in the second set of latches.
I fixed it for writing only one word at a time like this:
if(Address % 4)
{
__builtin_tblwtl(0, 0xFFFF);
__builtin_tblwth(0, 0x00FF);
__builtin_tblwtl(2, ValueL);
__builtin_tblwth(2, ValueH);
}
else
{
__builtin_tblwtl(0, ValueL);
__builtin_tblwth(0, ValueH);
__builtin_tblwtl(2, 0xFFFF);
__builtin_tblwth(2, 0x00FF);
}
I want to know why I'm seeing this behavior?
2)I also want to write a full row.
That also doesn't seem to work for me and I don't know why because I'm doing what is in the DOC.
I tried a simple write row code and at the end I just read back the first 3 or 4 element that I wrote to see if it works:
NVMCON = 0x4002; //set for row programming
TBLPAG = 0x00FA; //set address for the write latches
NVMADRU = 0x007F; //upper address of the aux memory
NVMADR = 0xE7FA;
int latchoffset;
latchoffset = 0;
__builtin_tblwtl(latchoffset, 0);
__builtin_tblwth(latchoffset, 0); //current = 0, available = 1
latchoffset+=2;
__builtin_tblwtl(latchoffset, 1);
__builtin_tblwth(latchoffset, 1); //current = 0, available = 1
latchoffset+=2;
.
. all the way to 127(I know I could have done it in a loop)
.
__builtin_tblwtl(latchoffset, 127);
__builtin_tblwth(latchoffset, 127);
INTCON2bits.GIE = 0; //stop interrupt
__builtin_write_NVM();
while(NVMCONbits.WR == 1);
INTCON2bits.GIE = 1; //start interrupt
int testaddress;
testaddress = 0xE7FA;
status = NVMemReadIntH(testaddress);
status = NVMemReadIntL(testaddress);
testaddress += 2;
status = NVMemReadIntH(testaddress);
status = NVMemReadIntL(testaddress);
testaddress += 2;
status = NVMemReadIntH(testaddress);
status = NVMemReadIntL(testaddress);
testaddress += 2;
status = NVMemReadIntH(testaddress);
status = NVMemReadIntL(testaddress);
What I see is that the value that is stored in the address 0xE7FA is 125, in 0xE7FC is 126 and in 0xE7FE is 127. And the rest are all 0xFFFF.
Why is it taking only the last 3 latches and write them in the first 3 address?
Thanks in advance for your help people.
The dsPIC33 program memory space is treated as 24 bits wide, it is
more appropriate to think of each address of the program memory as a
lower and upper word, with the upper byte of the upper word being
unimplemented
(dsPIC33EPXXX datasheet)
There is a phantom byte every two program words.
Your code
if(Address % 4)
{
__builtin_tblwtl(0, 0xFFFF);
__builtin_tblwth(0, 0x00FF);
__builtin_tblwtl(2, ValueL);
__builtin_tblwth(2, ValueH);
}
else
{
__builtin_tblwtl(0, ValueL);
__builtin_tblwth(0, ValueH);
__builtin_tblwtl(2, 0xFFFF);
__builtin_tblwth(2, 0x00FF);
}
...will be fine for writing a bootloader if generating values from a valid Intel HEX file, but doesn't make it simple for storing data structures because the phantom byte is not taken into account.
If you create a uint32_t variable and look at the compiled HEX file, you'll notice that it in fact uses up the least significant words of two 24-bit program words. I.e. the 32-bit value is placed into a 64-bit range but only 48-bits out of the 64-bits are programmable, the others are phantom bytes (or zeros). Leaving three bytes per address modulo of 4 that are actually programmable.
What I tend to do if writing data is to keep everything 32-bit aligned and do the same as the compiler does.
Writing:
UINT32 value = ....;
:
__builtin_tblwtl(0, value.word.word_L); // least significant word of 32-bit value placed here
__builtin_tblwth(0, 0x00); // phantom byte + unused byte
__builtin_tblwtl(2, value.word.word_H); // most significant word of 32-bit value placed here
__builtin_tblwth(2, 0x00); // phantom byte + unused byte
Reading:
UINT32 *value
:
value->word.word_L = __builtin_tblrdl(offset);
value->word.word_H = __builtin_tblrdl(offset+2);
UINT32 structure:
typedef union _UINT32 {
uint32_t val32;
struct {
uint16_t word_L;
uint16_t word_H;
} word;
uint8_t bytes[4];
} UINT32;

Barcode4j - Generate check digit in an EAN13 barcode

When yo generate a barcode with Barcode4j as an image, you could obtain the human readable text, too, for instance:
EAN13 barcode example
In this picture we can see that the human readable text is: 1000000012026
In this example the barcode has been generated with the code 100000001202 and the number 6 is the check digit added by Barcode4j generator.
So, my question is: Is possible obtain the check digit of an EAN13 generated barcode with Barcode4j? Because I know how to render this as a image, but I don't know how to obtain the human readable text, as a plain text.
Regards,
Miguel.
Thanks to Barcode4j plugin, you can calculate the checksum with the barcode format you need. In Java 7, you can calculate the checkSum as this way:
private String calculateCodeWithcheckSum(String codigo){
EAN13Bean generator = new EAN13Bean();
UPCEANLogicImpl impl = generator.createLogicImpl();
codigo += impl.calcChecksum(codigo);
return codigo;
}
First, you need the EAN13 barcode format, so you can get the class that the plugin provides you, and call its only method: createLogicImpl().
This method is used to give you a class of type UPCEANLogicImpl.
This is the class you need, because you can find in it the method to calculate the checkSum. So, you only have to call the method calcChecksum giving your code (100000001202), and will give you the checkSum value (6).
You can check it in the next web: http://www.gs1.org/check-digit-calculator
Adding your code and the checkSum value will give you the value you need (1000000012026)
In case you what to compute the check digit without importing a java library here's the code:
public static String ean13CheckDigit(String barcode) {
int s = 0;
for (int i = 0; i < 12; i++) {
int c = Character.getNumericValue(barcode.charAt(i));
s += c * ( i%2 == 0? 1: 3);
}
s = (10 - s % 10) % 10;
barcode += s;
return barcode;
}

Count of the biggest bin in histogram, C#, sharp

I want to make histogram of my data so, I use histogram class at c# using MathNet.Numerics.Statistics.
double[] array = { 2, 2, 5,56,78,97,3,3,5,23,34,67,12,45,65 };
Vector<double> data = Vector<double>.Build.DenseOfArray(array);
int binAmount = 3;
Histogram _currentHistogram = new Histogram(data, binAmount);
How can I get the count of the biggest bin? Or just the index of the bigest bin? I try to get it by using GetBucketOf but to do this I need the element in this bucket :(
Is there any other way to do this? I read the documentation and Google and I can't find anything.
(Hi, I would use a comment for this but i just joined so today and don't yet have 50 reputation to comment!) I just had a look at - http://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Histogram.htm. That documentation page (footer says it was built using http://docu.jagregory.com/) shows a public property named Item which returns a Bucket. I'm wondering if that is the property you need to use because the automatically generated documentation states that the Item property "Gets' the n'th bucket" but isn't clear how the Item property acts as an indexer. Looking at your code i would try _currentHistogram.Item[n] first (if that doesn't work try _currentHistogram[n]) where you are iterating the Buckets in the histogram using something like -
var countOfBiggest = -1;
var indexOfBiggest = -1;
for (var n = 0; n < _currentHistogram.BucketCount; n++)
{
if (_currentHistogram.Item[n].Count > countOfBiggest)
{
countOfBiggest = _currentHistogram.Item[n].Count;
indexOfBiggest = n;
}
}
The code above assumes that Histogram uses 0-based and not 1-based indexing.

NSInteger cannot be used twice in same multiplication [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am stumped! I am using NSinteger but as all my inputs are integers and the results are therefore integers I can't see the problem that people doing division are having due to rounding.
I've looked in the developer guide for NSInteger and cannot find a warning.
And my searching of this site and google hasn't yielded results.
As I state at the end I can work around it, but I feel there is something simple I am missing.
All the variables are direct, no pointers yet in the first for loop, intergerOfInterest is 0, 1, 6, 7, 4, 5, 10, 11 which is very random, I have tried swapping integerValueA^2 for the calculation but with no effect. The second for loop however gives the correct answers, 6,12,20,30,2,56,72,90
for (loopCount = 0; loopCount < 8; loopCount++){
integerValueA = loopCount + 2;
integerValueB = integerValueA + 1;
intergerOfInterest = (integerValueA) * (integerValueA);
}
for (loopCount = 0; loopCount < 8; loopCount++){
integerValueA = loopCount + 2;
integerValueB = integerValueA + 1;
intergerOfInterest = (integerValueA) * (integerValueB);
}
There are several loops in the structure and the common factor between the correct number and the incorrect number is if the NSInteger is used more than once in a calculation. I figure this can't be right so does anyone have an idea how I am getting this wrong. Note: if I add an extra variable to store the second instance of the number (so in the first loop I use "(integerValueA) * (integerValueB - 1 )" it works fine.
Note: Edited to use naming conventions.
It sounds from the discussion in the comments that your original code looked something like this:
for(int i = 0; i < 8; i++) {
nVal = i ^ 2; // Supposed to be equivalent to nVal = i * i;
// Do something with nVal
}
The ^ operator in C is actually the bit-wise XOR operator, not the exponent operator. The above code takes i, flips bit 1, and assigns the result to nVal.
You want to use either of the following:
// Option 1
for(int i = 0; i < 8; i++) {
nVal = i * i;
// Do something with nVal
}
// Option 2
for(int i = 0; i < 8; i++) {
nVal = pow(i, 2);
// Do something with nVal
}
Is it possible you have a spelling error on your variable InteRgerOfInterest instead of IntEgerOfInterest ?
GH

Getting the index of an array element of EDT Dimension

I need to write a job where i could fetch the index of an array element of EDT Dimension
e.g. In my EDT Dimension i have array elements A B C when i click over them for properties I see the index for A as 1, B as 2 and C as 3. Now with a job ui want to fetch the index value. Kindly Assist.
I'm not sure if I did understand the real problem. Some code sample could help.
The Dimensions Table has some useful methods like arrayIdx2Code.
Maybe the following code helps:
static void Job1(Args _args)
{
Counter idx;
Dimension dimension;
DimensionCode dimensionCode;
str name;
;
for (idx = 1; idx <= dimof(dimension); idx++)
{
dimensionCode = Dimensions::arrayIdx2Code(idx);
name = enum2str(dimensionCode);
// if (name == 'B') ...
info(strfmt("%1: %2", idx, name));
}
}
I found a way but still looking if there is any other solution.
static void Job10(Args _args)
{
Dicttype dicttype;
counter i;
str test;
;
test = "Client";
dicttype = new dicttype(132);//132 here is the id of edt dimension
for (i=1;i<=dicttype.arraySize();i++)
{
if ( dicttype.label(i) == test)
{
break;
}
}
print i;
pause;
}
Array elements A B C from your example are nothing else but simple labels - they cannot be used as identifiers. First of all, for user convenience the labels can be modified anytime, then even if they aren't, the labels are different in different languages, and so on and so forth.
Overall your approach (querying DictType) would be correct but I cannot think of any scenario that would actually require such a code.
If you clarified your business requirements someone could come up with a better solution.

Resources