here is my strategy, not working properly, don't know why, sometimes the orders are not cancelled and he can't update properly.
#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 Aversion3 : Strategy
{
private int PositionCalc;
private double SLprice;
private double Entryprice;
private MyDonchianClose MyDonchianClose1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "Aversion3";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.ImmediatelySubmit;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
StopTargetHandling = StopTargetHandling.ByStrategyPosition;
BarsRequiredToTrade = 60;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Virtual_Account_Value = 10000;
Risk = 0.06;
Contract_Multiplier = 1;
Entry_Stop_Spread_TICKS = 100;
PositionCalc = 1;
SLprice = 1;
Entryprice = 1;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
MyDonchianClose1 = MyDonchianClose(Close, 54);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 55) //you have to put 2, unless it wont run because you are asking indicator data [1]barago, so need 2 bars at least.
return; //you have to put 55, so you would have at least 55 bars, unless he tries to enter position on 3 bars distance which is huge!
// Set 1
if (Position.MarketPosition == MarketPosition.Flat)
{
Entryprice=MyDonchianClose1.Upper[1]; //calculating what should be the position size of the trade
SLprice=MyDonchianClose1.Lower[1];
double PositionCalc=Math.Ceiling((Virtual_Account_Value*R isk)/((Entryprice-SLprice)*Contract_Multiplier));
EnterLongStopLimit(Convert.ToInt32(PositionCalc), (MyDonchianClose1.Upper[1] + (Entry_Stop_Spread_TICKS * TickSize)) , MyDonchianClose1.Upper[1], @"EnterLong");
}
// Set 2
if (Position.MarketPosition == MarketPosition.Long)
{
//here you put Position.Quantity=equal to currentposition, so the position size will be the same as we enter the trade.
ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), MyDonchianClose1.Lower[1], @"ExitLong", @"EnterLong");
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="Virtual_Account_Value", Description="This is the cash value before you start the strategy in the small virtual account", Order=1, GroupName="Parameters")]
public double Virtual_Account_Value
{ get; set; }
[NinjaScriptProperty]
[Range(0.01, double.MaxValue)]
[Display(Name="Risk", Description="Risk percent on each trade of the virtual account", Order=2, GroupName="Parameters")]
public double Risk
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Contract_Multiplier", Description="Multiplier for futures", Order=3, GroupName="Parameters")]
public int Contract_Multiplier
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(Name="Entry_Stop_Spread_TICKS", Description="what is the distance of the LMT in the StopLmt order", Order=4, GroupName="Parameters")]
public int Entry_Stop_Spread_TICKS
{ get; set; }
#endregion
}
}
would appreciate if you see something screamingly wrong... thanks in advance!
Comment