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

consecutive losses

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

    consecutive losses

    hi i need some help with some ninjascript
    what i am trying to do is print something when there is 3 consecutive losses and print again when there is another 3 new consecutive losses
    here is the code i have come up with so far, the problem with this code is that it keeps printing on the forth and 5th losses and so on (see image to better understand my point )

    any solutions

    Code:
    protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
          int quantity, Cbi.MarketPosition marketPosition)
    {
      
    	
    	
      if ( Position.MarketPosition == MarketPosition.Flat)
      {
        if (SystemPerformance.AllTrades.Count > 3)
        {
          Trade lastTrade1 = 
          SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
          - 1];
          Trade lastTrade2 = 
          SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
        -   2];
          Trade lastTrade3 = 
          SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
          - 3];
    							
          if(lastTrade1.ProfitPercent    <    0 && lastTrade2.ProfitPercent    <    0 && 
          lastTrade3.ProfitPercent    <    0  )
           {
             Print("three consecutive losses") ;
            }
         }
      }
    
    }

    thanks in advance
    Attached Files
    Last edited by maaz1598; 04-25-2017, 05:59 AM.

    #2
    Hello,

    Thank you for the post.

    I wanted to check, does this logic need to be in OnPositionUpdate specifically? Based on what I can see it looks like once you place a trade, this logic would be executed a few times in the same order as it runs through its states resulting in the prints you are seeing.

    To stop it from printing multiple times, you would likely need to check if the order was filled as one idea, this could be completed using OnExecutionUpdate.
    Another idea may be to use a bool variable. Once the print has occurred once, set a variable to true. In the condition that surrounds the prints, you could then check for this bool variable.If it is true do nothing, if it is false print one and set it to true. This would require resetting the variable somewhere in your logic as well.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by maaz1598 View Post
      hi i need some help with some ninjascript
      what i am trying to do is print something when there is 3 consecutive losses and print again when there is another 3 new consecutive losses
      here is the code i have come up with so far, the problem with this code is that it keeps printing on the forth and 5th losses and so on (see image to better understand my point )

      any solutions

      Code:
      protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
            int quantity, Cbi.MarketPosition marketPosition)
      {
        
      	
      	
        if ( Position.MarketPosition == MarketPosition.Flat)
        {
          if (SystemPerformance.AllTrades.Count > 3)
          {
            Trade lastTrade1 = 
            SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
            - 1];
            Trade lastTrade2 = 
            SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
          -   2];
            Trade lastTrade3 = 
            SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count 
            - 3];
      							
            if(lastTrade1.ProfitPercent    <    0 && lastTrade2.ProfitPercent    <    0 && 
            lastTrade3.ProfitPercent    <    0  )
             {
               Print("three consecutive losses") ;
              }
           }
        }
      
      }

      thanks in advance
      Your code as written does not look defective, but will result in the code being run any time that a position changes, including if you made another entry, for example; a place where it would clearly be unnecessary to get the information that this code calculates and presents.

      Better might be this psuedocode, run when there is an execution update.
      if the last execution was a target order or a stop loss
      and the last trade was a loss
      update an accumulator
      else reset the accumulator to zero.
      Then wherever you want to know, check the accumulator to see how many consecutive losses have been accumulated.

      Not to talk down at you, but as usual, when we first write down the technical spec. of what we want to do, the coding becomes almost trivial.
      Last edited by koganam; 04-25-2017, 12:09 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Skifree, Today, 03:41 AM
      1 response
      2 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      23 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      23 views
      0 likes
      Last Post Pattontje  
      Working...
      X