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

How to buy at the open of the next candle?

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

    How to buy at the open of the next candle?

    I thought this was going to be easy, but I can not figure it out.
    I am trading on a 3minute time frame and I am using a third party script that provides trend lines. Unfortunately, the third party script is not providing any way I can get data from their trend lines. Basically, I want to do a strategy that will enter a long position and create a profit target and stop loss target of 5 ticks at the open of the next candle after I start the strategy.

    For example: I am trading on the 3minute time frame, the current bar has 30second before it closes and the next candle begins. With 30seconds left, I decide that I want to enter long and create a profit target and stop loss of 5ticks. So I want to enable the strategy manually under the "strategies" tab and it will enter the long at the open of the next candle.

    I though the below code will work, but it did not work.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class FIVEtickLong : Strategy
    {
    int x=0;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "FIVEtickLong";
    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)
    {
    SetProfitTarget(CalculationMode.Ticks, 5);
    SetStopLoss(CalculationMode.Ticks, 5);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1)
    return;

    if (BarsInProgress != 0)
    {
    return;
    }
    else{
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    }
    }
    }
    }

    #2
    Hi zaro33,
    If you open up the Strategy Builder and put in an unconditional EnterLong order, the code looks slightly different. You don't need the "else {" before EnterLong.
    And you will also need less closing curly brackets "}" after EnterLong. I would guess "only" 3 closing curly brackets in total.
    NT-Roland

    Comment


      #3
      Trust me, I have try that and it does not work. I tried it again and it did some weird stuff. I did the changes below and this is what happens.
      1-I first enable the strategy stating "wait until flat" and I was on the 1minute time frame and I waited 5minutes and it did not enter and I made sure that it was on 0bars needed to trade.
      2-Then I enable the strategy with "fill immediately" it would say "order submitted" by the loudspeaker and it will show what seems to be the sell profit area and sell stop area, but it does not show any order on the chart from time to time. Also the profit and stop area is not 5ticks, it is different amount of ticks. Then if I go under the positions tab, there is no position there open from time to time and it correlates with order not shown in the chart.

      Again what I want to do is to be able to enable the strategy before the current bar closes. Then enter a long position at the open of the next candle and have it create the 5ticks profit and 5tick stop loss. **I am using a simulation account (simulation money), but I don't think this is an issue since I have done crossover strategy and test them in the simulation account and they behave as expected.

      If you don't believe me, try running your own simulation, it seems like it would be simple but it is not for some reason.


      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class FIVEtickLong : Strategy
      {
      int x=0;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "FIVEtickLong";
      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 = 0;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      // SetProfitTarget(@"5", CalculationMode.Ticks, 0);
      // SetStopLoss(@"5", CalculationMode.Ticks, 0, false);
      SetProfitTarget(CalculationMode.Ticks, 5);
      SetStopLoss(CalculationMode.Ticks, 5);
      }
      }

      protected override void OnBarUpdate()
      {
      if (CurrentBars[0] < 1)
      return;

      if (BarsInProgress != 0)
      {
      return;
      }

      EnterLong(Convert.ToInt32(DefaultQuantity), "");



      }
      }
      }

      Comment


        #4
        Unfortunately, I have try that already and it did not work. I actually retry putting the EnterLong outside the else and like you say and it does weird things.
        For example, if I set it to "wait until flat", it does not enter. I have waited more than 10minutes on a 1minute time frame and it did not enter.
        if I set it to "fill immediately" it would enter, but it does not show the fill value from time to time on the chart and on the "positions" tab. Also the 5tick profit target and stop loss show up on weird values.

        You can try it yourself and see that while it looks simple it does not actually work.
        I'm just trying to enable the strategy during the current bar so it enter a buy at the open of the next bar. Sounds simple, but it is not working for me.

        Comment


          #5
          Hello zaro33,

          Thanks for your posts. Please note that in your other thread on this topic I have put a link back to this thread.

          In the OnBarUpdate(), you want to place a condition to ignore the historical data as your only interest here is the currently forming bar.

          Here is what you can add (based on your previous example and assumes you are using Calculate.OnBarClose):

          Code:
                   else if (State == State.Configure)
                      {
                          SetProfitTarget(CalculationMode.Ticks, 5);
                          SetStopLoss(CalculationMode.Ticks, 5);
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (State != State.Realtime) return;
          
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                          EnterLong();
                      }
                  }
          Note that once enabled this will continue entering long orders on each new bar.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thank you Paul, that works perfect.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            43 views
            0 likes
            Last Post jeronymite  
            Started by frankthearm, Today, 09:08 AM
            4 responses
            9 views
            0 likes
            Last Post frankthearm  
            Started by yertle, Today, 08:38 AM
            5 responses
            15 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by adeelshahzad, Today, 03:54 AM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by bill2023, Yesterday, 08:51 AM
            6 responses
            27 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X