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

Position Flat

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

    Position Flat

    Hello,


    Hopefully a simple question. I have code that is trying to only take trades when position is flat. This was working when I was only check the OnBarUpdate for the primary 1min bars. I then went to the added tick bars and it doesn't seem to like it.


    Any help as to why this is not functioning in my code?


    #region Variables
    // Wizard generated variables
    private int profit = 10; // Default setting for Profit
    private int stop = 5; // Default setting for Stop
    private int zonePos = 1; // Default setting for ZonePos
    private int zoneNeg = 1; // Default setting for ZoneNeg
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SetProfitTarget("", CalculationMode.Ticks, Profit);
    SetStopLoss("", CalculationMode.Ticks, Stop, false);

    // Add a 5 minute Bars object to the strategy
    Add(PeriodType.Minute, 15);
    //Add(PeriodType.Minute, 1);
    Add(PeriodType.Tick, 1);
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Ignore bar update events for the supplementary Bars object added above
    if (BarsInProgress == 0 || BarsInProgress == 1)
    return;
    if (Position.MarketPosition != MarketPosition.Flat)
    return;
    // Condition set 1
    if (Closes[2][0] <= Lows[1][1] + ZonePos * TickSize
    && Closes[2][0] >= Lows[1][1] - ZonePos * TickSize
    && ToTime(Time[0]) >= ToTime(8, 45, 0)
    && ToTime(Time[0]) <= ToTime(10, 30, 0))
    {
    Print("15m low " + Lows[1][1]);
    Print("Trigger long " + Closes[2][0]);
    EnterLong(DefaultQuantity, "");
    }

    #2
    Hello bobajob78,

    Thanks for your post.

    Based on your comment here:
    // Ignore bar update events for the supplementary Bars object added above
    if (BarsInProgress == 0 || BarsInProgress == 1)
    return;


    It seems you are returning on BarsInProgress 0 which are the chart bars, not the supplemental bars. To only process on the chart bars you can either use:


    if (BarsInProgress == 1 || BarsInProgress == 2)
    return;


    or

    if (BarsInProgress != 0)
    return;
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks,


      I was purposefully (in error maybe) trying to only update on the tick events, that's why I was returning on the other bars.


      Essentially I am trying to place my trade a early as possible rather than waiting for a close of a minute bar.


      Is this preventing the code from properly evaluating if I am flat or not? is it related to the chart bars?

      Comment


        #4
        I think I can achieve what I need by making the primary chart a tick based chart. That way the strategy can execute on that timeframe. I guess in essence you can't execute a strategy on a timeframe smaller than the primary chart?

        Comment


          #5
          Hello bobajob78,

          Thanks for your reply and clarifying your goals.

          Not trying to complicate this but I think you would be better off removing the tick bars and just running the strategy CalculateOnBarClose = false which will process your code on each tick which effectively is what you are doing by adding the tick bars (unless you are adding the tick bars for historical processing to give you the tick processing there).

          With regard to the position evaluation, is the issue that there are multiple orders being placed? As you have only advised: "I then went to the added tick bars and it doesn't seem to like it." and "Is this preventing the code from properly evaluating if I am flat or not?" If that is not the actual observation, please elaborate on what you are seeing.

          EDIT: It looks like we cross posted. Yes you can run on a smaller time frame than the chart bars but there are a number of considerations as addressed in the help section here: https://ninjatrader.com/support/help...nstruments.htm, so once you clarify what you are seeing we can address those.
          Last edited by NinjaTrader_PaulH; 06-12-2018, 01:21 PM.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hello bobajob78,

            Thanks for your reply.

            I edited my prior post to respond to your post #4.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thanks - yes I am including ticks because I need to backtest historical data and need that granularity for sensible fills.


              The code below, I am trying only open a trade when flat. But it seems to continually execute - i.e. ignore the position for some reason:


              protected override void OnBarUpdate()
              {
              // Ignore bar update events for the supplementary Bars object added above
              if (BarsInProgress == 2)
              {
              if (Position.MarketPosition != MarketPosition.Flat)
              return;
              // Condition set 1
              if (Closes[2][0] <= Lows[1][1] + ZonePos * TickSize
              && Closes[2][0] >= Lows[1][1] - ZonePos * TickSize
              && ToTime(Time[0]) >= ToTime(8, 45, 0)
              && ToTime(Time[0]) <= ToTime(10, 30, 0))
              {
              Print("15m low " + Lows[1][1]);
              Print("Trigger long " + Closes[2][0]);
              EnterLong(DefaultQuantity, "");
              }
              }
              // Condition set 2
              //if (Closes[2][0] >= Highs[1][1] - ZoneNeg * TickSize
              // && Closes[2][0] <= Highs[1][1] + ZoneNeg * TickSize
              // && ToTime(Time[0]) >= ToTime(8, 45, 0)
              // && ToTime(Time[0]) <= ToTime(10, 30, 0))
              //{
              // Print("15m high " + Highs[1][1]);
              // Print("Trigger short " + Closes[2][0]);
              // EnterShort(DefaultQuantity, "");
              //}
              }

              Comment


                #8
                Hello bobajob78,

                Thanks for your reply and clarification.

                Ticks can come in quickly and in bunches and can cause your code to execute many many times in a short period of time. Meanwhile, when an order is placed, it takes physical time to transmit the order, and go through the various order states until it reports back as filled which is when the position would be updated. So it is best to think of the order process as asynchronous to your code and when running on CalculateOnBarClose = false or with an added 1 tick series you code can execute a number of times before position is updated.

                What you can do is to place a limit so that you only place one order per (chart)bar. This can be done by saving the CurrentBar[0] number into an integer variable and then checking to see if it is a different bar when placing the order.

                For example:

                if (CurrentBars[0] != savedBar && Closes[2][0] <= Lows[1][1] + ZonePos * TickSize
                && Closes[2][0] >= Lows[1][1] - ZonePos * TickSize
                && ToTime(Time[0]) >= ToTime(8, 45, 0)
                && ToTime(Time[0]) <= ToTime(10, 30, 0))
                {
                Print("15m low " + Lows[1][1]);
                Print("Trigger long " + Closes[2][0]);
                EnterLong(DefaultQuantity, "");
                savedBar = CurrentBars[0]; // save the bar number of the chart bar (higher time frame)
                }


                So on the very first time, the conditions will be true and an order will be placed and saveBar will then be assigned the number of CurrentBars[0]. On the next tick, assuming the other conditions are true, the order will not be placed because savedBar is now equal to CurrentBars[0] and the condition CurrentBars[0] != savedBar will no longer be true thus preventing re-order.

                Note, you would need to declare savedBar as an int in the region variables: private int savedBar;
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the explanation Paul. Most useful!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by bsbisme, Yesterday, 02:08 PM
                  1 response
                  15 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by prdecast, Today, 06:07 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post prdecast  
                  Started by i019945nj, 12-14-2023, 06:41 AM
                  3 responses
                  60 views
                  0 likes
                  Last Post i019945nj  
                  Started by TraderBCL, Today, 04:38 AM
                  2 responses
                  18 views
                  0 likes
                  Last Post TraderBCL  
                  Started by martin70, 03-24-2023, 04:58 AM
                  14 responses
                  106 views
                  0 likes
                  Last Post martin70  
                  Working...
                  X