Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating strategy with both long and short entries.

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

    Creating strategy with both long and short entries.

    I have created a strategy with both long and short entries.

    Here LE = calculated long entry price and SE = calculated short entry price.

    These levels are constant through out the trading session.
    protectedoverridevoid Initialize()
    {
    SetProfitTarget("", CalculationMode.Ticks, Target);
    SetStopLoss("", CalculationMode.Ticks, InitialStop, false);
    CalculateOnBarClose =
    false;
    }
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    if (CurrentBar > 1)
    {
    if (Bars.FirstBarOfSession)
    {
    myLongEntryOrder = null;
    myShortEntryOrder = null;
    LE = Open[0] – 10 *TickSize;
    SE = Open[0] + 10 *TickSize;
    }


    if (Position.MarketPosition == MarketPosition.Flat && LE > 0 && Low[0] > LE)
    {
    myLongEntryOrder = EnterLongLimit(LE);
    }
    if (Position.MarketPosition == MarketPosition.Flat && SE > 0 && High[0] < SE)
    {
    myShortEntryOrder = EnterShortLimit(SE);
    }
    }
    protected override void OnOrderUpdate(IOrder order)
    {
    if (myLongEntryOrder != null && myLongEntryOrder == order)
    {

    if (order.OrderState == OrderState.Filled)
    {myLongEntryOrder = null;}

    }

    if (myShortEntryOrder != null && myShortEntryOrder == order)
    {

    if (order.OrderState == OrderState.Filled)
    {myShortEntryOrder = null;}

    }
    if (ToTime(Time[0]) == ToTime(15, 10, 0))
    {
    myLongEntryOrder = null;
    myShortEntryOrder = null;
    }
    }

    I got an error with taking trades on one direction. What is the problem on code?
    Any suggestions? Please help?
    Last edited by Antony; 12-12-2011, 12:24 PM.

    #2
    Antony,

    What is the error message? This would be helpful in identifying your issue.

    Also, I would recommend using try/catch blocks. You can have it print to the output window and it will identify what line the error comes from in your code.

    Here is a reference sample : http://www.ninjatrader.com/support/f...ead.php?t=9825
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      **NT** An Enter() method to submit an entry order at '04-01-2011 0:00:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.

      Comment


        #4
        Antony,

        I believe you are opening a long limit while a short is open and/or vice verci.

        Here is an explanation :

        An order entry will be ignored if :
        • The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction

        If you want to do this you would need to use the unmanaged approach. Please find a link for more information below.

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by Antony View Post
          I have created a strategy with both long and short entries.

          Here LE = calculated long entry price and SE = calculated short entry price.

          These levels are constant through out the trading session.
          protectedoverridevoid Initialize()
          {
          SetProfitTarget("", CalculationMode.Ticks, Target);
          SetStopLoss("", CalculationMode.Ticks, InitialStop, false);
          CalculateOnBarClose =
          false;
          }
          protected override void OnBarUpdate()
          {
          if (CurrentBar < 1)
          return;
          if (CurrentBar > 1)
          {
          if (Bars.FirstBarOfSession)
          {
          myLongEntryOrder = null;
          myShortEntryOrder = null;
          LE = Open[0] – 10 *TickSize;
          SE = Open[0] + 10 *TickSize;
          }


          if (Position.MarketPosition == MarketPosition.Flat && LE > 0 && Low[0] > LE)
          {
          myLongEntryOrder = EnterLongLimit(LE);
          }
          if (Position.MarketPosition == MarketPosition.Flat && SE > 0 && High[0] < SE)
          {
          myShortEntryOrder = EnterShortLimit(SE);
          }
          }
          protected override void OnOrderUpdate(IOrder order)
          {
          if (myLongEntryOrder != null && myLongEntryOrder == order)
          {

          if (order.OrderState == OrderState.Filled)
          {myLongEntryOrder = null;}

          }

          if (myShortEntryOrder != null && myShortEntryOrder == order)
          {

          if (order.OrderState == OrderState.Filled)
          {myShortEntryOrder = null;}

          }
          if (ToTime(Time[0]) == ToTime(15, 10, 0))
          {
          myLongEntryOrder = null;
          myShortEntryOrder = null;
          }
          }

          I got an error with taking trades on one direction. What is the problem on code?
          Any suggestions? Please help?
          If I remember right, you can do that with StopLimit() orders, but you certainly cannot do it with Limit() orders, and certainly not if the orders are protected limit orders.

          As a matter of fact, you cannot even do it manually and achieve a result that makes sense. The first order will put you in long, and the next order would simply take you back flat, leaving your 4 protective OCO orders in place. Try it on the DOM in SIM and you will see what I mean.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          663 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          376 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          110 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          575 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          580 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X