Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Understanding states and data persistence

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

    #16
    Hello DerkWehler,

    The async C# style of coding is not valid for use in NinjaScript, any sample you find that includes that type of use you can ignore. You should never use C# threading logic in NinjaScript because that will cause problems with how your script processes the code in those methods.

    However, assuming I still have no historical ability, if my computer crashes suddenly, then when I restart, it has no idea how to calculate how much it already made today. Strange to me how this is designed; I'm an MT4 veteran and in that, you can just iterate over all closed trades and filter by date.
    ​This is not true, when you restart the strategy all of the time it previously spent in realtime is now historical bars. When you start the strategy it processes from the first bar up to the most recent bar. As long as your conditions to trade become true again while it processes the historical bars it will accumulate the values and reach the same or near same result.

    So it would seem I still need the ability to save that state and reload it on start up. That code says to do this:

    if (Bars.IsFirstBarOfSession)
    ​ priorTradesCumProfit = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
    You shouldn't have to do that. The CumProfit of AllTrades includes historical trades. Lets do a quick example of the crash situation. Lets assume you have only the current session on the chart and no prior sessions. When you enable the strategy it processes the bars as historical. Lets also assume that there was no condition to trade during that time so right now AllTrades has a count of 0. In realtime the strategy entered and exited a trade then we assume the computer crashed. When you start back up and re enable the strategy it processes the historical bars again but the entry condition is now true because the time where it was true in realtime is now a historical bar. AllTrades count is now 1 before entering realtime again so the strategy knows what trades it previously placed in realtime.



    So for now, I would keep this:

    Code:
    protected override void OnBarUpdate()
    {
    // No point in trying to do historical trades without the signals
    if (State == State.Historical)
    return;
    ...
    }
    ..which means my:

    Code:
    if (BarsInProgress == 0)
    {
    if (Bars.IsFirstBarOfSession)
    {
    pDaily = SystemPerformance.AllTrades.TradesPerformance.NetP rofit;
    }
    ...
    }


    ...will never be called unless it's running at 6pm ET. But I can make the same assignment at the start of my trading session, and then I need to save it somewhere, and read it back in if it exists.

    So can you offer a link to how to do that correctly?​
    By using a return statement to prevent historical processing you are intentionally preventing the strategy from being able to recalculate its values. That type of strategy generally cannot be used with other start behaviors and will not have the ability to resume anything if its disabled and re enabled. In that situation you would have to add custom logic to save any values you need but if we again assume the crash scenario that also means you need to save the value on every bar update which on smaller series you may run into file read/write errors. If you did not save the value for every update the value may not be up to date the next time you start the strategy. The TradePerformance will also be cleared and you cannot reset that so using the values going forward it would start again at 0. This would be a much more complex way of working with the strategy instead of letting it process normally to recalculate the orders as historical so the performance values are updated.

    Comment


      #17
      Originally posted by NinjaTrader_Jesse View Post

      The async C# style of coding is not valid for use in NinjaScript, any sample you find that includes that type of use you can ignore. You should never use C# threading logic in NinjaScript because that will cause problems with how your script processes the code in those methods.
      True, but it is necessary for reading a URL response; cannot just wait :-)

      Originally posted by NinjaTrader_Jesse View Post
      By using a return statement to prevent historical processing you are intentionally preventing the strategy from being able to recalculate its values. That type of strategy generally cannot be used with other start behaviors and will not have the ability to resume anything if its disabled and re enabled. In that situation you would have to add custom logic to save any values you need but if we again assume the crash scenario that also means you need to save the value on every bar update which on smaller series you may run into file read/write errors. If you did not save the value for every update the value may not be up to date the next time you start the strategy. The TradePerformance will also be cleared and you cannot reset that so using the values going forward it would start again at 0. This would be a much more complex way of working with the strategy instead of letting it process normally to recalculate the orders as historical so the performance values are updated.
      RE: The whole discussion about history: Getting live signals from URL means that I cannot do history. At least not yet. But I understand the value and function of the historical now, and will always try to program with that in mind.

      A bit off-topic, but hate to start a new thread for simple question. In using EnterLong(), like I do here:

      if (Position.MarketPosition == MarketPosition.Short)
      ExitShort("Reversal", "Short");
      EnterLong(Quantity, "Long");

      Are the first two lines necessary or does it automatically close any short position?
      Can't believe the docs here don't mention that....

      Comment


        #18
        Hello DerkWehler,

        Please make sure to start new threads for unrelated questions in the future, that helps to keep threads short and readable to others.

        The code you have shown is not valid, calling ExitShort and EnterLong at the same time does not allow for a position update and will result in a duplicated exit. If you are in a short position and call EnterLong that will reverse the position.

        The docs do mention this, I highly recommend to stop and fully read the order handling section completely before doing any type of programming. There are specific rules on how orders can be submitted and this section outlines those items.

        •Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position.



        Keep in mind the page you linked is specifically for monitoring executions, that page will only contain information about that override. The help guide is broken into different sections which explain different topics so you will need to navigate the help guide in some cases when you want to find details on a specific topic.

        Comment


          #19
          someone tried this method?

          Comment


            #20
            Originally posted by lersighvarbeanc1978 View Post
            someone tried this method?
            Can you be more specific?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            571 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            548 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            549 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X