Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Only entering if condition first time

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

    Only entering if condition first time

    Code:
    #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 EEdgeScalpingUnlocked : Strategy
        {
            private double StopLoss, ProfitTarget;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "EEdgeScalpingUnlocked";
                    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                                    = true;
                    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)
                {
    
                }
                    else if (State == State.DataLoaded)
                {                
    
                    ClearOutputWindow();
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 3)
                    return;
    
                 // Set 1
                if ((Close[0] > Close[1])
                     && (Close[1] > Close[2])
                     && ((Close[0] - (Open[0]))  >= 5)
                     && ((Close[1] - (Open[1]))  >= 5) && Position.MarketPosition == MarketPosition.Flat )
                {
                    Print(Time[0] + " Adding Long Position");
                    Draw.ArrowUp(this, @"EEdgeScalping Arrow up_1", true, 0, (Low[0] - 1) , Brushes.Lime);
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    StopLoss = 0;
                    ProfitTarget = 0;
                }
    
    
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    Print(Time[0] + " Setting ProfitTarget and StopLoss");
                    if (ProfitTarget == 0 || ProfitTarget < Close[2])
                    {
                        if (ProfitTarget == 0)
                        {
                            ProfitTarget = Position.AveragePrice + 10;
                        }
                        else
                        {
                             ProfitTarget = Close[2];    
                        }
                        Print(Time[0] + " Setting ProfitTarget to " + ProfitTarget);
                        SetProfitTarget("", CalculationMode.Price, ProfitTarget);
    
                    }
    
    
                      if (StopLoss == 0 || Open[2] > StopLoss)
                      {
                          StopLoss = Open[2];
                          Print(Time[0] + " SettingStopLoss to " + StopLoss);
                          SetStopLoss("", CalculationMode.Price, StopLoss, false);
                      }
    
                }
            }
        }
    }
    ​

    I am not sure why my code is not entering if (Position.MarketPosition == MarketPosition.Long) -when I am adding Long Position.


    Please see the log.

    HTML Code:
    12/13/2023 5:40:00 AM Adding Long Position
    12/13/2023 5:45:00 AM Setting ProfitTarget and StopLoss
    12/13/2023 5:45:00 AM Setting ProfitTarget to 16630.75
    12/13/2023 5:45:00 AM SettingStopLoss to 16606.5
    12/13/2023 5:50:00 AM Setting ProfitTarget and StopLoss
    12/13/2023 5:50:00 AM SettingStopLoss to 16611.5
    12/13/2023 5:55:00 AM Adding Long Position
    12/13/2023 7:45:00 AM Adding Long Position
    12/13/2023 11:10:00 AM Adding Long Position
    12/13/2023 11:25:00 AM Adding Long Position
    12/13/2023 11:40:00 AM Adding Long Position
    12/13/2023 11:45:00 AM Adding Long Position
    12/13/2023 12:35:00 PM Adding Long Position
    12/13/2023 12:40:00 PM Adding Long Position
    12/13/2023 4:20:00 PM Adding Long Position
    12/14/2023 12:30:00 AM Adding Long Position
    12/14/2023 5:45:00 AM Adding Long Position
    12/14/2023 8:50:00 AM Adding Long Position
    12/14/2023 11:05:00 AM Adding Long Position
    12/14/2023 11:20:00 AM Adding Long Position
    12/14/2023 11:25:00 AM Adding Long Position
    12/14/2023 11:30:00 AM Adding Long Position
    12/14/2023 3:20:00 PM Adding Long Position
    12/15/2023 3:15:00 AM Adding Long Position
    12/15/2023 6:00:00 AM Adding Long Position
    12/15/2023 6:05:00 AM Adding Long Position
    12/15/2023 6:40:00 AM Adding Long Position
    12/15/2023 6:45:00 AM Adding Long Position
    12/15/2023 7:10:00 AM Adding Long Position
    12/15/2023 7:55:00 AM Adding Long Position
    12/15/2023 8:25:00 AM Adding Long Position
    12/15/2023 8:30:00 AM Adding Long Position
    12/15/2023 10:30:00 AM Adding Long Position
    12/15/2023 11:10:00 AM Adding Long Position
    12/15/2023 12:55:00 PM Adding Long Position
    12/17/2023 11:45:00 PM Adding Long Position
    12/18/2023 12:40:00 AM Adding Long Position
    12/18/2023 7:00:00 AM Adding Long Position
    12/18/2023 7:05:00 AM Adding Long Position
    12/18/2023 8:15:00 AM Adding Long Position
    12/18/2023 8:20:00 AM Adding Long Position
    12/18/2023 8:35:00 AM Adding Long Position
    12/18/2023 9:10:00 AM Adding Long Position
    12/18/2023 11:45:00 AM Adding Long Position
    12/19/2023 3:30:00 AM Adding Long Position
    12/19/2023 4:20:00 AM Adding Long Position
    12/19/2023 6:40:00 AM Adding Long Position
    12/19/2023 7:15:00 AM Adding Long Position
    12/19/2023 7:55:00 AM Adding Long Position
    12/19/2023 8:45:00 AM Adding Long Position
    12/19/2023 10:30:00 AM Adding Long Position
    12/19/2023 1:00:00 PM Adding Long Position
    12/20/2023 6:35:00 AM Adding Long Position
    12/20/2023 6:40:00 AM Adding Long Position
    12/20/2023 6:45:00 AM Adding Long Position
    12/20/2023 7:10:00 AM Adding Long Position
    12/20/2023 7:25:00 AM Adding Long Position
    12/20/2023 9:45:00 AM Adding Long Position
    12/21/2023 1:15:00 AM Adding Long Position
    12/21/2023 3:10:00 AM Adding Long Position
    12/21/2023 5:40:00 AM Adding Long Position
    12/21/2023 5:45:00 AM Adding Long Position
    12/21/2023 7:20:00 AM Adding Long Position
    12/21/2023 7:25:00 AM Adding Long Position
    12/21/2023 8:05:00 AM Adding Long Position
    12/21/2023 8:30:00 AM Adding Long Position
    12/21/2023 11:00:00 AM Adding Long Position
    12/21/2023 11:35:00 AM Adding Long Position
    12/21/2023 12:20:00 PM Adding Long Position
    12/21/2023 12:25:00 PM Adding Long Position
    12/21/2023 12:30:00 PM Adding Long Position
    12/21/2023 12:55:00 PM Adding Long Position
    12/22/2023 12:35:00 AM Adding Long Position
    12/22/2023 5:45:00 AM Adding Long Position
    12/22/2023 6:45:00 AM Adding Long Position
    12/22/2023 11:45:00 AM Adding Long Position
    12/22/2023 11:50:00 AM Adding Long Position
    12/22/2023 11:55:00 AM Adding Long Position
    12/22/2023 12:00:00 PM Adding Long Position
    12/26/2023 1:35:00 AM Adding Long Position
    12/26/2023 7:40:00 AM Adding Long Position
    12/26/2023 8:15:00 AM Adding Long Position
    12/26/2023 8:35:00 AM Adding Long Position
    12/26/2023 9:50:00 AM Adding Long Position
    12/26/2023 12:30:00 PM Adding Long Position
    12/26/2023 12:35:00 PM Adding Long Position
    12/27/2023 10:10:00 AM Adding Long Position
    12/27/2023 11:25:00 AM Adding Long Position
    12/27/2023 11:30:00 AM Adding Long Position
    12/27/2023 12:05:00 PM Adding Long Position
    12/27/2023 1:05:00 PM Adding Long Position
    12/28/2023 7:30:00 AM Adding Long Position
    12/28/2023 10:40:00 AM Adding Long Position
    12/28/2023 12:45:00 PM Adding Long Position
    12/28/2023 1:05:00 PM Adding Long Position
    Enabling NinjaScript strategy 'EEdgeScalpingUnlocked/312501231' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=By strategy position ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    ​

    #2
    Added Method OnOrder Update but unable to dignose.

    Code:
    #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 EEdgeScalpingUnlocked : Strategy
        {
            private double StopLoss, ProfitTarget;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "EEdgeScalpingUnlocked";
                    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                                    = true;
                    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)
                {
    
                }
                    else if (State == State.DataLoaded)
                {                
    
                    ClearOutputWindow();
                }
            }
    
            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                                                  int quantity, int filled, double averageFillPrice,
                                                  Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
                Print(Time[0] + " DEBUG The most current order state is: " + order.OrderState);   // OrderState.PartFilled
                  Print(Time[0] + " DEBUG This particular order update state is: " + orderState); // OrderState.Working
                Print(Time[0] + "Order Object: " + order.ToString());
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 3)
                    return;
    
                 // Set 1
                if ((Close[0] > Close[1])
                     && (Close[1] > Close[2])
                     && ((Close[0] - (Open[0]))  >= 5)
                     && ((Close[1] - (Open[1]))  >= 5) && Position.MarketPosition == MarketPosition.Flat )
                {
                    Print(Time[0] + " Adding Long Position");
                    Draw.ArrowUp(this, @"EEdgeScalping Arrow up_1", true, 0, (Low[0] - 1) , Brushes.Lime);
                    EnterLong();
                    StopLoss = 0;
                    ProfitTarget = 0;
                }
    
                Print(Time[0] + " MarketPosition.Long = " + MarketPosition.Long + " Position.MarketPosition: "  + Position.MarketPosition);
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    Print(Time[0] + " Setting ProfitTarget and StopLoss");
                    if (ProfitTarget == 0 || ProfitTarget < Close[2])
                    {
                        if (ProfitTarget == 0)
                        {
                            ProfitTarget = Position.AveragePrice + 10;
                        }
                        else
                        {
                             ProfitTarget = Close[2];    
                        }
                        Print(Time[0] + " Setting ProfitTarget to " + ProfitTarget);
                        SetProfitTarget("", CalculationMode.Price, ProfitTarget);
    
                    }
    
    
                      if (StopLoss == 0 || Open[2] > StopLoss)
                      {
                          StopLoss = Open[2];
                          Print(Time[0] + " SettingStopLoss to " + StopLoss);
                          SetStopLoss("", CalculationMode.Price, StopLoss, false);
                      }
    
                }
            }
        }
    }
    ​
    Attached Files

    Comment


      #3
      Hello futtrader,

      Thank you for your post.

      Are there any errors in the log tab of the Control Center?

      I see you have print statements for some conditions, please add prints outside all conditions that print the date time of the bar and all values compared in every condition that places an order and enable TraceOrders.

      TraceOrders will confirm if EntriesPerDirection is causing orders to be ignored.


      To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

      In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

      The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

      Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

      Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

      I am happy to assist you with analyzing the output from the output window.

      Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

      Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

      https://ninjatrader.com/support/foru...121#post791121

      Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      62 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 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