NexusFi: Find Your Edge


Home Menu

 





Make strategy start another strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one MXASJ with 4 posts (0 thanks)
    2. looks_two kandlekid with 4 posts (4 thanks)
    3. looks_3 zwentz with 3 posts (1 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 8,349 views
    2. thumb_up 5 thanks given
    3. group 6 followers
    1. forum 13 posts
    2. attach_file 1 attachments




 
Search this Thread

Make strategy start another strategy

  #1 (permalink)
 zwentz 
Austin, TX
 
Experience: Advanced
Platform: NinjaTrader
Broker: Optimus Futures/Rithmic
Trading: Futures [ZB]
Posts: 36 since Sep 2009
Thanks Given: 12
Thanks Received: 22

Has anyone had any luck starting a strategy from within another strategy? Like say your conditions are met and you'd like to start a new strategy, for whatever reason.

I know you can disable a currently running strategy; take for instance you have two strategies called SimpleMA and ComplicatedMA and you want to disable complicated from simple.

 
Code
Account.Strategies[i].Disable(); //i is the index in the StrategiesCollection of your ComplicatedMA strategy
Now that disables the strategy, but there is no corresponding Enable(); or Start(); or Initialize(); function to start it, from what I can tell anyway.

So has anyone had any luck doing such a thing?

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
ZombieSqueeze
Platforms and Indicators
Deepmoney LLM
Elite Quantitative GenAI/LLM
Futures True Range Report
The Elite Circle
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
GFIs1 1 DAX trade per day journal
18 thanks
The Program
18 thanks
  #3 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20


Yes, here's how I did it (for example) ... sorry about the formatting ....

Instantiating a strategy from another strategy ...


CalledStrategy.cs
-----------------

 
Code
                            
// Called strategy (i.e. instantiated from main strategy) ...

namespace NinjaTrader.Strategy
{
          public class 
CalledStrategy Strategy
         
{

                  protected 
override void Initialize()
                   {
                         ... 

                         
Enabled true;
                }

                 
// Guts ...

           
}

CallingStrategy.cs
------------------

 
Code
                            
// Calling strategy (i.e. instantiates the called strategy)  ...

namespace NinjaTrader.Strategy
{
          public class 
CallingStrategy Strategy
         
{
                 
// Declare Called Strategy.

                
CalledStrategy cStrat;

                protected 
override void Initialize()
                 {

                        
// Instantiate Called Strategy.

                       
cStrat = new CalledStrategy();

                       
Enabled true;
              }

               protected 
override void OnTermination()
                 {
                       
base.Dispose();
               }
    
                protected 
override void OnBarUpdate()
               {
                       
// Use Called strat here ...

                      
cStrat.xyz();  // for example;
               
}
        }


Reply With Quote
Thanked by:
  #4 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Thanks kandlekid,

I haven't tried it yet but I want to make sure my logic is correct. Your CalledStrat is any strat as long as enabled=true, and your CallingStrat invokes the CalledStrat as a method???

Example:

MyTrendStrat is a stand-alone strat that works well in trending markets, and enabled=true.
MyChopStrat is a stand-alone strat that works well in chop, and enabled =true.

MyUberStrat has logic that decides if we are trending or in chop. It is the only strat I enable at the start of a session.

MyUberStrat would contain:

Variables

MyTrendStrat tradeTrend; // Can you really do this???
MyChopStrat tradeChop;

Initialize

tradeTrend = new MyTrendStrat();
tradeChop = new MyChopStrat();

//enabled = true here should only apply to MyUberStrat... right?

Then you lost me in OnBarUpdate().

If my MyUberStrat has chop and trend bool logic, what I want to do is this:

OnBarUpdate

if (chop) tradeChop();
if (trend) tradeTrend();

?

Is that the gist of it?

This is SQL weekend for me so I'm preoccupied but if what you say is true and robust (a "master" strategy can start and stop other strategies), that opens up some new avenues for me and the Ninja community in general.

Reply With Quote
  #5 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20

> Your CalledStrat is any strat as long as enabled=true, and your CallingStrat invokes the CalledStrat as a method???

You're invoking the constructor of the strategy. So it has to be the same name as the class.

>Variables

> MyTrendStrat tradeTrend; // Can you really do this???
> MyChopStrat tradeChop;

Sure.

> Initialize

> tradeTrend = new MyTrendStrat();
> tradeChop = new MyChopStrat();

> //enabled = true here should only apply to MyUberStrat... right?

I think you want to enable all the strats ... that's what I do.

> Then you lost me in OnBarUpdate().

> If my MyUberStrat has chop and trend bool logic, what I want to do is this:

> OnBarUpdate

> if (chop) tradeChop();
> if (trend) tradeTrend();

Ok, so if you add tradeChop() and tradeTrend() to MyChopStrat() and MyTrendStrat() as public member functions, then you can do ...

if (chop) tradeChop.tradeChop();
if (trend) tradeTrend .tradeTrend();

So you're calling the member functions from the instantiated objects.

>Is that the gist of it?

I think so ..

Strategies are really just C# classes ...

Reply With Quote
Thanked by:
  #6 (permalink)
 zwentz 
Austin, TX
 
Experience: Advanced
Platform: NinjaTrader
Broker: Optimus Futures/Rithmic
Trading: Futures [ZB]
Posts: 36 since Sep 2009
Thanks Given: 12
Thanks Received: 22

Hey kandlekid,

Alright so I tried this, basically the only addition I needed to make was adding -

 
Code
Enabled = true;
- in the Initialize(); function of my called strategy. However, Initialize never gets called and so Enabled is never set to true. And I know from my own testing that just setting the strategy's Enabled property to true from the calling strategy doesn't work.

Here's my called strategy, DoNothing:

 
Code
public class DoNothing : Strategy
{
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			Enabled = true;
        }

        protected override void OnBarUpdate()
        {
        }
}
And here's where the parent strategy is calling it:

 
Code
if (!exists)
{
	Period test = new Period(PeriodType.Minute,1,MarketDataType.Last);
	DoNothing nts = new DoNothing();
	nts.Account = Acct;
	nts.SetBarsPeriodInstrument(test,position.Instrument);
	Acct.Strategies.Add(nts);
}
Am I missing something?

Started this thread Reply With Quote
  #7 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20

Hi zwentz,

I just realized that there are 2 threads here, yours and MXASJ's.

The NT 7 doc says that Initialize is called once upon starting a strat.
So I guess you have to determine if the strategy is ever started (seems
circular, because that's the problem you're trying to solve).

I guess I can really only comment on my method of starting a sub
strategy. Anyway, what exactly is Acct and what are you doing with it?

i.e.

nts.Account = Acct;

Acct.Strategies.Add(nts);

Reply With Quote
  #8 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

I think zwentz and I are asking the same thing, just differently. This does not work:

 
Code
public class MasterStratTest : Strategy
    {
        #region Variables
 
        SampleMACrossOver cStrat;
 
        #endregion
 
        protected override void Initialize()
        {
            cStrat = new SampleMACrossOver();
            Enabled = true;
 
            CalculateOnBarClose = true;
        }
 
 
        protected override void OnBarUpdate()
        {
            cStrat.// Can't find anything that would load and enable SampleMACrossOver 
 
        }
 
 
 
        #region Properties
 
        #endregion
    }
In your initial note you have "xyz" as an example. Like the comments in the code above state, I can't find anything that would load and enable another strategy.

Reply With Quote
  #9 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20


Quoting 
In your initial note you have "xyz" as an example. Like the comments in the
code above state, I can't find anything that would load and enable another strategy.

 
Code
// You instantiated SampleMACrossOver here (i.e. you created the strategy).

cStrat = new SampleMACrossOver();


// Here you want to call a member function (method) of SampleMACrossOver().

protected override void OnBarUpdate
{

    // For example ...

    if ( cStrat.ShortMACrossedOverLongMA() )
       Go long ...
   else if ( cStrat.ShortMACrossedUnderLongMA() )
       Go short ...
}
Hope that helps. I'm not sure exactly when the strategy is loaded and enabled, but I've had no problems using
my method above. All of my strategies are enabled after I've applied them to the chart (as long as I have a
simulated, live or market replay connection).

Reply With Quote
  #10 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


Ok then we are talking about different things.

If you want to call methods external to a strategy you can also use the UserDefinedMethods partial class.

Reply With Quote




Last Updated on April 4, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts