Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Anti-Martingale Trading system for Amibroker (AFL)

Rating:
4 / 5 (Votes 7)
Tags:
position sizing, amibroker

Anti-Martingale system using Dynamic Position Sizing

Martingale or Anti-Martingale systems are widely popular across casinos across the globe. In Martingale system, the gambler doubles up his bet size on every loss so that he covers up all his loss through a single win. While in Anti-Martingale system, the gambler doubles up his bet size on every profit. The same concepts can be applied in Trading systems too. In Anti Martingale trading system, the trader increases his position size gradually if the trade goes in his favour, while the position size is decreased if the trade goes against him. The basic assumption behind this strategy is that profits from your winning trades is always higher than the losses from your losing trades. This strategy is particularly very helpful in trending markets. It’s a statistically proven fact that Anti-Martingale strategy performs better that Martingale strategy over a long term.

Amibroker has dynamic position sizing capabilities which would enable us to devise and backtest Anti Martingale strategy. In Amibroker terminology, increasing your position size is termed as Scaling-In, while decreasing your position size is termed as Scaling-Out. Two special constants: sigScaleIn / sigScaleOut provide means to tell the backtester when you want to scale-in/out

In order to change the position size you have to:

  • Assign sigScaleIn to BUY/SHORT variable if you want to scale-in (increase size of) LONG/SHORT position.
  • Assign sigScaleOut to BUY/SHORT variable if you want to scale-out (decrease size of) LONG/SHORT position.

Read more about this system with backtest report and equity curve (here).

Screenshots

Indicator / Formula

Copy & Paste Friendly
//------------------------------------------------------
//
//  Formula Name:    Anti-Martingale Trading system
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------

_SECTION_BEGIN("Anti Martingale Trading Syatem");

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 100000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",50);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

//Specify ScaleIn and ScaleOut parameters
ScaleInPoints=100;
ScaleOutPoints=50;
ScaleInSize=150;
ScaleOutSize=75;


//Buy and Sell Condition
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

BuyPrice=ValueWhen(Buy,C);

for( i = 1; i < BarCount; i++ ) { Profit[i]=Close[i]-BuyPrice[i]>=ScaleInPoints;
    Loss[i]=Close[i]-BuyPrice[i]<=-ScaleOutPoints;
    if(Profit[i]==1)
    ScaleInPoints=(Close[i]-BuyPrice[i])+100;
    if(Loss[i]==1)
    ScaleOutPoints=-(Close[i]-BuyPrice[i])+50;
    if(Sell[i])
    {
    ScaleInPoints=100;
    ScaleOutPoints=50;
    }
}

InTrade = Flip( Buy, Sell );

DoScaleIn = InTrade AND Profit;
DoScaleOut= InTrade AND Loss;


Buy = Buy + sigScaleIn * DoScaleIn + sigScaleOut * DoScaleOut;

PositionSize = IIf( DoScaleOut,ScaleOutSize, ScaleInSize); 

Plot( Close, "Price", colorWhite, styleCandle );

SetPositionSize(PositionSize,spsShares);

PlotShapes(IIf(Cross( MACD(), Signal() ), shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cross( MACD(), Signal() ), shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cross( MACD(), Signal() ), shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cross( Signal(), MACD() ), shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Cross( Signal(), MACD() ), shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Cross( Signal(), MACD() ), shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(DoScaleIn, shapeSmallUpTriangle, shapeNone),colorBlue, 0, L, Offset=-45);
PlotShapes(IIf(DoScaleOut, shapeSmallDownTriangle, shapeNone),colorBlue, 0, H, Offset=-45);
_SECTION_END();

14 comments

1. empottasch

very nice one, amazing results at the first glance

2. empottasch

i will be watching your site for sure!. Didn’t look at your other strategies yet. I adjusted your system posted here. Instead of using fixed scaleinpoints and scaleoutpoints you could also use the ATR function to determine the points to use. This is what I did and works good even with a slippage of 2 ticks

3. empottasch

ah, an error found.

the buyprice is redefined using BuyPrice=ValueWhen(Buy,C);

you may use: BuyPrice=ValueWhen(Buy,C);

but then later you have to use

BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

so rather than setting these prices at the start of the code you need to set them after

Buy = Buy + sigScaleIn * DoScaleIn + sigScaleOut * DoScaleOut;

4. empottasch

intuitively I already thought it makes no sense since if it would work then a simple MACD system would also work, which it does not. Now what you do is scale in at prices of your initial buy price, and that is incorrect

or part of your code needs to be corrected to:

iBuyPrice=ValueWhen(Buy,C);
 
for( i = 1; i < BarCount; i++ ) { Profit[i]=Close[i]-iBuyPrice[i]>=ScaleInPoints;
    Loss[i]=Close[i]-iBuyPrice[i]<=-ScaleOutPoints;
    if(Profit[i]==1)
    ScaleInPoints=(Close[i]-iBuyPrice[i])+100;
    if(Loss[i]==1)
    ScaleOutPoints=-(Close[i]-iBuyPrice[i])+50;
    if(Sell[i])
    {
    ScaleInPoints=100;
    ScaleOutPoints=50;
    }
}

5. snehil2010

Hello,

Thanks for reviewing this system! I will review the correction you suggested very soon. Can you also advise how did you set up Scale-In and Scale-out points using ATR? What was the logic used?

Thanks,
Snehil

6. empottasch

hi Snehil,

my point is that you define at the start of the code:

BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

which is correct.

But then later you redefine the Buyprice as:

BuyPrice=ValueWhen(Buy,C);

this array you use to determine where to scale in or out which is correct but because you redefined the buyprice it will scale in and out at the price of the initial Buy defined by the MACD cross. When you look at the detailed backtest report you will see that the scalein and scaleout prices are not correct, they use the price of the initial buy.

I will send you my adjusted code to your private address tomorrow.

Still your code is interesting and I got all excited because it looks so professional and seemed to be working great. But in my opinion, because of the fact you redefine your BuyPrice array the scalein/out prices are incorrect.

7. ram_energy

Dear Ed sir,

Please post your adjusted code here it must useful to all

Thanks

8. empottasch

hi,

i posted code here: http://wisestocktrader.com/indicatorpasties/1652-antimarti

made few changes also that you can see in the chart what the price is you pay when scaling in/out.

9. ram_energy

Dear ed sir,

Thanks a lot for posting

10. snehil2010

Thank you. I can see your modified code and it makes complete sense. I would update the same in my website too.

11. empottasch

hi, one thing I did incorrectly is that the slippage applied will lead to a wrong scaleout price since these prices are stored in the buyprice array. So you will need to adjust this. So when slip = 0 it is correct in my opinion but if you add slippage you will need to adjust

so:

slip = TickSize * 2;
BuyPrice = Open + slip;
SellPrice = Open - slip;

would need to be rewritten as:

slip = TickSize * 2;
//BuyPrice = Open + slip;
BuyPrice = iif( Buy == 1 or Buy == sigscalein, Min( H, Open + slip ), iif( Buy == sigscaleout, Max( L, Open - slip ), 0 ) );
SellPrice = Max( L, Open - slip );
12. ulages

Hello Folks,

what i need to do to apply this code for short trade?

13. praveen5344

I am not getting any thing on the chart…….just getting blank chart when applied the formula……..I am using Amibroker 5.9

14. tradermind

is wrong this code,empottasch clearify the problem; What do they want to prove? the correct code, and results as ever, is poor

Leave Comment

Please login here to leave a comment.

Back