![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| ATM Strategies (Discretionary Trading) Support for Advanced Trade Management, AutoTrail, AutoBreakeven, Stop Strategy and Simulated Stops etc... |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
I am having trouble with the stoploss modification to autotrail. I have downloaded
SamplePriceModification.zip and compiled it based on my specs, but the modification never gets executed when running a strategy. It seems to work when running a backtest, not entirely sure though. Is there something I'm missing? Lets say I simply want to lock in 4 ticks profit once 13 ticks profit is reached, is the following correct?// Resets the stop loss to the original value when all positions are closed if (Position.MarketPosition == MarketPosition.Flat) { SetStopLoss(CalculationMode.Ticks, stoplossticks); } // 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 + # ticks, set stop loss to breakeven if (Close[4] > Position.AvgPrice + 13 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); } } // If a short position is open, allow for stop loss modification to breakeven else if (Position.MarketPosition == MarketPosition.Short) { // Once the price is less than entry price - # ticks, set stop loss to breakeven if (Close[-4] > Position.AvgPrice - 13 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); } |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
dargente,
The problem with your code is when you access Close[]. You go Close[-4]. This does not work. The indexing is for barsAgo. 0 means the current bar. 1 means the previous bar. 2 means the bar after that and so on.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
ok figured it out thanks. besides the close[0] error, i was basing the modified stop loss on the current price rather than on avg price.
Last edited by dargente; 11-27-2008 at 07:44 AM.
Reason: should be ok now
|
|
|
|
|
|
#4 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
it looks like the modified stop loss is being activated on the long side, but something is definitely wrong with the short side. or are the stoploss mods that i have for the long positions actually being applied to shorts as well? do i need to separately identify these? how can i allocate different stop loss orders for each long/short positions?
![]() Code:
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// 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 + # ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 10 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice * TickSize);
}
if (Close[0] > Position.AvgPrice + 20 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
}
// If a short position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Short)
Last edited by dargente; 11-27-2008 at 12:25 PM.
|
|
|
|
|
|
#5 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
Your hunch is correct. If you want separate stops for longs and shorts I suggest you use the fromEntrySignal parameter for the stop losses and tie them to specific long/short entries.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
not sure if with this code the stops are tied to specific longs or shorts, but the exits seem to be working correctly, except... my stop losses are retraceable, meaning depending on the OnBarClose my stop loss orders may move in any direction, instead of being locked in and only increasing as next target has been reached. its funny to see this happen when it has a beneficial effect, but i'd like to have more control over the exits to minimize loss. perhaps SetStopLoss is not the best solution? can you offer any insight as to how i can optimize this strategy? thanks
update: think i need to use if (Position.MarketPosition == MarketPosition.Long) ExitLongStop(yourParametersHere); will look into that, just not sure how to configure it for multiple exits... Code:
protected override void Initialize()
{
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
SetStopLoss(CalculationMode.Ticks, stoplossticks);
SetProfitTarget(CalculationMode.Ticks, profittargetticks);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Entry 1
if (Position.MarketPosition == MarketPosition.Flat)
if ...
EnterLong("Long Entry 1");
// Entry 2
if (Position.MarketPosition == MarketPosition.Flat)
if ...
EnterShort("Short Entry 1");
// Entry 3
if (Position.MarketPosition == MarketPosition.Flat)
if ...
EnterLong("Long Entry 2");
// Entry 4
if (Position.MarketPosition == MarketPosition.Flat)
if ...
EnterShort("Short Entry 2");
// Exit 1
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// If a long position is open, allow for stop loss modification to breakeven +-
if (Position.MarketPosition == MarketPosition.Long)
// Once the price is greater than entry price + # ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 10 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5 * TickSize);
}
// Once the price is greater than entry price + # ticks, set stop loss to breakeven +-
if (Close[0] > Position.AvgPrice + 20 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
}
}
// Exit 2
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// 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 + # ticks, set stop loss to breakeven
if (Close[0] < Position.AvgPrice - 10 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 5 * TickSize);
}
// Once the price is less than entry price + # ticks, set stop loss to breakeven +-
if (Close[0] < Position.AvgPrice - 20 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice - 10 * TickSize);
}
Code:
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of ticks away from entry price for the Stop Loss order")]
[Category("Parameters")]
public int StopLossTicks
{
get { return stoplossticks; }
set { stoplossticks = Math.Max(0, value); }
}
/// <summary>
/// </summary>
[Description("Number of ticks away from entry price for the Profit Target order")]
[Category("Parameters")]
public int ProfitTargetTicks
{
get { return profittargetticks; }
set { profittargetticks = Math.Max(0, value); }
}
#endregion
Last edited by dargente; 11-29-2008 at 12:08 PM.
Reason: toiling
|
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
The reason your stop orders are moving is because you keep resubmitting. Your condition is simply if you are long or if you are short. On each OnBarUpdate() in which that condition is true you end up resubmitting your stop loss at a different price. To prevent this I suggest you use some flag variables.
Code:
if (Position.MarketPosition == MarketPosition.Long && stopPlaced == false)
{
SetStopLoss(...);
stopPlaced = true;
}
if (Position.MarketPosition == MarketPosition.Flat && stopPlaced == true)
{
stopPlaced = false;
}
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
hi josh,
can you give a little more specifics on this; i'm having trouble figuring it out. |
|
|
|
|
|
#9 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
Your original condition was if you are long -> do something. This condition is evaluated to true on every single bar. This is why your stops keep moving because you keep changing it on every single bar. What you need to do is only submit it once and stop submitting it after that. That is what my prior post shows you how to do.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Member
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
|
i appreciate your help. the error message states:
The name 'stopPlaced' does not exist in the current context |
|
|
|
|
|
#11 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
That is because you need to create it as a bool variable first.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Oct 2007
Location: Southy Florida
Posts: 263
Thanks: 16
Thanked 1 time in 1 post
|
And how would you do that?
|
|
|
|
|
|
#13 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
In the variables section
private bool someVariable = false;
Josh
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| autotrail on manualorder | vk2205 | Automated Trading | 3 | 08-28-2008 08:43 AM |
| Ticks, AutoTrail, Autobreakeven? | knvb8 | ATM Strategies (Discretionary Trading) | 1 | 05-07-2008 01:42 AM |
| Autotrail in strategy | nybangali | Strategy Development | 1 | 12-11-2007 09:36 AM |
| AutoTrail Error | Jappenzeller | ATM Strategies (Discretionary Trading) | 3 | 06-03-2007 06:12 PM |
| Setting up autotrail strategy | T4M | ATM Strategies (Discretionary Trading) | 5 | 05-01-2007 01:02 AM |