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

Store Market Position

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

    Store Market Position

    hello,


    one quick question:

    how does someone store Market position?

    I have a double called "buyPrice" where I would like to store the price. I tried this below;


    if (blah blah blah)
    {
    EnterLong();
    buyPrice = MarketPosition[0];
    }


    I receive CS0118 errors (MarketPosition is a type but used like a variable).


    Thanks.

    #2
    MarketPosition does not contain price. It only contains information on if you are long or short. If you are trying to save your price you would want to check for changes in MarketPosition and then save out the Position.AvgPrice property.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      thanks. I am really trying to simply determine if a transaction (buy and sell) resulted in a gain or loss to feed a Boolean.

      I will search for the Position.AvgPrice property info.

      Comment


        #4
        In that case it may be easier for you to use the TradeCollection class.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          thank you.

          This will be used to pause trading (for X number of bars) in the case of a losing trade.

          Comment


            #6
            I am now testing this;



            //Trade Conditions
            Trade lastTrade = Performance.AllTrades[0];
            // Condition set BUY didLose FALSE
            if (blahblah && didLose == false)
            {
            EnterLong(LotSize, "");
            }
            // Condition set BUY didLose TRUE
            if (blahblah && didLose == true && BarsSinceExit() > 9)
            {
            EnterLong(LotSize, "");
            }
            // Condition set SELL
            if (blahblah && BarsSinceEntry() >1)
            {
            ExitLong();
            }
            // Condition set didLose TRUE
            if (lastTrade.ProfitPercent < 0)
            {
            didLose = true;
            }
            // Condition set didLose FALSE
            if (lastTrade.ProfitPercent >= 0)
            {
            didLose = false;
            }



            BarsSinceExit() seems to be keeping it from trading...
            Last edited by Faspomy; 10-23-2008, 04:36 PM.

            Comment


              #7
              Well if you've never exited you would never have a BarsSinceExit return greater than 9.

              I recommend following these debugging procedures till you get your conditions working the way you intend: http://www.ninjatrader-support.com/v...ead.php?t=3418
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Josh,

                thank you.


                Is there another way to pause trading instead of BarsSinceExit() ?

                Comment


                  #9
                  Use flag variables.
                  Code:
                  if (flag == true)
                  {
                       EnterLong();
                       flag = false;
                  }
                  
                  if (some exit condition)
                  {
                       ExitLong();
                       flag = true;
                  }
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Josh.


                    Sorry I wasn't specific enough.

                    Is there any way to specify a number of bars to wait (other than BarsSinceExit()) ?

                    Comment


                      #11
                      Faspomy,

                      I do something similar. You can check BarsSinceExit to make sure you've indeed actually entered a position before. Here is code I use:

                      Code:
                      && (BarsSinceExit() == -1 || BarsSinceExit() >= 120)
                      This basically says if -1 (no trade has gone off) or if has been 120+ bars, then do what I want to do say EnterLong or whatever.

                      Hope this helps.

                      Comment


                        #12
                        ctrlbrk,

                        thank you for the info.

                        are you doing anything else to your strategy to enable BarsSinceExit() ?

                        Every time I try to use BarsSinceExit(), I receive the following error;

                        Error on calling OnBarUpdate method for strategy. Index was out of range. Must be non-negative and less than the size of the collection.

                        Comment


                          #13
                          Faspomy,

                          I am not doing anything special. I am calling it within:

                          Code:
                          protected override void OnBarUpdate()
                          You'll have to ask the experts or maybe post your exact code around the BarsSinceExit() lines

                          Mike

                          Comment


                            #14
                            Mike,

                            Thank you.

                            This is an SMA cross strategy.




                            PHP Code:
                                   protected override void Initialize()
                                    {
                                        
                            //SetStopLoss("", CalculationMode.Ticks, 20, false);
                                        
                            Add(SMA3(5,5));
                                        
                            CalculateOnBarClose false;
                                        
                            TraceOrders true;
                                    }

                                    
                            /// <summary>
                                    /// Called on each bar update event (incoming tick)
                                    /// </summary>
                                    
                            protected override void OnBarUpdate()
                                    {    
                                        
                            //Trade Conditions
                                        
                            Trade lastTrade  Performance.AllTrades[0];
                                        
                            // Condition set BUY didLose FALSE
                                        
                            if (SMA3(55).SMAline1[0] > SMA3(55).SMAline2[0]  &&  didLose == false  &&  BarsSinceExit() != )
                                        {
                                            
                            EnterLong(LotSize"");
                                            
                            didLose false;
                                            Print(
                            "Strategy has entered BUY didLose FALSE statement. Close: "Close[0] + " Open: " Open[0]);

                                        }
                                        
                            // Condition set BUY didLose TRUE
                                        
                            if (SMA3(55).SMAline1[0] > SMA3(55).SMAline2[0]  &&  didLose == true   &&  BarsSinceExit() > 9)
                                        {
                                            
                            EnterLong(LotSize"");
                                            
                            didLose false;
                                            Print(
                            "Strategy has entered BUY didLose TRUE statement. Close: "Close[0] + " Open: " Open[0]);
                                        }
                                        
                            // Condition set SELL
                                        
                            if (SMA3(55).SMAline1[0] < SMA3(55).SMAline2[0] && BarsSinceEntry() >1)
                                        {
                                            
                            ExitLong();
                                            Print(
                            "Strategy has entered SELL statement. Close: "Close[0] + " Open: " Open[0]);
                                        }
                                        
                            // Condition set didLose TRUE
                                        
                            if (lastTrade.ProfitPercent 0)
                                        {
                                            
                            didLose true;
                                            Print(
                            "Strategy has entered didLose = TRUE statement. Close: "Close[0] + " Open: " Open[0]);
                                        }
                                        
                            // Condition set didLose FALSE
                                        
                            if (lastTrade.ProfitPercent >= 0)
                                        {
                                            
                            didLose false;
                                            Print(
                            "Strategy has entered didLose = FALSE statement. Close: "Close[0] + " Open: " Open[0]);
                                        }
                                    } 

                            Comment


                              #15
                              Also note; when I remove all instances of BarsSinceExit(), the strategy works without errors in the Log.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by usasugardefender, Today, 01:42 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post usasugardefender  
                              Started by haas88, 03-21-2024, 02:22 AM
                              15 responses
                              182 views
                              0 likes
                              Last Post haas88
                              by haas88
                               
                              Started by brianfnoel, Today, 01:24 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post brianfnoel  
                              Started by bill2023, Yesterday, 08:21 AM
                              2 responses
                              14 views
                              0 likes
                              Last Post bill2023  
                              Started by ynoldsany, Today, 01:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post ynoldsany  
                              Working...
                              X