I tried to build the following Strategy using Rules, but it will not run. Can you help?
The basic Strategy is as follows:
Buy at Market when AvePriceC makes a new low during the preceding 5 bars; then turns up.
Sell at Market when AvePriceC makes a new high during the preceding 5 bars; then turns down. OR Sell when Trailing StopLoss = 4%
These are the Rules I created:
Scale: Daily
Data Range: 5-Yrs
PosSize: $10,000
Symbol: SPY
Buy at Market
- Indicator makes a new low within period.
-- Indicator = AveragePriceC.Series (Bars); Period = 5
- Indicator is above its value a number of bars ago.
-- Indicator = AveragePriceC.Series (Bars); Lookback Period = 1
Sell at Market
- Indicator makes a new high within period.
-- Indicator = AveragePriceC.Series (Bars); Period = 5
- Indicator is below its value a number of bars ago.
-- Indicator = AveragePriceC.Series (Bars); Lookback Period = 1
OR
Sell at Percent Based Trailing Stop
- Percent = 4.00
I suspect the problem is that for the Buy condition, the value of APC cannot be lower than any of its values during the 5 preceding bars and also be higher than the preceding bar ( and vice versa for Sell condition). It seems that the first condition must be revised to reference bars -6 thru -2 instead of bars -5 thru -1, but I cannot figure how to do that using Rules. Can you?
If this strategy cannot be created using Rules, but can be created using code (I am not a programmer), can I convert the above Rules to code by clicking on "Convert to Code-Base Strategy" and then manually edit the code?
Size:
Color:
Below is the above Rule-base strategy converted to code.
I am not a programmer. If I am correct regarding why the strategy will not run (i.e. -- there is a conflict between the BUY conditions: "lowest of last 5 bars" and "last bar higher than previous", and vice versa for the SELL conditions), will you please either (a) tell me what the sytax is to change the BUY condition from "lowest of last 5 bars" to "lowest of bars -6 thru -2 (and vice versa for SELL condition), or (b) simply make the changes herein.
Thanks.
CODE:
Please log in to see this code.
Size:
Color:
Here's my quick take:
CODE:
Please log in to see this code.
Size:
Color:
Thank you, Eugene.
Size:
Color:
Eugene, I think something is wrong with the code.
If the code is executed using SPY, a SELL Trade is generated on 04/23, and a BUY Trade is generated on 04/26. Neither of these trades complies with the strategy.
To Sell at Market on 04/23, AvePriceC at Close on 04/22 should have been lower than AvePriceC at Close on 04/21 (it was) AND AvePriceC at Close on 04/21 should have been highest of AvePriceC Series 04/15 thru 04/21 (it was not); - OR - Closing Price on 04/22 should have been lower than the 4% Trailing StopLoss (it was not).
To Buy at Market on 04/26, AvePriceC at Close on 04/23 should have been higher than AvePriceC at Close on 04/22 (it was), AND AvePriceC at Close on 04/22 should have been lowest of AvePriceC Series 04/16 thru 04/22 (it was not).
Please check this. Thanks.
Size:
Color:
Your first idea was clear:
"- Indicator makes a new low within period."
This is what my code, which I believe to be correct, does.
Now, your requirement switches to:
"should have been lowest of AvePriceC Series 04/16 thru 04/22 (it was not)."
Size:
Color:
I'm not sure I understand how the two statements differ; but if they do, I apologize for being confusing (non-programmer trying to communicate with programmer). Let me try to describe the strategy more precisely:
Buy at Market
-- AveragePriceC at {Current Bar minus-1} is lowest value within the 5-bar period that ranges from {Current Bar minus-1} thru {Current Bar minus-5}.
-- AveragePriceC at {Current Bar} is higher than AveragePriceC at {Current Bar minus-1}
Sell at Market
-- AveragePriceC at {Current Bar minus-1} is highest value within the 5-bar period that ranges from {Current Bar minus-1} thru {Current Bar minus-5}
-- AveragePriceC at {Current Bar} is lower than AveragePriceC at {Current Bar minus-1}
OR
Sell at Percent Based Trailing Stop
- Percent = 4.00
Thank you for your patience.
Size:
Color:
I guess your original insight about the problem was correct: the 2 rules contradict. Nonetheless, the first description of the rule set was really vague, and couldn't help at all.
Now that we know about "current bar minus 1", I guess you simply need to change this in the entry logic
of your original code:
CODE:
Please log in to see this code.
to this:
CODE:
Please log in to see this code.
And a similar change to the exit block.
Size:
Color: