Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with my code? Strategy not behaving as expected.

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

    Problem with my code? Strategy not behaving as expected.

    Hi,

    I have written a simple strategy with ema crossover and scaling out in 4 phases. Pasting the code below. When I backtest this, I see only first order gets executed for e.g. 'up1' and 'dn1' only. What am I doing wrong here? Thanks for your help!

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(EMA(Fema));
    Add(EMA(Sema));
    Add(EMA(Fema));
    Add(EMA(Sema));
    SetProfitTarget("up1", CalculationMode.Percent, Tgt1);
    SetProfitTarget("up2", CalculationMode.Percent, Tgt2);
    SetProfitTarget("up3", CalculationMode.Percent, Tgt3);
    SetProfitTarget("up4", CalculationMode.Percent, Tgt4);
    SetProfitTarget("dn1", CalculationMode.Percent, Tgt1);
    SetProfitTarget("dn2", CalculationMode.Percent, Tgt2);
    SetProfitTarget("dn3", CalculationMode.Percent, Tgt3);
    SetProfitTarget("dn4", CalculationMode.Percent, Tgt4);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(Fema), EMA(Sema), 1))
    {
    EnterLong(Qty, "up1");
    EnterLong(Qty, "up2");
    EnterLong(Qty, "up3");
    EnterLong(Qty, "up4");
    ExitShort("dn1out", "dn1");
    ExitShort("dn2out", "dn2");
    ExitShort("dn3out", "dn3");
    ExitShort("dn4out", "dn4");
    }

    // Condition set 2
    if (CrossBelow(EMA(Fema), EMA(Sema), 1))
    {
    EnterShort(Qty, "dn1");
    EnterShort(Qty, "dn2");
    EnterShort(Qty, "dn3");
    EnterShort(Qty, "dn4");
    ExitLong("up1out", "up1");
    ExitLong("up2out", "up2");
    ExitLong("up3out", "up3");
    ExitLong("up4out", "up4");
    }

    }

    #2
    Hi phobos,

    What is your strategies EntryHandling property here? You probably want it set to UniqueEntries.

    A basic scale in/out strategy is available here:


    You do not want both Enter and Exit in the same block. Reversals are automatically handled by the Enter methods, so you do not need separate exits.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi Ryan,
      I am using the wizard. So where do i set the EntryHandling property?

      And what do you mean by "Reversals are automatically handled by the Enter methods, so you do not need separate exits." I am trying to enter in a single phase at the same time but scale out in 4 chunks if profit target is meet or some other reversal condition is met. I read somewhere there is a bug in NT wherein if i want to scale out, then i need to enter separate 'Enter' commands even if they are all at same time and price.

      Thanks!

      Comment


        #4
        If you create in the wizard, just set EntryHandling when you run (or backtest) the strategy. There's a drop down menu for it.


        There's never a reason to include both ExitLong and EnterShort in the same block. You either want a reversal or a straight exit. Enter methods take care of reversals. Exit statements are used for straight exits.

        If you do not want your enter methods to reverse, add a condition to these entries that checks market position == flat. For help with this condition, see the following link then section: How to create market position comparisons

        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks Ryan. The entry problem is resolved.

          Now I am having difficulty programming the exits through the wizard. This is what I want to do:
          I have either entered long or short '4x' shares upon MA crossover. Now I want to exit:
          1. 'x' shares either after I meet 1st profit target which i am defining as user input variable or on a MA crossover to opposite side that of the entry signal.
          2. Sell another 'x' shares at 2nd profit target and so on.
          So if i hit 4th profit target, then I am out of all '4x' shares.

          How do i put this in the wizard?

          Thanks.....

          Comment


            #6
            It looks like you have the general idea of it. You have to work with the signal names that you specify when you enter. The exits, including targets and stops, can reference these entries by matching fromEntrySignal value.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Hi Ryan,

              Looks like I found the issue via some debugging. Pasting some code lines and the output:

              private double tgt1 = 0.03;
              Print(" Target : " + tgt1);
              SetProfitTarget("up1", CalculationMode.Percent, tgt1);

              Output of print:
              Target : 0.03
              8/20/2011 5:56:08 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='up1' Mode=Percent Value=0.03 Currency=0 Simulated=False
              Target : 1
              8/20/2011 5:56:08 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='up1' Mode=Percent Value=1 Currency=0 Simulated=False

              Why is tgt1 being rounded off?

              Comment


                #8
                Also, when i run the optimizer, how can I give the tgt1 value?

                Like this? 0.03;0.09;0.01

                Comment


                  #9
                  phobos, is this the only place where you set / define the target value? Do you reset the stops / targets to some initial default when flat in the strategy?

                  For the optimizer, please create a user defined input for your target:

                  Comment


                    #10
                    Hi Bertrand,

                    Yes this is the only place where i define it. The code is very simple and basic and I am not using anything related to flat.

                    Comment


                      #11
                      private double tgt1 = 0.03;
                      Mode=Percent Value=1

                      Is double tg1 part of a public input? Check the properties region for Tg1 - You likely have a Math.Max(value, 1); there. Change to 0 to accept values smaller than 1.
                      Ryan M.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      88 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      48 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      31 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      34 views
                      0 likes
                      Last Post TheRealMorford  
                      Started by Mindset, 02-28-2026, 06:16 AM
                      0 responses
                      68 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Working...
                      X