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

Consecutive Bars

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

    Consecutive Bars

    Looking for help to build an order script that will buy the open of a bar if the two prior bars are both green, and sell on that same bar’s close. Basic coding using Open[]/Close[] doesn’t seem to be helping.

    #2
    Hello bgump55,

    Welcome to the NinjaTrader forums!

    Print(string.Format("{0} | IsFirstTickOfBar: {1} && Close[0]: {2} > Open[0]: [3] && Close[1]: {4} > Open[1]: {5}", Time[0], IsFirstTickOfBar, Close[0], Open[0], Close[1], Open[1]));

    if (IsFirstTickOfBar == true && Close[0] > Open[0] && Close[1] > Open[1])
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you!

      Comment


        #4
        Follow up. I would like to set stop and profit once I enter trade using the code below. The SetStopLoss and SetProfitTarget in the State.Configure section don't seem to be working at all. Is there a way to say if ticks = -8 exit or if ticks = 16 exit?



        bool market_open = ToTime(Time[0]) > 080000 && ToTime(Time[0]) < 133000;
        bool tradeOpen = false;
        double Bar1len = Close[0] - Open[0];
        // double influenceBar = High[4] - (Open[2]+3);

        if (market_open && IsFirstTickOfBar == true && Close[0] < Open[0] && Close[1] < Open[1] && Close[2] > Open[2] && Bar1len < -1.25)

        EnterShort();
        tradeOpen = true;

        Comment


          #5
          Hello bgump55,

          else if (State == State.Configure)
          {
          SetStopLoss(CalculationMode.Ticks, 16);
          SetProfitTarget(CalculationMode.Ticks 8);
          }
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            So that doesn't seem to work. It enters the trade but closes it only at End of Day. It makes one trade a day. Here is the whole code:


            namespace NinjaTrader.NinjaScript.Strategies
            {
            public class Test2Unlocked : Strategy
            {
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Strategy here.";
            Name = "Test2Unlocked";
            Calculate = Calculate.OnBarClose;
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.AllEntries;
            IsExitOnSessionCloseStrategy = true;
            ExitOnSessionCloseSeconds = 30;
            IsFillLimitOnTouch = false;
            MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
            OrderFillResolution = OrderFillResolution.Standard;
            Slippage = 0;
            StartBehavior = StartBehavior.WaitUntilFlat;
            TimeInForce = TimeInForce.Gtc;
            TraceOrders = false;
            RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
            StopTargetHandling = StopTargetHandling.PerEntryExecution;
            BarsRequiredToTrade = 20;
            // Disable this property for performance gains in Strategy Analyzer optimizations
            // See the Help Guide for additional information
            IsInstantiatedOnEachOptimizationIteration = true;
            }
            else if (State == State.Configure)
            {
            SetStopLoss(@"8", CalculationMode.Ticks, 0, false);
            SetProfitTarget(@"16", CalculationMode.Ticks, 0);
            }
            }

            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0)
            return;

            bool market_open = ToTime(Time[0]) > 080000 && ToTime(Time[0]) < 133000;
            bool tradeOpen = false;
            double Bar1len = Close[0] - Open[0];

            if (market_open && IsFirstTickOfBar == true && Close[0] < Open[0] && Close[1] < Open[1] && Close[2] > Open[2] && Bar1len < -1.25)

            EnterShort();
            tradeOpen = true;


            }
            }
            }​

            Comment


              #7
              Hello bgump55,

              SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool isSimulatedStop)


              ​You have the fromEntrySignal named "8" but there is not entry order with the name "8". The fromEntrySignal attaches the stop loss to a specific entry. Your entry has no signalName so this should be an empty string.

              You have the number of ticks set to 0. The distance in ticks has be 1 or greater. If you supply a 0, no stop will be submitted.

              Have you tried the code I suggested to you in post # 5?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Ah ok I see it now! Yes yes this makes lots of sense. Thank you again!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by JoMoon2024, Today, 06:56 AM
                0 responses
                6 views
                0 likes
                Last Post JoMoon2024  
                Started by Haiasi, 04-25-2024, 06:53 PM
                2 responses
                17 views
                0 likes
                Last Post Massinisa  
                Started by Creamers, Today, 05:32 AM
                0 responses
                5 views
                0 likes
                Last Post Creamers  
                Started by Segwin, 05-07-2018, 02:15 PM
                12 responses
                1,786 views
                0 likes
                Last Post Leafcutter  
                Started by poplagelu, Today, 05:00 AM
                0 responses
                3 views
                0 likes
                Last Post poplagelu  
                Working...
                X