Here is the goal:
1 - Set stoploss for 2 car entry
2 - Move stoploss to entry price when current price moves to 4 ticks past
3 - Take profit target of 1 car at 4 ticks
4 - Trail stop final car 4 ticks back
Here is what I have cobbled together so far from multiple sources (SampleScaleOut, SamplePriceModification) (deleted the sections that seemed unimportant):
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Short Trending check
/// </summary>
[Description("Short Trending check")]
public class TrendShort : Strategy
{
#region Variables
// Wizard generated variables
private int slopeLow = -20; // Default setting for SlopeLow
private int stochLow = 30; // Default setting for StochLow
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
SetStopLoss(CalculationMode.Ticks, 5);
SetProfitTarget("Short 1a", CalculationMode.Ticks, 4);
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition for stop loss/nbe move - reset the stop loss when closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, 5);
}
// If short, allow for stoploss mod to b/e
else if (Position.MarketPosition == MarketPosition.Short)
{
// Once price is less than entry price - 4 ticks, set stop loss to b/e
if (Close[0] < Position.AvgPrice + 4 * TickSize)
{
SetTrailStop(CalculationMode.Ticks, 5);
}
}
(If Conditions)
EnterShort("Short 1a");
EnterShort("Short 1b");
}
}
#region Properties
[Description("")]
[Category("Parameters")]
public int SlopeLow
{
get { return slopeLow; }
set { slopeLow = Math.Max(-70, value); }
}
[Description("")]
[Category("Parameters")]
public int StochLow
{
get { return stochLow; }
set { stochLow = Math.Max(1, value); }
}
#endregion
}
}
So as you can see, I am using the "split order" method in order to take profit on 1 car, whilst performing action on the other. You probably also notice my use of the TrailStop command in the midst of checking and resetting if/when price moves past the 4 tick target.
The script compiles ok - but it does not reveal the results I would like - specifically its still taking both cars out at 4 ticks, instead of trailstopping the second.
I hope one of you senior types will show mercy on me, and teach me what I am missing - probably something simple

Thanks:
Bannor

Comment