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
?

Comment