Announcement

Collapse
No announcement yet.

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 donto, Today, 06:41 AM
        0 responses
        5 views
        0 likes
        Last Post donto
        by donto
         
        Started by nelslynn, 12-08-2024, 03:52 PM
        10 responses
        36 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by Mirasol, Today, 05:21 AM
        0 responses
        6 views
        0 likes
        Last Post Mirasol
        by Mirasol
         
        Started by davydhnz, 12-09-2024, 06:00 AM
        7 responses
        47 views
        0 likes
        Last Post davydhnz  
        Started by donto, Yesterday, 07:12 AM
        2 responses
        15 views
        0 likes
        Last Post donto
        by donto
         
        Working...
        X