Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Execution

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

    Strategy Execution

    Hi

    When I assigned a strategy on an instrument,it executes when the conditions are met. But it executes again and again on the same instrument when the target is completed. But I want it execute only ones. Please see the code and attached picture for clarification.

    protected override void OnBarUpdate()
    {
    if ( (Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
    {

    EnterLongLimit(100, GetCurrentBid(), "");

    SetProfitTarget("", CalculationMode.Percent, 0.01);

    SetStopLoss("", CalculationMode.Percent, 0.01, false);

    }
    }


    Thanks
    Imran.
    Attached Files

    #2
    Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times. For debugging tips: http://www.ninjatrader.com/support/f...ead.php?t=3418

    If you're getting the correct initial entry you can do a couple different things to prevent additional trading. You could set a custom variable to change states or you could use performance tracking to limit the total number of trades.

    For a performance trade example: http://www.ninjatrader.com/support/f...ead.php?t=4084

    Let me know if I can further assist.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Hi

      When I assigned a strategy on an instrument,it executes when the conditions are met. But it executes again and again on the same instrument when the target is completed. But I want it execute only ones. Please see the code and attached picture for clarification.

      protected override void OnBarUpdate()
      {
      if ( (Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
      {

      EnterLongLimit(100, GetCurrentBid(), "");

      SetProfitTarget("", CalculationMode.Percent, 0.01);

      SetStopLoss("", CalculationMode.Percent, 0.01, false);

      }
      }

      e.g : When the conditions are met it buy the stock like AAPL at $100 and I set the target 1%. then sell it at $101. And again immediately it buys at $101.......and going the process like this.

      Comment


        #4
        Hello,

        Are you saying you're getting multiple entries you do not want?

        You could also check market position to ensure you only place entries when flat: http://www.ninjatrader.com/support/h...etposition.htm

        Let me know if I can further assist.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Hello,

          Are you saying you're getting multiple entries you do not want?

          You could also check market position to ensure you only place entries when flat: http://www.ninjatrader.com/support/h...etposition.htm
          Hi Lance

          Actually the order is submitting after the position was flat. And somebody on the forum advised me. If executing against a daily chart, you cannot do anything intraday unless you are going to use a multi-timeframe setup, so that you can use the intraday timeframe to monitor and control the number of executions.

          Thanks
          Imran

          Comment


            #6
            In a backtest this would be true. However in real time you are able to enter and exit intrabar but it depends on what you're trying to do.



            Let me know if I can further assist.
            LanceNinjaTrader Customer Service

            Comment


              #7
              When I assigned a strategy on an instrument,it executes when the conditions are met. But it executes again and again on the same instrument when the target is completed. But I want it execute only ones. Please see the code and attached picture for clarification.

              protected override void OnBarUpdate()
              {
              if ( (Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
              {

              EnterLongLimit(100, GetCurrentBid(), "");

              SetProfitTarget("", CalculationMode.Percent, 0.01);

              SetStopLoss("", CalculationMode.Percent, 0.01, false);

              }
              }
              So please tell me what is the solution for this ...

              Comment


                #8
                Hello,

                If you want to only have the order placed once and never again you could do this with a custom variable.

                pseudo code

                bool mySwitch = false;

                OnBarUpdate()
                {

                if ( mySwitch
                &&(Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
                {

                mySwitch = true;
                EnterLongLimit(100, GetCurrentBid(), "");

                SetProfitTarget("", CalculationMode.Percent, 0.01);

                SetStopLoss("", CalculationMode.Percent, 0.01, false);

                }

                }

                For a performance trade example: http://www.ninjatrader.com/support/f...ead.php?t=4084

                Let me know if I can further assist.
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  If you want to only have the order placed once and never again you could do this with a custom variable.

                  pseudo code

                  bool mySwitch = false;

                  OnBarUpdate()
                  {

                  if ( mySwitch
                  &&(Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
                  {

                  mySwitch = true;
                  EnterLongLimit(100, GetCurrentBid(), "");

                  SetProfitTarget("", CalculationMode.Percent, 0.01);

                  SetStopLoss("", CalculationMode.Percent, 0.01, false);

                  }

                  }
                  Hi Lance,

                  I wrote the code like you said. But the condition was not getting satisfied when I set the bool variable "bool mySwitch = false". If I deleted the bool variable from the if condition It getting satisfied.

                  bool mySwitch = false;

                  OnBarUpdate()
                  {

                  if ( mySwitch==false &&(Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1))
                  {

                  mySwitch = true;
                  EnterLongLimit(100, GetCurrentBid(), "");

                  SetProfitTarget("", CalculationMode.Percent, 0.01);

                  SetStopLoss("", CalculationMode.Percent, 0.01, false);

                  }

                  Comment


                    #10
                    Hello,

                    Your snippet here looks correct in terms of the switch logic.

                    If no trade is being executed it would be because of one of the other conditions you have to have met first.
                    You'll want to use debugging to determine when something is not triggering as expected.

                    Looking at your chart do you see a point when
                    (Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1)
                    are both true?

                    What is not working as you expect?
                    LanceNinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      Your snippet here looks correct in terms of the switch logic.

                      If no trade is being executed it would be because of one of the other conditions you have to have met first.
                      You'll want to use debugging to determine when something is not triggering as expected.

                      Looking at your chart do you see a point when
                      (Close[0] > Close[1] * (1 + 0.0025)) && CrossAbove(RSI(10, 3), 30, 1)
                      are both true?

                      What is not working as you expect?
                      Lance B.
                      Hi Lance

                      If I inserted bool variable the statement has not executed. If I removed the bool variable from the statement its working.

                      Comment


                        #12
                        Could you please attach the complete script so that I may test on my end?

                        Located in (MY) Documents\NinjaTrader 7\bin\Custom\Strategy
                        LanceNinjaTrader Customer Service

                        Comment


                          #13
                          protected override void Initialize()
                          {
                          CalculateOnBarClose = false;


                          SetProfitTarget("", CalculationMode.Percent, 0.01);

                          SetStopLoss("", CalculationMode.Percent, 0.01, false);

                          }

                          protected override void OnBarUpdate()
                          {

                          if ( Close[0] > Close[1] * (1 + 0.0025) && CrossAbove(RSI(10, 3), 30, 1))
                          {

                          EnterLongLimit(100, GetCurrentBid(), "");

                          }
                          }

                          Comment


                            #14
                            I will need the complete source file to test, a posted snippet will not work.

                            Located in (MY) Documents\NinjaTrader 7\bin\Custom\Strategy

                            When creating a new post, if you use the advanced option you can post the complete .cs file. If you're unable to do this you can send it in to support at ninjatrader dot com
                            LanceNinjaTrader Customer Service

                            Comment


                              #15
                              Please find an attachment.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              659 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              374 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X