First, I am already struggeling to Enter a limit order. For some reason it does not submit it.
Second problem is canceling the order if it is not filled after 1 Bar, therefore I am coding everything in the section OnBarUpdate().
Already apologizing in advance, some stuff might be irrelevant.
#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 GOD : Strategy
{
private EMA EMA1;
private EMA EMA50;
private SMA SMA1;
private int periodEMA;
private int periodSMA;
private double constant1;
private double constant2;
private double selflongconstant1;
private double selflongconstant2;
private double priorSum;
private double sum;
private double Price;
private double selfSMA;
private double selfEMA;
private int selflongperiodEMA;
private double selflongEMA;
private double ConvergencePrice;
private Order ConvergenceEntryPrice = null;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "GOD";
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 = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 11);
SMA1 = SMA(Close, 14);
EMA50 = EMA(50);
EMA1.Plots[0].Brush = Brushes.Turquoise;
SMA1.Plots[0].Brush = Brushes.Snow;
AddChartIndicator(EMA1);
AddChartIndicator(SMA1);
periodEMA = 11;
periodSMA = 14;
selflongperiodEMA = 200;
constant1 = 2.0 / (1 + periodEMA);
constant2 = 1 - (2.0 / (1 + periodEMA));
selflongconstant1 = 2.0 / (1 + selflongperiodEMA);
selflongconstant2 = 1 - (2.0 / (1 + selflongperiodEMA));
priorSum = 0;
sum = 0;
Price = 0;
selfSMA = 0;
selfEMA = 0;
selflongEMA = 0;
ConvergencePrice = 0;
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 20) return;
CancelOrder(ConvergenceEntryPrice);
// SMA
{
if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
{
if (CurrentBar == 0)
Value[0] = Input[0];
else
{
double last = Value[1] * Math.Min(CurrentBar, periodSMA);
if (CurrentBar >= periodSMA)
Value[0] = (last + Input[0] - Input[periodSMA]) / Math.Min(CurrentBar, periodSMA);
else
Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, periodSMA) + 1));
}
}
else
{
if (IsFirstTickOfBar)
priorSum = sum;
if (priorSum == 0)
{
sum = Input[0] + Input[1] + Input[2] + Input[3] + Input[4] + Input[5] + Input[6] + Input[7] + Input[8] + Input[9] + Input[10] + Input[11] + Input[12] + Input[13];
}
else
{
sum = priorSum + Input[0] - Input[14];
selfSMA = sum / periodSMA;
// SMA = sum - Input[13] + Price / periodSMA;
selfEMA = Input[0]*constant1 + EMA1[1]*constant2;
// EMA = Price*constant1 + selfEMA * constant2;
Price = (sum - Input[13] - selfEMA * constant2 * periodSMA)/(constant1*periodSMA-1);
//Print(selfEMA);
//Print(selfSMA);
Print("The Cross Price is");
Print(Price);
selflongEMA = Input[0]*selflongconstant1 + EMA50[0]*selflongconstant2;
// EMA = ConvergencePrice * constant1 + selflongEMA * constant2
ConvergencePrice = (selflongEMA * selflongconstant2) / (1 - selflongconstant1);
Print(EMA50[0]);
Print("The Convergence Price is");
Print(ConvergencePrice);
if (EMA50[0] > Input[0])
{
EnterShortLimit(3, ConvergencePrice, "ConvergenceEntryPrice");
Print("hello");
}
if (EMA50[0] < Input[0])
{
EnterLongLimit(3, ConvergencePrice, "ConvergenceEntryPrice");
}
}
}
}
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if(order.Name == "ConvergenceEntryPrice")
{
ConvergenceEntryPrice = order;
}
Print(orderState);
}
Comment