• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 StevenNelson, Today, 12:23 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_PatrickG  
      Started by kokobeef, Today, 12:32 PM
      2 responses
      9 views
      0 likes
      Last Post kokobeef  
      Started by Jackrpo, Today, 11:14 AM
      2 responses
      9 views
      0 likes
      Last Post Jackrpo
      by Jackrpo
       
      Started by DariusDeck, Yesterday, 04:22 PM
      7 responses
      49 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by laszlonagy, Today, 09:28 AM
      2 responses
      10 views
      0 likes
      Last Post laszlonagy  
      Working...
      X