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

EnterShort() returns null

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

    EnterShort() returns null

    Hello, i'm very new in NinjaScript!
    I'm working on strategy with several instruments. Strategy works fine for first time in each instrument, but next i'm getting null when trying to open position. Can i somehow check error message?
    Code:
    if (orderMain == null)
      orderMain = EnterLong(BarsInProgress, quantity, singnalName);

    #2
    Hello schattencheg,

    Thank you for the post, and welcome to the NinjaTrader forum!

    Are all of the parameters in the EnterLong() method set to a value? If the orderMain object is null and you try to use it, you will get this error. You can add a check to see if it is null before using it, just like you did while you set the object, instead you would use the != operator.

    I look forward to being of further assistance.
    Last edited by NinjaTrader_ChrisL; 08-17-2018, 08:28 AM.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for fast react.
      All the parameters is set to values.
      I just want to know what exactly prevent order to be created.

      Comment


        #4
        Hello schattencheg,

        Thanks for the reply.

        I would need to see the full code to know why the order object is null. You may export your script and post it here if you wish.

        See these instructions on exporting your script for attachment:


        I look forward to your reply.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          As i found problem in CloseMarket method:
          Code:
                  public void CloseMarket(string comment = "")
                  {
                      /*Add check for order Cancelled succesfully*/
                      PLTools.Log("Closing market " + comment);
                      if (orderPack[BarsInProgress].orderMain != null)
                      {
                          if ((orderPack[BarsInProgress].orderMain.OrderAction == OrderAction.Buy)|| (orderPack[BarsInProgress].orderMain.OrderAction == OrderAction.BuyToCover))
                          {
                              ExitLong(BarsInProgress, comment, orderPack[BarsInProgress].orderMain.Name);
                          }
                          else
                          if ((orderPack[BarsInProgress].orderMain.OrderAction == OrderAction.Sell)||(orderPack[BarsInProgress].orderMain.OrderAction == OrderAction.SellShort))
                          {
                              ExitShort(BarsInProgress, comment, orderPack[BarsInProgress].orderMain.Name);
                          }
                          else
                              CancelOrder(orderPack[BarsInProgress].orderMain);
                          orderPack[BarsInProgress].orderMain = null;
                      }
                      if (orderPack[BarsInProgress].orderStopLoss != null)
                      {
                          CancelOrder(orderPack[BarsInProgress].orderStopLoss);
                          orderPack[BarsInProgress].orderStopLoss = null;
                      }
                      if (orderPack[BarsInProgress].orderTakeProfits != null)
                      {
                          foreach (var i in orderPack[BarsInProgress].orderTakeProfits)
                              CancelOrder(i);
                          orderPack[BarsInProgress].orderTakeProfits.Clear();
                      }
                      Positions[BarsInProgress].Close();
                      Position.Close();
                  }
          After evaluating this method Position.MarketPosition still not Flat, help me please find out why
          Attached Files

          Comment


            #6
            Hello schattencheg,

            Thanks for the reply.

            You can override the OnPositionUpdate event method to keep track of your position while the strategy is running. Using this method, you can set up a boolean variable at the class level, set it to true while the position is long/short, and wait until that bool becomes false to initiate your CloseMarket method.

            https://ninjatrader.com/support/help...tionupdate.htm - OnPositionUpdate

            Please let me know If I can assist further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hello!
              Thanks for help!
              Now can you explain information in strategy analyzer?
              So, i've fix errors and my strategy now works for three instruments. I select one from strategy analizer window and two hardcoded in strategy.
              What exactly does mean 48000% in percent view of graph? Why 48000 positive % but in currency huge dropdown?
              Attached Files
              Last edited by schattencheg; 08-20-2018, 05:57 AM.

              Comment


                #8
                Can you please give an answer to my last question about graph?

                Comment


                  #9
                  Hello schattencheg,

                  Thank you for the reply.

                  Could you please post the updated version of your script? I will test on the same time frame once I receive your reply.

                  I look forward to assisting further.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Here updated code and graph
                    Attached Files

                    Comment


                      #11
                      Hello schattenche, thank you for the reply.

                      This result is expected to happen since cumulative profit in percentile mode will reinvest the profits from previous trades.

                      More information on this item can be found here:


                      Please let me know if I can assist further.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the link, but i still don't get it. On "Summary" tab we can see following:
                        Total net profit: -283.08$ / 49764,37%
                        Minus 283$ and Plus 49764%
                        If we reinvest we just calculate (final account size)/(initial account size)*100%, so if we have starting account size 100000$ and get profit -283 then we have 99,717%, eg -0,283%
                        And if we calculate from %: profit=(final percents)*(initital account size)/100%-(initital account size), so we have 49664370$ of net profit.
                        Am i right?
                        Last edited by schattencheg; 08-23-2018, 07:33 AM.

                        Comment


                          #13
                          Hello schattencheg,

                          Thanks for the reply.

                          I tested your code once more. It looks like a strategy was made from the strategy builder, then was edited through an external editor to add additional logic. You will need to unlock the code from the Strategy Builder if you want to edit the code manually. To help with debugging, I removed the two data series that you added and the Strategy Analyzer output correct PnL results, so the error is coming from a combination of the custom code and the additional data series being used.

                          Please let me know if you have any questions.
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello, Chris!
                            Thanks for your answer, I'll check it once more time later with one instrument , now I have another question.

                            I faced the following problem: due to the strategy logic when we open position also set StopLoss order and three TakeProfit orders. When we hit first profit level we move StopLoss level closer to current position, e.g. to OpenPrice of our position. When we hit next profit level, we set new StopLoss level at first profit level, etc.
                            For example if StopLoss = 150 points and TakeProfit = 200, and we enter Long position at 1.0000 then we set:
                            StopLoss = 0.0850
                            OpenPrice = 1.0000
                            TakeProfit1 = 1.0200
                            TakeProfit2 = 1.0400
                            TakeProfit3 = 1.0600
                            And if we hit TakeProfit1 level we change StopLoss to the position entry price: StopLoss = 1.00000
                            Now, the question is:
                            When we operate with Daily bars and open position in raising bar which overlaps TakeProfit1 level, then we change StopLoss price to initial one, namely open price, we immediately strikes this stop order. You can see this on the attached image. Why can it be? How we should test this logic?
                            Attached Files

                            Comment


                              #15
                              Hello schattencheg,

                              Thanks for the reply.

                              You may enable a higher resolution data series in which to fill orders on. This will help to get better fill estimations in a backtest. Turn "Order Fill Resolution" to "High", then select a more granular series than a day bar, like 1-minute bars. After you have enabled this, test the strategy again.

                              I look forward to hearing of your results.
                              Last edited by NinjaTrader_ChrisL; 08-24-2018, 08:55 AM.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by elderan, Today, 08:03 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post elderan
                              by elderan
                               
                              Started by algospoke, Today, 06:40 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post algospoke  
                              Started by maybeimnotrader, Today, 05:46 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post maybeimnotrader  
                              Started by quantismo, Today, 05:13 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post quantismo  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              8 responses
                              169 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X