Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Do Nothing Unless Symbol & Date Match

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Do Nothing Unless Symbol & Date Match

    Hi,

    I'm hoping to ask for some help please. My strategy runs against a basket of stocks each day, and each day the stocks within that basket change. E.g. on 12/27 the stocks may be ABST, ACB, AGC, while on 12/28 the stocks may be BCRX, BOWX, BRKL. Currently when I use the Strategy Analyzer to backtest the stocks, I am needing to run the test day by day, changing the date and instrument list each time (which is obviously a tedious exercise!).

    I'm hoping to be able to make this smoother and hoping for some help please/be pointed in the right direction. I'm imagining something along the lines of:
    1. Create an array (or list, or keypairs - not sure which is best) with date and stock symbols, akin to:

    Code:
    //Obviously not real code
    private array myArray;
    myArray = ({12-27-2021, ABST}, {12-27-2021, ACB}, {12-27-2021, AGC}, {12-28-2021, BCRX}, {12-28-2021, BOWX}, {12-28-2021, BRKL})
    2. At the start of OnBarUpdate(), add logic that checks if the current Symbol & Date are within the array. If No, then 'return', if yes, process the strategy as normal. Something akin to:

    Code:
    //Obviously not real code
    protected override void OnBarUpdate()
    {
    if ( 'Instrument.FullName Not within Array[1]' || 'Date Not within Array[0]' ) return; //not sure yet how to reference the date for the current bar.
    ...
    3. I can then create a master instrument list for all symbols used across the dates, and run one Strategy Analyzer backtest for this master list for the full date range.

    I'm fairly new to C# so happy to research to get the code running, but would really appreciate any pointers you could provide to set me in the right direction please.

    Many thanks in advance

    ChainsawDR

    #2
    Hi ChainsawDR, thanks for writing in.

    It is possible to check if the strategy is running on a certain day or symbol and to control execution based on that criteria. e.g.

    if(Instrument.FullName == "SomeInstrument" && (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) < 120000) || (ToTime(Time[0]) >= 140000 && ToTime(Time[0]) < 154500))
    {
    //trade
    }

    For the array idea, you can either make a custom Class with a DateTime and String property and make a List<Class> (see here for classes and here for List<T>) Or you can use a Dictionary<DateTime, String> a data structure that I find useful when I need to associate two values of different types with each other.

    Kind regards,
    -ChrisL

    Comment


      #3
      Thank you ChrisL! This is great. I've reviewed and attempted the Dictionary approach, and have got it working, however I know that I haven't done it properly (so was hoping to trouble you once more to ask if it's an easy change that you know how to solve). I really struggled to get the DateTime logic working, so reverted to entering the Date into the dictionary as a string, and converted the current Time[0] into a string using the same ShortDatePattern format. If you're able to share how I should be entering and checking for the DateTime that'd be a great help (but no worries if not, it's at least working in a string format). Here's my code:

      Code:
      public class MyVolumeSRv1 : Strategy
      {
      private Dictionary<string, string> myDictionary = new Dictionary<string, string>();
      ....
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"MyVolumeSRv1 Strategy";
      ...
      myDictionary.Add("11/30/2021", "AHPI");
      ....
      
      protected override void OnBarUpdate()
      {
      foreach(KeyValuePair<string, string> kvp in myDictionary)
      {
      if (kvp.Value == Instrument.FullName && kvp.Key == Time[0].ToString("d"))
      {
      Print(string.Format("Dictionary Lookup is working for {0} and on Date {1}", Instrument.FullName, Time[0].ToString("d")));
      }
      }
      ...
      Thanks again for your help, this is a gamechanger for me!

      Comment


        #4
        Hi, thanks for the follow up.

        A <DateTime, string> dictionary would be use like so:

        Code:
        private Dictionary<DateTime,string> myDictionary;
        ...
        protected override void OnStateChange()
        {
        else if (State == State.Configure)
        {
            myDictionary = new Dictionary<DateTime,string>();
        }
            else if (State == State.DataLoaded)
        {
            myDictionary.Add(new DateTime(1980, 1, 1, 12, 0, 0, 0), "ES 03-81"); //add preset values
            myDictionary.Add(new DateTime(1993, 1, 1, 12, 0, 0, 0), "NQ 03-94");
        }
        ...
        protected override void OnBarUpdate()
        {
            myDictionary.Add(Time[0], Instrument.FullName); //add new values in OnBarUpdate
        
            if(myDictionary.ContainsKey(Time[0])) //search for an entry
            {
                Print(myDictionary[Time[0]]);
            }
        }

        Comment


          #5
          Thanks ChrisL. Because the Time aspect of DateTime is set to 0, 0, 0, will a Time[0] value that contains an actual Time component match correctly? Eg if Time[0] returns 1993, 1, 1, 9, 30, 0 will a dictionary entry of 1993, 1, 1, 0, 0, 0 still trigger a match? (It’s the unwanted “time” part of DateTime that is confusing me).

          Also, in my prior example I included a Foreach KeyValuePair logic because I need to match both the Date & Symbol within the same dictionary entry (Eg entries for ABC 12/1/2021 & DEF 12/2/2021 shouldn’t allow ABC to run on 12/2/2021). Your example contained “
          if(myDictionary.ContainsKey(Time[0]))”. Was this just to illustrate how to match DateTime correctly, and am I correct in thinking that I should continue my Foreach approach and not attempt something like:
          ”if(myDictionary.ContainsKey(Time[0]) && myDictionary.ContainsValue(
          Instrument.FullName))”

          Thanks again ChrisL!

          Comment


            #6
            Hi Chainsaw, a datetime needs all the components to match correctly, you would other wise compare DateTime.Year, DateTime.Month, DateTime.Day, DateTime.Hour, and DateTime.Minute to see if these values match so you do not need to get the exact millisecond. The ContainsKey and ContainsValue methods will search the dictionary for the matching key or value, it depends on the needs of the strategy whether you use a Foreach or this lookup method.

            Best regards,
            -ChrisL

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            55 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            132 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            73 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            45 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X