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
Scale Out of a Position
Collapse
X
-
Ray - thx. easy when you know how!
Josh - have been thinking about this. As written I believe the strategy is doing the following:
1/ price hits first target - stop loss moved to -5. No problem here.
2/ price hits second target - stop loss doesn't know where to go since 2x conditions are true, i.e. Close[0] > Position.AvgPrice + 5 AND Close[0] > Position.AvgPrice + 10 ARE BOTH TRUE.
3/ if price then starts to retrace to a point where Close[0] > Position.AvgPrice + 5 ONLY is true, then then stop loss moved to -5.
4/ if price retraces further, final contract closed out at -5.
The above sounds logical.........but I am no expert in logic.
Have included the Output from the Output window below.
Long trade entered at 12270. -5 stop loss = 12265.
Hope it helps !
thx
David
19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1a' FromEntrySignal=''
19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1b' FromEntrySignal=''
19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1c' FromEntrySignal=''
19/01/2008 21:12:37 Entered internal PlaceOrder() method at 19/01/2008 21:12:37: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long 1d' FromEntrySignal=''
19/01/2008 21:13:07 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
19/01/2008 21:13:07 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12266 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
19/01/2008 21:13:07 Raise: 12265.75
19/01/2008 21:13:07 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12270.75 Currency=0 Simulated=False
19/01/2008 21:13:07 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=PendingChange Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12271 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
19/01/2008 21:13:07 Breakeven: 12270.75
19/01/2008 21:13:17 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
19/01/2008 21:13:17 Raise: 12265.75
19/01/2008 21:13:17 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12270.75 Currency=0 Simulated=False
19/01/2008 21:13:17 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12271 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
19/01/2008 21:13:17 Breakeven: 12270.75
19/01/2008 21:13:27 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=12265.75 Currency=0 Simulated=False
19/01/2008 21:13:27 Amended stop order: Order='c7b4004e9e5a43ec884fcf29c81df117/Sim101' Name='Stop loss' State=Accepted Instrument='YM ##-##' Action=Sell Limit price=0 Stop price=12266 Quantity=1 Strategy='JMACrossContinuationMod' Type=Stop Tif=Gtc Oco='45a4ebd375514276a031e250427b3e59' Filled=0 Fill price=0 Token='c7b4004e9e5a43ec884fcf29c81df117' Gtd='01/12/2099 00:00:00'
19/01/2008 21:13:27 Raise: 12265.75
19/01/2008 21:13:47 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=20 Currency=0 Simulated=False
Comment
-
Good assessment. When both conditions are true it will take the last modification to your stop loss. It will do the +5 mod and then come to the code where it will then do the +10 mod. What we want to do for both of our stop loss mod conditions is probably add a bool to them. This will allow us only to modify the stop loss once per condition evaluated to true. Does this make sense?
If our Close[0] raised to above price by 10 ticks we want our stop loss to be raised to breakeven. Adding the bool will prevent the strategy from ever lowering the stop loss.Josh P.NinjaTrader Customer Service
Comment
-
if (Close[0] > Position.AvgPrice + 10 * TickSize && breakevenSet == false)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
breakevenSet = true;
}
if (breakevenSet && Position.MarketPosition == MarketPosition.Flat)
{
breakevenSet = false;
}
Create breakevenSet in the Variables section up top.
private bool breakevenSet = false;
Josh P.NinjaTrader Customer Service
Comment
-
Adding the text in bold fixes the issue.
Josh - help greatly appreciated. it is 11:30pm here in Europe.............now off to bed !!!!!!
thx
David
if (Close[0] > Position.AvgPrice + 5 * TickSize
&& breakevenSet == false)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5 * TickSize);
Print(Time[0] + " Raise: " + (Position.AvgPrice - 5 * TickSize));
}
if (Close[0] > Position.AvgPrice + 10 * TickSize
&& breakevenSet == false)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
Print(Time[0] + " Breakeven: " + Position.AvgPrice);
breakevenSet = true;
Comment
-
Josh
One more comment
It may be worth updating your sample strategy code that you placed in the forum (that I used as the baseline for this code) with what I have learnt here based on scaling out of a position with more than two contracts. People like me rely a lot on thise sample codes you place in that part of the forum.
many thx
David
Comment
-
I was originally suggesting using 2 bools (one for each condition). I only posted code for one of condition set. Sorry if that caused confusion. Your current implementation uses 1 bool for both conditions. That could work, but it would still allow for unnecessary resubmissions of the Position.AvgPrice + 5 *TickSize stop loss modification.
If you use two bools you will only allow for the strategy to modify the stop loss once for each condition set.
Code:if[FONT=Courier New] (Close[[/FONT][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][FONT=Courier New]] > Position.AvgPrice + [/FONT][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][FONT=Courier New] * TickSize [/FONT][FONT=Courier New]&& raiseSet == [/FONT][FONT=Courier New][COLOR=#0000ff]false[/COLOR][/FONT][FONT=Courier New])[/FONT][FONT=Courier New] {[/FONT] [FONT=Courier New]SetStopLoss(CalculationMode.Price, Position.AvgPrice - [/FONT][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][FONT=Courier New] * TickSize);[/FONT][FONT=Courier New] raiseSet = true;[/FONT][FONT=Courier New] }[/FONT] [FONT=Courier New][COLOR=#0000ff]if[/COLOR][/FONT][FONT=Courier New] (Close[[/FONT][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][FONT=Courier New]] > Position.AvgPrice + [/FONT][FONT=Courier New][COLOR=#800080]10[/COLOR][/FONT][FONT=Courier New] * TickSize [/FONT][FONT=Courier New]&& breakevenSet == [/FONT][FONT=Courier New][COLOR=#0000ff]false[/COLOR][/FONT][FONT=Courier New])[/FONT] [FONT=Courier New]{[/FONT] [FONT=Courier New]SetStopLoss(CalculationMode.Price, Position.AvgPrice);[/FONT] [FONT=Courier New]breakevenSet = [/FONT][FONT=Courier New][COLOR=#0000ff]true[/COLOR][/FONT][FONT=Courier New];[/FONT] } if ((breakevenSet && Position.MarketPosition == MarketPosition.Flat) || (raiseSet && Position.MarketPosition == MarketPosition.Flat)) { breakevenSet = false; raiseSet = false; }
Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by algospoke, Yesterday, 06:40 PM
|
2 responses
23 views
0 likes
|
Last Post
by algospoke
Today, 07:04 PM
|
||
Started by ghoul, Today, 06:02 PM
|
3 responses
15 views
0 likes
|
Last Post Today, 06:43 PM | ||
Started by jeronymite, 04-12-2024, 04:26 PM
|
3 responses
46 views
0 likes
|
Last Post
by jeronymite
Yesterday, 10:10 PM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
7 responses
23 views
0 likes
|
Last Post Today, 06:16 PM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
10 responses
181 views
0 likes
|
Last Post
by jeronymite
Today, 06:11 PM
|
Comment