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

Help with simple sell order automation using daily bars

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

    Help with simple sell order automation using daily bars

    I want to sell a position of a stock I own if today's close is greater than the high of yesterday.

    Here is my simply code:

    Code:
    protected override void Initialize()
    {
        CalculateOnBarClose = false;
    }
    
    protected override void OnBarUpdate()
    {
    
        if (Close[0] > High[1])
        {
            Print("Sell my shares");
        } else {
            Print("Close = " + Close[0] + ", Previous High = " + High[1]);
        }
    }​​
    I also attach my Edit Strategy configuration in the screenshot.

    I don't see any printout in the Output windows. It's like the OnBarUpdate did not get called or since we are using Daily bar, it will call at the end of the stock market ?

    Thanks for any input or help you might have.
    Andy
    Attached Files

    #2
    Hello mechinvestor,

    Thank you for your post.

    This was originally posted in the NinjaTrader 8 section of the forum; as this has to do with NinjaTrader 7 based on your screenshot, I have moved it to the appropriate NinjaTrader 7 > Strategy Development topic.

    Since you are not seeing any prints, please check the Log tab of the Control Center for any errors. I suspect there may be an error along the lines of "Index was out of range" and you may need to add a check in your script to make sure there are enough bars prior to accessing High[1]. There is a tip page in the help guide about this here:


    In order to access High[1] you would need at least 1 bar already formed on the chart. You could try adding the following check at the beginning of OnBarUpdate():
    Code:
    if (CurrentBar < 1)
    return;
    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick response. I figured out my issue, when making my connection with IB, I needed to specify the vendor for historical data. In this case, I needed to choose IB for historical data.

      Now, I'm running into another simple problem. The Time[0] is always returning 9pm for every tick. This is the code I'm using:
      Code:
      Print("Time = " + Time[0] + ", Sell my shares" + ", Close = " + Close[0] + ", Previous High = " + High[1]);
      and I'm getting this output.

      HTML Code:
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.03, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.03, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.01, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.01, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.01, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 452.01, Previous High = 450.79
      Time = 8/4/2023 9:00:00 PM, Sell my shares, Close = 451.98, Previous High = 450.79​
      Any ideas ?

      Thanks.
      Andy

      Comment


        #4
        Hello Andy,

        Thank you for your reply.

        The Time series gives the value of the referenced bar's timestamp. Bars are stamped with the closing time of the bar. 9:00:00PM is likely the end of the session and the timestamp of the daily bars you are working with. Is there something else you are trying to achieve in your print? If so, please provide a description and I would be glad to assist you further.

        Please let me know if you have any additional questions or concerns.
        Emily C.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,406 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        98 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        8 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        160 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        9 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X