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

Managed approach EnterLongLimit

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

    Managed approach EnterLongLimit

    I created a strategy with 2 seperate EnterLongLimit at the same price. The order is created but for some reason the next bar the order gets cancelled.

    The questions I have are the following


    Can one SetStopLoss AND SetProfitTarget before the the order is executed?

    If not do I setStopLoss and ProfitTarget on OnPostionUpdate OR OnExecttionUpdate?



    if(Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort("Short1");

    if(nTarget2Contracts != 0)
    ExitShort("Short2");

    }

    if(Position.MarketPosition == MarketPosition.Flat)
    {

    SetProfitTarget("Long1",CalculationMode.Price,dPri ceA,true);
    SetProfitTarget("Long2",CalculationMode.Price,dPri ceB,true);
    SetStopLoss("Long1",CalculationMode.Price,dStopLos sA,false);
    if(nTarget2Contracts != 0)
    SetStopLoss("Long2",CalculationMode.Price,dStopLos sA,false);


    EnterLongLimit(nTarget1Contracts,dLimitPriceA,"Lon g1");
    if(nTarget2Contracts != 0)
    EnterLongLimit(nTarget2Contracts,dLimitPriceA,"Lon g2");
    }



    #2
    Hello ballboy11,

    If isLiveUntilCancelled is not used, it defaults to false and the order will be cancelled when the submission bar closes if the order is not resubmitted.

    Below is a link to the help guide. Please see the definition for isLiveUntilCancelled.



    Set methods should be set before the entry order is placed. This is because they cannot be unset and will continue to use old values for new orders.
    Below is a link to an example that uses set methods and sets these before the entry is placed.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3

      I added the OnOrderUpdate Logic and I am starting to have Issues
      If I keep the OnOrderLogic update out and my order is not filled, my limit order will stay activated until filled.

      I need to cancel all my orders and re submit my new limit order.


      private Order oEntryOrder1 = null;




      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
      double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {

      if (order.Name == "Long1")
      oEntryOrder1 = order;

      if (order.Name == "Short1")
      oEntryOrder1 = order;

      // Evaluates for all updates to myEntryOrder.
      if (oEntryOrder1 != null && oEntryOrder1 == order)
      {
      // Check if myEntryOrder is cancelled.
      if (oEntryOrder1.OrderState == OrderState.Cancelled)
      {
      // Reset myEntryOrder back to null
      oEntryOrder1 = null;
      }
      }
      }

      //bar update logic

      if(CurrentBar>3)
      {

      if(bShort)
      {

      bLong = true;
      bShort = false;

      //************************************************** ************************
      if (oEntryOrder1 != null) //THIS IS THE AREA I AM HAVING PROBLEMS WITH
      CancelOrder(oEntryOrder1);
      //************************************************** ****************************
      if(Position.MarketPosition == MarketPosition.Short)
      {
      ExitShort("Short1");
      }


      if(Position.MarketPosition == MarketPosition.Flat)
      {
      {

      SetProfitTarget("Long1",CalculationMode.Price,dTar get,true);
      SetStopLoss("Long1",CalculationMode.Price,dStopLos s,false);
      EnterLongLimit(0,true,nTarget1Contracts,dLimitPric e,"Long1");
      }
      }
      }
      else
      if(bLong)
      {
      bLong = false;
      bShort = true;

      //************************************************** *********************************************
      if (oEntryOrder1 != null)
      CancelOrder(oEntryOrder1);
      //************************************************** ***************************************
      if(Position.MarketPosition == MarketPosition.Long)

      {
      ExitLong("Long1");
      }




      if(Position.MarketPosition == MarketPosition.Flat)
      {

      SetProfitTarget("Short1",CalculationMode.Price,dTa rget,true);
      SetStopLoss("Short1",CalculationMode.Price,dStopLo ss,false);
      EnterShortLimit(0, true,nTarget1Contracts,dLimitPrice,"Short1");

      }


      }

      }








      Comment


        #4
        Hello ballboy11,

        What is the issue you are having?

        Is an error appearing?

        What is the error message?

        Is the order in a working or accepted state when supplying to CancelOrder()?

        Why are you cancelling an order and resubmitting it instead of keeping it alive and changing the price if desired?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I just got this error when it was transitioning from a buy to a sell

          Comment


            #6
            Hello ballboy11,

            The error indicates a historical order was attempted to be transitioned to a live order without using GetRealtimeOrder() on that order.

            You will need to use GetRealtimeOrder() in State.Realtime to transition an order from historical to live.

            Below is a link to the help guide.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I read the GetRealTimeOrder but I am confused. I thought my logic closed the order.
              Where would I use the GetRealTimeOrder????



              if(Momentum[0] <Middle[0] && bCrossedDown == false)
              {
              bCrossedUp = false; //should only enter once until next crossover
              bCrossedDown = true;

              bManualStopMovement =false; //set until double clicked



              if(Position.MarketPosition == MarketPosition.Long)

              {
              if(nTarget1Contracts != 0)
              ExitLong("Long1"); // if contract still live close position

              }
              else
              {
              if (oEntryOrder1 != null)
              CancelOrder(oEntryOrder1); // if postion active and not filled cancel order

              }



              if(Position.MarketPosition == MarketPosition.Flat)
              {


              if(nTarget1Contracts != 0 && bManualStopMovement ==false ) // not sure how to override manual stop
              {

              //ENTER ORDER

              SetProfitTarget("Short1",CalculationMode.Price,dAT R1,true);
              SetStopLoss("Short1",CalculationMode.Price,dATR8,f alse);


              if(bUseMar****rder == true)
              EnterShort(nTarget1Contracts,"Short1");
              else
              EnterShortLimit(0, true,nTarget1Contracts,dATR7,"Short1");
              }





              }


              }







              Comment


                #8
                Hello ballboy11,

                The error indicates that your strategy did not close the order in historical but it was modified after the script entered real-time. For these type of orders, you will need to transition them using GetRealtimeOrder exactly as the help guide shows. This can occur with the Order object you have used as you are manually controlling the orders lifecycle when using Order objects.

                You use the order object oEntryOrder1 in your code, so this is what you would use in place of "myOrder" which is what the help guide sample uses. You would need to make it look just like the sample in your script as well to avoid this message.

                I look forward to being of further assistance.

                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by traderqz, Today, 12:06 AM
                6 responses
                12 views
                0 likes
                Last Post traderqz  
                Started by Skifree, Today, 03:41 AM
                3 responses
                12 views
                0 likes
                Last Post Skifree
                by Skifree
                 
                Started by traderqz, Yesterday, 09:06 AM
                5 responses
                33 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by guillembm, Today, 11:25 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by owensd, 04-21-2024, 11:34 PM
                9 responses
                34 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X