Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

wrapping order methods in a class that resides in a separate cs file

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

    #16
    Hello Gaby,

    I removed all prefix 'myStrategy' then the compiler says "needs to have an object reference in order to use non-static methods/attributes 'NinjaTrader.Cbi.Position.MarketPosition.get'. What class of object do I need? Originally I suppose it's the instance of GGB ...

    Below is the code segment.

    private void ProcessCrossingAbove()
    {
    if (myFunction == _STATIC._LINE_FUNCTION[_STATIC._LONGSHORT] || myFunction == _STATIC._LINE_FUNCTION[_STATIC._LONG])
    {
    if (Position.MarketPosition == MarketPosition.Flat) {
    K2W_EnterLong();

    }
    else if (Position.MarketPosition == MarketPosition.Long) {
    //AddLong()
    K2W_EnterLong();

    }
    else {//Position.MarketPosition == MarketPosition.Short
    K2W_ExitShort();
    }
    }// of if;
    }

    Regards,
    Steven​

    Comment


      #17
      Hello,

      You need to reference the strategy instance in order to access it's MarketPosition, so yes you are correct it would be an instance of GGB. You should add myStrategy back, it should be defined as an instance of GGB.

      private GGB myStrategy;

      Comment


        #18
        Hi Gaby,

        This compiler error is draining me. Summarized a few points below and could you when you're not too busy take a look. Also the code is attached.

        Meanwhile I'll try the partial class as you suggested yesterday.

        Best Regards,
        Steven

        Click image for larger version

Name:	image.png
Views:	80
Size:	122.0 KB
ID:	1283054

        Click image for larger version

Name:	image.png
Views:	74
Size:	51.7 KB
ID:	1283055


        Click image for larger version

Name:	image.png
Views:	76
Size:	366.9 KB
ID:	1283056

        Click image for larger version

Name:	image.png
Views:	76
Size:	297.4 KB
ID:	1283057

        Click image for larger version

Name:	image.png
Views:	75
Size:	90.1 KB
ID:	1283058
        ​​

        Comment


          #19
          source code attached.
          Attached Files

          Comment


            #20
            Hello,

            The error is coming from myStrategy.MarketPosition.Flat. This enum is a system property and is not defined in his script, it's defined in the core of NinjaTrader.

            To resolve, change the line of code to:

            if (myStrategy.Position.MarketPosition == MarketPosition.Flat)

            Please let me know if you have any other questions.

            Comment


              #21
              Hi Gaby,

              Just went thru the PartialMethodCallsCustomStrategyExample. Looks like it's not what I can use because I can't make class '_MagicWand' a partial class of GGBv1d0. There will be other classes inheriting from _MagicWand later so making it a partial class of another class will lose its logical independence and extensibility.

              The passing GGBv1d0 instance reference approach theoretically must work. I'll look further into it. Thanks for the help.

              Regards,
              Steven

              Comment


                #22
                sorry, I missed your previous message. Shall revert on 'if (myStrategy.Position.MarketPosition == MarketPosition.Flat)'.

                Steven

                Comment


                  #23
                  Oh Yeah! It works. Thank you ).......))))))........))))).....) so much, Gaby.

                  :-)
                  Steven

                  Comment


                    #24
                    Hi Gaby,

                    May I ask why when the long position is entered, the stop loss order did not take effect immediately (I did not see the screen with a stop order)? In testing this a few times, there's once the stop order did appear, but a few bars (one minute) later. What are the possible reasons?

                    Thank you for the help.

                    Regards,
                    Steven


                    Click image for larger version

Name:	image.png
Views:	67
Size:	54.0 KB
ID:	1283331

                    Comment


                      #25
                      Jo Gaby,

                      I looked at the trace file re below long trade - there's not a record of the stop order. Your comments would be highly appreciated. Thanks. /Steven

                      Click image for larger version

Name:	image.png
Views:	63
Size:	95.3 KB
ID:	1283338
                      Click image for larger version

Name:	image.png
Views:	63
Size:	546.6 KB
ID:	1283339

                      Comment


                        #26
                        Hello,

                        An exit order from the managed approach cannot be submitted until after the entry order has filled and a position has been taken.

                        You should submit your exits from OnExecutionUpdate(), or in a separate condition in OnBarUpdate that checks the market position.

                        Please see the example script 'ProfitChaseStopTrailExitOrders' from the link below which demonstrates:

                        https://forum.ninjatrader.com/forum/...269#post802269

                        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects 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 date time of the bar and all values compared in every condition that places an order.

                        The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

                        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                        Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

                        I am happy to assist you with analyzing the output from the output window.

                        Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

                        Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

                        https://ninjatrader.com/support/foru...121#post791121

                        Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​

                        Comment


                          #27
                          Thank you, Gaby. Lots of info for me to digest.

                          Did a few tests. A few questions - in the body of OnExecutionUpdate() -

                          1. Below statement never gets the stop order shown on the screen. What could possibly go wrong? A stop order that does not get cancelled at bar end is sexy.
                          ExitShortStopMarket(0, true, execution.Order.Quantity, execution.Order.AverageFillPrice + 20*TickSize, "MW short stop loss", "shortEntry");

                          2. Below pops up the stop order; however, after I drag the price up and down (trying to modify the price), it goes back to where it was initially. Would it work if I use OnOrderUpdate() to track the change of stopPrice then re-place the order?
                          stopPrice= execution.Order.AverageFillPrice + 40*TickSize;
                          ExitShortStopMarket(stopPrice);
                          3. Below works almost perfectly - the stop price shows up, and I can change the price with my mouse - only that - I need to put a variable in so that I can manipulate the stop price in different scenarios. Why does ExitShortStopMarket() behave differently?
                          ExitShortStopMarket(execution.Order.AverageFillPri ce + 40*TickSize);

                          As always, thank you for your valuable input.

                          Regards,
                          Steven​

                          Comment


                            #28
                            Hello,

                            It’s possible to manually modify stops and targets submitted by a NinjaScript Strategy by using exit orders, such as ExitLongStopMarket(), with isLiveUntilCancelled set to true.

                            ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

                            The strategy logic will need to submit the order only once and not be modified again from the script. This will allow you to manually modify the orders without them moving back to the original price from the strategy. We suggest submitting the orders from OnExecutionUpdate() when the entry order fills.

                            Have a look at the ManuallyModifiableStopTargetExample_NT8 which demonstrates placing an exit stop and limit order which can be manually modified.

                            In order to figure out why your ExitShortStopMarket method isn't being executed, please follow the directions from my previous post on debugging using prints and enabled TraceOrders.

                            Comment


                              #29
                              Thank you Gaby. But where is ' ManuallyModifiableStopTargetExample_NT8'? /Steven

                              Comment


                                #30
                                Hello,

                                My apologies, I am attaching it below.
                                Attached Files

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Yesterday, 05:17 AM
                                0 responses
                                71 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                143 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                76 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                47 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                51 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X