I'm getting stuck with switching timeframe arrays.
I am experimenting building out a breakout system with a chandelier stop. It buys when the highest high is higher than the most recent highest high for the most recent 'n' candles. See in the following example, the green line turns positive indicating a buy, and the apply stop trails below... The blue moving average is an aggressive filter.
The code (for an exploration, note I have Filter = Buy, but I remove that if backtesting;
/*
Set position size
we want to make it so that out risk is equal to the distance between the entry and the stop.
so we calculate the stop distance, which is stoplength.
Lets do some explorations to see what is what
*/
eqt=Param("Initial Equity",10000,1000,100000);
SetOption("InitialEquity",eqt);
SetOption("MaxOpenPositions",20);
PlotOHLC(O,H,L,C,"OHLC",colorBlack,styleCandle);
period = Param("High/Low Value Period",20,3,100);
upbreak = Plot(HHV(H,period),"Highest High",colorGreen,styleDashed,Null,Null,Null,1,2);
//...for short, add later>>downbreak =Plot(LLV(L,period),"Lowest Low",colorGreen,styleLine,Null,Null,Null,1,2);
filt_period=Param("Moving Average Filter",20,5,200,1);
maFilt =MA(C,filt_period);
Plot(maFilt,"MA Filter",colorBlue,styleLine|styleThick,Null,Null,Null,Null,2);
//Buys and Sells
Buysignal = (HHV(H,period)>Ref(HHV(H,period),-1)) AND C>maFilt;
Buy = Buysignal;
Sell = 0;
// Calculate the stop
atrMultip = Param("ATR Multiplier",2.2,0.1,5,0.1);
stopLength=atrMultip*ATR(10);
ApplyStop(stopTypeTrailing,stopModePoint,stopLength,1,True,1);
//Plot the Trailing stop
Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, H-stopLength ), Null );
Plot( stopline, "trailing stop line", colorRed, styleLine|styleThick);
/*
We want 1% loss to equal 1% of the equity. So, if the risk distance is 50 and we have 10000, and we are prepared to lose 1%
then we are prepared to lose $100.We buy 2 shares. If the risk distance is 25 and we have 10000, and we are prepared to lose 1%
($100, then that is 4 shares.
So, (equity/100) tells us how much 1% risk is per trade (or 1R)
R/stopLength gives us the number of shares we can buy.
*/
Filter=Buy;
r=eqt/100;
numShares = r/stopLength;
R_Multip=Param("R Multiplier",2,0.5,4,0.5);
SetPositionSize(numshares*R_multip,spsShares);
AddColumn(eqt,"Equity",1.4);
AddColumn(r,"R Value",1.4);
AddColumn(2*r,"2R Value", 1.4);
AddColumn(Close,"Entry",1.4);
AddColumn(ATR(10),"ATR",1.4);
AddColumn(atrMultip,"ATR Multiplier",1.4);
AddColumn(stopLength,"Stop Length",1.4);
AddColumn(stopLine,"Stop Level",1.4);
AddColumn(numShares,"Numb. Shares",1.4);
TimeFrameSet(4*inHourly);
ema4_8=MA(C,8);
ema4_34=MA(C,34);
TimeFrameRestore();
TimeFrameExpand(ema4_8,4*inHourly);
TimeFrameExpand(ema4_34,4*inHourly);
AddColumn(ema4_8,"4 Hourly EMA8",1.4);
AddColumn(EMA(C,8),"Hourly EMA8",1.4);
AddColumn(ema4_34,"4 Hourly EMA34",1.4);
AddColumn(EMA(C,34),"Hourly EMA34",1.4);
The backtest results are excellent in a bull market but as expected, in a downtrending market, it performs very poorly, so I am looking to filter it off on a higher time frame. I'm running this on an hourly time frame, and I am trying to experiment with what would happen if we use an EMA 8 > EMA34 on the 4 hourly, as a filter. The results I am getting in my exploration suggest that I have used the timeframeset(), timeframerestore() and timeframeexpand() functions incorrectly as the figues quoted by the system for the 4 hourly EMA8 and EMA34, are incorrect. See for yourself...
For example, the 4 hour EMA8 at 1/1/2023,1600h,was about 1196 but the system I have created says it was 1699, so I've done something wrong.
I've checked my data manually as well as on the charts in Amibroker and using Tradingview,... and I also follow ETHUSDT quite closely so I am certain the number my system has come up with is incorrect.
It's probably something really obvious but I can't see what I am doing wrong.
Can anyone lend some advice? Thanks in advance.