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

  • 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:


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

Latest Posts

Collapse

Topics Statistics Last Post
Started by burtoninlondon, Today, 12:38 AM
0 responses
10 views
0 likes
Last Post burtoninlondon  
Started by AaronKoRn, Yesterday, 09:49 PM
0 responses
14 views
0 likes
Last Post AaronKoRn  
Started by carnitron, Yesterday, 08:42 PM
0 responses
11 views
0 likes
Last Post carnitron  
Started by strategist007, Yesterday, 07:51 PM
0 responses
14 views
0 likes
Last Post strategist007  
Started by StockTrader88, 03-06-2021, 08:58 AM
44 responses
3,983 views
3 likes
Last Post jhudas88  
Working...
X