1.To make sure I take no more than 2 trades per current bar (of any bar type)
2. To perform a different type/quantity on the 2nd trade executed on the current bar
The initial strategy was created with Strategy Wizard and worked fine without my added code when I unlocked. All code typed in blue & italic is what I added to the code after unlocking it:
namespace NinjaTrader.NinjaScript.Strategies
{
public class MyStrategy : Strategy
{
public int TradesTaken;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyStrategy";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = true;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Tick;
OrderFillResolutionValue = 1;
Slippage = 0;
StartBehavior = StartBehavior.ImmediatelySubmit;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
SetProfitTarget("", CalculationMode.Ticks, 31);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
if (IsFirstTickOfBar)
{
TradesTaken = 0;
}
// Set 1
if (Condition 1)
&& (Condition 2)
&& (Condition 3)
&& (TradesTaken == 0)
{
EnterLongLimit(Convert.ToInt32(DefaultQuantity), (Close[1] + (45 * TickSize)) , "");
TradesTaken++;
}
// Set 2
if (Condition 1)
&& (Condition 2)
&& (Condition 3)
&& (TradesTaken == 0)
{
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (Close[1] + (-30 * TickSize)) , "");
TradesTaken++;
}
// Set 3
if (Condition 1)
&& (Condition 2)
&& (Condition 3)
&& (Condition 4)
&& (TradesTaken == 1)
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
EnterLongLimit((Convert.ToInt32(DefaultQuantity * 3) ), (Close[1] + (45 * TickSize)) , "");
TradesTaken++;
}
// Set 4
if (Condition 1)
&& (Condition 2)
&& (Condition 3)
&& (Condition 4)
&& (TradesTaken == 1)
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
EnterShortLimit((Convert.ToInt32(DefaultQuantity * 3)), (Close[1] + (-30 * TickSize)) , "");
TradesTaken++;
}
I've tried replacing "public int" with "private int", I've tried assigning "public/private int TradesTaken = 0", I've tried defining a "TradesTaken" field and properties
I tried "TradesTaken += 1" and "++TradesTaken"
When I remove the parts of code I added, it will perform set 1 and 2 (more than twice of course). I don't bother testing for sets 3 & 4 because they're useless without having the ability of tracking how many trades I've taken.
Any help would be greatly appreciated.
Thank you.
Comment