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

put setprofittarget in secondary data series

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

    put setprofittarget in secondary data series

    Hello,

    I need support to enter setprofittarget in secondary data series after enter order on it.

    I tried something like below but after enter the position in the secondary series, the setprofittarget is always hit immediately instead go where should go.

    Code:
    private double Ask, Bid;
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {​
       if (BarsInProgress == 1)
       {
          Ask = marketDataUpdate.Ask;
          Bid = marketDataUpdate.Bid;
    
          if (Positions[1].MarketPosition != MarketPosition.Flat && Positions[0].MarketPosition == MarketPosition.Flat)
          {
            if (Positions[1].MarketPosition == MarketPosition.Long)
               SetProfitTarget(CalculationMode.Price, (Ask + (8 * TickSize)));
    
            else if (Positions[1].MarketPosition == MarketPosition.Short)
               SetProfitTarget(CalculationMode.Price, (Bid - (8 * TickSize)));
          } 
       }
    }

    #2
    Hello diorfo57,

    Thanks for your post.

    I do not see anything standing out in your script specifically that would cause the behavior you are reporting.

    That said, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Let us know if we may assist further.​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      I think I found the problem.

      Looks like the NT8 plataform is not reading Positions[0] (primary series) positions inside BarsInProgress == 1 (secondary ambient).

      Because all codes from Positions[0] are automatically set to "true" no matter what's the real value.

      Do you mind to check if is it the case?

      Comment


        #4
        Hello diorfo57,

        The position for any instrument is updated when OnPositionUpdate() is run.

        Because all codes from Positions[0] are automatically set to "true" no matter what's the real value.
        This would be incorrect. The position for each instrument would be correct for that instrument.

        When printing the position from OnBarUpdate(), this will have the last updated position shown in OnPositionUpdate() at the time the position is checked.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello diorfo57,

          The position for any instrument is updated when OnPositionUpdate() is run.



          This would be incorrect. The position for each instrument would be correct for that instrument.

          When printing the position from OnBarUpdate(), this will have the last updated position shown in OnPositionUpdate() at the time the position is checked.
          Hi Chelsea.

          I need to use positions check in OnMarketData event.

          I tried many times but seems not work properly when you try to check Positions[0] condition in BarsInProgress == 1 enviroment.

          It always send "true" value.

          Could you please test it if to you work properly and may be a problem in my computer (like missing some update? or something like that)

          thank you.​

          Comment


            #6
            If the secondary data series is of the same instrument, using SetStopLoss or SetProfitTarget on the secondary data series is specifically disclaimed in the documentation.

            https://ninjatrader.com/support/help...etstoploss.htm
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Now I think I know what's happening.

              The secondary series is taking the take profit from primary series, because in "executions" sheet I found the name "Profit target" in secondary series exit when the position is closed, even when I set no take profit parameters in BarsInProgress == 1 environment, and I have some SetProfitTarget parameters in BarsInProgress == 0 enviroment.

              I'm testing it in playback.

              I'll test in real environment to check if it has the same behavior.

              Comment


                #8
                Originally posted by QuantKey_Bruce View Post
                If the secondary data series is of the same instrument, using SetStopLoss or SetProfitTarget on the secondary data series is specifically disclaimed in the documentation.

                https://ninjatrader.com/support/help...etstoploss.htm
                My secondary series is a different instrument.

                Comment


                  #9
                  Reading the documentation, I saw this information in blue.

                  Maybe this is the answer.

                  But I'm using different instruments.

                  Shouldn't SetStopLoss or SetProfitTarget work properly in BarsInProgress == 1 with different instruments?​

                  Comment


                    #10
                    The first bars context is the lowest index Bars object for that instrument. If you have a chart of SPY and did only AddDataSeries ("SPX"); the lowest index Bars object of "SPX" is 1.
                    Bruce DeVault
                    QuantKey Trading Vendor Services
                    NinjaTrader Ecosystem Vendor - QuantKey

                    Comment


                      #11
                      Originally posted by QuantKey_Bruce View Post
                      The first bars context is the lowest index Bars object for that instrument. If you have a chart of SPY and did only AddDataSeries ("SPX"); the lowest index Bars object of "SPX" is 1.
                      This is clear to me.

                      What is not clear is that I have orders from index 1 instrument taking profit targets from index 0, even when I set no profit target parameters for index 1.

                      In your above example, is like SPX taking profits from SPY SetProfitTargets.

                      Makes no sense.

                      Comment


                        #12
                        I'm not sure what you're asking exactly. If you mean SetProfitTarget on SPX but exit on SPY, then no, it doesn't do that. You could, however, manually exit at market on SPY when SPX hits a certain number. SetProfitTarget and SetStopLoss are always about the primary series being traded, not another data series.
                        Bruce DeVault
                        QuantKey Trading Vendor Services
                        NinjaTrader Ecosystem Vendor - QuantKey

                        Comment


                          #13
                          Hello diorfo57,

                          Note, set methods like SetProfitTarget are only updated for the primary series (or first added series of a different instrument). It is fine to submit an order at any time, during any BarsInProgress and it will be submitted, but the order itself is tied to the primary series for updates and fills. (In real-time the real-time market data fills the order, but in historical only the primary series bar closes can fill the order)

                          BarsInProgress should only be used in OnBarUpdate(). This is specifically stating which series is updating OnBarUpdate(). This is not related to OnMarketData()
                          https://ninjatrader.com/support/help...inprogress.htm

                          That said, you can check any of the instrument Positions[] at any time. They will return whatever the current position is for that instrument. The positions are updated with OnPositionUpdate() runs.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chelsea,

                            Now I realized the solution to me is migrate to Unmanaged Approach order type.

                            Thanks anyway for supporting.

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello diorfo57,

                              Note, set methods like SetProfitTarget are only updated for the primary series (or first added series of a different instrument). It is fine to submit an order at any time, during any BarsInProgress and it will be submitted, but the order itself is tied to the primary series for updates and fills. (In real-time the real-time market data fills the order, but in historical only the primary series bar closes can fill the order)

                              BarsInProgress should only be used in OnBarUpdate(). This is specifically stating which series is updating OnBarUpdate(). This is not related to OnMarketData()
                              https://ninjatrader.com/support/help...inprogress.htm

                              That said, you can check any of the instrument Positions[] at any time. They will return whatever the current position is for that instrument. The positions are updated with OnPositionUpdate() runs.
                              Hi Chelsea,

                              I need support with another issue.

                              I changed my code with Unmanaged Approach and I solved the above order management problem.

                              But now I'm having another issue.

                              When the script do 3 trades, it stops placing orders for no reason, even with no error in output console.

                              With managed order approuch I used to use the command EntriesPerDirection = 999 to avoid this issue, but in helpguide documentation I read this command no longer works with Unmanaged Approach.

                              How can I solve this issue of script stop placing orders with no script erros in Unmanaged Approach?

                              Thank you.​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,982 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X