Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Interactions of several Strategies

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

    Interactions of several Strategies

    Hello everyone.

    Problem:
    I want to create a Strategy that should catch enable/disable of other strategies in the terminal. Lets call it "Strategy0".
    For example, if I add a new "Strategy1" - in the "Strategy0" log I want to catch the event "Strategy1 is added". As soon as I enable Strategy1 - in the Strategy0 log I want to catch the event "Strategy1 is running". As soon as I disable Strategy1 - in the Strategy0 log I want to catch the event "Strategy1 is stopped", etc.

    I managed to get access to the Strategy1 State from Strategy0, but I can’t find the event that needs to be caught (the moment of changing the Strategy1 State):
    Code:
            
    void TryToCatch(){
          foreach(StrategyBase str in myAccount.Strategies){
                Print("STR:");
                Print(str.GetType().ToString());
                Print(str.Name.ToString());
                Print(str.State.ToString());
          }
    }

    How can I solve this problem?
    Last edited by webus; 05-26-2020, 03:18 AM.

    #2
    Hello webus,

    Thank you for the post.

    What is your overall goal here for catching the log event? Are you trying to invoke some action or do some custom logging?

    The events you see in the log are not something you can subscribe to or "catch" and the actual event of stopping/starting is not something you could subscribe to. You might be able to wire up your own type of event or do something in the strategies Terminated state for this.

    Can you provide more specific details on what this action is going to do as that may paint a more clear picture of how to get there.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_Jesse,

      Originally posted by NinjaTrader_Jesse View Post
      What is your overall goal here for catching the log event? Are you trying to invoke some action or do some custom logging?
      My main goal is to send an email if any of the strategies is turned off for any reason. I have many strategies and I do not want to add this code to each of these strategies. I am looking for a universal way.

      It seemed to me that the most suitable is to create a separate strategy that will track the state changes in other running strategies. The only problem is I don’t know how to get the exact time of this event.

      Comment


        #4
        Hello webus,

        Thank you for the reply.

        In this situation it would likely be best to just make a shared method as a partial class so that your strategies can all implement that code. It is not reliable to try and watch strategies from an outside script to do this type of monitoring as there is not a way to subscribe to their events. This code would be better placed in the Terminated state and DataLoaded states of your strategies so they can immediately call the code when the actual event happens.

        A simple example of a partial class would be:

        Code:
        public partial class Strategy {
            public void DoStartLogging() { }
            public void DoTerminatedLogging() { }
        }
        That could go in a separate file in the strategies folder and then each strategy could call those methods from its DataLoaded or Terminated states. Its only 1 line of modification to each strategy that way. You then edit the partial class to include your logging or any other code you need called.

        Please let me know if I may be of further assistance.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi NinjaTrader_Jesse,
          Originally posted by NinjaTrader_Jesse View Post
          Hello webus,

          Thank you for the reply.

          In this situation it would likely be best to just make a shared method as a partial class so that your strategies can all implement that code. It is not reliable to try and watch strategies from an outside script to do this type of monitoring as there is not a way to subscribe to their events. This code would be better placed in the Terminated state and DataLoaded states of your strategies so they can immediately call the code when the actual event happens.

          A simple example of a partial class would be:

          Code:
          public partial class Strategy {
          public void DoStartLogging() { }
          public void DoTerminatedLogging() { }
          }
          That could go in a separate file in the strategies folder and then each strategy could call those methods from its DataLoaded or Terminated states. Its only 1 line of modification to each strategy that way. You then edit the partial class to include your logging or any other code you need called.

          Please let me know if I may be of further assistance.

          I look forward to being of further assistance.
          It is a pity that it is impossible to catch the moment of a change in the states of a strategy from other strategies.
          But thanks for your solution.

          Comment


            #6
            hello webus

            I believe that you can get very close to what you want by having Strategy 1 generate a "heartbeat" signal in one of the exposed strategy variables (Variable0 thru Variable9) and then monitor that Strategy variable with Strategy0 by using lock(Account.All)/lock(Account.Strategies).

            The heartbeat is an analog value that increments on each OnBarUpdate - while it is incrementing, Strategy0 knows that Strategy1 is executing. If the heartbeat value stops changing, then Strategy1 is no longer executing.

            Strategy0 locks the Account and loops thru the Strategies to gather the heartbeat info. In case of some kind of timing hiccup between Strategy0 and Strategy1, you may want to add a delay to Strategy0's heartbeat monitoring logic, i.e. wait for the heartbeat to be lost for 2 OnBarUpdates of Strategy0 before taking any action.

            I believe that I had this kind of code working a couple of years ago, but have been doing a total code rewrite since then, and I don't remember if I fully vetted it. Will be revisiting it soon.

            Hope that helps.
            Cheers!

            Comment


              #7
              Hi Zigfried,

              Originally posted by Zigfried View Post
              hello webus

              I believe that you can get very close to what you want by having Strategy 1 generate a "heartbeat" signal in one of the exposed strategy variables (Variable0 thru Variable9) and then monitor that Strategy variable with Strategy0 by using lock(Account.All)/lock(Account.Strategies).

              The heartbeat is an analog value that increments on each OnBarUpdate - while it is incrementing, Strategy0 knows that Strategy1 is executing. If the heartbeat value stops changing, then Strategy1 is no longer executing.

              Strategy0 locks the Account and loops thru the Strategies to gather the heartbeat info. In case of some kind of timing hiccup between Strategy0 and Strategy1, you may want to add a delay to Strategy0's heartbeat monitoring logic, i.e. wait for the heartbeat to be lost for 2 OnBarUpdates of Strategy0 before taking any action.

              I believe that I had this kind of code working a couple of years ago, but have been doing a total code rewrite since then, and I don't remember if I fully vetted it. Will be revisiting it soon.

              Hope that helps.
              Cheers!
              Thank you for the reply, but it seems to me that this will not work, since I am most interested in the moment of the terminated state. The problem is to catch this moment, because after this state is fulfilled, I will not be able to access the strategy.

              But I'll try.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Davide999, 05-18-2023, 03:55 AM
              4 responses
              554 views
              1 like
              Last Post kcwasher  
              Started by Stanfillirenfro, Today, 07:23 AM
              2 responses
              9 views
              0 likes
              Last Post Stanfillirenfro  
              Started by rexsole, Today, 08:39 AM
              2 responses
              6 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by cmtjoancolmenero, Yesterday, 03:58 PM
              6 responses
              29 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by gbourque, Today, 06:39 AM
              2 responses
              14 views
              0 likes
              Last Post gbourque  
              Working...
              X