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

understanding my logic

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

    understanding my logic

    I have a strategy but am a little confused. I turned it on last night and wokeup to see if it is working. It did add positions but I dont understand why I had 2 positions running instead of 1.


    if(CheckLogic() && bUpTrend == false )
    {
    bUpTrend = true;
    bDownTrend = false;

    if(Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(1,"LongEntry");
    }
    else if(Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort("ShortEntry");
    EnterLong(1,"LongEntry");

    }




    }

    if(CheckLogic() && bDownTrend == false )
    {
    bUpTrend = false;
    bDownTrend = true;

    if(Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort(1,"ShortEntry");
    }
    else if(Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong("LongEntry");
    EnterShort(1,"ShortEntry");

    }


    }

    why would I have 2 positions?

    #2
    Hello ballboy11,

    Thank you for your note.

    What do you have your EntriesPerDIrection set to? I'd make sure it's set to 1.

    When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

    If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

    The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

    From the help guide:
    "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."



    If you're still experiencing odd behavior after ensuring you're not calling both an entry and exit in the same run of OnBarUpdate, I would suggest adding prints to your code that may help you see what the variable values are at different stages of code execution. This should assist you with locating any issues in your logic that may cause issues such as you're experiencing.

    This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.



    If you run into issues like you're seeing here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently. With the printout information you can assess what is different between the two.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thank you I get it now, Last question on managed strategies, If I set a stop loss and switch, will the stoploss be cancelled as well



      if(Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("ShortEntry",CalculationMode.Price, dHighPrice + 2* TickSize,true);
      EnterShort(1,"ShortEntry");

      }
      else if(Position.MarketPosition == MarketPosition.Long)
      {
      SetStopLoss("ShortEntry",CalculationMode.Price, dHighPrice + 2* TickSize,true);
      EnterShort(1,"ShortEntry");

      }


      and vice versa to close position and reverse


      if(Position.MarketPosition == MarketPosition.Flat)
      { SetStopLoss("LongEntry",CalculationMode.Price, dLowPrice - 2* TickSize,true);
      EnterLong(1,"LongEntry");

      }
      else if(Position.MarketPosition == MarketPosition.Short)
      {
      SetStopLoss("LongEntry",CalculationMode.Price, dLowPrice - 2* TickSize,true);
      EnterLong(1,"LongEntry");


      }

      Comment


        #4
        Hello ballboy11,

        Thank you for your reply.

        Yes, you're correct. When the initial trade is exited, the associated stop loss will also be cancelled.

        Here is a link to our help guide page on SetStopLoss():


        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        382 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        1 view
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Started by judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Working...
        X