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

riskmanager AddOn help needed

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

    riskmanager AddOn help needed

    Hi

    disclaimer: I'm quite new to c# and handling orders in ninjatrader.

    I try to build a risk manager that flattens my trade if the positions are too much in loss. Currently I'm using OnAccountItemUpdate event for checking if the account is below my allowed limit and if yes then I try to flatten the trades. unfortunately something is wrong with my code is I just got ninjatrader in a state that I couldn't flatten my trades by anymeans from ninjatrader UI. The error can be in many places and now I start to narrow it down. I don't know if my general idea is a correct one, so please let me know if this should work as intended or is there better way to approach this issue?:

    Code:
    private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
    {
    
    lock (m_account.Positions)
    {
    
    
    foreach (Position position in m_account.Positions)
    {
    
    double profitLoss = position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency);
    if(profitLoss <= singlePositionMaxRisk)
    {
    
    NinjaTrader.Code.Output.Process(string.Format("Ris kManager Max trade loss triggered at loss of: {0} at price of {1}", profitLoss, position.AveragePrice), PrintTo.OutputTab1);
    
    
    flattenAllTrades();// kill the trades
    m_status = string.Format("Position max loss reached. 5min cooloff time started at {0} ",DateTime.Now);
    PlaySound(@"F:\TRADING\NinjaTrader\riskManagerAudi o\maxStopAmountHit.wav");
    }
    
    
    }
    }//END of LOCK
    }
    and for flattening the trades I have this method:

    Code:
    private void flattenAllTrades() //kill all trades
    {
    NinjaTrader.Code.Output.Process(string.Format("fla ttenAllTrades called"), PrintTo.OutputTab1);
    
    foreach (Position position in m_account.Positions)
    {
    
    NinjaTrader.Code.Output.Process(string.Format("fla ttening trade on instrument {0} ", position.Instrument), PrintTo.OutputTab1);
    m_account.Flatten(new [] {position.Instrument});
    m_account.CancelAllOrders(position.Instrument);
    }
    
    }​
    thanks



    #2
    Hello vesipeto,

    When you say that you couldn't use the UI to flatten, what was the reason that the UI was unusable? Was there popup error messages or was it frozen?

    From the given code I don't see anything that would specifically be a problem. Depending on what the issue you observed was may help to better understand if this code relates to the issue.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hey

      what seems to happen is that there is tons of overllapping close orders that are pending and Ninjatrader just freezes and the UI is not usable any more I need to restart and reset the sim account to get rid of the orders. I try to understand what is happening and one thought is that since I'm using OnaccountItemUpdate to send the flatten order then maybe there is multiple orders send before the orders are executed and that is causing some jamming. Btw, I'm using rhitmic as my data source atm if that makes any difference.

      Comment


        #4
        Hello vesipeto,

        Are you calling the logic to close orders many times? You should only see close orders for the positions that are open and not multiple times. If you are calling that more that once in a small amount of time that may be leading to a problem.

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        6 responses
        29 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        13 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Waxavi, Today, 02:00 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X