Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

20250319 y Trade Starts & Closes Immediately SamePrice?

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

    20250319 y Trade Starts & Closes Immediately SamePrice?

    ninjatrader 8 ninjascript c#
    strategy example code
    why code below . . .
    line 071 protected override void OnStateChange()
    line 072
    line 073 {
    line 074 if (State == State.SetDefaults)
    line 075 {
    line 076 Description = @"BA bdir";
    line 077 Name = "___<stgy_name>";
    line 078 Calculate = Calculate.OnEachTick;
    line 079
    line 080 IsInstantiatedOnEachOptimizationIteration = false;
    line 081 BarsRequiredToTrade = 1;
    line 082 TraceOrders = true;
    line 083
    line 084 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"EnableXtraTracing = "+EnableXtraTracing+" @ "+str_dt_now);}
    line 085 }
    line 086 else if (State == State.Configure)
    line 087 {
    line 088 // AddDataSeries("MYM 12-24", Data.BarsPeriodType.Minute, 2, Data.MarketDataType.Last);
    line 089 }
    line 090 else if (State == State.DataLoaded)
    line 091 {
    line 092 }
    line 093 }
    line 094
    line 095 protected override void OnBarUpdate()
    line 096 {
    line 097 if (CurrentBars[0] < 1 )
    line 098 // if (CurrentBars[0] < 1 || Times[0][0].TimeOfDay < new TimeSpan(9, 30, 0))
    line 099 return;
    line 100
    line 101 DateTime currentCandleTime = Times[0][0];
    line 102 str_cndl_dt = GtTymStmp(currentCandleTime,"yyyyMMddHHmmssffff");
    line 103
    line 104 if (!positionTaken)
    line 105 {
    line 106 if (Close[0] >= Close[1] + 2 * TickSize)
    line 107 {
    line 108 stopPrice = Close[0] + currentPts4Profit;
    line 109 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"stopPrice = "+stopPrice+" @ "+str_cndl_dt);}
    line 110 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"currentContracts = "+currentContracts+" @ "+str_cndl_dt);}
    line 111 EnterLong(currentContracts, "LongEntry");
    line 112 SetStopLoss("LongEntry", CalculationMode.Price, stopPrice, false); // Set stop-loss
    line 113 entryPrice = Close[0];
    line 114 positionTaken = true;
    line 115 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"LONGpositionTaken entryPrice = "+entryPrice+" @ "+str_cndl_dt);}
    line 116 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"LONGpositionTaken currentContracts = "+currentContracts+" @ "+str_cndl_dt);}
    line 117 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"LONGpositionTaken stopPrice = "+stopPrice+" @ "+str_cndl_dt);}
    line 118 }
    line 119 else if (Close[0] <= Close[1] - 2 * TickSize)
    line 120 {
    line 121 stopPrice = Close[0] - currentPts4Profit;
    line 122 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"stopPrice = "+stopPrice+" @ "+str_cndl_dt);}
    line 123 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"currentContracts = "+currentContracts+" @ "+str_cndl_dt);}
    line 124 EnterShort(currentContracts, "ShortEntry");
    line 125 SetStopLoss("ShortEntry", CalculationMode.Price, stopPrice, false); // Set stop-loss
    line 126 entryPrice = Close[0];
    line 127 positionTaken = true;
    line 128 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"SHORTpositionTaken entryPrice = "+entryPrice+" @ "+str_cndl_dt);}
    line 129 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"SHORTpositionTaken currentContracts = "+currentContracts+" @ "+str_cndl_dt);}
    line 130 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"SHORTpositionTaken stopPrice = "+stopPrice+" @ "+str_cndl_dt);}
    line 131 }
    line 132 }

    results in these trace lines in NinjScript Output
    line 122 stopPrice = 19493 @ 202503131802000000
    line 123 currentContracts = 1 @ 202503131802000000
    3/13/2025 6:02:00 PM Strategy '___<stgy_name>/353805475': Entered internal SubmitOrderManaged() method at 3/13/2025 6:02:00 PM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ShortEntry' FromEntrySignal=''
    3/13/2025 6:02:00 PM Strategy '___<stgy_name>/353805475': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='ShortEntry' Mode=Price Value=19493 IsSimulatedStop=False IsMarketIfTouched=False
    line 128 SHORTpositionTaken entryPrice = 19513 @ 202503131802000000
    line 129 SHORTpositionTaken currentContracts = 1 @ 202503131802000000
    line 130 SHORTpositionTaken stopPrice = 19493 @ 202503131802000000
    and on chart

    there is only one trade with

    ShortEntry [email protected]
    Stop loss [email protected]



    why is this trade entering and stopping on same candle with both
    ShortEntry [email protected]
    Stop loss [email protected]
    ?

    since Ninja
    TraceOrders = true;
    is showing this . . .
    3/13/2025 6:02:00 PM Strategy ___<stgy_name>/353805475': Entered internal SubmitOrderManaged() method at 3/13/2025 6:02:00 PM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ShortEntry' FromEntrySignal=''
    3/13/2025 6:02:00 PM Strategy '___<stgy_name>/353805475': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='ShortEntry' Mode=Price Value=19493 IsSimulatedStop=False IsMarketIfTouched=False
    which shows order tracing with FromEntrySignal='ShortEntry' Mode=Price Value=19493
    which is correct stopPrice

    ?​
    Attached Files

    #2
    Hello slingonce,

    Thank you for your post.

    Please note set methods cannot be unset. This means it is important to call the Set method with CalculationMode.Ticks to reset this before calling an entry method (not after).​

    If you want to dynamically change/modify the price of the stop loss and profit target, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:
    The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:

    https://ninjatrader.com/support/helpGuides/nt8/modifying\_the\_price\_of\_stop\_lo.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    20 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    119 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    63 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    45 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X