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

Code for A Market Entry At Bar Close Price?

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

    #31
    Code:
    double cashValuePTick = TickSize * Instrument.MasterInstrument.PointValue;
    			
    			 if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry() > 1  )
    			 
    				{
    					SetStopLoss("", CalculationMode.Price, Low[1] -  TickSize*1, false);
    				}
    I'm lost , how do I define a value for cashValuePTick I am assuming I want cashValuePTick to be a value of 50 and then create a statement if Low[1} > cashValuePTick

    {
    SetStopLoss("", CalculationMode.Price, Low[1] - TickSize*1, false);
    }

    I can't get anything to work though... so please help as this should finish this and get me back to trading

    Comment


      #32
      Okay,

      I think I got it, can you just check this out and let me know if it is okay , clean and should not cause and problems...



      Code:
      double cashValuePTick = TickSize * Instrument.MasterInstrument.PointValue;
      // Resets the stop loss to the original value when all positions are closed
      			if (Position.MarketPosition == MarketPosition.Flat)
      			{
      				SetStopLoss(50, false);
      			}
      			
      			// If a long position is open, allow for stop loss modification to breakeven
      			else if (Position.MarketPosition == MarketPosition.Long)
      			{
      				// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
      				if ( Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) >= 50  && Low[1] < Position.AvgPrice +  TickSize*1)
      				{
      					SetStopLoss(CalculationMode.Price, Position.AvgPrice +  TickSize*1);
      				}
      				
      				else if
      					 ( Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) < 50   && Low[1] > cashValuePTick - 49)
      				{
      					SetStopLoss(CalculationMode.Price, Low[1] -  TickSize*1);
      				}
      					
      			}	
      	if (Position.MarketPosition == MarketPosition.Flat)
      			{
      				SetStopLoss(50, false);
      			}
      			
      			// If a short position is open, allow for stop loss modification to breakeven
      			else if (Position.MarketPosition == MarketPosition.Short)
      			{
      				// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
      				if ( Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) >= 50  && High[1] > Position.AvgPrice +  TickSize*1)
      				{
      					SetStopLoss(CalculationMode.Price, Position.AvgPrice -  TickSize*1);
      				}
      				
      				else if
      					 ( Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) < 50   && High[1] < cashValuePTick + 49)
      				{
      					SetStopLoss(CalculationMode.Price, High[1] +  TickSize*1);
      				}
      			}

      Comment


        #33
        Code looks clean. Have you tested this to ensure that it follows what you are expecting?
        Cal H.NinjaTrader Customer Service

        Comment


          #34
          Hey Cal,

          It seemed to be working in Market Sim last night, but something weird just happened in Sim through IB connection today.. The PnL went 50$ before entry candle finished so moved to BE plus 1 Tick... The in the Control center , still on entry candle the "State" began flickering between "accepted" and Pneding and the "stop price" was being changed from $7.59 to $7.02 (a huge difference on 1700 shares from my original 50$ .03 stop) it never set the stop and went back to the 7.59 or BE + 1 Tick...

          Not sure why this happned but need to figure out.. Otherwise it seems to be working and doing what I need

          Comment


            #35
            tshirtdeal,

            Do you have the updated script so I can run a few tests on my end?
            Cal H.NinjaTrader Customer Service

            Comment


              #36
              Strategy

              Hey Cal, today it is not working at all in live Sim.. i did'nt change anything but all orders are being rejected for being above/below market value on the stop order.. I did'nt change anything. not sure why orders went through fine yesterday and rejected today...

              Is there a way to enter market orders 1 tick above/blow open/FirstTickOfBar or keep orders from being rejected like this all the time...

              Please let me know if you see problems with my stop orders also... It should be a 50 dollar stop, move to breakeven once PnL >= 50 and /or SetStopLoss trail Low[1]

              Thanks for any help!
              Attached Files

              Comment


                #37
                Tshirtdeal,

                I ran a couple tests with this on different instruments and didn't run into any rejected orders.

                I did see where the code may be not working as expected for you. The trail stop and else if condition for long market position are both true at the same time. However, they are placing the stop order at the same price together. May want to incorporate the trail stop with your other MarketPosition check with the other Set() orders.

                If you are looking to have the BreakEven happen first try using a bool switch between the two

                Example -
                Code:
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                	// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                	if ( Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) >= 50  && Low[1] < Position.AvgPrice +  TickSize*1)
                	{
                		SetStopLoss(CalculationMode.Price, Position.AvgPrice +  TickSize*1);				
                	}
                				
                	else if (!boolSwitch && Position.GetProfitLoss(Close[0] , PerformanceUnit.Currency) < 50 && BarsSinceEntry() > 0  && Low[1] > cashValuePTick - 49)
                	{
                		SetStopLoss(CalculationMode.Price, Low[1] -  TickSize*1);
                                boolSwitch = true					
                	}
                
                        else if ( boolSwitch && Position.MarketPosition == MarketPosition.Long && BarsSinceEntry() > 0 && Low[1] > Position.AvgPrice +  TickSize*1)
                	{
                		SetStopLoss("", CalculationMode.Price, Low[1] -  TickSize*1, false);
                	}
                }
                What you would need to do is when the trade is exited, is then reset the boolSwitch back to false so that if you enter long again it follows the same logic.
                Cal H.NinjaTrader Customer Service

                Comment


                  #38
                  Thanks for the help Cal!

                  Would the my PC clock being out of sync with IB cause rejected orders? The problem you mention with the stop logic both being true is only for the long positions?

                  also, what is the best way to get a line at my original hard stop cash value? I have tried everything and nothing works... If I have 1000 shares with a 50 cash SetStopLoss at 10$ Average Position Price, it is a 5 cent stop subtracted from the 10$ stop Drawline at $9.95 nothing seems to draw at that price..

                  Comment


                    #39
                    Great, now I see another problem.... The stoploss moves up to breakeven + 1 tick when I go above my profit, but if the profit goes back down below the 50$ profit the stoploss gets reset to the low... i want the stoploss to stay break even once the profit is reached and not reverse ...

                    how do I do that?

                    Comment


                      #40
                      3rdStrategy

                      Hey Cal,

                      can you take one more look at this and see if the code is clean and problem free and I fixed the problems? I added the boolSwitch to the longs and shorts... is it correct? and correct in Variables section?

                      It seems to be running correct now, just want to double check that it is safe to trade


                      also any help with how to pinpoint my "cash" stop with a drawline command would be appreciated .. I
                      Attached Files

                      Comment


                        #41
                        Tshirtdeal,

                        I have not seen an instance where an unsynced PC clock would cause an order rejection.

                        The rejection you noted having was due to a stop order being placed on the wrong side of the market.

                        Again, you may want to set up another bool switch here so that the condition doesn't become true and only sets the stop loss value where you want it.
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #42
                          Man, I am lost again...

                          So there is a problem with my order logic now where it is not always setting the a Stop order 1 tick above the FirstTickOfBar?

                          In the log the orders were rejected for Buy orders being below the market.. It happened more times then not early at the open... you are saying a bool command will keep it from happening on entry orders?

                          Comment


                            #43
                            Tshirtdeal,

                            Please send me your log and trace files for today so that I may look into what occurred.

                            You can do this by going to the Control Center-> Help-> Mail to Support.

                            Please reference myself in the subject of the email:ATTN Cal and put a reference to this thread in the body
                            Cal H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by jclose, Yesterday, 09:37 PM
                            1 response
                            11 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by firefoxforum12, Yesterday, 08:53 PM
                            1 response
                            14 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by kmunroe478, Yesterday, 05:39 PM
                            2 responses
                            15 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by kevinenergy, 02-17-2023, 12:42 PM
                            115 responses
                            2,700 views
                            1 like
                            Last Post kevinenergy  
                            Started by prdecast, Today, 06:07 AM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Working...
                            X