• 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

  • maaz1598
    started a topic consecutive losses

    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.

  • koganam
    replied
    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.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    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.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by digibob, Today, 03:00 PM
0 responses
1 view
0 likes
Last Post digibob
by digibob
 
Started by yeshujbp, Today, 02:50 PM
0 responses
2 views
0 likes
Last Post yeshujbp  
Started by tonynt, Today, 12:39 PM
0 responses
11 views
0 likes
Last Post tonynt
by tonynt
 
Started by Martyb, 06-13-2019, 05:14 AM
10 responses
78 views
0 likes
Last Post iq200
by iq200
 
Started by Erwin Beckers, 11-04-2018, 02:39 AM
4 responses
71 views
2 likes
Last Post brucerobinson  
Working...
X