#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// bandom padaryt kazka prasmingo su %
[Description("bandom padaryt kazka prasmingo su % ")]
public class darkarta : Strategy
{
#region Variables
// Wizard generated variables
private double profit_Target = 0.1; // Default setting for Profit_Target
private double stop_Loss = 0.1; // Default setting for Stop_Loss
// User defined variables (add any user defined variables below)
#endregion
/// This method is used to configure the strategy and is called once before any strategy method is called.
protected override void Initialize()
{
// The following settings configure your strategy to execute only one entry for each uniquely named entry signal.
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
/* These Set methods will place Profit Target and Trail Stop orders for our entry orders.
Notice how the Profit Target order is only tied to our order named 'Long 1a'. This is the crucial step in achieving the following behavior.
If the price never reaches our Profit Target, both long positions will be closed via our Trail Stop.
If the price does hit our Profit Target, half of our position will be closed leaving the remainder to be closed by our Trail Stop. */
SetProfitTarget("Long 1a", CalculationMode.Percent, Profit_Target);
SetTrailStop(CalculationMode.Percent, Stop_Loss);
//show indicators on every chart in the strategy analyzer
CalculateOnBarClose = true;
}
/// Called on each bar update event (incoming tick)
protected override void OnBarUpdate()
{
// Condition set 1
if (EMA(5)[0] > SMA(30)[0]
&& Aroon(14).Up[0] > 80
&& Aroon(14).Down[0] < 25)
{
/* Enters two long positions.
We submit two orders to allow us to be able to scale out half of the position at a time in the future.
With individual entry names we can differentiate between the first half and the second half of our long position.
This lets us place a Profit Target order only for the first half and Trail Stops for both. */
EnterLong("Long 1a");
EnterLong("Long 1b");
}
// Condition set 2
if (EMA(5)[0] > SMA(30)[0]
&& Aroon(14).Down[0] > 80
&& Aroon(14).Up[0] < 25)
{
EnterShort(DefaultQuantity, "");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public double Profit_Target
{
get { return profit_Target; }
set { profit_Target = Math.Max(0.00, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double Stop_Loss
{
get { return stop_Loss; }
set { stop_Loss = Math.Max(0.00, value); }
}
#endregion
}
}
When I go to strategy analyzer/charts, why do I sometimes see the occurence of 1 long(Long 1a), instead of the expected 2 longs as usual (Long 1a + Long 1b). I noticed it happens when system exits half the position. So looks like the system trigers 1 new long (Long 1a) after profit taking and returns to the allowed 2 longs (Long 1a + Long 1b).
Question 2:
In the code above, where do I add this code below to ensure that once the price is greater than entry price by 12%, I want to take profit by closing the Long 1a, and for the Long 1b set the stop loss to breakeven (level where I entered with both Long 1a and Long 1b)
protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss("", CalculationMode.Percent, Stop_Loss, false);
}
// 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 by 12%, set stop loss to breakeven
if (Close[0] > Position.AvgPrice * 1.12)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
// Condition set 1
Do I need all the conditions in this second code or just the one with if (Close[0] > Position.AvgPrice * 1.12)?
Question 4:
How do I set a different profit target (say 4%) ONLY for Long 1b?
Question5:
Finally, is the second code correct?, I mean would it do what I want it to do in question 2? I amended the code slightly, then went to the chart to check the result and could not see if it was actually working.
MANY THANKS

Comment