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

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.
    Emily C.NinjaTrader Customer Service

    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 Creamers, 04-27-2024, 05:32 AM
      13 responses
      74 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by tradebot, Yesterday, 01:25 PM
      4 responses
      18 views
      0 likes
      Last Post tradebot  
      Started by manueldecastro, Yesterday, 10:26 AM
      5 responses
      24 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by memonic, 05-01-2024, 01:23 PM
      5 responses
      31 views
      0 likes
      Last Post memonic
      by memonic
       
      Started by dcriador, Yesterday, 10:45 AM
      2 responses
      21 views
      0 likes
      Last Post dcriador  
      Working...
      X