Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strange Profit Target Orders in Live-Trading

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

    Strange Profit Target Orders in Live-Trading

    Hi,

    I have got some issues with my profit targets in live trading. As soon as I change the account in my strategy from Sim101 to my real IB acc the strategy is sending wrong profit target orders.

    Everything works fine in Sim101 acc though.

    The Take Profit for a long position sent by strategy is below entry price and vice versa for a short position. I printed the values for tp and sl calculated as you can see in my code below and the values are correct.

    Something strange must happen in SetProfitTarget(CalculationMode.Price,tp) because the value for tp is correct.

    This is an example for a misstrade i got today. These are the printed values:
    Market Position
    Short
    Average Position Price
    4050,75
    execution Price
    4050,75
    execution avg order Price
    4050,75
    TP
    4038,5
    SL
    4058

    But the TP Limit Order the strategy sent had a limit price of 4057.25.


    Code:
    protected override void OnExecution(IExecution execution)
            {
    
                if (Position.MarketPosition != MarketPosition.Flat)
                {
    				
                    if (execution.Order.Name.Contains("Trend"))
                    {
                        double sl = (Position.MarketPosition == MarketPosition.Long) ? customRound(Position.AvgPrice - _targetRangeTrend * _slPercTrend) : customRound(Position.AvgPrice + _targetRangeTrend * _slPercTrend);
                        double tp = (Position.MarketPosition == MarketPosition.Long) ? customRound(Position.AvgPrice + _targetRangeTrend * _tpPercTrend) : customRound(Position.AvgPrice - _targetRangeTrend * _tpPercTrend);
    					
    					
    					
    					
    					
    					
                        SetProfitTarget(CalculationMode.Price, tp);
                        SetStopLoss(CalculationMode.Price, sl);
    					
    									
    					Print("Market Position");
    					Print(Position.MarketPosition.ToString());
    					Print("Average Position Price");
    					Print(Position.AvgPrice);
    					Print("execution Price");
    					Print(execution.Price);
    					Print("execution avg order Price");
    					Print(execution.Order.AvgFillPrice);
    					Print("TP");
    					Print(tp);
    					Print("SL");
    					Print(sl);
                    }
    
                }
    
            }
    I am really confused by that and got no idea what could have possibly gone wrong there and hope you've got some ideas for me on that issue.

    Thanks in advance!

    #2
    Hi Upswing,

    Thanks for your post.

    From what is shown here in OnExecution() it appears that the SetProfitTarget should be set to the value of tp which is printed after it is set.

    Where are you printing the value of SetProfitTarget? Is this coming from TraceOrders?
    Or are you printing this in OnOrderUpdate()?

    Is SetProfitTarget being triggered multiple times?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      First of all thanks for the fast reply!

      Yes I want to set TakeProfit on the value "tp" and this works perfectly fine in backtesting and live mode on Sim101.

      I am pretty sure SetProfitTarget is only triggered once. There is no other place in the code where I call it and Print(...) is just called once after a position is entered.

      I am printing the values for tp and sl etc directly after I call SetProfitTarget(...) and SetStopLoss(...). This all happens the callback OnExecution(...) as you can see in my code.

      I just don't get what could possibly gone wrong there because the calculation seems completely correct for tp.

      I added "TraceOrders = true" now as well. I didn't know about this property before as I am still quite new to NinjaTrader and so I printed the values myself as you can see im my code.

      Comment


        #4
        I just restarted the strategy for Instrument NQ right now and the simulated historical Take Profit Order shown in the chart for the specific example I wrote about in my first post is correct.

        The fact that this only happens in Live Trading with my real IB acc is what confuses me the most. Thats why my first guess was that there is something going wrong in the communication between IB and NT but as you can see in the printed output the values for the execution overgiven to OnExecution() are correct and the value of tp is calculated correctly as well.

        Comment


          #5
          Hi Upswing,

          I would be interested in the output you are getting from TraceOrders when you submit the profit target to your live account.

          The TraceOrder information should show the price the ProfitTarget is being set to.

          This should appear at the same time as the prints you have added.

          Can you post this output to highlight the discrepancy?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            The problem is that I just added trace order right now and I don't have a live real money trade to show you. But I post the Output as soon as I get a new live trade which should be tomorrow in worst case.

            Comment


              #7
              Ok I just got a strange trade on NQ again. This is the output with the values printed right after SendProfitTarget() :

              Market Position
              Short
              Average Position Price
              4012,5
              execution Price
              4012,5
              execution avg order Price
              4012,5
              TP
              4003,5
              SL
              4018

              The value of the variable "tp" is 4003,5 which is correct. And this is the output generated by TraceOrders:

              15.09.2014 15:32:00 Cancelled pending exit order, since associated position is closed: Order='NT-00213/U0000000' Name='Profit target' State=Working Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
              15.09.2014 15:32:00 Cancelled OCO paired order: BarsInProgress=0: Order='NT-00213/U0000000' Name='Profit target' State=Cancelled Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
              16.09.2014 15:31:00 Entered internal PlaceOrder() method at 16.09.2014 15:31:00: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Entry Short Trend' FromEntrySignal=''
              16.09.2014 15:31:01 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Price Value=4003,5 Currency=0 Simulated=False
              16.09.2014 15:31:01 Amended target order: Order='962426fabba8492681890bcb78836440/U0000000' Name='Profit target' State=PendingSubmit Instrument='NQ 09-14' Action=BuyToCover Limit price=4003,5 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NQ 09-14/0001010c.541766b7.01.01-1275' Filled=0 Fill price=0 Token='4894700361cb43848fd492106adef73a' Gtd='01.12.2099 00:00:00'
              16.09.2014 15:31:01 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=4018 Currency=0 Simulated=False
              16.09.2014 15:31:01 Amended stop order: Order='1628759a743d4369b389173250c9ed78/U0000000' Name='Stop loss' State=PendingSubmit Instrument='NQ 09-14' Action=BuyToCover Limit price=0 Stop price=4018 Quantity=1 Strategy='MarketRangerMix' Type=Stop Tif=Gtc Oco='NQ 09-14/0001010c.541766b7.01.01-1275' Filled=0 Fill price=0 Token='04669865420c4972b6f1df3eeae408aa' Gtd='01.12.2099 00:00:00'
              It seems like even though the variable tp has the value 4003.5 NT sends TP with a totally different value. I just recognized that the TP sent, had the value of the tp it should have had the last time the trade occured!

              So my guess would be that even though tp is calculated correctly with a new value right before SendProfitTarget the ProfitTarget from last time is somehow waiting in the system and as soon as a position is entered gets send. But then again why is Stop Loss send correctly. I really hope you get my idea there .

              aprecciate your help! Upswing

              Comment


                #8
                I also should mention that 30 mins later I had a trade on ES (same script) which worked just fine!

                Comment


                  #9
                  Hi Upswing:

                  In the output I see:
                  16.09.2014 15:31:01 Amended target order: Order='962426fabba8492681890bcb78836440/U0000000' Name='Profit target' State=PendingSubmit Instrument='NQ 09-14' Action=BuyToCover Limit price=4003,5 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NQ 09-14/0001010c.541766b7.01.01-1275' Filled=0 Fill price=0 Token='4894700361cb43848fd492106adef73a' Gtd='01.12.2099 00:00:00'

                  Is 4003,5 an incorrect price?

                  What price are you seeing the Profit Target as getting set to?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea,

                    thats true at first. But please take a look at the last lines of Output :

                    15.09.2014 15:32:00 Cancelled pending exit order, since associated position is closed: Order='NT-00213/U0000000' Name='Profit target' State=Working Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
                    15.09.2014 15:32:00 Cancelled OCO paired order: BarsInProgress=0: Order='NT-00213/U0000000' Name='Profit target' State=Cancelled Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
                    it seems as if at first the price was correct and then it got changed internally somehow. The price 4082,25 is exactly the supposed TP from the last position the strategy entered historically ( in simulation) on 12.09.2014.

                    Comment


                      #11
                      Hello Upswing,

                      That is a cancellation not a submission.

                      15.09.2014 15:32:00 Cancelled pending exit order, since associated position is closed: Order='NT-00213/U0000000' Name='Profit target' State=Working Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
                      15.09.2014 15:32:00 Cancelled OCO paired order: BarsInProgress=0: Order='NT-00213/U0000000' Name='Profit target' State=Cancelled Instrument='NQ 09-14' Action=Sell Limit price=4082,25 Stop price=0 Quantity=1 Strategy='MarketRangerMix' Type=Limit Tif=Gtc Oco='NT-00134-1275' Filled=0 Fill price=0 Token='9fbfa49e2dc640709b84583a3564aba9' Gtd='01.12.2099 00:00:00'
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        I know that this is a cancellation, but as you can see this is the TakeProfit Order cancelled.

                        So it seems as if the Limit-Order he tried to place at first had a price of 4003.5 but the order actually recieved at IB had a price of 4082.5. And this is the one got cancelled as you can see.

                        Is there a workaround to place the TP/SL orders myself without using SendProfitTarget()?

                        Like somehow generating the order myself with all attributes and sending it then?

                        Comment


                          #13
                          Hi Upswing,

                          I'm not quite sure what you mean. The cancellation happens before the new profit target is submitted which happens after the new entry... This cancellation appears to be with a different order.

                          Yes, you can place an ExitLongLimit or ExitShortLimit as a profit target. Or you can use the unmanaged approach and send any order you want.

                          http://www.ninjatrader.com/support/h...tlonglimit.htm

                          http://www.ninjatrader.com/support/h...ubmitorder.htm
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Yeah. sorry your right. I was just confused because that was exactly the Take Profit Order Price the strategy posted to IB.

                            I actually solved the problem by resetting the tp/sl by SetProfitTarget(0) and SetStopLoss(0) which seems to work since three days.

                            I reset those whenever I get an execution and the position is flat and every morning. It actually seemed to be the last profit target which was still in the system that caused the problem.

                            But just today i had a new issue with an overfill but I will open a new thread since it is something different I guess.

                            Thanks for your help though Chelsea. I will look into the approach you suggested as well soon.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by wzgy0920, 04-20-2024, 06:09 PM
                            2 responses
                            26 views
                            0 likes
                            Last Post wzgy0920  
                            Started by wzgy0920, 02-22-2024, 01:11 AM
                            5 responses
                            32 views
                            0 likes
                            Last Post wzgy0920  
                            Started by wzgy0920, Yesterday, 09:53 PM
                            2 responses
                            49 views
                            0 likes
                            Last Post wzgy0920  
                            Started by Kensonprib, 04-28-2021, 10:11 AM
                            5 responses
                            192 views
                            0 likes
                            Last Post Hasadafa  
                            Started by GussJ, 03-04-2020, 03:11 PM
                            11 responses
                            3,234 views
                            0 likes
                            Last Post xiinteractive  
                            Working...
                            X