Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to write this pseudocode into its proper C# format

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

    How to write this pseudocode into its proper C# format

    Hello,

    I need help turning this short pseudocode into a C# sentence if possible

    /*In this example, the value "Benchmark" is a higher value that serves as an ExitShort() stop loss when in a short position and OnBarUpdate updates on each tick*/
    //Trading NQ Example values: Benchmark = 12000.00, currentEntryPrice = execution.Price (11990.00 - Entry is always 10 points below)


    if (Position.MarketPosition == MarketPosition.Short)
    {
    /*pseudocode*/
    if ((current bid && ask is greater than or equal to Benchmark) && (current bid && ask is not lower than or equal to currentEntryPrice))
    {
    ExitShort()
    }
    }

    I was thinking of writing something like this because on my last trade, I entered short, then the market rose to hit my Benchmark value but did not exit. Instead, the market came back down, passed my initial entry price and executed the ExitShort() method a few points below my entry. One might say that this is good since I exited for a small profit but actually I missed out on +200 points going short that day because of a faulty exit.

    What should have happened was : ExitShort() at Benchmark, Enter again when market hits my Entry Price (10 points below Benchmark) and take profit and my desired profit target.

    I assume my exit executed where it did because volatility was very high and my account experienced major slippage.

    If writing something like this is not possible, is it then possible to ignore an ExitShort() method under any circumstances?


    #2
    Hello Don22Trader1,

    Thank you for your post.

    You may get the current bid price with GetCurrentBid() and the current ask with GetCurrentAsk(). For more information, please see the following help guide pages:
    https://ninjatrader.com/support/help...currentbid.htm
    https://ninjatrader.com/support/help...currentask.htm

    With this in mind, as long as you already have your Benchmark and currentEntryPrice values saved to those variables, the if statement could look something like this:
    if ((current bid && ask is greater than or equal to Benchmark) && (current bid && ask is not lower than or equal to currentEntryPrice))​
    if ((GetCurrentBid() >= Benchmark && GetCurrentAsk >= Benchmark) && (currentEntryPrice < GetCurrentBid() && currentEntryPrice < GetCurrentAsk());

    I am glad to provide this snippet to you as a simple example. Through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

    If you are looking for more hands-on assistance with writing or debugging your code you could contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.

    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    55 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    132 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X