HI, im looking to have my strategy only time during the times i set. I then am looking to have it close all positions at the end time 23:00. i am having trouble having my script adhear to it. any help on the code below?
bool isTradingTime = DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 23;
if (!isTradingTime)
{
ExitLong();
ExitShort();
return;
}
if (shouldEnterShort && isTradingTime)
{
EnterLong("Long");
}
if (shouldEnterLong && isTradingTime)
{
EnterShort("Short");
}
if (shouldExitShort && isTradingTime)
{
ExitLong("LongX");
}
if (shouldExitLong && isTradingTime)
{
ExitShort("ShortX");
}
if (Position.MarketPosition == MarketPosition.Flat)
{
return;
}
First off, DateTime.Now.Hour is the time on your machine, not the last time stamp of any of the data series in your strategy. To get the last time stamp of the primary data series as an example, you could write this code instead:
Second, I think you mixed up your variable names and the order submission methods in the middle 4 if blocks. For example, in the code below, either the variable in the if condition should be “shouldEnterLong” or the code in the if block should be “EnterShort(“Short”)”:
if (shouldEnterShort && isTradingTime)
{
EnterLong(“Long”);
}
Third, and this is the least important because it doesn’t actually make your logic wrong, you don’t need “&& isTradingTime” in any of the conditions after the first return; in the if (!isTradingTime) block.