Your help please folks…
The most simple of code is producing a strange result. The cross of the blue SMA 8 and green SMA 20 is the trigger for crossflag to equal 1, and yet when I graph crossflag it says it’s zero.
The entry itself is correct by these simple rules, but it becomes a problem when I add more complicated variables to derive “crossflag”. I can’t trust my system until crossflag becomes 1, -1 or zero when it ought to.
How can this code actually enter when crossflag is graphing at zero? I just can’t understand! And is there a different way of presenting this logic to make crossflag graph at 1 in the bar I’ve highlighted in the screengrab?
Thanks in advance for your help.
Actually where your mouse is sitting no crossover occurred, as Sma8 is below Sma20, while on the right side of your chart there’s been a crossover, which is correctly reported.
But SMA8 is lower than SMA20 to the left of the mouse, and then it is higher one bar to the right of the mouse. How could this happen without there being a crossover?
Yes, right.
I need the code to figure out what’s the cause.
Oh sorry, I forgot to include it! And now I can’t see the “include code” button, so here it is below in plain text.
DEFPARAM CumulateOrders = False
Longcross= Average[8](close) > Average[20](close)
Shortcross= Average[8](close) < Average[20](close)
if longcross and not longcross[1] then
crossflag=1
endif
if not onmarket and crossflag=1 then
sl=abs(close-lowest[3](low))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
BUY 1 PERPOINT AT market
crossflag=0
endif
if shortcross and not shortcross[1] then
crossflag=-1
endif
if not onmarket and crossflag=-1 then
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
Sellshort 1 PERPOINT AT market
crossflag=0
endif
graph crossflag
Hi Mike – allow me to jump in. Assuming that what you showed is the full code :
if longcross and not longcross[1] then
crossflag=1
endif
Since this is the only time you set crossflag to 1, the culprit has to be the combined condition which won’t become true.
Besides that, for me it would look odd if two subsequent bars would cross (any signal). Thus there it has to go wrong ?
Additionally, this looks a bit odd as well :
if not onmarket and crossflag=-1 then
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
Sellshort 1 PERPOINT AT market
crossflag=0
endif
First off, this suggests that you want to know crossflag at the next call (bar) of your code. Do notice that this would require a
once crossflag = 0
somewhere at the start of your code.
Also, personally I would have the Set Stop Loss (and I think Set Target Profit as well) after the Sellshort. It may be fine, but if I would be the PRT code parser ;-), I would set those only at the next call of the code and then it would not even happen because by that time OnMarket will be True.
Possibly it is fine anyway, but I would not depend on that (the code executes from top to bottom and how to set a SL if there is no position yet).
Maybe Roberto has better ideas for you ?
Thanks for the suggestion, Peter. I haven’t quite managed to work out how to use ONCE statements effectively, but I’ll have another look in the manual to see how I could apply it here. The SL code has been fine in previous systems, so I think I’ll just leave that one.
What I’d really like to do is have the flag reset to zero on the flip of the PSAR, and then have the cross creating 1 or -1. Or alternatively have each PSAR flip indicate that it’s going to be a long cross or a short cross. I attempted it with this code but it didn’t work:
DEFPARAM CumulateOrders = False
Averagecrosslong= Average[8](close) >Average[20](close)
Averagecrossshort= Average[8](close) <Average[20](close)
if SAR[0.02,0.02,0.2] > SAR[0.02,0.02,0.2][1] then
longsar=1
shortsar=0
endif
if SAR[0.02,0.02,0.2] < SAR[0.02,0.02,0.2] then
shortsar=1
longsar=0
endif
if longsar=1 and averagecrosslong and not averagecrosslong[1] then
crossflag=1
endif
if not onmarket and longsar=1 and crossflag=1 then
sl=abs(close-lowest[3](low))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
BUY 1 PERPOINT AT market
crossflag=0
longsar=0
endif
if averagecrossshort and not averagecrossshort[1] then
crossflag=-1
endif
if not onmarket and shortsar=1 and crossflag=-1 then
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
Sellshort 1 PERPOINT AT market
crossflag=0
shortsar=0
endif
graph crossflag
or
DEFPARAM CumulateOrders = False
Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close)
Averagecrossshort= Average[8](close) CROSSES under Average[20](close)
if SAR[0.02,0.02,0.2] > SAR[0.02,0.02,0.2][1] then
longsar=1
shortsar=0
endif
if SAR[0.02,0.02,0.2] < SAR[0.02,0.02,0.2] then
shortsar=1
longsar=0
endif
crossflag=0
if longsar=1 and averagecrosslong then
crossflag=1
endif
if not onmarket and crossflag=1 then
sl=abs(close-lowest[3](low))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
BUY 1 PERPOINT AT market
longsar=0
endif
if shortsar=1 and averagecrossshort then
crossflag=-1
endif
if not onmarket and crossflag=-1 then
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
Sellshort 1 PERPOINT AT market
crossflag=0
shortsar=0
endif
graph crossflag
JSParticipant
Senior
DEFPARAM CumulateOrders = False
VL = 1
Longcross= Average[8](close) > Average[20](close)
Shortcross= Average[8](close) < Average[20](close)
//if longcross and not longcross[1] then
//crossflag=1
//endif
if Longcross=1 then
//sl=abs(close-lowest[3](low))+5
//tp=sl * VL
//Set target profit tp
//Set stop loss sl
BUY 1 PERPOINT AT market
//crossflag=0
endif
//if shortcross and not shortcross[1] then
//crossflag=-1
//endif
if Shortcross=1 then
//sl= abs(close-highest[3](high))+5
//tp=sl * VL
//Set target profit tp
//Set stop loss sl
Sellshort 1 PERPOINT AT market
//crossflag=0
endif
graph Longcross
graph Shortcross
When using only Longcross and Shortcross there is no problem…
(SL and TP omitted for a better view)
JSParticipant
Senior
If you want to use Crossflag then remove from your code:
“not onmarket” (because your always on market)
“crossflag=0”
DEFPARAM CumulateOrders = False
Longcross= Average[8](close) > Average[20](close)
Shortcross= Average[8](close) < Average[20](close)
if longcross and not longcross[1] then
crossflag=1
endif
if crossflag=1 then
//sl=abs(close-lowest[3](low))+5
//tp=sl * VL
//Set target profit tp
//Set stop loss sl
BUY 1 PERPOINT AT market
//crossflag=0
endif
if shortcross and not shortcross[1] then
crossflag=-1
endif
if crossflag=-1 then
//sl= abs(close-highest[3](high))+5
//tp=sl * VL
//Set target profit tp
//Set stop loss sl
Sellshort 1 PERPOINT AT market
//crossflag=0
endif
graph crossflag
Thanks for the suggestions. If I remove not onmarket, when I have a live trade, won’t it enter in a counter direction when these signals are met?
I haven’t quite managed to work out how to use ONCE statements effectively
Once MyCounter = 0 // Cause its value to be known at a next (bar) call.
MyCounter = MyCounter + 1
Graph MyCounter // Goes up and up and up and ...
MyCounter = 0
MyCounter = MyCounter + 1
Graph MyCounter // Always shows 1.
Easy ! (and the most useful)
DEFPARAM CumulateOrders = False Averagecrosslong= Average[8](close) CROSSES OVER Average[20](close) Averagecrossshort= Average[8](close) CROSSES under Average[20](close) if SAR[0.02,0.02,0.2] > SAR[0.02,0.02,0.2][1] then longsar=1 shortsar=0 endif if SAR[0.02,0.02,0.2] < SAR[0.02,0.02,0.2] then shortsar=1 longsar=0 endif crossflag=0 if longsar=1 and averagecrosslong then crossflag=1 endif if not onmarket and crossflag=1 then sl=abs(close-lowest[3](low))+5 tp=sl * VL Set target profit tp Set stop loss sl BUY 1 PERPOINT AT market longsar=0 endif if shortsar=1 and averagecrossshort then crossflag=-1 endif if not onmarket and crossflag=-1 then sl= abs(close-highest[3](high))+5 tp=sl * VL Set target profit tp Set stop loss sl Sellshort 1 PERPOINT AT market crossflag=0 shortsar=0 endif graph crossflag
That code was okay for only longs, but it didn’t reset properly when I added in shorts. But I think I’ve solved it with this amended version of your code. I added “crossflag=0” at the very top and removed any instances of “crossflag=0” from the rest of the code.
if SAR[0.02,0.02,0.2] > SAR[0.02,0.02,0.2][1] then
longsar=1
shortsar=0
endif
if SAR[0.02,0.02,0.2] < SAR[0.02,0.02,0.2][1] then
shortsar=1
longsar=0
endif
if longsar=1 and averagecrosslong then
crossflag=1
endif
if not onmarket and crossflag=1 then
sl=abs(close-lowest[3](low))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
BUY 1 PERPOINT AT market
longsar=0
endif
if shortsar=1 and averagecrossshort then
crossflag=-1
endif
if not onmarket and crossflag=-1 then
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
Sellshort 1 PERPOINT AT market
shortsar=0
endif
graph crossflag
JSParticipant
Senior
No, it will open only in the direction of “crossflag”
Add Crossflag = 0 at the top of your code and everything works fine 🙂
DEFPARAM CumulateOrders = False
Crossflag = 0
VL = 1
Longcross= Average[8](close) crosses over Average[20](close)
Shortcross= Average[8](close) crosses under Average[20](close)
if longcross and not longcross[1] then
crossflag=1
endif
if crossflag=1 then
BUY 1 PERPOINT AT market
sl=abs(close-lowest[3](low))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
//crossflag=0
endif
if shortcross and not shortcross[1] then
crossflag=-1
endif
if crossflag=-1 then
Sellshort 1 PERPOINT AT market
sl= abs(close-highest[3](high))+5
tp=sl * VL
Set target profit tp
Set stop loss sl
//crossflag=0
endif
graph crossflag
once is to initialise the variable once on the first reading of algo