PDA

View Full Version : Changing the value of a double stop


kiriru
10-23-2010, 11:40 PM
Hello,
I am using a private double as the variable for my trailing stop the default value being 20 ticks.
While trading 3 contracts, I am trying to adjust the value of my Trailer to 15 ticks after the first target, and 10 ticks after the second target.

See below:

/* if (Position.MarketPosition == MarketPosition.Flat)
{
stop = 20;
}
//Adjust the stop
if (Position.Quantity == 2 && Position.MarketPosition != MarketPosition.Flat)
{
stop = 15;
}

if (Position.Quantity == 1 && Position.MarketPosition != MarketPosition.Flat)
{
stop = 10;
}*/

I have tried this under OnOrderUpdate and OnPositionUpdate methods but it is not working for me.
Also tried OnBarUpdate but it won't work there either. By this I mean, it uses 20 ticks in all trades.

Q1. Is it really possible to change the value an active trailing stop this way just like in an ATM template ? I want to take advantage of the Set methods and be able to adjust this.

NB: I am aware of the use of LongStopLimit , ShortStop bla bla bla under OnOrderUpdate methods to emulate an ATM strategy but this negates the use of the Set methods.

Thanks,
Kiriru

NinjaTrader_Austin
10-24-2010, 12:49 PM
kiriru, it is definitly possible to change the price of a stop just like how an ATM strategy does. Can you please post the full code you're using here so we can see how it all fits together?

You are correct in that the LongStopLimit and ShortStop order types negate the use of the Set() methods, but they do offer more control and flexibility in implementation.

kiriru
10-25-2010, 10:09 AM
Here's the code:


#region Variables

private double stop = 20;
private int target1 = 19;
private int target2 = 29;


#endregion


protected override void Initialize()
{

EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
SetTrailStop("", CalculationMode.Ticks, stop, false);

CalculateOnBarClose = true;
ExitOnClose = true;
}


protected override void OnBarUpdate()


{

if (Position.MarketPosition == MarketPosition.Flat)
{
stop = 20;
}
//Adjust the stop
if (Position.Quantity == 2 && Position.MarketPosition != MarketPosition.Flat)
{
stop = 15;
}

if (Position.Quantity == 1 && Position.MarketPosition != MarketPosition.Flat)
{
stop = 10;
}


// Condition set 1

if Close[0] > Open[0]


{
EnterLong(Quantity, "Long A");
// EnterLong(Quantity, "Long A1");
EnterLong(Quantity, "Long B");
EnterLong(Quantity, "Long C");


SetProfitTarget("Long A", CalculationMode.Ticks, target1);

SetProfitTarget("Long B", CalculationMode.Ticks, target2);


}

// Condition set 2
If Close[0] < Open[0]

{
EnterShort(Quantity, "Short A");
EnterShort(Quantity, "Short B");
EnterShort(Quantity, "Short C");

SetProfitTarget("Short A", CalculationMode.Ticks, target1);
SetProfitTarget("Short B", CalculationMode.Ticks, target2);


}

}

NinjaTrader_Brett
10-25-2010, 10:20 AM
Hello,

Is this code still locked and you can only edit it with the Strategy Wizard have unlocked it to add code?

I look forward to assisting you further.

NinjaTrader_RyanM
10-25-2010, 10:23 AM
Hello kiriru,

What's happening is that Initialize() is not called while you are actively running the strategy. You will have to use the Set statement in OnBarUpdate(). A sample on modifying targets and stop losses with Set statements is available here:
Strategy: Modifying the price of stop loss and profit target orders (http://www.ninjatrader.com/support/forum/showthread.php?t=3222)

kiriru
10-26-2010, 08:23 AM
Thanks Ryan,
This is we exactly what I needed.
Thanks indeed.
Kiriru