• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Start behavior

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

    Start behavior

    Hello,

    Im 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 Im 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 Im 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:
                https://ninjatrader.com/support/help..._positions.htm

                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 Im 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

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by mkoning, Today, 04:20 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post mkoning
                        by mkoning
                         
                        Started by itrader46, Today, 02:59 PM
                        3 responses
                        7 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by madams212121, Today, 12:10 PM
                        13 responses
                        28 views
                        0 likes
                        Last Post NinjaTrader_Jim  
                        Started by jvanelli, Today, 12:19 PM
                        1 response
                        20 views
                        0 likes
                        Last Post NinjaTrader_BrendanC  
                        Started by raaxus, Today, 11:17 AM
                        3 responses
                        21 views
                        0 likes
                        Last Post NinjaTrader_ChrisL  
                        Working...
                        X