I'm new Ninja user. I alredy developed my simple strategy and strategy works well until I tryed to modify stop loss price to breakeven. I used the sample shown here: http://www.ninjatrader-support.com/v...ead.php?t=3222
Now strategy when moves stop loss to breakeven and then reverse postition do not reset stop loss to original position and place stop order with the price from former turn. This price is over the market price what is case of error and strategy termination.
Can anybody help me to explan where the problem is???
Strategy has condition:
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
but looks it does not work????
Rgds
Czarek
My strategy below:
//
// Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Sample strategy using StopLoss and ProfitTarget orders.
/// </summary>
[Description("Sample strategy using StopLoss and ProfitTarget orders.")]
public class SamplePriceModification : Strategy
{
#region Variables
private int stoplossticks = 20;
private int profittargetticks = 100;
#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()
{
/* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
SetStopLoss(CalculationMode.Ticks, stoplossticks);
SetProfitTarget(CalculationMode.Ticks, profittargetticks);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// 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+50 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 50 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
// Condition set 1
if (Close[1] < Close[0]
&& Close[2] < Close[0]
&& Close[3] < Close[0]
&& Close[4] < Close[0]
&& Close[5] < Close[0]
&& Close[6] < Close[0]
&& Close[7] < Close[0]
&& Close[8] < Close[0]
&& Close[9] < Close[0]
&& Close[10] < Close[0]
&& Open[0] < Close[0]
&& Open[1] < Close[0]
&& Open[2] < Close[0]
&& Open[3] < Close[0]
&& Open[4] < Close[0]
&& Open[5] < Close[0]
&& Open[6] < Close[0]
&& Open[7] < Close[0]
&& Open[8] < Close[0]
&& Open[9] < Close[0]
&& Open[10] < Close[0])
{
EnterLong(1, "");
}
// Condition set 2
if (Close[1] > Close[0]
&& Close[2] > Close[0]
&& Close[3] > Close[0]
&& Close[4] > Close[0]
&& Close[5] > Close[0]
&& Close[6] > Close[0]
&& Close[7] > Close[0]
&& Close[8] > Close[0]
&& Close[9] > Close[0]
&& Close[10] > Close[0]
&& Open[0] > Close[0]
&& Open[1] > Close[0]
&& Open[2] > Close[0]
&& Open[3] > Close[0]
&& Open[4] > Close[0]
&& Open[5] > Close[0]
&& Open[6] > Close[0]
&& Open[7] > Close[0]
&& Open[8] > Close[0]
&& Open[9] > Close[0]
&& Open[10] > Close[0])
{
EnterShort(1, "");
}
}
#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
}
}

Comment