Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade not executing but Output prints all conditions true

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

    Trade not executing but Output prints all conditions true

    Hi everyone,

    I am currently building a NinjaScript Strategy which I am testing in BarReplay Mode.
    In the strategy I have these lines under the OnBarUpdate() method (the _crosslong, _macdthreshold.. etc. conditions are being initialized beforehand):

    var _enterlong = _crosslong && _macdthresholdlong && _stochlong && timefilter;
    if (_enterlong)
    {
    Print("All long entry conditions met! Attempting to enter long...");
    }

    var _entershort = _crossshort && _macdthresholdshort && _stochshort && timefilter;
    if (_entershort)
    {
    Print("All short entry conditions met! Attempting to enter short...");
    }

    followed by these lines which I have copy pasted from the SampleAtmStrategy:

    // LONG ENTRY
    if (_enterlong && orderId.Length == 0 && atmStrategyId.Length == 0)
    {
    isAtmStrategyCreated = false; // reset atm strategy created check to false
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(OrderAction.Buy, OrderType.Market, Low[0], 0, TimeInForce.Day, orderId, _ATMstrategy, atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
    //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
    if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
    isAtmStrategyCreated = true;
    lastTradeBar = CurrentBar;
    });
    }

    //SHORT ENTRY
    ....

    Now, when I see all my conditions begin met (I have the indicators plotted on the Chart via State.DataLoaded), the Output window prints "All long entry conditions met! Attempting to enter long..."
    But still, it doesn't open a position.

    However, when I replace the _entershort in the entry conditions with only _crosslong for example, it will execute trades.

    I've tried everything to debug, using Print methods and even enabled TraceOrders, but somehow it doesn't submit orders, when using the _enterlong variable.

    Any idea what might be causing this?

    Cheers,
    totalnewb (fitting, I know)

    #2
    and traceorders are not showing a reason to reject the long order? I'm assuming you already tried to put Print statements within the section of code for your long and short entries. Could you possibly show the difference, if any, of your long and short entry code as well as the output?

    Comment


      #3
      Hey,

      TraceOrders is not showing anything at all. Not even an Order submitted.
      I have tried putting prints after each check, and there's an output. The reason must be somewhere in the Entry Order.

      If I put the Prints like I showed above, it prints out that all Conditions are met.

      If I put the Prints after the Entry Check, e.g. here:

      // LONG ENTRY
      if (_enterlong && orderId.Length == 0 && atmStrategyId.Length == 0)
      {
      Print("All long entry conditions met! Attempting to enter long...");
      isAtmStrategyCreated = false; // reset atm strategy created check to false
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(OrderAction.Buy, OrderType.Market, Low[0], 0, TimeInForce.Day, orderId, _ATMstrategy, atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
      //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
      if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
      isAtmStrategyCreated = true;
      lastTradeBar = CurrentBar;
      });
      }​

      It doesn't print. What is bugging me is if I replace the _enterlong variable with one of the other ones, it does execute.

      Long and Short doesn't matter, it doesn't work either way. And besides, they're pretty much identical aside from the OrderAction, because they're ATM Orders.

      Comment


        #4
        Ok. Adding that print would rule out the orderID.Length and atmStrategyId.Length as the culprits. What I would do, and have done when troubleshooting, is to put prints for everything, if not to see exactly when things stops working. So adding Print("atmStrategyId " + atmStrategyId); after it being set to make sure it's being set to something. repeat for orderID. I try putting it at the start of checking if the atm strategy create resulting in an error...something like Print("entering this block of code..."); or something.

        You can always get rid of those print statements after everything is up and running. Try adding more print statements and see what happens. It sounds like it should be relatively easy to figure this out.

        Comment


          #5
          Thanks for the tip with Printing absolutely everything!
          I've boiled it down to my second and third entry conditions.
          Somehow, it doesn't trigger this MACD calculation.

          if (orderId.Length == 0 && atmStrategyId.Length == 0)
          {
          Print("atmStrategyId " + atmStrategyId);
          if (MACD(8, 21, 5)[0] >= X)
          {
          Print("MACD Thresh reached");

          Even though I've initiated the MACD indicator and I'm plotting it on the chart correctly (plus if I print it during each BarUpdate, it shows me that the value is way beyond my threshold.

          Any idea why that is?

          Comment


            #6
            I have now started a new Strategy script that utilises basic EnterLong and EnterShort methods.
            After initialising the indicators exactly the same and using the same entry logic (even the same var names), it enters trades.

            I am thinking I need to completely start the script from scratch....

            Anyways, thanks for your help!

            Comment


              #7
              yeah, no problem. I was gonna say, to use a print statement for the MACD to see what the number is producing and see if that matches what you are expecting. I tried to put a random plot of the MACD just to see what would happen and my value happened to be -1.581 something. If you wanted to go back to it if you wanna work it out.

              I've had to completely rethink things from time to time. Start another thread if you run into any other problems. Good luck.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rubiijonson, Today, 01:02 AM
              0 responses
              4 views
              0 likes
              Last Post rubiijonson  
              Started by fkronloff, Today, 12:25 AM
              0 responses
              3 views
              0 likes
              Last Post fkronloff  
              Started by Zadomani, Yesterday, 11:39 PM
              0 responses
              10 views
              0 likes
              Last Post Zadomani  
              Started by ToNovy, 01-27-2025, 11:22 PM
              20 responses
              197 views
              0 likes
              Last Post ToNovy
              by ToNovy
               
              Started by hypercube, 03-07-2025, 12:22 AM
              2 responses
              38 views
              0 likes
              Last Post hypercube  
              Working...
              X