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

Start behavior

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

    Start behavior

    Hello,

    I´m lost with the start behavior of scripts. I read the helpguide about this topic and I watch the video. I do not understand, sorry.

    What do I have to set if I want to have a new entry from the script and ignoring whatever might have been before?
    I mean the start of the script and when conditions are true.
    Lets say the script is enabled and there would be a position from the script but I prevented this entry. The script should do the entry when I click the button for the entry, and only doing the entry now like there is no position already (calculated by the conditions of the script).

    I do enable the script in orderstab and it is calculating in onbarupdate. When I click the buttons (I have from sample LongShortToolbarButtons B9) and the conditions are true then is should do the entry.

    Thank you!
    Tony

    #2
    Hello tonynt,

    Thank you for your post.

    I'm a little confused about the behavior of your script. Are you both having the strategy submit orders based on conditions, but also on clicking the buttons? Would you be able to provide sample code?

    If you simply want a strategy to ignore any historical trades it would have taken prior to being enabled, you can simply add a check to make sure you're not processing on historical data to the script at the beginning of OnBarUpdate():

    if (State == State.Historical)
    return;

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      thank you for your reply.

      When enabling the script its calculating from orderflowcumulativedelta ints and bools....
      But I dont want to have every entry. So I added the buttons from the sample LongShortToolbarButtonsB9 and when I click there the long or the short button and the ints and bools are matching with my conditions I want to have it should do then entry.

      But today it occurred strange behavior and so I assume there is something wrong from the startbehavior.

      I know about the if state historical return but the script should start calculate when enabling (which it is doing as it should) AND when I click the entry button and this moment of time the ints and bools are what I want then it should do the entry. So there might be again some "virtual" position since I enabled the script, and this should be ignored to have a "fresh entry" only when conditons true AND button clicked.

      Thank you!
      Tony

      Comment


        #4
        Hello tonynt,

        Thank you for your reply.

        To clarify, the only orders your strategy takes come from your buttons, is that correct?

        What is the start behavior you were using when this occurred?

        Were there any prior open positions on the account/instrument when the strategy was started, either from a manual trade or leftover from a previous strategy run?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate,

          thank you for your reply. Yes, the entries come from the buttons. Means the script is calculating from the moment its enabled.

          But even when conditions are true there is no entry, because I watch other instruments as well to determine if there should be an entry.

          So, when I think "now I want to go long" then I click the long button, and when conditions are true (within a certain number of bars that I´m counting) then it does the entry. If my button is clicked and condition is not true, then no entry.

          Now I have Wait Untill Flat.

          There were no positions, bettter "there should not have been open positions" because then I´m already in a trade. So I thought the reason might be that Ninja is in a virtual position, I dont see directly (I forgot to mention that there was a red flag at the orders tab when this occurred)

          Thank you!
          Tony
          Last edited by tonynt; 02-21-2020, 02:37 PM. Reason: typo

          Comment


            #6
            Hello tonynt,

            Thank you for your reply.

            If you are triggering the entries from the buttons and only from the buttons, there would not be any virtual positions prior to an order being placed as it would require you to click the button to place any orders.

            Could you provide a screenshot of what you see in the Strategies tab of the Control Center when this occurs?

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hello Kate,

              thank you for your reply. This sounds logically. But what I read in the helpguide and the forum is that a red flag means that strategy and account is not in sync. So from this I thought there is a problem with the startbehavior. I post only a snippet of the code to show you the "entry condtions":

              if (shortButtonClicked == true && shorttrade==false && Positions[4].MarketPosition != MarketPosition.Short)
              {
              if(Closes[3][0]<Highs[3][0]
              //&& "other conditions true"...
              && entryOrderSd1a==null
              )
              {
              entryOrderSd1a=EnterShortLimit(4,true,1,GetCurrent Bid(), "Sd1a");
              entryOrderSd1b=EnterShortLimit(4,true,1,GetCurrent Bid(), "Sd1b");
              barNumberOfOrder=CurrentBars[2];
              }

              Thank you!
              Tony

              Comment


                #8
                Hello tonynt,

                Thank you for your reply.

                If you have your entries set to only fire if the button is pressed, the existing position on the account could not have been placed by the strategy on the current run.

                The most likely scenario is that an active position was left from either a manual entry or from a previous run of your strategy. I would suggest using "Wait Until Flat, Synchronize account" as your start behavior to avoid this in the future.

                When your strategy starts using Wait Until Flat, Synchronize Account, it will check for any active orders previously generated by the strategy on your account and cancel those first. Should the strategy be unable to cancel and receive confirmation on the cancellation of these orders within 40 seconds the strategy will not start and an alert will be issued. After the strategy is successful in cancelling any orders that required action, it will check your current Account Position and compare it to a flat state. On multi-instrument strategies it will perform this check for all instruments used by the strategy.
                • If the Account Position is flat already, no reconciliatory order will be submitted. The strategy will then wait for the Strategy Position to reach a flat state as well before submitting any orders live.
                • If the Account Position is not flat, NinjaTrader will submit a market order(s) to reconcile the Account Position to a flat state. The strategy will then wait for the Strategy Position to reach a flat state before submitting live orders.
                Basically, if you have a position open on the instrument you start the strategy on, this will make sure your account is back to flat when you start.

                Further information regarding start behaviors may be found here:


                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you!

                  Comment


                    #10
                    Hello,

                    today I´m experiencing a new strange behavior (this morning I changed to WaitUntilFlatSynchronizeAccount). Now the entry is done but the stop and the target of one of both contracts is cancelled immediately with the entry. I have the stop and target in onexecution:

                    f (entryOrderSd1a == execution.Order)
                    {
                    if (execution.Order.OrderState == OrderState.Filled) // || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                    shortButton.Content = Positions[4].Quantity.ToString();
                    SetStopLoss("Sd1a", CalculationMode.Ticks, sts1, false);
                    SetProfitTarget("Sd1a", CalculationMode.Ticks, ts1);

                    avgFillSd1a=execution.Order.AverageFillPrice;
                    target1short=avgFillSd1a-ts1*TickSize;
                    target2short=avgFillSd1a-ts2*TickSize;
                    stop1short=avgFillSd1a+sts1*TickSize;
                    stop2short=avgFillSd1a+sts2*TickSize;
                    shortdir=true;
                    shorttrade=true;
                    }}

                    if(execution.Name=="Profit target" && execution.Order.FromEntrySignal=="Sd1a")
                    {SetStopLoss("Sd1b", CalculationMode.Price, avgFillSd1a+1*TickSize, false);// stop2short=avgFillSd1a+2*TickSize;}

                    if (entryOrderSd1b == execution.Order)
                    {
                    if (execution.Order.OrderState == OrderState.Filled) // || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                    SetStopLoss("Sd1b", CalculationMode.Ticks, sts2, false);
                    SetProfitTarget("Sd1b", CalculationMode.Ticks, ts2);
                    }}

                    if(execution.Name=="Stop loss" && execution.Order.FromEntrySignal=="Sd1b")
                    {changeshortbutton=2;}


                    Is there anythong wrong with this please? (or causing a problem with modified start behavior?)

                    Thank you!
                    Tony

                    Comment


                      #11
                      Hello,

                      add info: now I see in the log tab the message:
                      24.02.2020 08:48:36 Default Strategy 'Tx1DxNQv31ds4SET/178461692': Error on calling 'OnExecutionUpdate' method on bar 2984: The calling thread cannot access this object because a different thread owns it.
                      But I have no idea why as it is the same code as before.

                      Thank you!
                      Tony

                      Comment


                        #12
                        Hello again,

                        I found the error! I did another change in the scripts last night and did recall only now.

                        Thank you for your support!
                        Tony
                        Last edited by tonynt; 02-24-2020, 08:11 AM. Reason: typo

                        Comment


                          #13
                          Hello,

                          I have in SetDefaults "StartBehavior.WaitUntilFlatSynchronizeAccount ;" but sometimes I have situations as you might see in the attached screenshot. You will know from the executions what marketposition has been, I can not tell. But I saw that a position was closed (why?), the stop and target persist (why?)....

                          Thank you!
                          Tony
                          Attached Files

                          Comment


                            #14
                            Hello tonynt,

                            Thank you for your reply.

                            It appears your previous position was an ATM strategy order. When you start a strategy using one of the Synchronize Account behaviors, it doesn't see the active orders as ATM strategies are different from NinjaScript strategies. So all it submits is a market order to synchronize the account position, but does not submit cancellations for the stop and target as they're not being managed by the strategy. You'd see the same behavior if you had an ATM Strategy running and tried submitting an opposite market order to close it - the position itself would be closed by the market order, but not the stop and target (you'd need to use the Close button to close the full ATM).

                            It is best practice to close any manually submitted positions prior to starting a strategy. The Start behavior is really meant to just synchronize the position itself unless you're restarting a strategy after it was previously disabled, leaving you with open positions.

                            Please let us know if we may be of further assistance to you.
                            Kate W.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Kate,

                              it occurs again that after an entry there was "synchronize". Please see the attached screenshot from executions tab.

                              I have in the code StartBehavior.WaitUntilFlatSynchronizeAccount;

                              And I have done entries only from script strategies, but not with ATM.

                              The close is manually from me then because there was a naked long.


                              ??

                              Thank you!
                              Tony
                              Attached Files
                              Last edited by tonynt; 05-08-2020, 08:30 AM. Reason: add info

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,234 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,414 views
                              0 likes
                              Last Post Traderontheroad  
                              Started by firefoxforum12, Yesterday, 08:53 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post firefoxforum12  
                              Working...
                              X