// Downloaded From https://www.WiseStockTrader.com
//------------------------------------------------------
//
//  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( 0, 0, 0, 0 );
SetOption( "InitialEquity", 100000 );
SetOption( "FuturesMode" , True );
SetOption( "MinShares", 1 );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.01 );
//SetOption( "AccountMargin", 10 );
SetOption( "RefreshWhenCompleted", True );
SetOption( "AllowPositionShrinking", True );

period = 14;

ScaleInPointsLoop = Ref( ATR( period ) * 2, -1 ) / 2;
ScaleOutPointsLoop = ScaleInPointsLoop / 2;
ScaleInSize = 2;
ScaleOutSize = 1;

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

BuyPrice = ValueWhen( Buy, C );

ScaleInPoints = ScaleInPointsLoop[period];
ScaleOutPoints =  ScaleOutPointsLoop[period];

for( i = period + 1; i < BarCount; i++ )
{
    Profit[i] = Close[i] - BuyPrice[i] >= ScaleInPoints;
    Loss[i] = Close[i] - BuyPrice[i] <= -ScaleOutPoints;

    if( Sell[i] )
    {
        ScaleInPoints = ScaleInPointsLoop[i];
        ScaleOutPoints = ScaleOutPointsLoop[i];
    }
    else
        if( Profit[i] == 1 )
        {
            ScaleInPoints = ( Close[i] - BuyPrice[i] ) + ScaleInPointsLoop[i];
        }
        else
            if( Loss[i] == 1 )
            {
                ScaleOutPoints = -( Close[i] - BuyPrice[i] ) + ScaleOutPointsLoop[i];
            }
}

InTrade = Flip( Buy, Sell );

DoScaleIn = Ref( InTrade, -0 ) AND Ref( Profit, -0 );
DoScaleOut = Ref( InTrade, -0 ) AND Ref( Loss, -0 );

Buy = Buy + sigScaleIn * DoScaleIn + sigScaleOut * DoScaleOut;
Buy = Ref( Buy, -1 );
Sell = Ref( Sell, -1 );

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

PositionSize = IIf( DoScaleOut, ScaleOutSize, ScaleInSize );

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 0, chartShowDates );
Plot( Close, "Price", colorWhite, styleCandle );

SetPositionSize( PositionSize, spsShares );

PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -20 );
PlotShapes( IIf( Buy == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleIn, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -20 );
PlotShapes( IIf( Buy == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleOut, shapeDownArrow, shapeNone ), colorViolet, 0, H, -20 );
PlotShapes( IIf( Buy == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -20 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
_SECTION_END();