So the red is where I need to be long and then just close say 2 points after, I have this for the most part but the stop loss is generally my problem now. I basically need to enter a position with say a 2 tick stop loss, if it hits the stop loss then it wasnt the "absolute bottom", thus I re enter the trade and sit another 2 tick stop loss, this occurs until it goes back up and hits the take profit. Currently I use Bollinger bands to measure the trend, here is my current code:
public class LabeledDataStrategy : Strategy { private Bollinger Bollinger20; private int totalContracts = 1; private int counter = 0; public double ConvertToEpochTimestamp(DateTime dateTime) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); TimeSpan diff = dateTime.ToUniversalTime() - origin; return Math.Floor(diff.TotalSeconds); } protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"LabeledDataStrategy"; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; Calculate = Calculate.OnEachTick; BarsRequiredToPlot = 15; Name = "LabeledDataStrategy"; period = 5; stopLoss = .5; } } protected override void OnBarUpdate() { if(counter == 0) { counter++; } if (Position.MarketPosition == MarketPosition.Long) { if (Close[0] >= Position.AveragePrice + 8 * TickSize) { ExitLong("LONG"); } } double lower = Bollinger(2, period).Lower[0]; if (Position.MarketPosition == MarketPosition.Flat) { if (Close[0] - lower <= .5 || Low[0] <= lower) { EnterLong(totalContracts, "LONG"); SetStopLoss(CalculationMode.Ticks, 1); } } } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "period", Order = 1, GroupName = "Parameters")] public int period { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "stopLoss", Order = 1, GroupName = "Parameters")] public double stopLoss { get; set; } }
Comment