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

Breakeven logic

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

    #16
    Hello Newuser,

    If you were to enclose both print statements with brackets, do you still experience the same issue with the first print statement?

    For example,

    protected override void OnExecution(IExecution execution)
    {
    if (entryOrder != null && entryOrder == execution.Order)
    {
    Print("Execution Price=" + execution.Price);
    Print("Execution="+execution.ToString());
    }
    }
    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #17
      Unfortunately enclosing the print statements in brackets as per your suggestion didn’t work. In fact with the brackets I don’t get any values printed at all, which suggests that the
      Code:
      if (entryOrder != null && entryOrder == execution.Order)
      criteria are never being met(?) I definitely know that trades are being placed because Position.AvgPrice prints values and I get valid trades in the backtest, but clearly something isn’t working with OnExecution.

      I wondered if the problem might be caused by where I declare the
      Code:
      private IOrder entryOrder = null;
      object in the script, but I tried with that line in several different places (in the user defined variables, just above the OnExecution method) and it didn’t make any difference.

      Comment


        #18
        Hello NewUser,

        I have attached a script for NinjaTrader 7 which takes a trade and prints the position on OnExecution.

        You could declare entryOrder under UserDefined Variables, or below namespace NinjaTrader.Strategy.

        Does the script print values for you?

        I look forward to your reply.
        Attached Files
        Alan P.NinjaTrader Customer Service

        Comment


          #19
          I uploaded the script provided and it did print values for execution price in a backtest.

          I notice the script you provided resets the entryOrder variable to null after the position is closed. I'm not sure if it's an issue but I don't reset the entryOrder variable anywhere in my script. I don't even know how I would do so given the nature of my exit strategy.

          Comment


            #20
            Hello NewUser,

            You would want to reset the value of entryOrder variable to null when the position is closed if you’d like to get new prints for any new executions, otherwise you’ll just have 1 print.

            Without the full code I’m unable to test on my end, if you’d like to submit your code I could take a look at why your print statements are not executing please send an email to [email protected] with Attn: Alan P in the Subject line. Also within the email please include a link to this thread.

            I’ve provided some links you may find helpful on debugging.

            Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

            TraceOrders: http://ninjatrader.com/support/forum...ead.php?t=3627

            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #21
              I had another look at the test script that you provided, read over your last couple of replies again and figured out where I went wrong. The first mistake I made was not assigning the entryOrder object to the entry itself in OnBarUpdate i.e. entryOrder = EnterLongLimit() and the second mistake was not setting the entryOrder value to null afterwards. Once I fixed those two errors I started getting values printed for execution and execution.Price in my script (thanks).

              I was thinking about how/when to set the entryOrder value to null and further to post #22 in this thread http://ninjatrader.com/support/forum...t=47791&page=2
              (where the poster also has multiple simultaneous trades open) I wondered if I could simply set entryOrder = Null right after the trade has been opened? I figure this is important as it may have implications for my breakeven logic, now that I am using execution.Price to adjust my stop and/or profit target.

              I have provided a concise version of my code as it stands below – can you have a look please and see if anything is glaringly wrong because it’s not working as expected. While I get an accurate print for the execution.Price I notice now that both long and short trades are being closed out by the profit target for a loss i.e. the profit target is being set lower than the entry for buy orders and greater than the entry for sell orders.

              I also tried with entryOrders = null; right at the end of the OnExecution() but that made no difference.

              Code:
              OnBarUpdate()
              { //begin OnBarUpdate
              If ( //buy conditions) 
              {	entryOrder = EnterLongLimit()	}
              //I set my stop and profit this way as I can get consecutive signals and trades on consecutive candles
              If( BarsSinceEntry() == 0)
              { 	SetStopLoss();
              	SetProfitTarget();
              }
              
              if (Position.MarketPosition != MarketPosition.Flat)
              {	entryOrder = null;	}
              
              If ( //sell conditions) 
              {	entryOrder = EnterShortLimit()	}
              //I set my stop and profit this way as I can get consecutive signals and trades on consecutive candles
              If( BarsSinceEntry() == 0)
              { 	SetStopLoss();
              	SetProfitTarget();
              }
              
              if (Position.MarketPosition != MarketPosition.Flat)
              {	entryOrder = null;	}
              } //end OnBarUpdate
              
              protected override void OnExecution(IExecution execution)
              {
              if (entryOrder != null && entryOrder == execution.Order)
              { //begin breakeven logic
              	if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntry()==1 && Close[0]<=execution.Price)
              { SetStopLoss(CalculationMode.Price,execution.Price); }
              	else if (Position.MarketPosition == MarketPosition.Short && BarsSinceEntry()==1 && Close[0]>execution.Price)
              { SetProfitTarget(CalculationMode.Price,execution.Price); }
              
              if(Position.MarketPosition == MarketPosition.Long && BarsSinceEntry()==1 && Close[0]>=execution.Price)
              { SetStopLoss(CalculationMode.Price,execution.Price); }
              	else if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry()==1 && Close[0]<execution.Price)
              { SetProfitTarget(CalculationMode.Price,execution.Price); }
              
              	} //end breakeven logic

              Comment


                #22
                Hello newuser,

                I would suggest uniquely naming your entry orders so you can tie your profit target and stop losses specific to specific entries.

                Example of uniquely naming your entries,


                So you could have for your entry order,
                EnterLong(1, “BuyLong”);

                EnterLong() section of Helpguide: http://ninjatrader.com/support/helpG...?enterlong.htm

                Then for setting your profit target,
                SetProfitTarget("BuyLong", CalculationMode.Ticks,Close[0]+10*TickSize)
                SetStopLoss("BuyLong", CalculationMode.Ticks,Close[0]-10*TickSize, false);

                Regarding setting entryOrder=null while still in a position, you may have issues getting your executions to print as you require entryOrder not being equal to null.

                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment


                  #23
                  So I found this post http://ninjatrader.com/support/forum...ad.php?t=37960 which explains that BarsSinceEntry does not work inside the OnExecution method as OnExecution is only called when an order event triggers, and so the breakeven logic in my previous post is clearly flawed.

                  What I believe I can do (correct me if I’m wrong) is use OnExecution to assign the AvgFillPrice to a new variable that I can then use in OnBarUpdate like so:

                  Code:
                  //declared in the user defined variables section
                  Private double entryprice = 0.00; 
                  
                  If ( //buy conditions) 
                  {	entryOrder = EnterLongLimit()	}
                  entryprice = entryOrder.AvgFillPrice;
                  
                  If( BarsSinceEntry() == 0)
                  { 	SetStopLoss();
                  	SetProfitTarget();
                  }
                  Am I correct in thinking that the above code will only assign the execution price to the entry price variable if and when a trade is actually triggered? (call that question 1)

                  But I am still a little confused on where I should be setting entryOrder = null; in the code. I presume the reason why the IOrder object needs to be set to null again is so it can accept a new value? Is that true, or is there another reason why the reset needs to happen? (call that question 2) If I can get my head around why that needs to occur then I might be able to figure out where I should be putting that line of code.

                  Comment


                    #24
                    Hello NewUser,

                    If you’d like to assign the execution price to the entry price variable you could do that in OnExecution rather than under your buy condition if statement. When your buy statement is true it will submit a buy order at the same time it goes to assign its execution price, which wouldn’t be known until it comes back from the exchange.

                    The reason I set the entryOrder=null when a closing position was submitted, was so that a new entry order could be placed in my example. This is not something you have to do but rather something I did in the example I provided you.

                    Please let us know if you need further assistance.
                    Alan P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by arvidvanstaey, Today, 02:19 PM
                    4 responses
                    10 views
                    0 likes
                    Last Post arvidvanstaey  
                    Started by samish18, 04-17-2024, 08:57 AM
                    16 responses
                    56 views
                    0 likes
                    Last Post samish18  
                    Started by jordanq2, Today, 03:10 PM
                    2 responses
                    8 views
                    0 likes
                    Last Post jordanq2  
                    Started by traderqz, Today, 12:06 AM
                    10 responses
                    18 views
                    0 likes
                    Last Post traderqz  
                    Started by algospoke, 04-17-2024, 06:40 PM
                    5 responses
                    47 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X