I'm storing a global boolean variable which I set to true when I call enterLong(), and false when I call exitLong(). Same thing for enterShort() and exitShort(). When I plot the value of this variable, I'm seeing individual spikes, rather than a series of 1's that align with my trades, and 0's that align with flat periods. What am I doing wrong?
The reason I'm doing this is because I have different exit logic for trend-following trades than I do for counter-trend trades, and I can't figure out how to only run my trend-following exit logic with trend-following trades. If I could get this boolean to work I would have solution for it.
I could think of 2 reasons without seeing the code: 1) plot() only executes when you enter 2) it is just an issue of appearance on a 'condensed" default chart. Try to zoom in and see it bar by bar.
Okay, here's the code. I changed the variable from a boolean to an integer just to try different approaches. The variable we care about is called HoldTrend. As you can see, the followTrend() function is called in every run, and the plot() function for HoldTrend is called every time.
And attached is a screenshot, the bottom plot is the one showing the HoldTrend value. As you can see, the counterTrend() function has been commented out, so only followTrend() trades are being made, and while we have a long trade showing in the chart, there is not a matching series of 1's in the bottom chart for that length of time.
Main only runs once at the beginning of the backtest, right? I added this line because the Zorro manual says:
Quote
Variables can be defined outside functions or inside functions. When defined outside, they are called global variables and are accessible by all functions. They keep their values until the script is compiled the next time.
and
Quote
Static and global variables keep their values until the script is compiled again. So make sure to initialize them at script start - if(Init) - to their initial values if required.
I wasn't really sure if was relevant or not, but since main should only run at the beginning, I felt there was no harm in ensuring the value was initialized once at the start for each test.