Announcement

Collapse
No announcement yet.

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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    20 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    120 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    63 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    45 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X