Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I'm having trouble with a simple bool type switch... can anyone see the problem with this code? Here is a piece of sample code -
variables: BoolExitLong(False);
....
//exit code
if marketposition[0] = 1 then begin
if close[0] < average(close,20)[0] then
BoolExitLong = True;
if BoolExitLong = True and close[0] > average(close,50)[0] then
sell ("boolexL") next bar at market;
BoolExitLong = False;
//some more code
end;
What I'm trying to achieve here is "if a certain low level is hit, then sell at next opportunity at a higher level". Everything else works, but this doesn't... when I do get it to work its just constantly stopping out long trades - so clearly not shutting off properly. But also not really working in the first place...
Thanks in advance.
Can you help answer these questions from other members on NexusFi?
Hard to say at this point. The algorithm at least appears to be functional [at least according to a quick comparison of output of the following test indicator with close price in a 240 minute chart of spot EUR.USD (i.e., it appears to be generating a sell signal when close lies between the 20 and 50 SMAs and 20MA > 50MA)]
Assuming there are no errors in the rest of the code logically the go-long algorithm may be entering trades after 20 MA rises above 50 but while the close is still between them, in which case the trade would be immediately stopped out. In the chart below there are a few places this might occur (points where one might go long between the 20MA and 50MA when 20 > 50), but this does not appear to be the rule.
Hi bnichols,
I really appreciate the help. That was terrific - however, I'm still stuck on why it won't work for me.
how can I be seeing output like this…
1121107.00 557.00Bool was TRUE and EXIT level reached
1121107.00 557.00Turning Bool OFF
1121107.00 618.00Bool was TRUE and EXIT level reached
1121107.00 618.00Turning Bool OFF
1121106.00 1957.00Initial Bool ON
1121106.00 1957.00Turning Bool OFF
1121107.00 824.00Initial Bool ON
1121107.00 824.00Turning Bool OFF
with the following code blocks:
if marketposition(0) = 1 then begin
if close[0] < KeltnerChannelLower(close,Kexitlen,KeltWidex)[0] then begin
Print(D," ",T,"Initial Bool ON");
BoolExitLong = 1;
end;
if BoolExitLong = 1 then begin
if close[0] > KeltnerChannelLower(close,Keltlen,keltwid)[0] then
Print(D," ",T,"Bool was TRUE and EXIT level reached");
sell ("bool was true exit") next bar at market;
Print(D," ",T,"Turning Bool OFF");
BoolExitLong = 0;
end;
...
end;
How is it running through that last block of code and acting on the last part but not the middle part? Doesn't it only execute the two actions (and two print statements) if the first two were true? How is it only executing one of the actions?
Referring to this part of the output:
1121106.00 1957.00Initial Bool ON
1121106.00 1957.00Turning Bool OFF
1121107.00 824.00Initial Bool ON
1121107.00 824.00Turning Bool OFF
Here is an image of whats happening as well - it appears to be closing the position on the "Bool was true exit level reached" exit - so selling at market right there, however it's not fulfilling the the second requirement of that block of code:
Close must be greater than KeltnerChannelLower
"if close[0] > KeltnerChannelLower(close,20,1.5)[0] then"
As you can see its closing below the Keltner Channel Lower, its only supposed to close when it retraces back up to the kelt band. The KeltnerChannelLower function normally works so that shouldn't be the issue...
While I don't see any issues with the output versus the code snippet in post #4 (i.e., it appears to be doing exactly what it's told to do), the comment in post #5 (" it's not fulfilling the the second requirement of that block of code: Close must be greater than KeltnerChannelLower") suggests that you might be missing a begin/end block.
Right now the code will exit the position as soon as price drops below the lower Keltner boundary because the lines
simply prints out some text if the test is true, rather than controlling the exit.
BTW, I wrote a signal to test the exit policy you describe and it appears to have a fairly robust expectancy, which means while it might be improved it may make money as is once optimized for your instrument, more money with more investment, so good luck with it
Edited to add: Which is a warning it will probably lose money trading micro lots with realistic slippage and commission
Hi Bnichols and Ehlaban, thank you very much for your help. Much appreciated!
I tried both of these suggestions:
However, I was still getting a million improper exits >> as compared to what I was trying to achieve. So I started to think that it might be something else that's causing the issue.
I decided to start over with this section of code so that it is very simple and all parts of the code can be seen. I am only looking at the long side of the code to make this even simpler.
So I'll try to explain again what I am trying to do:
The entry is not important here. The exit is what I am trying to get right. The idea is that once the price closes below a certain level then an exit signal is generated (the bool turning to TRUE, no trades taking place). This means that the price is lower than the KeltnerChannel (in this case - could be a bollinger band etc) - now we're looking for a retracement to get out of the long position. I have the retracement level required set as an 18 period moving average which should always be in between the keltners.
So the bool can only be turned on when we're in a long position and when we're below the lower keltner and it will always exit once we retrace up to the moving average. It should not continue holding beyond that level...
I also have the bool being turned off after any other type of exit so that the bool is only True just before an exit takes place.
I have it acting fairly well now (as I had intended) However, I still can't account why it will only exit half of the time when "it is supposed to" - according to my intent.
Here is my current simple code (all of it):
This issue remaining is shown below - As you can see the code has us in a long trade coming into the section shown - then price drops below the keltnerchannel lower bound around 18:45 which should have turned on the bool exit (CloseTrade = True) - it should have then been looking for price to close above the 18 period moving average. It moves above the average but nothing happens. Same thing further along at 7/11/2012 7:00-11:00 and then moves back above and through the moving average - but no exit once it gets to that level. And then for some reason the CloseTrade trade happens later on after another move below the lower keltner....
Any thoughts here? I'm really stuck...
Thanks again for all of the help.