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

Buy or sell at a tens place logic

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

    Buy or sell at a tens place logic

    I am trying to learn C# and ninjascript. If I wanted a strategy to buy at the first 10s place below the opening price or sell at the first 10s place above the opening price how would I do this? Example, price opens at 4995 on mes. I want it to take a short position if it hits 5000 or a long position if it hits 4990.

    I think in C# the math would be something like this:

    public static void Main (string[] args) {
    // Input the starting number
    Console.Write("Enter the starting number: ");
    int number = Convert.ToInt32(Console.ReadLine());

    // First 10s place above
    int first10sPlaceAbove = (int)Math.Ceiling((double)number / 10) * 10;

    // First 10s place below
    int first10sPlaceBelow = (int)Math.Floor((double)number / 10) * 10;

    Console.WriteLine("First 10s place above: " + first10sPlaceAbove);
    Console.WriteLine("First 10s place below: " + first10sPlaceBelow);
    }

    But for ninjascript I want the starting number to be the opening price and I want the printed lines to be long and short positions. Any help is appreciated, thanks

    Would it be like this:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] == 0)
    {
    // Get the opening price of the current bar
    openingPrice = Bars.GetOpen(0);

    // Calculate the first 10s place above and below the opening price
    int first10sPlaceAbove = (int)Math.Ceiling(openingPrice / 10) * 10;
    int first10sPlaceBelow = (int)Math.Floor(openingPrice / 10) * 10;

    // Check if we have already placed orders
    if (!Position.MarketPosition.HasAnyPosition)
    {
    // Place a sell order (short position) if the price crosses above the first 10s place
    if (Close[0] > first10sPlaceAbove)
    {
    EnterShort();
    }
    // Place a buy order (long position) if the price crosses below the first 10s place
    else if (Close[0] < first10sPlaceBelow)
    {
    EnterLong();
    }
    }
    }
    }
    Last edited by JGriff5646; 02-08-2024, 08:38 PM.

    #2
    Hello JGriff5646,

    To get the current bar open price you would use Open[0]

    The other code that you supplied looks to be invalid, if you are using an external program to generate NinjaScript code please note that those programs will generally provide inaccurate or invalid code. I would suggest using the help guide to learn NinjaScript.

    For the type you use for the price you should use a double so you retain the decimal, an int does not have a decimal.

    To check the market position you can see the following link: https://ninjatrader.com/support/help...etposition.htm

    You can use Print statements in NinjaScript which lets you output what your script is doing to the output window similar to the console in C#.

    https://ninjatrader.com/support/helpGuides/nt8/print.htm
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AaronKoRn, Today, 09:49 PM
    0 responses
    5 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Today, 08:42 PM
    0 responses
    8 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Today, 07:51 PM
    0 responses
    9 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,975 views
    3 likes
    Last Post jhudas88  
    Started by rbeckmann05, Today, 06:48 PM
    0 responses
    9 views
    0 likes
    Last Post rbeckmann05  
    Working...
    X