DEIO

Index/bond strategy based on slow accumulation of contrarian orders (BUY) and MoneyManagement "antimartingala" style

Category: Strategies By: DEIO Created: March 23, 2017, 7:32 PM
March 23, 2017, 7:32 PM
Strategies
39 Comments
Index/bond strategy based on slow accumulation of contrarian orders (BUY) and MoneyManagement "antimartingala" style

hi,

I reviewed the last strategy using contrarian orders and I added more filters in order not to accumulate too much contracts.

The base logic is “buy”.

Furthermore the system try to follow automatically the uptrend when it appears.

Inside the code I trid to explain the main parameters to use.

Actually such code is the automatisation of a my old “manual” strategy that I use since 2010 (using a simply Excel that give me the operating signals) using ETF instruments (It seems to me that even on IG it is possible to use ETF – isn’t it?).

The strategy that I am posting today works on long periods of time, so the broker (i.e. IG) withdraw an amount of cash every night (CFD cash) .

For what I know this is a blocking problem because overnight interest destroy any good strategy.

If I am wrong on what I’m saying please let me know if you can help me to resolve this problem (that’s why I sopke about ETF instrument which are not hit by this issue).

Moreover using CFD future with own expiry date I suppose that the issue on overnight interest doesn’t exist.  Isn’t it?

But this way, how can I think to use an automatic strategy if I have to rollover every expiry date ?

What do you think on this ? keep in mind that I’m not an expert on future …. 🙂

Anyway I would be happy if you can “play” with the parameters of my strategy in order to know your opinion (let’s try on the main index or monetary market).

Thx a lot.

code:

//-----------------------------------------------------------------------------------------------
//TF a 12 h
//dax min 1 Eur.
//-----------------------------------------------------------------------------------------------
//TAKE PROFIT NEAR ZERO IN ORDER TO NOT ACCUMULATE TOO MUCH
//-----------------------------------------------------------------------------------------------
//IF NOT ON MARKET TRY TO ENTER AGAIN AS SOON A CORRECTION APPEARS, TRYING TO FOLLOW
//THE POTENTIAL CURRENT UPTREND
//-----------------------------------------------------------------------------------------------
//IF I BOUGHT THE PREVIOUS BAR DON'T BUY THE FOLLOWING BAR IN ORDER TO NOT ACCUMULATE TOO MUCH
//-----------------------------------------------------------------------------------------------
//MAIN PARAMETER IN ORDER TO CHANGE THE RISK LEVEL ARE:
//acc----> % of decrease of the instrument compared to the last buy price (THE LOWER IS AND MORE
// RISK AND CASH RETURN WILL APEEAR)
//-----------------------------------------------------------------------------------------------
//tp-----> % beyond which the entire position is closed (THE HIGHER IS AND MORE
// RISK AND CASH RETURN WILL APEEAR) - below zero is safer !
//-----------------------------------------------------------------------------------------------
//MONEY MANAGENT "ANTIMARTINGALA" STYLE - I SUPPOSE CLEARER RESULTS ON A LONGER PERIOD OF BACKTEST
//-----------------------------------------------------------------------------------------------

DEFPARAM cumulateorders = true

TIMEWORK = 070000
TIMESTOP = 210000
acc = 5

if justone = 0 then
 capital = 10000 * pointvalue
 justone = 1
endif

perccap = 0.5
margin = 0.5 * close * pointvalue / 100
period=5
stdev=STD[period](close)
//LOOK FOR A LOCAL VOLATILITY DECREASE
//I WANT TO BUY AGAIN ONLY WHEN BEAR MARKET SHOWS A LOCAL PAUSE
if (stdev[0]+stdev[1]+stdev[2]+stdev[3]+stdev[4]) < (stdev[1]+stdev[2]+stdev[3]+stdev[4]+stdev[5]) then
 SWBUY=1
else
 SWBUY=0
ENDIF

IF TIME > TIMEWORK AND TIME < TIMESTOP THEN
 if not onmarket then
  sizebuy = ROUND((capital *perccap /100)/ margin)
  tp = 0.25
  IF CLOSE < CLOSE[1] THEN
   BUY sizebuy SHARES AT MARKET
  endif
endif

if LONGONMARKET then
 perf = (close - POSITIONPRICE)/POSITIONPRICE * 100
 if perf > tp then
  SELl AT MARKET
  perfc = (close - POSITIONPRICE) * POINTVALUE * COUNTOFPOSITION
  capital = capital + PERFC
 ELSE
  IF LASTBARBUY = 0 THEN
   if (((CLOSE-TRADEPRICE)/TRADEPRICE)*100) < -acc AND SWBUY=1 then
    sizebuy = ROUND((capital *perccap /100)/ margin)
    BUY sizebuy SHARES AT MARKET
    LASTBARBUY = 1
   ENDIF
 ELSE
  LASTBARBUY = 0
 ENDIF
ENDIF
ENDIF
ENDIF

 

Download
Filename: indexbond-contrarian-orders.itf
Downloads: 390
DEIO
DEIO Veteran
I usually let my code do the talking, which explains why my bio is as empty as a newly created file. Bio to be initialized...
Author’s Profile

Comments

DEIO
9 years ago
#

Hi sublime06, I tank you for the compliments, but first of all I STRONGLY want to warn you to use such strategy in real, because deep and long bear market seasons could last a long time during which the strategy would go on buying and I don't know if it is what you want. Don't forget that markets in general are rising for7/8 years, so it's easy that strategy with a long goal are gaining money, so be aware of it. About the strategy I had tried to comment inside the code the logic code, but be patient but I can't say more that that. Anyway the base concept is to buy only when the volatility of the market decrease due to the fact that when it's increasing easily we are in a bear market that probably will go on, so I wait for a decrease of it and then BUY. I hope I am usefull for you and anyway go on try to code by using the manual or finding on the site the answers to the many issue opened which are a lot ! thx again bye

sublime06
9 years ago
#

bonjour Deio. la stratégie est vraiment exceptionnel. ;) malheureusement je n'ai pas de connaissance en codage. pourrait tu expliqué la méthode du déclenchement de la position pour comprendre le fonctionnement ? je ne s'ai pas faire par programmation.. mais si je peu avoir la création simplifier, je pourrait essayer d'amélioré le système. j ai de très bon résulta en réel, ( 1 mois % gain 89%/ Ratio, 21,82 ) mais si je change les paramètres j ai beaucoup d'erreur de code et je ne peu les résoudre. Merci

Koenigsleon
9 years ago
#

(2min/dax Basis, its for me better) DEFPARAM cumulateorders = true indicator1 = STE[10](close) c1 = (indicator1 < 1.7) TIMEWORK = 070000 TIMESTOP = 210000 acc = 5 if justone = 0 then capital = 10000 * pointvalue justone = 1 endif perccap = 0.5 margin = 0.5 * close * pointvalue / 100 period=5 stdev=STD[period](close) //LOOK FOR A LOCAL VOLATILITY DECREASE //I WANT TO BUY AGAIN ONLY WHEN BEAR MARKET SHOWS A LOCAL PAUSE if (stdev[0]+stdev[1]+stdev[2]+stdev[3]+stdev[4]) TIMEWORK AND TIME < TIMESTOP THEN if not onmarket then sizebuy = ROUND((capital *perccap /100)/ margin) tp = 0.25 IF CLOSE tp then SELl AT MARKET perfc = (close - POSITIONPRICE) * POINTVALUE * COUNTOFPOSITION capital = capital + PERFC ELSE IF LASTBARBUY = 0 THEN if (((CLOSE-TRADEPRICE)/TRADEPRICE)*100) < -acc AND SWBUY=1 then sizebuy = ROUND((capital *perccap /100)/ margin) BUY sizebuy SHARES AT MARKET LASTBARBUY = 1 ENDIF ELSE LASTBARBUY = 0 ENDIF ENDIF ENDIF ENDIF SET STOP pLOSS 19.0

DEIO
9 years ago
#

HI Koenigsleon , Maybe there is some mistakes in the code you pasted above. I can't use it because I get errors. Anyway pay attention to the backtest because on TF 2 min you can't obtain a solid backtest . bye

hhintzen
9 years ago
#

Di Deio,

I know, i test your system. I will trey what it does in short market.

Can you tell me how I can turn strategy from long to short ???

 

Harry 

 

hhintzen
9 years ago
#

Hey Deio, Good strategy, but Why only long postitions?? We are once again come in a baer market, what does your system do than?
Is it possible to test a system in a baer market? o
Oly short positions ?
Harry

DEIO
9 years ago
#

hi Harry,

surely it's possible, I will try .... even if I think that the setting could be a little bit different (I suppose), keeping in mind that

usually the bear market is faster (during the falling) than bull market (during the rising).

bye

rpcasanas30779
10 years ago
#

Hi, Tried test this strategy on USTech 100 ( Nasdaq) and looks excellent return ( 90+% Accuray) 2kbars in 1H TF. By the way this is in wlak forward method. My query is, how you will set contract when enabaled at ProOrder?

Regards,

Lance

Oskar Bergvall
10 years ago
#

With tp= -0,1 and with the MACD I'd say it's very safe in 4 hour I cannot find a single fail.

DEIO
10 years ago
#

Hi all,

I posted a new version on the related topic.

https://www.prorealcode.com/topic/indexbond-contrarian-orders/

Bye

 

Oskar Bergvall
10 years ago
#

Maybe it's possible to add so it can only buy when EM50 is positiv and sell if it goes negative as a "stop" if the market changes?

Oskar Bergvall
10 years ago
#

I tried adding MACD. So if will only buy if the minussignal crosses over to positive. I tried on a bond in Sweden called "fingerprint" which went down 37% some time ago. With the original code backtesting I lost all my capital. With the MACD it didn't buy at all since the signal was negative. Requires more testing, and of course it could still go bad, but abit safer :)

 

DEFPARAM cumulateorders = true

indicator1 = MACD[12,26,9](close)
c1 = (indicator1 > 0)

IF c1 THEN
TIMEWORK = 070000
TIMESTOP = 210000
acc = 5

if justone = 0 then
capital = 10000 * pointvalue
justone = 1
endif

perccap = 0.5
margin = 0.5 * close * pointvalue / 100
period=5
stdev=STD[period](close)
//LOOK FOR A LOCAL VOLATILITY DECREASE
//I WANT TO BUY AGAIN ONLY WHEN BEAR MARKET SHOWS A LOCAL PAUSE
if (stdev[0]+stdev[1]+stdev[2]+stdev[3]+stdev[4]) < (stdev[1]+stdev[2]+stdev[3]+stdev[4]+stdev[5]) then
SWBUY=1
else
SWBUY=0
ENDIF

IF TIME > TIMEWORK AND TIME < TIMESTOP THEN
if not onmarket then
sizebuy = ROUND((capital *perccap /100)/ margin)
tp = 0.1
IF CLOSE < CLOSE[1] THEN
BUY sizebuy SHARES AT MARKET
endif
endif

if LONGONMARKET then
perf = (close - POSITIONPRICE)/POSITIONPRICE * 100
if perf > tp then
SELl AT MARKET
perfc = (close - POSITIONPRICE) * POINTVALUE * COUNTOFPOSITION
capital = capital + PERFC
ELSE
IF LASTBARBUY = 0 THEN
if (((CLOSE-TRADEPRICE)/TRADEPRICE)*100) < -acc AND SWBUY=1 then
sizebuy = ROUND((capital *perccap /100)/ margin)
BUY sizebuy SHARES AT MARKET
LASTBARBUY = 1
ENDIF
ELSE
LASTBARBUY = 0
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

Oskar Bergvall
10 years ago
#

Thanks for the answer!

I tried this but it seems to work alot worse for some indexes/bonds. I had one idea that could work: If it ONLY acts when for example MA200 is very positive. The few times your original code works bad is on downtrend and MA200 is mostly down then. I suck at programming but going to see if I can get it in and test.

DEIO
10 years ago
#

Hi Oskar,

it's possible to add any filter you want, even thoug such good results  strongly depends on the performance index which rose a lot in the last years.

So pay attention weather index markets should fall down because this strategy could not be so brilliant in the sense that you should accumulate many contracts....

Adding a stop loss is always a good idea ..... i tried to make a version of the strategy which closes under a certain level and obviously results are not so good...but we are more safe...

here to code (it's just an axample):

//-----------------------------------------------------------------------------------------------
//TF a 6 h
//dax min 1 Eur.

DEFPARAM cumulateorders = true
IF DayOfWeek = 5 THEN
TIMEWORK = 070000
TIMESTOP = 210000
acc = 0.5

period=10
stdev=STD[period](close)
//LOOK FOR A LOCAL VOLATILITY DECREASE
//I WANT TO BUY AGAIN ONLY WHEN BEAR MARKET SHOWS A LOCAL PAUSE
if (stdev[0]+stdev[1]+stdev[2]+stdev[3]+stdev[4]) < (stdev[1]+stdev[2]+stdev[3]+stdev[4]+stdev[5]) then
SWBUY=1
else
SWBUY=0
ENDIF

IF TIME > TIMEWORK AND TIME < TIMESTOP THEN
if not onmarket then
sizebuy = 1
tp = 1
IF CLOSE < CLOSE[1] AND SWBUY=1 THEN
BUY sizebuy SHARES AT MARKET
endif
endif

if LONGONMARKET then
perf = (close - POSITIONPRICE)/POSITIONPRICE * 100
if perf > tp then
SELl AT MARKET
perfc = (close - POSITIONPRICE) * POINTVALUE * COUNTOFPOSITION
capital = capital + PERFC
ELSE
if (((CLOSE-TRADEPRICE)/TRADEPRICE)*100) < -acc then
sell at market
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

 

Oskar Bergvall
10 years ago
#

This looks very intresting. I'm trying to add some sort of stop but doesnt seem to work well with it. Sometimes it buys and doesn't sell...ever :)

Bard
10 years ago
#

 Hi Deio, that's what I thought, yet I tried it with your parameters (4 hr Nikkei's) and with the 5 day and 30 day look back for the Close. but the 18th Dec wide bar occurs and the system still accumulates long positions into a steep market breakdown. I really like what you have created. I'm sure with a few more modifications (trend filter) it can attain a far better DD. Let me look at it some more.

https://www.dropbox.com/s/0bl4zh8lyvqtxug/5%20day%20look%20back.png?dl=0
https://www.dropbox.com/s/fmls9kn4cudx0c5/30%20day%20look%20back.png?dl=0

Cheers



DEIO
10 years ago
#

Hi Bard,

please attach the entire code you used which gave you such results on Nikkei, because I can't find Buy ad 18 dec  (year??).

I have been working in otrer to find another filter that can help us...on DD

I also tried the strategy on tiny timeframe, but I think it's very very much risky.

Anyway I am waiting for the code.

thx for you cooperation.

bye

 

 

Bard
10 years ago
#

 p.s. I just tried the above changes on the 1 hour Dax it results in a far more healthy DD of 26% and gain of 121%. Ave trade gain may be a bit low though. (I've beeb using the Dow Jones widest spread of 3.8 tics)

Maybe also perhaps it would benefit the strategy if it had a rule that made sure it didn't accumulate more positions after the market has a sharp very large wide ranging bar to the downside (short bar) as this typically signifies further downside movement? 

DEIO
10 years ago
#

Hi Bard,

The use of standard deviation should just avoid To buy during wide bars of bear market.

Anyway i'll check Again.

Bye

Bard
10 years ago
#

Hi Deio

Thanks for the further clarifications. I was curious to see how the system worked on the overnight session and switched the official opening hours of 9am to 17.45pm times to:
TIMEWORK = 174500
TIMESTOP = 090000
But it didn't take any trades? What am I missing?
Also what is the reasoning behind using 12 hour bars? There is marginally less drawdown using 4 hour bars (DD=52%.not 55% but still 230% gain - 5th Dec 2012 to 6th Ap 2017)

Clearly although changing the IF CLOSE < CLOSE[1] to IF CLOSE < CLOSE[30] and changing tp to 0.50 and acc to 4, will bring Drawdown (DD) down to a more "reasonable" 35% (although it changes the % profit to 164% (from 230%)). You can guarantee the DD will be 50% more than the backtest result during real live trading, right? (Richard Dennis has been known to let DD's hit +70% but that = not sleeping at night and getting an ulcer...)

I will look at this code again, I have an idea for a trend oscillator filter that can be up to 90% accurate. If it improves the system I will update the results here.

I also ran the backtest with the same code on an identical chart (that had far less indicators to speed the backtests up) and got two totally different results:
pls see screenshot. Any ideas @Nicolas ?

Cheers
Bard

https://www.dropbox.com/s/h9iysb01mx801rw/2%20different%20results.png?dl=0

Bard
10 years ago
#

I have opened a new thread on Optimal F position sizing (and a better modification that takes into account drawdowns) here: 
https://www.prorealcode.com/topic/ralph-vinces-optimal-f-positioning-sizing/

p.s. Nicolas, I accidentally used the links creation (insert/edit link) to post a link here in the comment and the "page jumping to the bottom" issue occurred again so I could not get back to my reply/comment to Deio I had just written but not posted  (because the page jumps to the bottom) and I refreshed the page and lost the whole comment... yet again!


DEIO
10 years ago
#

Hi Bard,

Thx for you replies.

Rgarding the filters of my strategy as you can see on the code the only filter applied is a decrease of the standard deviation together an amount of decline of the instrument (these two conditions aren't so frequent).

Maybe it's not enough  .... but I usually try not to use usual oscillators.

Another filter that I applied on the strategy is to work only on Friday (if you try that you will see that the number of contracts will decrease and so the risk):

IF DayOfWeek = 5 THEN

....

....

I agree with you that with a long bear market this approach become too risky (with derivative instruments - less risky using instruments like ETF (no CFD) no margin - no overnight interest - but less gain too.....- I do this since 2010 (real) . But this is another story.

While in case of trend the only condition is that if I'm not in the market, because I closed the previous position opened and today the price is lower than yesterday, I go in the market again).

Obviously these criterias doesn't always work,  the same way classic oscillator/indicator do.

About Money management I'll try to read Ralph Vinces ideas.

In my opinion the real problem with any application of money management is the avaiability of a large amount of bars in the backtest, otherwise

the backtest has not a real validity and all remain just theory.

Anyway I hope to post different strategies I'm testing, where finally I can be more comfortable by use of CFD.

Thx again for your cooperation.

bye

Alex975
10 years ago
#

 Ciao Deio,

interesting strategy.. thanks to show it

Bard
10 years ago
#

No worries Deio, I hope the cost of funding calculations help. 

Re: "trying to buy near the lowest price of the period" - what I meant is, where is the trend filter in the system? After all volatility can drop and you could enter long but still end up buying into a prolonged bear market? So I was just wondering how the system works in those cases, remember bond prices have hit a 300 year high. The only way prices have gone is up! What happens when the bubble bursts or China and Russia introduce a gold backed currency?

IG Index have only synthetic Futures contracts, they're not the eg actual Sept 2017 Futures Contracts although you can still roll them over like normal futures contracts that you'd get at a brokers. I am not sure how you would roll these in a backtest but I bet IG Index support could help? Perhaps if you find out you could update this thread?

The IG Index EFT Daily Funded Bet contracts are another synthetic contract and derivative of the underlying bond contracts.

I have added a new topic with no PRT code to get Ralph Vince's Optimal F programmed in to PRT code here: 
https://www.prorealcode.com/topic/ralph-vinces-optimal-f-positioning-sizing/

Thanks for the  Andrea Unger tip, I am reading Kevin Davey's Building Winning Algorithmic Trading Systems, he also won the World Cup Trading Championships, it's a very good book.

Cheers
Bard

triss1965@gmail.com
10 years ago
#

 Hi, how do I if I want to end SL? Whats the code?

 

Eva

DEIO
10 years ago
#

Hi Eva,

If you mean the application of stop loss, the strategy doesn't apply the classical SL, that's why such strategies are considered risky.

Anyway as I said in a previous reply, if you want to  see a sort of stop loss effect you can set the variable "tp" (now set on +0.25) on negative value (i.e. -0.5)  and check results which will be better or worse according to the setting of this variable.

thx.

bye

 

ProRealCode ProRealCode
Loading...