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

Counting an event

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

    Counting an event

    Hi,
    I am trying to keep a count of an event

    So I tried

    int event =0
    If (bullish trigger)
    {
    event = (event+1);
    }
    if (bearish trigger)
    {
    Event = (event-1)
    }
    If event < 0
    Sell blah blah blah

    it doesn't seem to be working
    I have tried loops using event++1
    etc and I am not sure why it won't work

    I am using it in OnBarUpdate()

    I would also like the event count to show on chart of that is possible, I know this is all pretty simple I am unsure why but my system won't trade when I have the condition (event= -1)

    Curious if my logic is flawed?
    tia
    Last edited by BurnOutTrader; 05-06-2022, 01:06 AM.

    #2
    Hello BurnOutTrader,

    I think the only problem here is where you defined the variable, that will count events for 1 bar and then on the next bar will be 0. If that was the intended goal then you have it right, it would be:


    Code:
    int event = 0;
    if (bullish trigger)
    {
        event++;
    }
    if (bearish trigger)
    {
        event--;
    }
    Print(event);
    
    if (event < 0)

    If you wanted to count for multiple bars you need to move the variable:

    Code:
    private int event = 0;
    protected override void OnBarUpdate()
    {
       if (bullish trigger)
        {
           event++;
       }
       if (bearish trigger)
       {
           event--;
       }
       Print(event);
    
       if (event < 0)
    }

    If the bullish/bearish conditions become true in the same bar you may need an else statement:

    Code:
    if (bullish trigger)
    {
    event++;
    }
    else if (bearish trigger)
    {
    event--;
    }
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello BurnOutTrader,

      I think the only problem here is where you defined the variable, that will count events for 1 bar and then on the next bar will be 0. If that was the intended goal then you have it right, it would be:
      That makes sense thankyou, I had a feeling that was the case, I have one more question,

      with logic like below, is there a more efficient place to put it than OnBarUpdate(), I am assuming that running this every bar is not great, also I am using multiple data series, if i dont specify a data series for updates, will it automatically use the main (default) data series?

      I am curious if i can specify which data series calls the update for custom logic as you can with Closes[1][1] etc
      or if it is better to simply move this code out of OnBarUpdate() altogether, but then I wonder how often it will run, i could probably place it under OnExecutionUpdate()

      forgive me if I am making things confusing I don't yet know the correct terminology to have this level of discussion

      Code:
      private int PositionSize = (int)Math.Ceiling(EquityRequiredPerContract+System Performance.AllTrades.TradesPerformance.Currency.C umProfit)/EquityRequiredPerContract;
      
      protected override void OnBarUpdate()
      
      // Auto Scale position size and protect equity
      
      if (PositionSize <= Size)
      {
      PositionSize = Size;
      }
      if (PositionSize >= Size*5)
      {
      PositionSize = (PositionSize/Size);
      }
      if (PositionSize >= Size*10)
      {
      PositionSize = (PositionSize/(Size*2));
      }
      if (PositionSize >= Size*20)
      {
      PositionSize = (PositionSize/(Size*4));
      }
      Last edited by BurnOutTrader; 05-06-2022, 03:32 PM.

      Comment


        #4
        Hello BurnOutTrader,

        The expectation of most scripts is to run its logic for each bar, what you have shown is not going to take much for processing power at all so that would be fine.

        If you are using multiple data series then this logic would be executed for each series, OnBarUpdate is expected to be called for each series. if you wanted to isolate to only the primary you can add

        Code:
        protected override void OnBarUpdate()
        {
            if(BarsInProgress != 0) return;
        You can also move that code to any other NinjaScript override which is relevant to the use case.

        One other suggestion that I would make would be to move the following code from a variable into the method you are using, that ensure a current value is used instead of the value from when the script first started. If the goal was for when the script first started and to not update this value then you could ignore this change.

        private int PositionSize = (int)Math.Ceiling(EquityRequiredPerContract+System Performance.AllTrades.TradesPerformance.Currency.C umProfit)/EquityRequiredPerContract;

        to
        Code:
        protected override void OnBarUpdate()
        {
            int PositionSize = (int)Math.Ceiling(EquityRequiredPerContract+System Performance.AllTrades.TradesPerformance.Currency.C umProfit)/EquityRequiredPerContract;
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rhyminkevin, Today, 04:58 PM
        1 response
        35 views
        0 likes
        Last Post Anfedport  
        Started by iceman2018, Today, 05:07 PM
        0 responses
        4 views
        0 likes
        Last Post iceman2018  
        Started by lightsun47, Today, 03:51 PM
        0 responses
        7 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        14 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        49 views
        0 likes
        Last Post futtrader  
        Working...
        X