Need help/advice with array

Post Reply
gringoh

Need help/advice with array

Post by gringoh »

Hi guys,

I have a question which should be an easy one for good coders.

In “MPTM is trading multiple pairs”, I am trying to add a filter to avoid trading a selected list of pairs. I think it is possible to manage that with an extern string and an array which should look like this:

Code: Select all

extern string     ExcludedPairs ="AUDCAD,AUDCHF,AUDNZD, CADCHF, CHFJPY ";

… code to manage the excluded pairs as an array.
I tried several things but I was not able to do it, so at the moment my code looks like this:

Code: Select all

extern bool       ExcludePairs     = true;

extern string     ExclusePair101          ="AUDCAD";
extern string     ExclusePair102          ="AUDCHF";
extern string     ExclusePair103          ="AUDNZD";
extern string     ExclusePair104          ="CADCHF";
extern string     ExclusePair105          ="CHFJPY";

void Strategy(string symbol)
{
//...

                  if (ExcludePairs)
                  {
                     if (StringCompare(symbol, ExclusePair101)==0) CheckExcludePair = exclude;
                     if (StringCompare(symbol, ExclusePair102)==0) CheckExcludePair = exclude;
                     if (StringCompare(symbol, ExclusePair103)==0) CheckExcludePair = exclude;
                     if (StringCompare(symbol, ExclusePair104)==0) CheckExcludePair = exclude;
                     if (StringCompare(symbol, ExclusePair105)==0) CheckExcludePair = exclude;
                  }
 }
This code is a bit long, and I am sure it could be improved with an array.

Any help would be appreciated, for “MPTM is trading multiple pairs”, and for me to understand how to build and use arrays.

Thanks for your help.
Cheers,
Gringoh
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Need help/advice with array

Post by SteveHopwood »

My method will not be the best one, but I evolved it many years ago and use it ever since.

Look at SBB because this is typical.
extern string PairsToTrade = "AUDCAD,AUDCHF,AUDNZD,AUDJPY,AUDUSD,CADCHF,CADJPY,CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURNZD,EURJPY,EURUSD,GBPCHF,GBPJPY,GBPUSD,NZDUSD,NZDJPY,USDCAD,USDCHF,USDJPY";

holds the pairs the multi-trader trades. In your case it will be your excluded pairs.

I declare these three variables:
string InputString;//Holds the contents PairsToTrade for tidying yp etc
int NoOfPairs;// Holds the number of pairs passed by the user via the inputs screen
string TradePair[]; //Array to hold the pairs traded by the user

Then OnInit() contains the call to the function that sets up my trade pairs array:
ExtractPairs();

ExtractPairs() contains calls to some more functions that clean up the strings, calculate the number of pairs to be traded then resize any associated arrays.

It is not degree-standard code but it works and has done for years. Adapt it if nobody comes up with a better solution.

:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
gringoh

Need help/advice with array

Post by gringoh »

Thanks for your answer Steve.

Before to ask this question, I checked your code and tried to adapt it without success :oops:

I am going to try again and decompose your code step by step until I get it.

Cheers,
G
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Need help/advice with array

Post by renexxxx »

Hi Gringoh,

Using arrays can be quite fancy, but in your case this is much easier with a simple StringFind. I have attached some code to demonstrate.

Have fun.!
You do not have the required permissions to view the files attached to this post.
gringoh

Need help/advice with array

Post by gringoh »

Hi Rene,

Many thanks for your help :good:

Your function with arrays is exactly what I was looking for and is working perfectly :yahoo:

May I ask you some clarifications, just to understand exactly what this code is doing, I don’t want to stay stupid and use this code without understanding how it works.

Code: Select all

     // Remove spaces
     string inputString = ExcludedPairs;
     StringReplace(inputString," ","");
Clear

Code: Select all

     // Remove leading and trailing ','
     while ( StringFind(inputString,",",0) == 0 ) inputString = StringSubstr(inputString,1);
First, find commas, and then extract commas?

Code: Select all

    while ( StringFind(inputString,",",StringLen(inputString)-1) == StringLen(inputString)-1 ) inputString = StringSubstr(inputString,0,StringLen(inputString)-1);
This part is the one I really don't understand

Code: Select all

     // Split the clean-up inputString into an array of strings
     const ushort comma = StringGetCharacter(",",0);
     StringSplit(inputString,comma,pairsToExclude);
     //
     // Print the exclude pairs
     for(int iPair=0; iPair < ArraySize(pairsToExclude); iPair++) {
        PrintFormat("Excluded: %s", pairsToExclude[iPair] );
     }
     // Check is the given 'symbol' is excluded
     bool isExcluded = false;
     for(int iPair=0; iPair < ArraySize(pairsToExclude); iPair++) {
        if ( symbol == pairsToExclude[iPair] ) {
           isExcluded = true;
           break;
        }
Clear

Many thanks again.

Cheers,
G
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Need help/advice with array

Post by renexxxx »

Hi Gringoh,

The first two parts of the code is just to clean up the input parameter 'ExcludedPairs'. If some idiot provides an input parameter such as: ",,,,EURUSD, , GBPUSD, , GBPCAD,,,,,", the StringSplit() function will not produce the right pairs. So, I was attempting to make the code idiot-proof. In fact, I should have gone a few steps further and remove consecutive ",,", make it case-insensitive, remove rubbish (such as '-%&#@') etc etc. Otherwise you can get posts like the following:
Dimwit wrote:The code you have worked so hard on and I have looked at for 0.5 second, is rubbish! When I enter parameter 'xxx', your code crashes with the following message 'yyy'. You may like to know to I have chucked your code in the bin, and have dropped my respect for you by 200 points
Anyway, more specifically:

Code: Select all

     // Remove leading and trailing ','
     while ( StringFind(inputString,",",0) == 0 ) inputString = StringSubstr(inputString,1);
StringFind(inputString,',',0) -- means: Look for comma's in inputString, starting at position 0, and return the first position where you find one. If the first position is position 0, that means that the first character is a comma. So, strip of that comma (inputString = StringSubstr(inputString,1) This can also be written as:

Code: Select all

     while ( StringSubstr(inputString,0,1) == "," ) inputString = StringSubstr(inputString,1);

Code: Select all

    while ( StringFind(inputString,",",StringLen(inputString)-1) == StringLen(inputString)-1 ) inputString = StringSubstr(inputString,0,StringLen(inputString)-1);
Similarly, look for comma's in inputString, starting at the last position. If the first position you find a comma is the last position (where else?), strip of that last comma. This can also be written as:

Code: Select all

     while ( StringSubstr(inputString,StringLen(inputString)-1,1) == "," ) inputString = StringSubstr(inputString,0,StringLen(inputString)-1);
Hope this clarifies.

Cheers,
Rene
gringoh

Need help/advice with array

Post by gringoh »

Many thanks Rene,

All clear now :) and coded added to MPTM is trading multiple pairs :clap:

:good:
Post Reply

Return to “Coders Hangout”