Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.MarketPosition == MarketPosition.Flat question

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

    Position.MarketPosition == MarketPosition.Flat question

    to Koganam ... (or anyone):

    attached is a t-bond day bar strategy in which the profitgoal and stoploss values are each 16 ticks. on most days the price moves far beyond 16 ticks in one or both directions. therefore when goal or stop is hit, position goes flat so i expect condition set 3 to set Variable0 to 0 ... and therefore, when OnBarUpdate is called on the next tick, that a new position will be entered. However, the strategy is taking only one position per day????

    i have begun trying to learn how to use the debugger so i can set breakpoints, etc but that is going to take me a while. in the meantime maybe someone can advise me on this issue. Condition Set 3 is:
    If (Position.MarketPosition == MarketPosition.Flat)
    {
    Variable0 = 0;
    }

    #2
    whoops, the upload didn't work. I tried it again when i noticed an ''invalid file'' message.
    it was/is a wordpad document.

    so here is the code directly.

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class KoganamNt8v3 : Strategy
    {
    private int calcIndex = 0;
    static int Variable0 = 0;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "KoganamNt8v3";
    Calculate = Calculate.OnEachTick;
    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;

    ProfitGoal = 16;
    StopLoss = 16;
    }
    else if (State == State.Configure)
    {
    SetProfitTarget("Long", CalculationMode.Ticks, ProfitGoal);
    SetStopLoss("Long", CalculationMode.Ticks, StopLoss, false);
    SetProfitTarget("Short", CalculationMode.Ticks, ProfitGoal);
    SetStopLoss("Short", CalculationMode.Ticks, StopLoss, false);
    }
    else if (State == State.Historical)
    calcIndex = 1;
    else if (State > State.Historical)
    calcIndex = 0;
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 2) return;

    // Condition set 1
    if (Close[1 - calcIndex] >= Open[1 - calcIndex]
    && Close[2 - calcIndex] >= Open[2 - calcIndex]
    && Variable0 == 0)
    {
    EnterLong(DefaultQuantity, "Long");
    Variable0 = 1;
    }

    // Condition set 2
    if (Close[1 - calcIndex] <= Open[1 - calcIndex]
    && Close[2 - calcIndex] <= Open[2 - calcIndex]
    && Variable0 == 0)
    {
    EnterShort(DefaultQuantity, "Short");
    Variable0 = -1;
    }

    // Condition set 3
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    Variable0 = 0;
    }
    }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "ProfitGoal", GroupName = "NinjaScriptParameters", Order = 0)]
    public int ProfitGoal
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "StopLoss", GroupName = "NinjaScriptParameters", Order = 1)]
    public int StopLoss
    { get; set; }
    }
    }

    Comment


      #3
      Originally posted by joemiller View Post
      whoops, the upload didn't work. I tried it again when i noticed an ''invalid file'' message.
      it was/is a wordpad document.

      so here is the code directly.

      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class KoganamNt8v3 : Strategy
      {
      private int calcIndex = 0;
      static int Variable0 = 0;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "KoganamNt8v3";
      Calculate = Calculate.OnEachTick;
      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;

      ProfitGoal = 16;
      StopLoss = 16;
      }
      else if (State == State.Configure)
      {
      SetProfitTarget("Long", CalculationMode.Ticks, ProfitGoal);
      SetStopLoss("Long", CalculationMode.Ticks, StopLoss, false);
      SetProfitTarget("Short", CalculationMode.Ticks, ProfitGoal);
      SetStopLoss("Short", CalculationMode.Ticks, StopLoss, false);
      }
      else if (State == State.Historical)
      calcIndex = 1;
      else if (State > State.Historical)
      calcIndex = 0;
      }

      protected override void OnBarUpdate()
      {
      if (CurrentBar < 2) return;

      // Condition set 1
      if (Close[1 - calcIndex] >= Open[1 - calcIndex]
      && Close[2 - calcIndex] >= Open[2 - calcIndex]
      && Variable0 == 0)
      {
      EnterLong(DefaultQuantity, "Long");
      Variable0 = 1;
      }

      // Condition set 2
      if (Close[1 - calcIndex] <= Open[1 - calcIndex]
      && Close[2 - calcIndex] <= Open[2 - calcIndex]
      && Variable0 == 0)
      {
      EnterShort(DefaultQuantity, "Short");
      Variable0 = -1;
      }

      // Condition set 3
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      Variable0 = 0;
      }
      }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "ProfitGoal", GroupName = "NinjaScriptParameters", Order = 0)]
      public int ProfitGoal
      { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "StopLoss", GroupName = "NinjaScriptParameters", Order = 1)]
      public int StopLoss
      { get; set; }
      }
      }
      Are you talking about things in the Strategy Analyzer, or on a realtime chart?

      Comment


        #4
        Re: ''Are you talking about things in the Strategy Analyzer, or on a realtime chart?''

        i'm talking about a realtime chart ... please see attachment.
        note that on november9 [day after election] the strategy correctly entered short at the opening and exited 16 ticks below entry. however,
        (1) it took no more positions that day, instead if immediately getting back in short?
        (2) exit should have been at a 16 tick profit goal instead of a 16 tick stoploss?

        please note that while trying to upgrade to nt8 from your nt7 code, a platform support team member was remotely logged on to my computer, took pity upon me and in just a couple of minutes created this compilable version [God bless him, spared me loads of frustration and hassle].

        Click image for larger version

Name:	NOV26.jpg
Views:	1
Size:	458.4 KB
ID:	881247

        Comment


          #5
          CORRECTION!!!!!
          (2) exit should have been at a 16 tick profit goal instead of a 16 tick stoploss?
          WAS WRONG!!!!

          it did exit correctly on a stop loss because after the open the price flew up to the stop before reversing and plummeting.

          very impressive ... one powerful sweet piece of software.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          65 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X