//------------------------------------------------------------------------
// : Formula1
// : 꽻׷
// : ʽӦ
// : ûӦ
// : Void
//------------------------------------------------------------------------
Params
    Numeric RiskRatio(1);  // 1% risk ratio
    Numeric ATRLength(20); // ATR window size
    Numeric boLength(20);  // breakout
    Numeric teLength(10);  // trailing exit
Vars
    Numeric N;
    NumericSeries AvgTR;
    Numeric TotalEquity;
    Numeric TurtleUnits;
    NumericSeries DonchianHi;
    NumericSeries DonchianLo;
    Numeric MinPoint;
    Numeric ExitLowestPrice;
    Numeric ExitHighestPrice;
    Numeric myEntryPrice;
    Numeric myExitPrice;
Begin
    if (!CallAuctionFilter()) return;

    MinPoint = MinMove * PriceScale;
    AvgTR = XAverage(TrueRange, ATRLength);
    N = AvgTR[1];
    // PlotNumeric("N", N);
    TotalEquity = Portfolio_CurrentCapital() + Portfolio_UsedMargin();
    TurtleUnits = (TotalEquity*RiskRatio/100) / (N*ContractUnit()*BigPointValue());
    TurtleUnits = IntPart(TurtleUnits);
    // PlotNumeric("TurtleUnits", TurtleUnits);

    DonchianHi = HighestFC(High[1], boLength);
    DonchianLo = LowestFC(Low[1], boLength);
    // PlotNumeric("Entry_Hi", DonchianHi);
    // PlotNumeric("Entry_Lo", DonchianLo);

    ExitLowestPrice = LowestFC(Low[1], teLength);
    ExitHighestPrice = HighestFC(High[1], teLength);

    if (MarketPosition == 0)
    {
        // ͷ
        if (high > DonchianHi && TurtleUnits >= 1)
        {
            myEntryPrice = min(high, DonchianHi+MinPoint);
            myEntryPrice = IIF(myEntryPrice < Open, Open, myEntryPrice);  // Ӧ
            Buy(TurtleUnits, myEntryPrice);
        }
        // ͷ
        if (Low < DonchianLo && TurtleUnits >= 1)
        {
            myEntryPrice = max(low, DonchianLo-MinPoint);
            myEntryPrice = IIF(myEntryPrice > Open, Open, myEntryPrice);  // Ӧ
            SellShort(TurtleUnits, myEntryPrice);
        }
    }

    if (MarketPosition == 1)
    {
        if (Low < ExitLowestPrice)
        {
            // ͷ˳
            myExitPrice = max(Low, ExitLowestPrice-MinPoint);
            myExitPrice = IIF(myExitPrice > Open, Open, myExitPrice); // Ӧ
            Sell(0, myExitPrice);
        }
        else
        {
            // ͷӲ߼...
            // ͷֹ߼...
        }
    }
    else if (MarketPosition == -1)
    {
        if (High > ExitHighestPrice)
        {
            // ͷ˳
            myExitPrice = min(High, ExitHighestPrice+MinPoint);
            myExitPrice = IIF(myExitPrice < Open, Open, myExitPrice); // Ӧ
            BuyToCover(0, myExitPrice);
        }
        else
        {
            // ͷӲ߼...
            // ͷֹ߼...
        }
    }
End

//------------------------------------------------------------------------
// 汾: 2018/06/27 124605
// ں˰汾: V2.7.2.14
// Ȩ: Void
// : TradeBlazer SoftwareTradeBlazerƽ̨
//           ÿһ汾TradeBlazerʽ޸ĺдȨ
//------------------------------------------------------------------------
