Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move to breakeven

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

    Move to breakeven

    In the following code if the profit in a long position moves above 8 tics I want to move the stop loss to entry price + 2 tics. Does the implementation look correct (called from OnBarUpdate)?

    private
    void MoveStopToBreakeven()
    {
    // profit trigger move stop to breakeven
    if(Position.MarketPosition == MarketPosition.Long)
    {
    double profit = GetCurrentBid() - Position.AvgPrice;

    if(profit >= TickSize* 8)
    {
    SetStopLoss(
    "LongEntryGem",CalculationMode.Price,Position.AvgPrice+TickSize* 2,true);


    }
    }
    }

    There is something in the help files about resetting the StopLoss to default value after the trade is exited. Is this the right idea:

    protectedoverridevoid OnPositionUpdate(IPosition position)
    {
    if (position.MarketPosition == MarketPosition.Flat)
    {


    SetStopLoss"LongEntryGem",CalculationMode.Ticks,10,true);

    }
    }

    #2
    Looks correct. You can also take a look at this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=3222
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Stop Loss

      I have changed the following line in the previous code snippet:

      SetStopLoss("LongEntryGem",CalculationMode.Price,Position.AvgPrice+TickSize* 2,true);

      to

      ExitLongStop(Position.AvgPrice+TickSize*2,"LongEntryGem");

      as in one of the help file examples.

      I am trying to verify that the stop loss has been changed to the new level so that in this case the stop is moved to position entry + 2 tics

      In the OnOrderUpdate I use:

      if(LongOrderGem != null)
      {
      Print(
      "LongEntryGem stop price = " + LongOrderGem.StopPrice.ToString());
      }

      where LongOrderGem is the IOrder object created when the position was opened but the print outputs a value of zero. Do I have to use the IOrder object created by the ExitLongStop statement which I'm not using here?

      Comment


        #4
        Yes. Just because you named your order with a signal name does not mean your IOrder object is named that same name. You need to actually assign it an IOrder object then you can access its values.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Order

          Thanks I have changed the implementation to:

          LongStopGem = ExitLongStop(Position.AvgPrice+TickSize*2,"LongEntryGem");

          Where LongStopGem is of type IOrder

          In the following code:

          protectedoverridevoid OnPositionUpdate(IPosition position)
          {
          if (position.MarketPosition == MarketPosition.Flat)
          {
          Print(
          "Order Exited " + DateTime.Now.ToLongTimeString());
          }

          if (position.MarketPosition == MarketPosition.Long)
          {
          Print(
          "Long Order Entered " + DateTime.Now.ToLongTimeString());
          }

          if (position.MarketPosition == MarketPosition.Short)
          {
          Print(
          "Short Order Entered " + DateTime.Now.ToLongTimeString());
          }

          }

          How would I calculate which position has been exited or entered i.e. I want the signal name?

          Comment


            #6
            What do you mean by which position? There is no differentiation between positions created by order A and order B for the same instrument.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Position

              For example in my strategy I have two different systems A & B which can generate long and short entries so my signals are named "LongA", "LongB", "ShortA","ShortB"

              In the position update event I would like to know if a long position has been entered whether is was "LongA" or "Long"

              If a position has been flattened which was it?

              Comment


                #8
                You should do this in OnExecution. There is no differentiation from a position stand point since there is only one position held by your strategy per instrument.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Order

                  In the following code I am trying to move the stop loss for a long order to breakeven + 2 tics when the position is in profit by 8 tics. Is the implementation correct?

                  protectedoverridevoid OnExecution(IExecution execution)
                  {
                  if (LongOrderGem != null && LongOrderGem.Token == execution.Order.Token)
                  {
                  if(execution.Order.OrderState == OrderState.Filled)
                  {
                  if(GetCurrentBid()- execution.Order.AvgFillPrice >= 8*TickSize)
                  {
                  // Stop-Loss order 4 ticks below our entry price
                  LongStopGem = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 2* TickSize, "LongStopGem", "LongEntryGem");

                  // Resets the entryOrder object to null after the order has been filled or partially filled
                  LongOrderGem = null;

                  Print(
                  "Stop loss moved to BREAKEVEN");
                  }
                  }
                  }

                  // Reset our stop order and target orders' IOrder objects after our position is closed.
                  if ((LongStopGem != null && LongStopGem.Token == execution.Order.Token))
                  {
                  if (execution.Order.OrderState == OrderState.Filled)
                  {
                  LongStopGem =
                  null;
                  }
                  }

                  }

                  Is it a good idea to remove the reference to the order:

                  LongOrderGem = null; as in the above code? I based this on the example code in SampleOnOrderUpdate.

                  Comment


                    #10
                    It could work. You will have to run it and see if it does what you want at this point.

                    You should set to null only when you are done using that IOrder object. Otherwise do not set it to null.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Order

                      It doesnt seem to work when backtesting the strategy using market replay. I think its because the OnExecution event is only fired when an order state is changed but I want to check that the position is in profit by 8 tics continuously by calling a function from the OnBarUpdate() method.

                      So OnBarUpdate()

                      calls the MoveOrderStop() - this moves stop loss order using ExitLongStop() when order in profit by 8 tics

                      And then monitor the stop loss order in
                      OnExecution()

                      Does that sound like a valid approach?

                      Comment


                        #12
                        OnExecution() only triggers when you receive an execution event. For sure this is not continuous.

                        Continuous monitoring can be done in OnBarUpdate() with CalculateOnBarClose = false. OnExecution() and OnOrderUpdate() will only update when their events are triggered.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Stop Loss

                          I'm using the following line in a function that is called by the OnBarUpdate() event:

                          LongStopGem = ExitLongStop(Position.AvgPrice+TickSize*commission ,"LongStopGem","LongEntryGem");

                          However when I try to reference the variable LongStopGem in both the OnExecution event and OnOrderUpdate event to see if the stop loss order has been filled this variable is always null and the inside Print() never fires:

                          if(LongStopGem != null)
                          {
                          if(LongStopGem.Token == order.Token)
                          {
                          Print(
                          "Order is " + order.ToString() + " \n");

                          }
                          }

                          When the original order was placed a stop loss was set then. Could this be causing the problem? An output of all the orders and executions seems to indicate that the sell stop order is never initialized.



                          Comment


                            #14
                            Where did you declare LongStopGem in? It should be declared in the Variables region of your code and not in OnBarUpdate().
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Variable

                              Yes its declared in the variables section along with all the other IOrder objects. These are all set to null in OnPositionUpdate when a position is flattened:

                              protectedoverridevoid OnPositionUpdate(IPosition position)
                              {
                              if (position.MarketPosition == MarketPosition.Flat)
                              {
                              // reset variables here
                              LongOrderTriggerGem = false;
                              ShortOrderTriggerGem =
                              false;

                              longprofittrigger =
                              false;
                              shortprofittrigger =
                              false;

                              LongOrderGem =
                              null;
                              ShortOrderGem =
                              null;
                              LongOrderCrosses =
                              null;
                              ShortOrderCrosses =
                              null;

                              LongStopGem =
                              null;
                              ShortStopGem =
                              null;
                              LongStopCrosses =
                              null;
                              ShortStopCrosses =
                              null;


                              Print(
                              "Order Exited " + DateTime.Now.ToLongTimeString() + " " + instrumentname);
                              }

                              if (position.MarketPosition == MarketPosition.Long)
                              {
                              Print(
                              "Long Order Entered " + DateTime.Now.ToLongTimeString() + " " + instrumentname);
                              }

                              if (position.MarketPosition == MarketPosition.Short)
                              {
                              Print(
                              "Short Order Entered " + DateTime.Now.ToLongTimeString() + " " + instrumentname);
                              }

                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sjsj2732, Yesterday, 04:31 AM
                              0 responses
                              38 views
                              0 likes
                              Last Post sjsj2732  
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              287 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              289 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              134 views
                              1 like
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              95 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Working...
                              X