Hello,
I am trying to code a strategy that trades only between 7:30 and 12:30 pst and am not getting the results I want.
Other requirements that are popping up are:
Enter only between 7:30 and 12:30 but take any exit signal up until the EOD
If no exit signal then Close the trade at the end of the day
This is a reversing signal trade so I was using the auto.buy order type but with that, I cant figure out how to separate the entry from exit signals.
If I used buy to open type it would not reverse the trade.
There are lines ####out since I was trying different things.
Thanks for any help you can provide
I am trying to code a strategy that trades only between 7:30 and 12:30 pst and am not getting the results I want.
Other requirements that are popping up are:
Enter only between 7:30 and 12:30 but take any exit signal up until the EOD
If no exit signal then Close the trade at the end of the day
This is a reversing signal trade so I was using the auto.buy order type but with that, I cant figure out how to separate the entry from exit signals.
If I used buy to open type it would not reverse the trade.
There are lines ####out since I was trying different things.
Thanks for any help you can provide
Code:
# Charles Schwab & Co. (c) 2013-2024
#
input price = close;
input fastLength = 5;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;
#######################################################
input StartTrading = 0730;
input EndTrading = 1230;
input MinutesTrading = 300;
def Today = if GetDay() == GetLastDay() then 1 else 0;
#def AfterStart = if SecondsFromTime(StartTrading) >= 0 then 1 else 0;
#def BeforeEnd = if SecondstillTime(EndTrading) >= 0 then 1 else 0;
#def OKToTrade = AfterStart and BeforeEnd;
def OKToTrade =SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) <= 0;
#########################################################################
plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
AddOrder(OrderType.BUY_AUTO, OKToTrade and FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "buy");
AddOrder(OrderType.SELL_AUTO, OKToTrade and FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "sell");
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-2] == 1 and afterEnd == 0) or (isRollover[-2] and firstBarOfDay[-2]) then 1 else 0;
#close eod
AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name =lastBarOfDay+ "EOD");
AddOrder(OrderType.BUY_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name = lastBarOfDay+"EOD");