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

Breakeven in multiple positions

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

    Breakeven in multiple positions

    Hello,

    i am having problems to set breakeven properly in my strategy.

    After my condition in formed, I would like to enter in trade, pending entry order is High[1] (calculateonbarclose = false), and as market goes, i would like to enter on High of each followed bar so i am in multiple trading positions.

    i would like to set breakeven condition at each placed trade, however my code works only in case one trade is open in a time, it does not work when multiple trades have been placed.

    Could you please help me how to set breakevens for each placed trade, using multiple entry orders?


    Condition
    Code:
        if (High[1] > High[2] && High[1] > High[3]    && formace1  == true && FirstTickOfBar)    
            
        {   
        ++c;    
        
        DrawArrowUp(CurrentBar.ToString(), false, 0, Low[0] - 1, Color.Black);    
        
        ABC_trend = EnterLongStop(0, false, 1, High[1] + 1 * TickSize, "trend" + c);    
        
        formace1 = true;    
        
        Print(Position.AvgPrice);
        }
    Breakeven code
    Code:
    if  (breakeven_test == false && Position.MarketPosition ==  MarketPosition.Long && Close[0] >= Position.AvgPrice + (2 *  (TickSize / 2)))    
        {    
         if (
            
        ABC_trend != null && ABC_trend.StopPrice < Position.AvgPrice
      )    
        {    
            exitStopLongTrend = ExitLongStop(0, true, 1, Position.AvgPrice    , "SL_Trend", "trend" + c);
            
            breakeven_test = true;
            
            Print("Breakeven_test: " + breakeven_test + c);
        
        
        }    
    }
    IOrders
    Code:
    if (ABC_trend != null && ABC_trend == execution.Order)    
        {    
        
        if (execution.Order.OrderState == OrderState.Filled)    
        {    
        
         exitStopLongTrend = ExitLongStop(0, true, 1,  execution.Order.AvgFillPrice -  70 * TickSize    , "SL_RH1_Trend" + c,  "trend" + c);    
        exitLimitLongTrend = ExitLongLimit(0, true, 1,  execution.Order.AvgFillPrice +  70 * TickSize, "TP_RH1_Trend" + c,  "trend" + c);    
        
        if (execution.Order.OrderState != OrderState.Filled)    
        {    
        ABC_trend     = null;
        }   
        }
    I have also attached the strategy in attachment.

    Thank you for support
    Attached Files

    #2
    Hello Filip88, and thank you for your question.

    So we can have a more complete picture of the environment you are using this strategy in, could we have a screenshot of your strategy settings through the GUI? These would be the settings you enter either through your chart or through the strategies tab of the control center.

    To send a screenshot with Windows 7 or newer I would recommend using Window's Snipping Tool.
    Click here for instructions
    Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
    Click here for detailed instruction
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for answer, I have attached screen of strategy settings.
      Attached Files

      Comment


        #4
        Thank you Flip88. After carefully reviewing your code, it appears what is happening is that your trade signals are getting out of sync. This is to say, if you enter two trades without exiting one, you will never again be able to access the fromEntrySignal for the first trade.

        To work around this while preserving your c variable's function as a unique identifier, I would like to recommend every time you place a trade, storing the signal for the trade in a C# queue, publicly available MSDN documentation



        This example, which is a modified version of your code, will exit all the trades you enqueued at once. My own code is in bold. You can modify this to suit your trading style.

        Code:
        [B][FONT=Courier New]private System.Collections.Generic.Queue<string> sigstack;[/FONT][/B]
        Code:
        [FONT=Courier New]
        protected override void Initialize()
        {
            [B]sigstack = new System.Collections.Generic.Queue<string>();[/B][/FONT]
        Code:
        [FONT=Courier New]if (High[1] > High[2] && High[1] > High[3]    && formace1  == true && FirstTickOfBar)  
        {   
            ++c;
        [B]    sigstack.Enqueue("trend" + c);[/B]
        [/FONT]
        Code:
        [FONT=Courier New]if  (breakeven_test == false && Position.MarketPosition ==  MarketPosition.Long && Close[0] >= Position.AvgPrice + (2 *  (TickSize / 2)))    
        {    
            if (ABC_trend != null && ABC_trend.StopPrice < Position.AvgPrice)    
            {
                [B]while(sigstack.Count > 0)
                {[/B]
                    [I]exitStopLongTrend [/I]= ExitLongStop(0, true, 1, Position.AvgPrice    , "SL_Trend", [B]sigstack.Dequeue()[/B]);
                [B]}[/B]
            }
        }
        [/FONT]
        Please note that the exitStopLongTrend part I italicized will need to be refactored if it ever shows up on the right hand side of an equals sign if you decide to post that code example verbatim, as it will only be set to the last enqueued trade.

        Please let us know if there are any other ways we can help.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much Jessica.

          Comment


            #6
            Hello,

            Sorry i need to ask again about the breakeven issue.

            I have implemented recommended code and stored my identifier in C# queue , however it seems like it still does not set breakeven in case i have multiple working orders. The 1st trade is ok and the price is moved to breakeven, however the breakeven of the second trade is not processed.

            Code:
             public class Strategy1 : Strategy   
                {   
            
                private System.Collections.Generic.Queue<string> sigstack;
                    
            #region Variables   
               
            private int trades = 0;   
               
              #endregion   
               
                    /// <summary>   
                    /// This method is used to configure the strategy and is called once before any strategy method is called.   
                    /// </summary>   
                    /// 
                    /// 
                    protected override void Initialize()   
                    {   
                CalculateOnBarClose = false;   
                BarsRequired = 8;   
                ExitOnClose = false;   
                TraceOrders = true;   
                EntriesPerDirection = 300;   
                EntryHandling = EntryHandling.AllEntries;   
               
                        
               sigstack = new System.Collections.Generic.Queue<string>();
                   
                        }   
               
                    /// <summary>   
                    /// Called on each bar update event (incoming tick)   
                    /// </summary>   
            
            
            protected override void OnBarUpdate()   
                {   
            
            //__________________________________________________________________________________________________
            // FORMATION 1A - Trend trading, FOLLOWED AFTER FORMATION 1   
               
                if (High[1] > High[2] && High[1] > High[3]    && formace1  == true && FirstTickOfBar)   
                   
                {  
                ++c;   
                sigstack.Enqueue("trend" + c);
                    
                    
                DrawArrowUp(CurrentBar.ToString(), false, 0, Low[0] - 1, Color.Black);   
                ABC_trend = EnterLongStop(0, false, 1, High[1] + 1 * TickSize, "trend" + c);   
                formace1 = true;   
                Print("Average price = " + Position.AvgPrice);
                }    
               
               
                if
                (
                FirstTickOfBar && High[1] <= High[2] && formace1 == true
                || FirstTickOfBar && High[1] <=  High[3]    && formace1 == true
                   
                )
                {
                formace1 = false;   
                }
               
               
            //__________________________________________________________________________________________________
            // FORMATION 1
            
                if
                (
                High[2] > High[3]&& High[2] > High[4] && High[2] > High[5] && High[1] < High[2]    
               
                && Position.MarketPosition == MarketPosition.Flat   
                && FirstTickOfBar   
               
                )    
                    {   
               
                ++a;   
                ++b; 
                DrawArrowUp(CurrentBar.ToString(), false, 0, Low[0], Color.Lime);    
            }
                //////// BREAKEVEN /////////   
               
               
                if (breakeven_test == false && Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (2 * (TickSize / 2)))   
                {   
                if (ABC_trend != null && ABC_trend.StopPrice < Position.AvgPrice)   
                {   
                    while(sigstack.Count > 0)
                        
                    {
                    exitStopLongTrend = ExitLongStop(0, true, 1, Position.AvgPrice    , "SL_Trend", sigstack.Dequeue());
                    }
                    
                    breakeven_test = true;
                    Print("Breakeven_test: " + breakeven_test + c);
            
                }   
                }       
             }
            i have attached strategy and strategy properties, sorry for another post, maybe there is something wrong in the code and i am missing it :/

            Thank you
            Attached Files

            Comment


              #7
              At a glance, the only thing I am noticing is that your entry and exit conditions are not mutually exclusive. Ideally, during a single OnBarUpdate call, you should only ever be able to do one or the other.

              Beyond this, I would highly recommend adding Print("message") and Log("message", LogLevel.Information) statements throughout your code so that you can have a clearer picture what is occurring during various times, and using the Market Replay connection to debug your code. If there are any questions about this process I can answer, or any other questions about NinjaScript I may answer, please don't hesitate to reach out and I will be happy to assist further.
              Jessica P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by FrazMann, Today, 11:21 AM
              0 responses
              0 views
              0 likes
              Last Post FrazMann  
              Started by geddyisodin, Yesterday, 05:20 AM
              8 responses
              51 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by cmtjoancolmenero, Yesterday, 03:58 PM
              10 responses
              36 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by DayTradingDEMON, Today, 09:28 AM
              4 responses
              24 views
              0 likes
              Last Post DayTradingDEMON  
              Started by George21, Today, 10:07 AM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Working...
              X