Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

how to close open position
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=146&t=49196
Page 1 of 1

Author:  nicbrunelli [ Wed 08 May, 2013, 16:24 ]
Post subject:  how to close open position

hi
I tried practicing a very simple strategy based on MA crosses, I tried like crazy to find the way to close an open position once the MA cross back again. Unfortunately I can't get it to work.

I attached the strategy, please help..

thanks

Attachments:
ema6ema24cross.vfs [42.73 KiB]
Downloaded 310 times

Author:  Para [ Thu 09 May, 2013, 09:25 ]
Post subject:  Re: how to close open position

Your strategies looks very complicated :mrgreen: .

I will have a look at it.

Author:  dirk_r1 [ Thu 09 May, 2013, 09:26 ]
Post subject:  Re: how to close open position

looks good

Author:  Para [ Thu 09 May, 2013, 10:40 ]
Post subject:  Re: how to close open position

Could you explain step by step what your srategy is suppose to do please?

I just want to be sure that what I read in your blocks corresponds to what you where trying to do.

Author:  doczero [ Thu 09 May, 2013, 10:59 ]
Post subject:  Re: how to close open position

Hi Nic,

I've fixed it up for you - some of the logic seemed to be wrong with the EMA cross - ie. when the fast ema was going above the slow ema it was selling (unless you want this as a mean reversion system - look up Howard Brandy mean reversion systems for some light reading lol - its a top book).

Also I've added in your exit on opposite cross. Some of things I would have changed is- timeframe to a global variable, I'd add in a fixed pip stoploss (define your risk), Position sizing... we'll I could do alot - but then it wouldnt be your system ;)

I've also changed the system to on bar, not on tick - and also steped the number of bars back 1 bar, as you will find with MA's a lot of false entry/exit signals due to the MA's crossing & uncrossing as the change tick by tick. I doubt you wanted this, and I guess you were looking for cleaner signals.

Attachments:
ema6ema24cross.vfs [48.4 KiB]
Downloaded 346 times

Author:  Para [ Thu 09 May, 2013, 13:54 ]
Post subject:  Re: how to close open position

Well done you! :idea:

That deserves a +! ;)

Author:  nicbrunelli [ Thu 09 May, 2013, 22:13 ]
Post subject:  Re: how to close open position

hello, thanks very much!!!!

it works now, like I wanted. I know it's not a profitable strategy, but my goal was to try to understand how to make it work.
I just did like in the tutorial video showed, exactly the same way, but wanted to add the feature: "close the current position and open the opposite as soon as the EMAS cross back from down to up and vice-versa". I just couldn't understand how to close an open position, I tried all day in many different ways, but I noticed that your approach is totally differnt than what was showed in the tutorial video.

but there something I don't understand: in the second box, the "ema6before" whyt did you set the variable "Time period TempVar404"?

thank you very much!!!!

NIC

Author:  doczero [ Thu 09 May, 2013, 22:25 ]
Post subject:  Re: how to close open position

nicbrunelli wrote:
hello, thanks very much!!!!]

but there something I don't understand: in the second box, the "ema6before" whyt did you set the variable "Time period TempVar404"?



Hi Nic,

I May have deleted that ema block and remade it. The time period temvar404 is automatically generated by VJF - you are free to change it back. It shouldnt affect the strategy.

So - just to continue the conversation - how would you make this profitable?

Personally - I'd throw in a stoploss (to limit downside and define the same risk every time) , the size of stoploss would be critical, so my first step would be to understand the average winner size (i.e over 100 trades what is the average winner), then I would start by chosing a stoploss 1/3 of that value.. example - if the average win was 90 pips, I would test first with a stoploss of 30 pips. What else would you do?

Author:  nicbrunelli [ Fri 10 May, 2013, 08:36 ]
Post subject:  Re: how to close open position

that would be a great idea.

And there's something else I would try to do, set the strategy so that: If a position is gaining the same size as the risk (ex: stop loss =30 pips. position is filled and gaining 30 pips) close half of the position and set the SL to breakeven. Is there a way to set that..?

like every "ema cross" strategy, it works well only in trending market, but loses in sideways market. So, one way to have it work on volatile-sideways market, is to set it to open trades on the opposite direction of the cross, so: sell when slow sma crosses above the fast ema, and buy in the opposite situation. It's kinda funny, but if there's no trend it works pretty well..

Author:  doczero [ Fri 10 May, 2013, 17:24 ]
Post subject:  Re: how to close open position

Hi Nic,

nicbrunelli wrote:
close half of the position and set the SL to breakeven. Is there a way to set that..?


First of all - I havent come across a way to partial close a trade , maybe SUPPORT and comment on partial closes.

I have also quickly thrown together the logic to set a breakeven stop. Now I do it a certain way on purpose. So what I mean is: The first block is to see if the trade is long or short, next block is to make sure the stop has not adjusted to breakeven already (we do this to make sure we dont accidently create a loop which would move the stop to breakeven every tick), following that we check the trade is in profit by the 30 pips, then we move the stop to breakeven.
Image

Reference my image and have a play around - see how you go! The more you experiment , the more you learn! I didnt put it in your system so you would have the opportunity to try yourself first =)

To continue our discussion - I wonder what would be more profitable - closing half the position and moving to breakeven, or just moving the stop to breakeven? (some testing for you to do =) enjoy)

Attachments:
breakeven.jpg [229.46 KiB]
Downloaded 925 times

Author:  nicbrunelli [ Fri 10 May, 2013, 17:50 ]
Post subject:  Re: how to close open position

hey Doczero, thank you very much, your help is very well appreciated..!!
usually, with my manual trading, I prefer to close half position and move the stop to breakeven, so that at least I have a partial profit. I try to use this money management on every strategy, whether is a breakout strategy, or a fibonacci strategy, or a trend-following "pin bar" strategy. Sometimes I might also divide the position i 3 parts and set 3 different targets as well, depending on the momentum and the follow-throu.

I will try to play along with the blocks as you suggested. I guess I will continue next week, because I will be flying to US all weekend. But so that you know, I appreciate your quick and wise answers, thank you very much. I will post new comments as soon as I come up with new ideas

have a great weekend!

Author:  doczero [ Sat 11 May, 2013, 00:20 ]
Post subject:  Re: how to close open position

Hi Nic,

This is the beauty of coding - if we cant do something easy (i.e have a block for closed partial) we can find a way to do it. So lets say the position size you want is 2 units. Why not open 2 trades at 1 unit, then when either position goes into profit 30 pips, then close the first and leave the second one open. I have to go out with the wifey - but when I get back i'll try show you how to do this! In the meantime tho - have a go at it, and see if you can work it out =)

Author:  doczero [ Sun 12 May, 2013, 10:22 ]
Post subject:  Re: how to close open position

Hi Nic,

Your system just got a whole lot bigger ;) (I was hoping you had a go a making it yourself - did you? How did you go? also you could also not download this version and have a try).

Please note - for anyone trying to learn, I have put notes into the code structure so you can learn!

Image

So this system is looking interesting - To enable it to work how you wanted, I needed to implement an order handling system (so it know if the order is the first or second trade opened). We now have a takeprofit when the trade hits 30pips, and a move to breakeven on the second position. Stoploss is working on all. I have backtested for accuracy of the logic and it looks good to me. To improve this system, I would make the following selectable Variables: Stoploss / Takeprofit / EMA 1 period / EMA 2 Period


The second thing I would do - is implement fixed fractional position sizing, it is the only way to position sizing in my opinion (to utilise the power of compounding!)

So if you want a challange , see if you can do all that yourself!

Good luck - have fun!

Attachments:
ema6ema24cross.vfs [61.35 KiB]
Downloaded 358 times
nicema.jpg [320.69 KiB]
Downloaded 887 times

Author:  vadim_berezhnoj [ Tue 14 May, 2013, 16:02 ]
Post subject:  Re: how to close open position

Hello Everyone.

Try a two little bit simpler solutions for SMA cross strategy in files attached.

Attachments:
SMA1.vfs [31.08 KiB]
Downloaded 320 times
SMACrossUser3.vfs [36.08 KiB]
Downloaded 316 times

Author:  doczero [ Tue 14 May, 2013, 21:35 ]
Post subject:  Re: how to close open position

Hello Vadim,

Thanks for the contribution to this thread! Can you explain why the code you attached is a better way of doing thing? (for my better understand) I have run the code and it does not act like the last version I posted (which is working correctly). I can not see partial exits (if you have a way of doing this with opening 1 order, it would be valuable to share - we have had to do this by opening 2 x 1/2 size orders) and a lot of the features seem to be missing. Not a criticism - just trying to learn more from you :P



Nic,

I have updated the attached code to be able to select via global variable the following:

SmallSMA period
LargeSMA period
Stoploss
Partial close Take Profit
and the usual defaults!

It is tested and working well =)

Attachments:
ema6ema24cross.vfs [60.98 KiB]
Downloaded 319 times

Author:  nicbrunelli [ Wed 15 May, 2013, 04:17 ]
Post subject:  Re: how to close open position

Hello everyone!

I haven't checked this thread for a couple of days but i'm happy to see it's growing more interesting. Thanks to all for contribuiting!

I was testing this strategy (not the last 2, I haven't downloaded them yet) and I made just a few changes: enter at market by tick instead of candle, so that it opens the order a little sooner, then I changed the large MA to 60 to catch larger trends.
It works very well when the market put up a nice trend, but in the ranging markets, when the 2 MAs cross each other more often, then you run into many stops, and they can eat up all the gains.

I noticed that often these ranging times are (in the eur/usd) during the asian session, when the european market is closed.
the solution to this problem could be:

- stop the strategy during the asian market and let it go during the european/American market (that's for eur/usd, but i should check some different pairs)
- have the strategy open opposite orders (buy instead of sell and sell instead of buy) during Asia session
- find a way to have the strategy recognize when the market is ranging. But how? whith a volatility indicator like ATR for example? or adding bollinger bands?

thanks guys for posting!

nic

Author:  vadim_berezhnoj [ Wed 15 May, 2013, 13:15 ]
Post subject:  Re: how to close open position

Hello Doc.

In examples I`v shown solution for "how to close open position". I`ll describe in details.
SMA cross strategy is to close previous positions and open new one when the "cross" happens.
First example(SMA1) shows how to close ALL existing positions with one little cycle.
Image

Second example(SMACrossUser3) is more complicated. There is one trick.
When Position.State variable is created it has NULL(none) state. So it can not be used in CLOSE or IF block. It will return error.
So at the beginning of the strategy that variable has to be set "CANCELED".
Image
Now it can be used in CLOSE block with no cycles.
Image
That is how the trick works. :twisted:

We can continue development of SMA Cross strategy with other features. But lets do it in separate thread.
I think question of that thread "Re: how to close open position" is answered.

Attachments:
Untitled3.png [79.72 KiB]
Downloaded 854 times
Untitled2.png [93.13 KiB]
Downloaded 857 times
Untitled1.png [111.4 KiB]
Downloaded 883 times

  Page 1 of 1