Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple Reverse System Problems

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

    Simple Reverse System Problems

    I'm trying to create a simple system that places a Buy Stop at the High of a bar and a Sell Stop at the low of a bar. I realize you can't have opposing orders so I'm taken one side to get the system going, then if the system is long for instance, having a protective stop move up to each successive closing bars' low, and the opposite for short positions. The code below works except for the following issues:

    1) If it's filled for 1 contract Long, the Stop called in the OnExecution section only has 1 contract to sell, instead of 2 so the system can close out the long and initiate a short position (but the code asks for 2)? The OnBarUpdate section works fine for long positions on subsequent bars.
    2) On short positions, the OnExecution section doesn't place a protective stop as requested, but only when the bar closes will it update to 2 contracts, one to cover and one to go long in the OnBarUpdate section.

    So two different issues depending on direction but exactly the same code? Am I missing something simple in the following code:


    Code:
     
    protectedoverridevoid OnBarUpdate()
     
    {
    {//Start of Entry Logic
     
    if (Low[0] >= Low[1] && Position.MarketPosition == MarketPosition.Flat)
    { 
    entryOrder = EnterShortStop(1,Low[0]-.25, "MyEntry");
    }
     
    {
    entryOrder = EnterLongStop(1,High[0]+.25, "MyEntry");
    } 
    }//End of Entry Logic
    {
    if (Position.MarketPosition == MarketPosition.Long)//Adjust stop for Long positions on every new bar.
     
    {
    stopOrder = ExitLongStop(0, true,2, Low[0]-.25, "MyStop", "MyEntry");
    }
    if (Position.MarketPosition == MarketPosition.Short)//Adjust stop for Short positions on every new bar.
    {
    stopOrder = ExitShortStop(0, true,2, High[0]+.25, "MyStop", "MyEntry");
    }
     
    }
    }
     
     
    protectedoverridevoid OnExecution(IExecution execution)
    {
     
    if (entryOrder != null && entryOrder.Token == execution.Order.Token)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    stopOrder = ExitLongStop(0, true, 2,Low[0]-.25, "MyStop", "MyEntry");
     
    }
    if (Position.MarketPosition == MarketPosition.Short)
    {
    stopOrder = ExitShortStop(0, true, 2, High[0]+.25, "MyStop", "MyEntry");
    } 
    // Resets the entryOrder object to null after the order has been filled or partially filled
     
    if (execution.Order.OrderState != OrderState.PartFilled)
     
    entryOrder = null;
     
    }
    }
     
    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder = null;
    // targetOrder = null;
    }
    }
    }
    Thanks for any advice...Dave

    #2
    Dave,

    Sorry not following what you mean by 1 and 2 contracts. If you are in a long 1 position, placing an exit for 2 contracts is not advisable.

    Also, this part of your code probably does not work as desired:
    Code:
    if (Low[0] >= Low[1] && Position.MarketPosition == MarketPosition.Flat)
    { 
    entryOrder = EnterShortStop(1,Low[0]-.25, "MyEntry");
    }
     
    {
    entryOrder = EnterLongStop(1,High[0]+.25, "MyEntry");}
    You are { } bracketing your long stop with no statement or logic as to when to use the long as opposed to the short.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Always in the market

      Josh, I'm trying to have the system ALWAYS be in the market, so the Stops serve both to close the current long, and then initiate a short at the same time, and vice versa.

      I see what you mean about my initial entry from a flat position; unfortunately that part works and the rest doesn't. Is there sample code that shows a stop and reverse system that works? Something as simple as long above the Open[0] and short below the Open[0], or some other pivot point?

      Thanks for your help .

      [quote=NinjaTrader_Josh;83625]Dave,

      Sorry not following what you mean by 1 and 2 contracts. If you are in a long 1 position, placing an exit for 2 contracts is not advisable.

      quote]

      Comment


        #4
        Dave,

        You need to adjust that part since it is likely throwing off your whole code throughout. You are even overwriting your prior IOrder too.

        You should not use exit orders to try to get you in reverse positions. If you want to stay in the market just submit your EnterLongStop(), after it fills submit your EnterShortStop(). The limitation is you can't have both working at the same time. You need one to fill and then you are free to submit the other one which will reverse you.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Okay, thanks Josh. I'll make those changes and give it a try.

          Originally posted by NinjaTrader_Josh View Post
          Dave,

          You need to adjust that part since it is likely throwing off your whole code throughout. You are even overwriting your prior IOrder too.

          You should not use exit orders to try to get you in reverse positions. If you want to stay in the market just submit your EnterLongStop(), after it fills submit your EnterShortStop(). The limitation is you can't have both working at the same time. You need one to fill and then you are free to submit the other one which will reverse you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by RideMe, 04-07-2024, 04:54 PM
          9 responses
          53 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by businessman1929, Yesterday, 01:28 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Touch-Ups, 04-27-2024, 10:36 AM
          3 responses
          30 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by D Trader, 05-02-2017, 09:12 AM
          21 responses
          4,289 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by leojimenezp, 04-20-2024, 05:49 PM
          3 responses
          38 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X