Selling When Should be Buying

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
I am trying to customize the Direction Trend Strategy (long position only) and I have just about done it using the trade manager but my main problem is that instead of buying on the buy signal, it sells the trade lot specified amount. Also, it wont seem to add the limit and stop loss orders. I am new to Java, but have experience with Javascript and PHP, so I can wrap my head around. Unfortunately this is just too much for me to digest. Any help is greatly appreciated. In the link below is the code for the strategy text format. It is essentially just extending the Direction Trend Strategy using its buy/sell signals, but like I said, I am having some issues with it.

Thanks so much. If you need a few dollars to debug, this, I am

 

Spin

Well-known member
Joined
May 22, 2019
Posts
478
Likes
191
Hello @wayum999 and welcome to the MW-forums :)

I had a look at the code you posted and noticed you did indeed use the standard MW-TradeManager as a basis, but you also removed a lot of lines.

My technique for getting coding issues like this resolved is adding 'debug' lines at strategic points in the code, and then add some more and finally add even more debug-lines :cool: I literally retrace all steps of the logic to be able to see what it is exactly my study does, when it does it and what is the result.

Specifically for your case I would open up a chart, use the replay mode to set a starting point somewhere right before a 'BUY' signal and start debugging (by opening up the Study Log window in the View - Display menu)

I am looking at lines 305 to 309 of your code to start:

Java:
//        Get Position type LONG or SHORT
        switch (getSettings().getPositionType()) {
  
//        If Position is LONG
        case LONG: // Only Long Positions are allowed.
          if (position == 0 && signal == Signals.BUY) {

I would make it look like this:

Java:
//        Get Position type LONG or SHORT
        switch (getSettings().getPositionType())
        debug ("getPositionType = " + getSettings().getPositionType());
        {
  
//        If Position is LONG
        case LONG: // Only Long Positions are allowed.
        debug ("position = " + position + " signal " + signal);
          if (position == 0 && signal == Signals.BUY) {

I would then closely inspect the output in the study log as I step forward through the bars in Replay Mode, and try to spot inconsistencies. (i.e.: do you get a BUY signal when your study fires one ?)

If that section looks ok, comment out these debuglines and move on to next. If you do this meticulously, you will eventually hit the right spot and figure out what needs to be corrected.(y)

Good luck !

PS: during debugging you might notice that some values can't be printed in debug statements, so you may need to turn them into strings: String printableValue = String.valueOf(unprintableValue);

PPS: This might also help
 
Last edited:

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
thanks very much i will give it my best shot on this, much appreciated!
 

wayum999

Active member
Joined
May 26, 2020
Posts
26
Likes
5
yes i was although i havent had time to create the short positions or test them, but i was able to do the long ones successfully and am running it now
 

Spin

Well-known member
Joined
May 22, 2019
Posts
478
Likes
191
Good job @wayum999 ! (y)

Perhaps post your code as a way of giving back to the community ?

That way others might benefit from it :)
 
Top