wolfParticipant
Junior
Hello, I’m BackTesting with some Exponential moving averages, and the backtest often opens a trade a candle or 2 after the first candle after the EMA cross. Making the results inaccurate. Using a slightly faster EMA to compensate is what I’ve been doing, but that creates other problems.
My code is very basic and is just calculating EMA on the close of the candle, and so is my indicator on the chart. So nothing wrong there.
Attached is an example.
Any ideas on how to fix this?
If you actually just have the 2 averages that constitute your signal we might be able to see exactly what you are describing but with 4 averages i am clueless to what you are actually describing ? Ambiguity isnt helping here , Clarity makes it much easier …
You should post your complete code for anyone to be of help.
Roberto
- A cross between 2 lines is confirmed at Close
- Code is read at Close and orders are launched at next bar Open
which in effect is a lag of what really should be 1 tick .
a signal during bar is useless thats why it is done this way , only once a bar is complete can a signal truly be verified
wolfParticipant
Junior
Hi, here’s my code. It’s very basic, so hopefully theres nothing wrong there.
EMA1 = ExponentialAverage[25](close)
EMA2 = ExponentialAverage[100](close)
cBuy = (EMA1 crosses over EMA2)
cSell = (EMA1 CROSSES under EMA2)
set stop %loss 0.5
IF cBuy THEN
BUY 1 Contract AT Market
ENDIF
IF cSell THEN
SELLSHORT 1 Contract AT Market
ENDIF
wolfParticipant
Junior
Sorry, I thought circling the only cross near the backtest order would be clear enough.
wolfParticipant
Junior
So to get ProOrder to execute when I want it, should I change by indicator/code to calculate on the open instead of the close? Because when I do so, it still opens the trade ‘late’.
The pictures attachted are with the code calculating the EMA on ‘open’ instead of ‘close’
You need to move your set stop %loss 0.5 down to the last line of code. Code is read from top to bottom at the close of each candle and so you will have to wait for a candle to pass after opening trades before it is read. At the bottom below the buy/sells it will put an order to the market just after buying or selling.
wolfParticipant
Junior
Thanks I’ve moved the stop loss to the bottom. Is there anyway to get my code to execute when I want it too? i.e. open of first candle after EMA cross
No all decisions are made at the close of a candle and new position are opened at the open of the next – so be aware of markets with big gaps!
The only way to open a position mid candle is to set market stop and limit orders at a chosen price but these orders will once again not be placed until the current candle has closed.
wolfParticipant
Junior
So, I guess I have to use a faster EMA in the ProOrder code, which is annoying since it stops me from being able to fully trust the data.
If PRT was able to check for MA crosses mid candle then I think that you would get many false signals anyway when markets are going sideways.
wolfParticipant
Junior
I get that, but in my case I dont want to open mid candle as soon as the EMAs cross, I want to open on the open of the first candle after the cross happens. This works fine in real time trading since the EMAs move, visually, on the open of a new candle(even if counting from ‘close’) but it backtesting they work differently it seems.
I dont want to open mid candle as soon as the EMAs cross, I want to open on the open of the first candle after the cross happens.
When a candle closes PRT will just check that a cross has occurred at that point in time (I guess it just compares the MA positions from this close to the previous close). It then opens a position at the next open. So it is doing what you want. I have to confess that I am struggling to see the problem at the moment 🙂