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

Am I doing this right?

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

    Am I doing this right?

    I am trying to implement my own profit target system, since I don't want limit orders sitting on the broker, I want to execute profit target at market.

    So I look at my back test, I short an instrument, and on every OnMarketData() update I test to see if I'm shorting. If I am, I get the latest Bid and call:

    if (Position.GetProfitLoss(GetCurrentAsk(), PerformanceUnit.Currency) >= desiredProfit)

    If the current unrealized gains are equal or greater to the profit target, I close the shorted position at market. I do the same in reverse for the long positions but using GetCurrentBid().

    However when I do my market data replay with data I recorded from last night, I see that the intstrument that was shorted lost a lot of value, but the position was never closed? The low for that bar was much lower than my profit target, so why didn't the market data event get fired? This event does fire for market data playback recordings right?

    #2
    Will SetProfitTarget work on a backtest with historical data? I think it should since the bars know the high and if the high is == or greater than the target it should be able to simulate this right?

    Maybe that is a better approach and maybe its not so bad to live with the limit orders?

    Comment


      #3
      Hey tradefaz,

      To answer your first question, I would debug print your Position.GetProfitLoss() statement. See what type of values it printing out (it will probably be in pips; a component of .00001 or .001 (remember TickSize is the smallest increment)). See what its printing and if you're comparing against desiredProfit is the proper scaling.

      Second, yes, SetProfitTarget will work. As an example, if you're using it on the EURUSD, the following sets a target of 8 pips.
      Code:
      SetProfitTarget(CalculationMode.Ticks, 80);
      Hope this helps.
      mrlogik
      NinjaTrader Ecosystem Vendor - Purelogik Trading

      Comment


        #4
        Originally posted by mrlogik View Post
        Hey tradefaz,

        To answer your first question, I would debug print your Position.GetProfitLoss() statement. See what type of values it printing out (it will probably be in pips; a component of .00001 or .001 (remember TickSize is the smallest increment)). See what its printing and if you're comparing against desiredProfit is the proper scaling.

        Second, yes, SetProfitTarget will work. As an example, if you're using it on the EURUSD, the following sets a target of 8 pips.
        Code:
        SetProfitTarget(CalculationMode.Ticks, 80);
        Hope this helps.
        Thanks for the help. But doesn't calling GetProfitLoss with a calculation mode of Currency return your dollar gains? I am sure it does. I guess the only thing I need to know now is when a position was closed as a result of a profit target. Or rather is there any event I can handle to know that the position was changed during a backtest? If a position is closed out I need to know so I can do other things on the next bar update.

        Comment


          #5
          Also I just tried setting SetProfitTarget and when I run my strategy it freezes up and I see NT is using 99% CPU. Is it normal for this to happen? I am using 3 minute charts.

          When I remove the call to SetProfitTarget, the strategy completes in a few seconds.

          Comment


            #6
            I'm not sure about GetProfitLoss with the calcuation mode; I don't use it. If you're using a pair that doesn't have USD in it, I dont think you will get dollar gains, but rather the base currency pair value (I think ...) You can check all this out with debug prints.

            I'm not sure if there's a way with one of NTs functions to reference a previous position, but you can just

            Code:
            //Variables
            private DataSeries vPositions;
            //Initialize()
            vPositions = new DataSeries(this);
            //OnBarUpdate()  
            vPositions.Set(Position.MarketPosition);
            Now you can reference vPositions to find a position change. You just have to be sure you set it / reference it at the correct time.
            mrlogik
            NinjaTrader Ecosystem Vendor - Purelogik Trading

            Comment


              #7
              Originally posted by tradefaz View Post
              Also I just tried setting SetProfitTarget and when I run my strategy it freezes up and I see NT is using 99% CPU. Is it normal for this to happen? I am using 3 minute charts.

              When I remove the call to SetProfitTarget, the strategy completes in a few seconds.

              that doesn't happen for me. You should only set it once in the Initialize fuction.
              mrlogik
              NinjaTrader Ecosystem Vendor - Purelogik Trading

              Comment


                #8
                Originally posted by mrlogik View Post
                I'm not sure about GetProfitLoss with the calcuation mode; I don't use it. If you're using a pair that doesn't have USD in it, I dont think you will get dollar gains, but rather the base currency pair value (I think ...) You can check all this out with debug prints.

                I'm not sure if there's a way with one of NTs functions to reference a previous position, but you can just

                Code:
                //Variables
                private DataSeries vPositions;
                //Initialize()
                vPositions = new DataSeries(this);
                //OnBarUpdate()  
                vPositions.Set(Position.MarketPosition);
                Now you can reference vPositions to find a position change. You just have to be sure you set it / reference it at the correct time.
                Thanks, I think the best thing is to simply use a SetProfitTarget. Ok, I tried calling it with Ticks instead of Price, and it no longer hangs. however it is not taking the profit at the right time.

                If I do a SetProfitTarget with Ticks and 50.0d, that should be 5 pips?

                But does it calculate this on close or based on the Low/High of each bar? It should really be based on the low or high since the order would be executed by the broker as soon as the profit target is met. But it seems to be ignoring the profit.

                Comment


                  #9
                  Originally posted by mrlogik View Post
                  that doesn't happen for me. You should only set it once in the Initialize fuction.
                  Yeah I set it once, and now that I set it with Ticks = 50 (for 5 pips?) it doesn't freeze. However it doesn't exit any time a bar reaches 5 pips of profit. So my question still is, does it calculate it on bar close? Or does it calculate it based on the high/low of the bar?

                  Comment


                    #10
                    Could this be part of my problem? I found this in the docs:

                    "Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position"

                    So after I go flat I am supposed to reset it to what? If I always want to exit a position as soon as its gained by 5 pips, shouldn't one call to SetProfitTarget be sufficient for the life of the strategy?

                    Comment


                      #11
                      No you don't have to do that. You're going for a static x pips. That is only if you're using CalculationMode.Price in the target function call.

                      I think it should use High / Low. Try this on EURUSD as I know 50 = 5 pips and see what that gives you.
                      mrlogik
                      NinjaTrader Ecosystem Vendor - Purelogik Trading

                      Comment


                        #12
                        Originally posted by mrlogik View Post
                        No you don't have to do that. You're going for a static x pips. That is only if you're using CalculationMode.Price in the target function call.

                        I think it should use High / Low. Try this on EURUSD as I know 50 = 5 pips and see what that gives you.
                        Is that an option somewhere because it appears that for me 5 = 5 pips.

                        Comment


                          #13
                          A tick is the smallest increment of change for a specific symbol.

                          If you're using MBT, you probably set in the Tools->Options->Misc
                          Quote Currencies (FX) in Tenth Pip, if not, you're probably right.

                          For MBT, you're using 1/10th pip increments, a tick for EURUSD is .00001 pips, or 1/10 of a pip. So, if you're using calculationmode.ticks, and you want 5 pips, its 5 x 10 = 50 TICKS.

                          I'm sorry, I guess that depends on the broker you're using and the granularity of their quotes.
                          mrlogik
                          NinjaTrader Ecosystem Vendor - Purelogik Trading

                          Comment


                            #14
                            Originally posted by mrlogik View Post
                            A tick is the smallest increment of change for a specific symbol.

                            If you're using MBT, you probably set in the Tools->Options->Misc
                            Quote Currencies (FX) in Tenth Pip, if not, you're probably right.

                            For MBT, you're using 1/10th pip increments, a tick for EURUSD is .00001 pips, or 1/10 of a pip. So, if you're using calculationmode.ticks, and you want 5 pips, its 5 x 10 = 50 TICKS.

                            I'm sorry, I guess that depends on the broker you're using and the granularity of their quotes.
                            Thanks, I'm using IB.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by helpwanted, Today, 03:06 AM
                            1 response
                            10 views
                            0 likes
                            Last Post sarafuenonly123  
                            Started by Brevo, Today, 01:45 AM
                            0 responses
                            8 views
                            0 likes
                            Last Post Brevo
                            by Brevo
                             
                            Started by aussugardefender, Today, 01:07 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post aussugardefender  
                            Started by pvincent, 06-23-2022, 12:53 PM
                            14 responses
                            242 views
                            0 likes
                            Last Post Nyman
                            by Nyman
                             
                            Started by TraderG23, 12-08-2023, 07:56 AM
                            9 responses
                            386 views
                            1 like
                            Last Post Gavini
                            by Gavini
                             
                            Working...
                            X