All I am trying to do is simply buy on same day of candle that opens greater than the day before, but I do not want to buy the next day that is why I had to use Calculate.OnPriceChange or something like that. I have my code below. When I take Tick Replay off it won't be able to work as intended (it will buy the next day but I won't have the Exit on session error). Exit on session close should always be the last candle which is May 3 in my example.
IN OTHER WORDS:
Like lets say we have daily candlesticks from today and 4 days before. now tomorrow the open will have a price. I want to trade on the open tomorrow based on if the open is > the close we had today.
What is going on?
Sry for the confusion, but my followingCandle is really reffering to the day before the current day candle so like the previous
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
namespace NinjaTrader.NinjaScript.Strategies
{
public class CandlestickStrategy : Strategy
{
[NinjaScriptProperty]
public int OrderQuantity { get; set; } = 1;
[NinjaScriptProperty]
public double StopLossPercentage { get; set; } = 0.2;
[NinjaScriptProperty]
public System.TimeSpan TradeTime { get; set; } = new System.TimeSpan(16, 5, 0);
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "CandlestickStrategy";
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 = 5;
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
Calculate = Calculate.OnPriceChange;
// Add a 1-tick series for intra-bar granularity
AddDataSeries(BarsPeriodType.Tick, 1);
// Set a percentage-based stop loss
SetStopLoss(CalculationMode.Percent, StopLossPercentage);
}
}
protected override void OnBarUpdate()
{
// Only process on the primary bar series and ensure enough bars are loaded
if (BarsInProgress != 0 || CurrentBar < 5)
return;
// Check if the current bar is a new bar
if (IsFirstTickOfBar)
{
// Get the bar index for the current trading day
int currentDayBarIndex = Bars.GetBar(Time[0].Date);
Print("Current Day Bar Index: " + currentDayBarIndex);
// Get the open price of the current trading day
double currentDayOpen = Open[currentDayBarIndex];
Print("Current Day Open: " + currentDayOpen);
// Define necessary variables
double followingCandleOpen = Open[0];
// Conditions for buying
if (followingCandleOpen > currentDayOpen)
{
EnterLong(OrderQuantity, "Buy4"); // Enter a long position with the "Buy4" label
}
}
}
}
}
Thank you,
MatHatter

Comment