Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to take a second trade- ATMStrategy

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

    Unable to take a second trade- ATMStrategy

    Hello NinjaTrader community,

    I hope you are all doing well. This is my first post here, and I must admit that I'm not a professional coder. I've been working on a trading strategy, and I'm currently facing an issue that has me stumped.

    My strategy takes only one trade and is unable to take a second trade even after my strategy criteria have been met. While I've gone through my code, everything seems fine to me, but I suspect I might have overlooked something.

    I'm sharing a snippet of my strategy code below. Please take a look and see if you can spot any potential issues. Your expertise would be highly appreciated. Thank you.



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Fast = 1;
    Slow = 18;

    //Description = @"Enter the description for your new custom Strategy here.";
    Name = "AkATMStrategyAndEMA";
    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)
    {
    smaFast = SMA(Fast);
    smaSlow = SMA(Slow);

    smaFast.Plots[0].Brush = Brushes.Goldenrod;
    smaSlow.Plots[0].Brush = Brushes.SeaGreen;

    AddChartIndicator(smaFast);
    AddChartIndicator(smaSlow);

    atmStrategyId = string.Empty;
    atmStrategyOrderId = string.Empty;
    }
    }

    protected override void OnBarUpdate()
    {
    if (State < State.Realtime)
    return;

    // Check if there's an active ATM strategy, and exit the method
    if (atmStrategyId.Length > 0)
    return;

    // Clear the ATM strategy ID after the trade is closed.
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    atmStrategyId = string.Empty;
    }

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (CrossAbove(smaFast, smaSlow, 1) && Close[0] > smaSlow[0])
    {
    atmStrategyId = GetAtmStrategyUniqueId();
    atmStrategyOrderId = GetAtmStrategyUniqueId();

    AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, High[0], 0, TimeInForce.Day,
    atmStrategyOrderId, "SampleATMEMA", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
    {
    if (atmCallbackId == atmStrategyId)
    {
    if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
    {
    isAtmStrategyCreated = true;
    }
    }
    });
    }
    else if (CrossBelow(smaFast, smaSlow, 1) && Close[0] < smaSlow[0])
    {
    atmStrategyId = GetAtmStrategyUniqueId();
    atmStrategyOrderId = GetAtmStrategyUniqueId();

    AtmStrategyCreate(OrderAction.Sell, OrderType.Limit, Low[0], 0, TimeInForce.Day,
    atmStrategyOrderId, "SampleATMEMA", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
    {
    if (atmCallbackId == atmStrategyId)
    {
    if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
    {
    isAtmStrategyCreated = true;
    }
    }
    });
    }
    }
    }​

    #2
    Hello ankitkundlu,

    Thank you for your post and welcome to the NinjaTrader forum community!

    I suggest adding print statements to debug and better understand your script. There is an overview of using prints for debugging here:


    What I suspect is that the following return statement might be causing your other logic in OnBarUpdate() to no longer be processed once the first atmStrategyId is created:
    Code:
    if (atmStrategyId.Length > 0)
    return;​
    You could confirm this by adding print statements throughout your script, especially ones that end up in a return, to see when those parts of your logic are being hit. I also suggest changing your check of the state to something such as if the State does not equal State.Realtime; I don't believe a less than comparison would make logical sense so it may not be evaluated. For example:
    Code:
    // if the state does not equal realtime, return
    if (State != State.Realtime)
    {
    Print(Time[0] + " State is not realtime.  State: " + State + " returning");
    return;
    }
    
    // Check if there's an active ATM strategy, and exit the method
    if (atmStrategyId.Length > 0)
    {
    Print(Time[0] " atmStrategyId.Length is less than 0.  Length: " + atmStrategyId.Length + " returning");
    return;
    }
    
    // Clear the ATM strategy ID after the trade is closed.
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    Print(Time[0] + " position is flat.  Emptying atmStrategyId string.");
    atmStrategyId = string.Empty;
    }​
    As you work through your script with print statements, this will help to identify areas of your logic that are not behaving as expected so you may modify your script and test again after each modification.

    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily,

      I wanted to let you know that you were right about the issue.

      Code:
      if (atmStrategyId.Length > 0)
              return;​

      Removing the return statement that you mentioned, which was preventing further processing of my logic, solved the problem. My code is now running as expected.

      Thank you for your support and expertise!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      116 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      61 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      40 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      43 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      82 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X