1) Orders don't get executed even when the conditions are met (Long and Short)
2) For some reason, the strategy starts with a current position size of 1 when it should start with 0
How do I fix the 2 issues listed above? I've pasted the code below and also attached screenshots of the log and output.
HERE'S THE CODE
region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TestZ20EMAForCoding : Strategy
{
private int Var_TakeProfit;
private int Var_StopLoss;
private double Var_20EMA_Price;
private double Var_Order_Entry_Price;
private int Var_ATR_Val;
private int Var_Position;
private EMA EMA1;
private RSI RSI1;
private MACD MACD1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "TestZ20EMAForCoding";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
ProfitTarget = 6;
StopLoss = 6;
Var_TakeProfit = 1;
Var_StopLoss = 1;
Var_Order_Entry_Price = 0;
Var_ATR_Val = 1;
Var_Position = 1;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 20);
RSI1 = RSI(Close, 14, 3);
MACD1 = MACD(Close, 12, 26, 10);
SetProfitTarget(Convert.ToString(ProfitTarget), CalculationMode.Currency, 0);
SetStopLoss(Convert.ToString(StopLoss), CalculationMode.Currency, 0, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 14)
return;
// Set 1
if ((Close[0] > EMA1[0])
&& (RSI1.Default[0] < 25)
&& (MACD1.Default[0] > MACD1.Default[3]))
// && (Position.Quantity == 0))
{
Print("LONG Entry");
Print(Position.Quantity);
EnterLong(Convert.ToInt32(DefaultQuantity), "");
ExitLongLimit(Convert.ToInt32(DefaultQuantity), 0, "", Convert.ToString(ProfitTarget));
ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), 0, "", Convert.ToString(StopLoss));
}
// Set 2
if ((Close[0] < EMA1[0])
&& (RSI1.Default[0] > 75)
&& (MACD1.Default[0] < MACD1.Default[3]))
// && (Position.Quantity == 0))
{
Print("SHORT Entry");
Print(Position.Quantity);
EnterShort(Convert.ToInt32(DefaultQuantity), "");
ExitShortLimit(Convert.ToInt32(DefaultQuantity), 0, "", Convert.ToString(ProfitTarget));
ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), 0, "", Convert.ToString(StopLoss));
}
// Set 3
// if (Position.Quantity > 0)
// {
// Var_Position = 1;
// Print(Convert.ToString(Position.Quantity));
// }
// Set 4
// if (Position.Quantity == 0)
// {
// Var_Position = 0;
// Print(@"Ok it enters here!!!!");
// }
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="ProfitTarget", Order=1, GroupName="Parameters")]
public int ProfitTarget
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="StopLoss", Order=2, GroupName="Parameters")]
public int StopLoss
{ get; set; }
#endregion
}
}
​

Comment