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

2 contracts instead of 1 per each signal

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

    2 contracts instead of 1 per each signal

    Hello, I created strategy. Key parts are:
    Code:
    protected override void Initialize()
    {
       SetTrailStop("MySignal", CalculationMode.Ticks, sl_size, false);
       TraceOrders = true;
       CalculateOnBarClose = false;
    }
    
    protected override void OnBarUpdate()
    {
         if (CheckLongConditions())
         {
            if (Position.MarketPosition != MarketPosition.Long) 
            {
              EnterLong(DefaultQuantity, "RenRev");
              Print("Enter LONG at " + Close[0]);
             }
          }
         if (CheckShortConditions())
         {
            if (Position.MarketPosition != MarketPosition.Short) 
            {
              EnterShort(DefaultQuantity, "RenRev");
              Print("Enter SHORT at " + Close[0]);
             }
          }
       }
    In fact is this reverse strategy - when exiting long, enter short. But for exit position I use only trailing stop.
    In backtest it works fine. But in live simulated trading, first trade is OK, but second trade (and all other trades) is realized with TWO contracts instead of one contract only.
    Problem is that I don't know, why does it behave this way.
    May be small screenshot should help anybody to answe me.
    On screen is first trade (11:41:57] with 1 contract, it exited at 12:10:04 using trail stop - and after that there is some Close positon which cause to sell second contract - but I don't want it!
    After this I enter new trade (its name is RenRev) and I add second short contract this way. After that again Trail stop with ONE contract and Close position with TWO contracts. What does it mean "Close position"? Why is it executed?
    Attached Files

    #2
    Please try changing DefaultQuantity to 1 in your code.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Default quantity is one contract already.

      Comment


        #4
        Please try hard coding it to 1.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I added line
          DefaultQuantity = 1;
          into Initialize() method, but behaviour is the same - first trade one contract, second and other trades 2 contracts.

          Comment


            #6
            What I am asking you to try is to not use DefaultQuantity and just hard code 1 into the order directly. Then be sure you have "Set order quantity" set to "by strategy".

            Then also use TraceOrders = true and see what the orders are submitting as. http://www.ninjatrader-support2.com/...ead.php?t=3627
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              OK, I have done it exactly as you had written. Result is the same, since second trade always two contracts.
              Code:
              6/30/2009 6:41:10 PM Entered internal PlaceOrder() method at 6/30/2009 6:41:10 PM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='RenRev' FromEntrySignal=''
              Enter LONG at 4815
              6/30/2009 6:47:06 PM Amended trail stop order: Order='a6f3d77449384117be3424af77a5f9ce/Sim101' Name='Trail stop' State=Accepted Instrument='FDAX 09-09' Action=Sell Limit price=0 Stop price=4813 Quantity=1 Strategy='RenkoRevTest1' Type=Stop Tif=Gtc Oco='51ec632489014a52ac111c6850a392d4' Filled=0 Fill price=0 Token='a6f3d77449384117be3424af77a5f9ce' Gtd='12/1/2099 12:00:00 AM'
              6/30/2009 7:04:09 PM Entered internal PlaceOrder() method at 6/30/2009 7:04:09 PM: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='RenRev' FromEntrySignal=''
              Enter SHORT at 4813
              6/30/2009 7:05:01 PM Amended trail stop order: Order='1960933586e041a5aa99df8bc18da209/Sim101' Name='Trail stop' State=Accepted Instrument='FDAX 09-09' Action=BuyToCover Limit price=0 Stop price=4815 Quantity=1 Strategy='RenkoRevTest1' Type=Stop Tif=Gtc Oco='edb75e922dbc487eade3d7bc463dc512' Filled=0 Fill price=0 Token='1960933586e041a5aa99df8bc18da209' Gtd='12/1/2099 12:00:00 AM'
              I suppose that main problem is with that "Close position". But I don't know, why is something like this executed ... ?
              Last edited by Lucinek; 06-30-2009, 11:34 AM.

              Comment


                #8
                "Close position" for sure is expected when you do reversal strategies. When you are long, it needs to first close your long and then get you into your short. Please try commenting out your trail stop for the time being. Your TraceOrders suggests the orders are actually only going in as quantity 1. This is accurate. So we need to isolate where the 2 is coming from. What do you have EntriesPerDirection set to?
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  When I commented out trail stop, it operates with one contract only.
                  But I need TS because:
                  1. I need to have TS on brokers servers in case of losing internet connection or HW failure.
                  2. This strategy is most often reversal, but not every trade ends by position revers - sometimes it is just stop-lossed and no new trade begin immedaitely.

                  So I suppose that it will be neccessary advanced order management in NinjaScropt code?

                  Comment


                    #10
                    1. What brokerage are you on? Please see this article: http://www.ninjatrader-support2.com/...ead.php?t=5349

                    2. Your TraceOrders indicates it submits orders as quantity of 1. Please ensure you are not reversing very close to where your stop loss is at. What can happen is overfills where both get filled before either of them was able to notify the other that it was already filled.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      ad 1.: I use Zen-Fire. But problem I have described is also using SIM account, so I suppoose that it is not problem of my broker.
                      ad 2.: Yes, orders are in appropriate quantity. Lets suppose that I'm in short position. In many cases I do revers of my position - but I just suppose that my TSL is activated and simultaneously I enter Long position using EnterLong() AT THE SAME PRICE.

                      EnterLong() causes my position REVERSE, if I'm short before? Is any other function just to "BUY 1 contract" instead of "EnterLong"? I dont care about what is first ... if my SL activation or my new command to opposite direction. But I suppose exactly what I want.

                      So if I'm short with 1 cont. and I call function "EnterLong", it buy TWO contracts - one to close short position and second to enter long? How to preserve of this behavior?

                      Comment


                        #12
                        Right. The submission of an EnterLong at the same price of your stop loss is what is causing the issue here. Instead of EnterLong just use ExitLong() if you don't want to do a reverse.

                        That will not necessarily solve your issue here though because if the trail stop executes first then the ExitLong won't see a position and then won't do an exit because there is nothing to exit.

                        I suggest you to NOT do these steps at the same time. If you want to submit a reversal at the same place as your trailing stop, wait for the trail stop to finish closing your position then call EnterLong.
                        Josh P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sdauteuil, 09-23-2021, 10:16 AM
                        4 responses
                        1,208 views
                        0 likes
                        Last Post jacobpescaia44  
                        Started by agclub, 04-21-2024, 08:57 PM
                        5 responses
                        34 views
                        0 likes
                        Last Post agclub
                        by agclub
                         
                        Started by ESHunter, Yesterday, 08:06 PM
                        2 responses
                        18 views
                        0 likes
                        Last Post ESHunter  
                        Started by ETFVoyageur, 05-07-2024, 07:05 PM
                        19 responses
                        150 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by ETFVoyageur, 05-11-2024, 10:13 PM
                        3 responses
                        26 views
                        0 likes
                        Last Post ETFVoyageur  
                        Working...
                        X