Stop adding positions in a Stop and Reverse strategy
Author: pholding2001
Creation Date: 1/18/2021 11:35 AM
profile picture

pholding2001

#1
Hi,

I've just bought Wealthlab and am playing around with it. I don't have much programming experience but am prepared to learn.

I'm using the strategy builder to build a simple moving average crossover system with a few more filters. It's a reversal system and therefore always in the market. The problem I'm having is the following:

Assuming I'm long, the close conditions are:

- FastMA crosses SlowMA
- Price must be below SlowMA
Or
- FastMA is below SlowMA
- Price is below SlowMA

There are occasions when there is a cross but price is not below the SlowMA and so it doesn't close. Price may then continue to trend up and the FastMA crosses the SlowMA again. Therein lies the problem, as that causes another trade to be entered.

I only ever want to have one long or short position on at a time.

I've play around with the "Position Management" buttons. When I select "single open position", it stops reversing from long to short or vice versa on the same day and so I've left it on "Multiple open positions allowed". I've played around with the "position based" conditions to no avail. Does anyone have any suggestions. Maybe I just need to tweak the code?

I've attached an example of what I mean.

Many thanks for your help.


profile picture

Eugene

#2
Hi,

Please check out this FAQ | Strategies and WealthScript > How to create a stop-and-reverse system in a Rule-based Strategy?
profile picture

pholding2001

#3
Hi Eugene,

Thanks ever so much for getting back to me and thanks for the link. I've been on the site and have found the line of code for this to work:

if (Positions.Count == 0){

Given my lack of programming knowledge, I copied the line and pasted it in the same location as my code, pressed the compile button and ran the script. It resulted in only one trade being made. Clearly, I've done something wrong. I noticed in the example code on the wiki there were "else" statements which I don't seem to have in my code. I'm not sure if that has something to do with it? I've attached the code with the line of code added. Do I have it in the right place?

Many thanks

Paul
profile picture

Eugene

#4
Paul, this is probably more complex than adding that line to this generated code (which has a lot of duplicate statements). It would be more practical to take the code sample from the Wiki as a starting point for adding your filters. What are they?
profile picture

pholding2001

#5
Hi Eugene,

Thanks for the quick reply. I thought it might be easier to take a screen shot of the rules I used from the strategy builder. I've attached a screenshot.
profile picture

Eugene

#6
Hope this helps. I believe it's human readable now so adjust to taste:

CODE:
Please log in to see this code.
profile picture

pholding2001

#7
Thanks so much for that, Eugene. You're a star.
profile picture

Eugene

#8
Glad to help you!
profile picture

pholding2001

#9
Hi Eugene,

I hope you're well today. I was wondering if you could help me with the following. One of the problems with moving averages is that the fast moving average can cross ever so slightly above/below the slow moving average before crossing back again. To avoid entering and exiting trades where this happens, I would like to specify that a trade only be executed when the fast moving average is a certain value more or less than the slow moving average, so that you can see daylight between the two before it is considered a cross.

I've written the following bool variable but it doesn't seem to work. The compiler doesn't point out an error and when I run the code, it doesn't make a difference to the number of trades even when I exaggerate the value. I've been reading up about the different types in C# and am wondering if this is the problem as I'm multiplying with a decimal and the MA value is perhaps an integer? Maybe I have to cast it somehow?

CODE:
Please log in to see this code.



Do you have any ideas?

Many thanks

Paul
profile picture

Eugene

#10
Paul, it's the other way round:
CODE:
Please log in to see this code.
profile picture

pholding2001

#11
Doh! Thanks Eugene.
profile picture

pholding2001

#12
Hi Eugene,

I hope you had a good weekend. I think my original post was somewhat misleading in it being a reversal strategy. It is but not in the traditional sense as I have a number of conditions to be fulfilled before each trade occurs, so it doesn't go long/short on the same bar necessarily. I'm trying to have: "these conditions OR these conditions".

It works fine when I just have one condition but as soon as I have an OR and more conditions it keeps putting more and more positions on. I would like to only have one long OR one short on at any one time and I've been struggling to figure out how to do that. Do you have any ideas? I'm confused as to how it works when there is only one condition but not more...

Many thanks

CODE:
Please log in to see this code.
profile picture

Eugene

#13
Hi Paul,

Hope this helps:
CODE:
Please log in to see this code.
profile picture

pholding2001

#14
Thank you very much, Eugene :)
profile picture

pholding2001

#15
Morning Eugene,

I've just run the code and it worked just fine. I then ran the code with a bool that doesn't have an OR condition and then it would only put short positions on (i.e., no longs). Here are the bools I used with it:

CODE:
Please log in to see this code.


I was hoping to have a solution in my code where I could have various bool variables and just turn them on and off by numbering the bool variable (e.g. goLong, goLong2 e.t.c.) in the same code without having to have a new code strategy. I'm wondering if that is maybe not possible after all? Do I need to save the above code when I have OR in my variables and the code I originally sent when I don't want OR?


Many thanks for your help.
profile picture

Eugene

#16
Did I miss the complete code with your edits?
profile picture

pholding2001

#17
Hi Eugene,

This is the code that I am using at the moment and the code before your changes and the one that is not working as soon as I put an OR statement in:

CODE:
Please log in to see this code.
profile picture

Eugene

#18
What OR statement do you put and where?
profile picture

pholding2001

#19
Sorry for the confusion. If you look at the bool variables, the OR is "||" in the variable. I would like to be able to run EITHER goLong and its associated variables at one time OR goLong2 and its associated variables at one time. I'm not sure if that's possible? It doesn't work with the way this code is set up. It works fine with goLong but as soon as I use goLong2, it just keeps putting more trades on, not respecting the fact that a trade is already live. When I used your latest amendment to my code, it worked fine for goLong2 but would not work for goLong, only selling short but not going long. Does that make sense?
profile picture

Eugene

#20
QUOTE:
I would like to be able to run EITHER goLong and its associated variables at one time OR goLong2 and its associated variables at one time.

It does not make sense the way it's designed:
CODE:
Please log in to see this code.

goLong will always take priority, goLong2 will never trigger.
profile picture

pholding2001

#21
Hi Eugene,

I think we might be getting our wires crossed. Is it possible that you understand what I'm writing as both goLong and goLong2 running simultaneously?

They are, in theory, two different strategies but instead of having the code in two different strategies, I was hoping to have them in one and when I want to run goLong, I just type that one and when I want goLong2, I get rid of goLong and type goLong2 instead.

Does that make sense? Sorry for any confusion.
profile picture

Eugene

#22
You might want to check out the Combination Strategy tool to have two strategies in one.
profile picture

pholding2001

#23
Hi Eugene,

No problem. Could you at least confirm to me that what I am trying to do is either possible or not in the way I'm currently trying to do it? I don't want to persevere, only to realise that I was trying to achieve something that is not possible. At my level of knowledge (which is currently miniscule), when something doesn't work, I never know whether it's my own inadequacy or because I'm asking the system to do something which is just impossible. If nothing else, it will help in my learning of the programming language.

Many thanks
profile picture

Eugene

#24
Paul, most likely it's something simple that can be implemented in code but your logic seems contradictory (post #20) and I'm lost in your system revisions.
profile picture

pholding2001

#25
That's understandable. I commend you on trying to follow all these different threads. It would be beyond me! You do an amazing job.

Maybe we should just ignore all that has gone before and start from the beginning and I will endeavour to explain myself better. Shall we just do that?
profile picture

Eugene

#26
Paul, given our busy schedule on getting Wealth-Lab 7 out the door I'd appreciate if you exhaust the built-in Combination Strategy tool before anything else. Each included strategy can have its own logic but they're combined by the tool and run together. Hope this helps.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).