Is there a way to set a condition that includes a list of currency symbol pairs and be alerted when that condition is met? What function would you use to list the symbols? Thanks in advance. The following code gives you a rough idea of what i'm trying to do.
for(i=0;i<Bars; i++)
{
//Want a list of symbols to scan multiple currency pairs for the following condition
if(Symbol("EURUSD";"AUDCAD";"USDJPY")Close[i+1]>Close[i+2])
{
a=a+1;
//This is what I want to happen if condition is met
Alert(Symbol()+" 1");
}
You can use SymbolsTotal and SymbolName to get a list of all the symbols in Market Watch. The following code should give you a start (although it will be permanently giving alerts, I think you need to check exactly what you want alerts for) .
for(int i=0; i<SymbolsTotal(true); i++)
{
string currencySymbol=SymbolName(i,true);
int a=0;
for(j=0; j<iBars(currencySymbol,0); j++)
{
if(iClose(currencySymbol,0,j)>iClose(currencySymbol,0,j+1)) a++;
{
if(a>0) Alert(StringConcatenate(currencySymbol,":",IntegerToString(a)));
}
Related
This is a pretty simple concept, but I'm not getting the results I'm wanting. I have an NSMutableArray that is populated with NSArrays, I want to loop through that NSMutableArray and remove certain NSArrays based on a key-value pair. My results have many of the NSArrays that I should be removing and I think it has something to do with the count of the NSMutableArray and the int I declare in the For Loop.
Here is my code: (restArray is the NSMutableArray)
for (int i=0; i<restArray.count; i++) {
NSArray *array = restArray[i];
if ([[array valueForKey:#"restaurant_status"] isEqualToString:#"0"]) {
[restArray removeObjectAtIndex:i];
}
}
Does someone know what I am doing wrong here?
It is not recommended to modify an array on what are you currently iterating.
Lets create a tmp array, and reverse your logic.
NSMutableArray * tmpArray = [NSMutableArray array];
for (int i=0; i<restArray.count; i++) {
NSArray *array = restArray[i];
if (![[array valueForKey:#"restaurant_status"] isEqualToString:#"0"] {
[tmpArray addObject:array];
}
}
So at the end of the iteration, you should end up with tmpArray having the arrays you needed.
Use NSPredicate:
NSArray *testArray = #[#{#"restaurant_status" : #"1"}, #{#"restaurant_status" : #"0"}];
NSArray *result = [testArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(restaurant_status == %#)", #"1"]];
When you remove an element all the elements past it shift down by one, e.g. If you remove the element at index 3 then the element previously at index 4 moves to index 3.
Every iteration you increase the index by one.
Combine the above two and you see that when you remove an element your code skips examining the following element.
The simple solution is to reverse the order of the iteration:
for (int i = restArray.count - 1; i >= 0; i--)
and then your algorithm will work.
Addendum
You can safely ignore this addendum if your arrays contain < 2^32 elements and you use Clang or GCC (and most other C compilers).
It has been raised in the comments that this answer has a problem if the array has 0 elements in it. Well yes & no...
First note that the code in the question is technically incorrect: count returns an NSUInteger which on a 64-bit machine is a 64-bit unsigned integer, the loop variable i is declared as an int which is 32-bit signed. If the array has more than 2^31-1 elements in it then the loop is incorrect.
Most people don't worry too much about this for some reason ;-) But let's fix it:
for (NSInteger i = restArray.count - 1; i >= 0; i--)
Back to the problem with an empty array: in this case count returns unsigned 0, C standard arithmetic conversions convert the literal 1 to unsigned, the subtraction is done using modular arithmetic, and the result is unsigned 2^64-1.
Now that unsigned value is stored into the signed i. In C converting from signed to unsigned of the same type is defined to be a simple bit-copy. However converting from unsigned to signed is only defined if the value is within range, and implementation defined otherwise.
Now 2^64-1 is greater than the maximum signed integer, 2^32-1, so the result is implementation defined. In practice most compilers, including Clang and GCC, choose to use bit-copy, and the result is signed -1. With this the above code works fine, both the NSInteger and the int (if you've less than 2^32-1 elements in your array) versions.
What the comments raise is how to avoid this implementation-defined behaviour. If this concerns you the following will handle the empty array case correctly with ease:
for (NSUInteger i = restArray.count; i > 0; )
{
i--; // decrement the index
// loop body as before
}
If the array is empty the loop test, i > 0, will fail immediately. If the array is non-empty i, being initialised to the count, will start as one greater than the maximum index and the decrement in the loop will adjust it - effectively in the loop test i contains the number of elements left to process and in the loop body after the decrement contains the index of the next element to process.
Isn't C fun (and mathematically incorrect by definition)!
I have an NSArray filled with only NSStrings
I understand that to iterate over a NSArray of n elements, all I have to do is use for (NSString *element in arrayOfElements). However, I was wondering if there is specific function that will perform a comparison between every string element in the array with each other. For example, if I have the array:
[#"apple", #"banana", #"peach", #"kiwi"],
how would I do the comparison so apple is compared to banana, peach and then kiwi; and then banana is against peach and wiki, and finally peach is against kiwi?
Try using nested for loops, ex:
for (int i = 0 ; i < array.count ; i ++) {
for (int j = i + 1 ; j < array.count ; j ++) {
// compare array[i] to array [j]
}
}
Edit: And although wottle's suggestion would work, I'd recommend mine in this case, since it won't waste iterations going over the same comparisons multiple times. What I've done in this algorithm by setting j = i + 1 is compare each element in the array only to the ones after it.
Given "the array will not have any duplicates, and every NSString will be unique" this sounds like a great case for using NSSet classes instead of NSArray. NSMutableSet provides:
minusSet:
Removes each object in another given set from the receiving
set, if present.
and
intersectSet:
Removes from the receiving set each object that isn’t a
member of another given set.
I'm not sure which operation you're looking for but it sounds like one of those should cover your exact use case.
What you're trying to do is a bit beyond what custom comparators were meant to do. Typically when you have a list and you want to run a custom comparator, you're doing it to sort the list. You seem to want to do some specific action when certain items in the list compare to others, and for that, I think a loop within a loop is your best bet. It won't be very good performance, so hopefully you are not expecting a large array:
-(void) compareArrayToSelf
{
NSArray *array=#[#"apple", #"bananna", #"peach", #"kiwi"];
for( NSString *value1 in array)
{
for( NSString *value2 in array)
{
if( ![value1 isEqualToString:value2] && [self compareArrayValue:value1 toOtherValue:value2])
{
//Do something with either value1 or value2
}
}
}
}
I've google and i can't find the exact closest thing to the function i'm seeking.
So this is the idea of the generating message on the iOS apps.
This just randomly generate random number depend on the length
// Generates alpha-numeric-random string
- (NSString *)genRandStringLength:(int)len {
static NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: #"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
return randomString;
}
What i'm seeking is for the apps to generate random messages store in the array list depending on the settled time.
This is how i want it to work
static NSString *letters = #"Hello all","You're awesome","This is awkward","Are you sleeping?";
I need help to generate random messages in the string of arrays . Thank you in advance
This is not a way of doing this certain task. you have to make one array with most of word instad of punting single string with all alphabet character And create random sentence. This is logical and programmatic task xcode not generate random word it self. you have to do some logical stuff your self.
Here i found one github example code please check Bellow you got idea how does you can achieve this task. Hope you getting some idea with this example.
https://github.com/dav/Objective-C-Lorem-Ipsum-Generator
I know these iterator questions have been asked and answered a thousand times, however when I compile the following code, I still get this error:
error C2440: 'initializing' : cannot convert from
'std::_Vector_iterator<_Myvec>' to 'cv::Vector<_Tp> *'
.
void iterate(vector<vector<cv::Point> >& contours){
Vector<Vector<cv::Point>>::iterator it = contours.begin();
for( int i = 0; i< contours.size(); i++ ){
if(contourArea(contours[i])>1000){
it++;
}else{
contours.erase(it);
}
}
}
I don't see anything wrong with this code. The template type of the vector for the new iterator is the same like the vector I'm getting the iterator from.
Can not convert errors are generally very literal. Often the types used can give hints where in your code somethng is wrong.
Here we have the use of uppercase Vector class to define your iterator, and you are assigning a lowercase Vector class to it.
Note you would probably have gotten a different error if Vector was not a real class.
I am new to iOS and need some help.
I am working on a project,
where i need to do a search in a nsmutable array which hold thousands of character which combination of different as well as same characters and i need to do a search of any word and if word found it should be the set equidistant.As a example. Suppose we have set of characters like,
ASDFGFDSRFCGFRDHUJKIHTGRDFESERTGADERWFSGETWGHWGDHWDGWDGOHARHDKHRSGDGHSVDFHDJJDKSKWREFGHUJDMSJDHJGDGAFGREFGSDSGDHHDHAHDJSGAFDHWRODRGKDHDHDGRGFDCVEDDSGDGDHDHNDHJDJSHGD.
and if we search "WORK" from the above set characters. Then it should show the results as darken the area above.
Distance between the word is 2.
Distance between the word is 1.
Rahul,I come upto this,go further developing and compairing.
This may help you:
NSString *characterstring=#"ASDFGFDSRFCGFRDHUJKIHTGRDFESERTGADERWFSGETWGHWGDHWDGWDGOHARHDKHRSGDGHSVDFHDJJDKSKWREFGHUJDMSJDHJGDGAFGREFGSDSGDHHDHAHDJSGAFDHWRODRGKDHDHDGRGFDCVEDDSGDGDHDHNDHJDJSHGD";
NSString *cmpString=#"WORK";
int s;
int p;
for (int j=0; j<cmpString.length; j++) {
for (int i=0; i<characterstring.length; i++) {
char b=[characterstring characterAtIndex:i];
if(b ==[cmpString characterAtIndex:j])
{
s=i;
}
if (j<cmpString.length-1) {
if(b ==[cmpString characterAtIndex:j+1])
{
p=i;
if (i+(p-s)<characterstring.length-1) {
if([characterstring characterAtIndex:i+(p-s)] ==[cmpString characterAtIndex:j])
{
p=i;
NSLog(#"[characterstring characterAtIndex:i+(p-s)]=%c",[characterstring characterAtIndex:i+(p-s)]);
}
}
NSLog(#"====%c",b);
}
}
}
}