.... CurrentStopPrice = 0; trailTriggerShort = Position.AveragePrice - (TickSize * TrailTriggerAmount); trailAreaLong = Low[1]; trailAreaShort = High[1]; //Add both of them together to define final entry point trailLong = trailAreaLong - StopLossOffset * TickSize; trailShort = trailAreaShort + StopLossOffset * TickSize; if ( ((Position.MarketPosition == MarketPosition.Short) //Needs to be in a short position && (Close[0] < trailTriggerShort)) ) { CurrentStopPrice = (Close[0] + (20 * TickSize)) ; } if ((CurrentStopPrice != 0) && (Position.MarketPosition == MarketPosition.Short)) { ExitShortStopMarket(0, true, Position.Quantity, trailShort, "SLS", "2ES"); //Sets Stop }
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!
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
NinjaTrader
Trail stop help
Collapse
X
-
Trail stop help
Hi i am little lost to trailing stop. I initialized CurrentStopPrice and TrailTriggerShort values. Initially i set SL to high or low of previous bar and then activate trail at trailTriggerShort. Then i am lost. I want to move StopLoss every 20 ticks.. I am also not sure about last part that i got from trailbuilder example..
Code:Tags: None
-
I am initilizing these variables before entering short and they print alright. However later when i am in a position and in profit it prints updated Print("Setting Stop Shorts ="+Stop_Price); but actually doesnt move StopLoss. What can be wrong?Code:Target_Price = (Close[0] - (tpByATR * TickSize)) ; Stop_Price = slShortMinMax[0] ; Stop_Trigger = (Close[0] - (Trail_Trigger * TickSize)) ; EnterShort(PositionSize, "2ES"); //Enters Long with specified Position Size
Code:if ((Position.MarketPosition == MarketPosition.Short && myTradeShort == true) && (Stop_Price != 0)) ...... if (slMinMax2) { Stop_Price = (Close[0] + (Trail_Size * TickSize)) ; Stop_Trigger = (Close[0] - (Trail_frequency * TickSize)) ; ExitShortStopMarket(0, true, Position.Quantity, Stop_Price, "SLS", "2ES"); Print("Setting Stop Shorts ="+Stop_Price); }
https://prnt.sc/XPqilFUUNc5N
Comment
-
Hello tkaboris,
To change a live until cancelled order you need to use ChangeOrder: https://ninjatrader.com/support/help...hangeorder.htmJesseNinjaTrader Customer Service
Comment
-
Hello tkaboris,
You specified true in the order which makes it live until cancelled:
ExitShortStopMarket(0, true, Position.Quantity, trailShort, "SLS", "2ES"); //Sets Stop
If you were looking at the strategy builder example that does not use live until cancelled and instead calls the order method on every bar where it should be active.
JesseNinjaTrader Customer Service
Comment
-
I see thank you for explanation..
Also so if live until cancelled is true, I can move SL or TP and strategy will respect and recognize those moves while, if live until cancelled is not true, then if I move SL or TP when strategy opens order then strategy wont recognize those moves?
Comment
-
Hello tkaboris,
If you mean manually moving the orders the strategy won't recognize those changes no matter what. With that true that would allow you to move the stop manually but the strategy won't know about that change. if you use any logic based on the price you set for the order the strategy would become out of sync at that point.
JesseNinjaTrader Customer Service
Comment
-
I am stuck and dont know what to do with this line, where to put it. I was searhing forum for example and couldnt find it. Usually trails are done without ChangeOrder seems like..
ChangeOrder(stopOrder, stopOrder.Quantity, 0, Position.AveragePrice);
if i remove that true part SL still doesnt get moved to correct Stop_Price level
if (slMinMax2) {
Stop_Price = (Close[0] + (Trail_Size * TickSize)) ;
Stop_Trigger = (Close[0] - (Trail_frequency * TickSize)) ;
ExitShortStopMarket(Position.Quantity, Stop_Price, "SLS", "2ES");
Print("-Setting Stop Shorts ="+Stop_Price);
}
Comment
-
Hello tkaboris,
It looks like there is still some confusion surrounding live until cancelled orders. What you now provided is not live until cancelled.
Previously you had:
Code:ExitShortStopMarket(0, true, Position.Quantity, Stop_Price, "SLS", "2ES");
This requires usiing change order, you would make another condition and then use ChangeOrder when you wanted to change that original order. You additionally should have logic in OnOrderUpdate to find that live until cancelled order and store it to a variable so you can use ChangeOrder. There is a sample of using a live until cancelled order here:
What you now provided is not live until cancel so ChangeOrder would not be used, you would continue to call the order on each OnBarupdate event to keep it active. In that use case you just change the stop price variable when you want it to move because you are resubmitting the order on every bar. This is exactly the way the strategy builder example that i linked in post 2 works.
JesseNinjaTrader Customer Service
Comment
-
I believe i am following an example however order gets rejected. I get this error. I am not sure about profit target. I use ProfitTarget as profit target and DynamicStopLoss and slMinMax2. My strategy is on EachTick. Please help
submitted a cancellation request for Order ID 'orderId='5fc793152f934502bf7e5bf64c6cb937' account='Playback101' name='PT' orderState=Working instrument='NQ 06-23' orderAction=BuyToCover orderType='Limit' limitPrice=13480.25 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=234860 time='2023-05-17 10:10:41' gtd='2099-12-01' statementDate='2023-05-17'' which has not been confirmed cancelled. Please check your account orders and positions
Entering here
TriggerState = 1;
EnterShort(PositionSize, "2ES"); //Enters Long with specified Position Size
and exiting with dynamic stop loss
if (Position.MarketPosition == MarketPosition.Short && myTradeShort == true )
{
if (FixedStopLoss)
{
ExitShortStopMarket(0, true, Position.Quantity, StopLossTicks, "SLS", "2ES"); //Sets Stop for position quantity at specified point
}
if (DynamicStopLoss)
if (slMinMax2) {
if ((TriggerState == 1)
&& (Position.MarketPosition == MarketPosition.Short))
{
TriggerState = 2;
StopPrice = (Position.AveragePrice + (ShortInitialStopDistance * TickSize)) ;
TriggerPrice = Convert.ToInt32((Position.AveragePrice + (ShortBreakEvenTrigger * TickSize)) );
}
if ((TriggerState == 2)
&& (Position.MarketPosition == MarketPosition.Short)
&& (Close[0] <= TriggerPrice))
{
TriggerState = 3;
StopPrice = Position.AveragePrice;
}
if ((TriggerState >= 2)
&& (Position.MarketPosition == MarketPosition.Short))
{
ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), Stop_Price, "SLS", "2ES");
}
if ((Position.MarketPosition == MarketPosition.Short)
&& (Close[0] <= (Position.AveragePrice + (ShortTargetTicks * TickSize)) ))
{
ExitShort(Convert.ToInt32(Position.Quantity), "ShortProfitTarget", "2ES");
}
// Stop_Price = (Close[0] + (Trail_Size * TickSize)) ;
// Stop_Trigger = (Close[0] - (Trail_frequency * TickSize)) ;
// ExitShortStopMarket(Position.Quantity, Stop_Price, "SLS", "2ES");
// Print("-Setting Stop Shorts ="+Stop_Price);
}
if (is2EShortKeltner)
{
ExitShortStopMarket(0, true, Position.Quantity, slShortMinMax[0], "SLS", "2ESK");
}
else
{
ExitShortStopMarket(0, true, Position.Quantity, slShort[0], "SLS", "2ES"); //Sets Stop for position quantity at specified point
}
if (ProfitTarget)
{
ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - (TickSize * ProfitTargetTicks), "PT", "2ES"); //Sets Profit at (Ticks) below entry fill price
}
if (UseTPByATR)
{
ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - (TickSize * tpByATR), "PT", "2ES"); //Sets Profit at (Ticks) below entry fill price
}
if (ProfitTargetRR)
{
if(is2EShortKeltner)
{
ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - (riskShort), "PT", "2ESK");
}
else {
ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - (riskShort ), "PT", "2ES"); //Sets Profit at (Ticks) below entry fill price
}
}
Last edited by tkaboris; 06-06-2023, 02:37 PM.
Comment
-
Hello tkaboris,
For simplicity lets try and tackle one thing at a time. We should first continue with your stop because there is still a problem with that.
From what I can see you combined some of the strategy builder conditions into your own code however with the modifications you made that will prevent it from working.
In this sample you are mixing live until canceled with not live until cancelled orders. you can use one or the other but not both. If you want to make a moveable stop you have two ways of doing that. If you don't use live until cancel you need to use a price variable and update the stop on each OnBarUpdate event exactly like the strategy builder example. If you are using live until cancelled you need to use ChangeOrder and supply the order object, this is more complicated because you need to use OnOrderUpdate as well.
The following three parts of this code are conflicting:
Code:if (Position.MarketPosition == MarketPosition.Short && myTradeShort == true ) { if (FixedStopLoss) { ExitShortStopMarket(0, true, Position.Quantity, StopLossTicks, "SLS", "2ES"); //Sets Stop for position quantity at specified point } }
Code:if (slMinMax2) { if ((TriggerState == 1) && (Position.MarketPosition == MarketPosition.Short)) { TriggerState = 2; StopPrice = (Position.AveragePrice + (ShortInitialStopDistance * TickSize)) ; TriggerPrice = Convert.ToInt32((Position.AveragePrice + (ShortBreakEvenTrigger * TickSize)) ); }
Code:if ((TriggerState >= 2) && (Position.MarketPosition == MarketPosition.Short)) { ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), Stop_Price, "SLS", "2ES"); }
The Second code block would be how the builder sample works to generate a stop price, that should come before the order submission as it is in the builder example. In this case you are setting a variable named StopPrice after you submit the stop order and that variable is also not being used with and of the orders so that code is currently useless.
The third block of code is submitting a non live until cancelled order like the builder example. That is going to be ignored because its the same named order from code block 1 is being resubmitted. That is using a third variable for its stop price also, you used an underscore in Stop_Price but you set the price variable earlier as StopPrice from code block 2.
There are quite a few issues with this code so I would likely suggest to start over from this point to avoid having mixed all of these concepts.
The easiest way forward would be to use the strategy builder sample as a starting point and make sure you are not using live until cancelled orders. Your logic would look identical to that sample meaning you shouldn't be adding extra conditions to the sets that use TriggerState. TriggerState is used to allow certain parts of the logic to work at the correct times to produce the desired result. Adding if(slMinMax2) to one of those conditions will prevent it from running in the correct order.
I would also suggest to completely remove any profit target logic at this point so you can isolate the code for just the stop, once you see that is working correctly you could move into adding the code for the profit target.JesseNinjaTrader Customer Service
Comment
-
Originally posted by tkaboris View PostI.....
Entering here
TriggerState = 1;
EnterShort(PositionSize, "2ES"); //Enters Long with specified Position Size
and exiting with dynamic stop loss
.....
all step
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by rocketman7, Today, 02:12 AM
|
7 responses
30 views
0 likes
|
Last Post Today, 09:23 AM | ||
Started by guillembm, Yesterday, 11:25 AM
|
3 responses
16 views
0 likes
|
Last Post Today, 09:11 AM | ||
Started by junkone, 04-21-2024, 07:17 AM
|
10 responses
149 views
0 likes
|
Last Post Today, 09:03 AM | ||
Started by tsantospinto, 04-12-2024, 07:04 PM
|
6 responses
101 views
0 likes
|
Last Post
by tsantospinto
Today, 08:57 AM
|
||
Started by trilliantrader, 04-18-2024, 08:16 AM
|
7 responses
28 views
0 likes
|
Last Post Today, 07:56 AM |
Comment