//------------------------------------------------------------------------
// : vip17_kong
// : vip17_kong
// : ʽӦ
// : ûӦ
// : Void
//------------------------------------------------------------------------
Params
	Numeric Fund(20000);       // Ͷ뱣֤		

Vars
	Numeric Lots(0);                 // 
	Numeric totalScore(5); //  ֵַĿǰǵȨ䣬ܹ6ָ꼰6֡
    Numeric rpmPeriod(14); // RPM Period
    Numeric bboPeriod(16); // BBO Period
    Numeric macdFastPeriod(12); // MACD Fast Period
    Numeric macdSlowPeriod(24); // MACD Slow Period
    Numeric macdSignalPeriod(9); // MACD Signal Period
    Numeric rsiPeriod(14); // RSI Period
    Numeric cciPeriod(14); // CCI Period
    Numeric stochasticKLength(14); // Stochastic %K Length
    Numeric stochasticKSmoothing(1); // Stochastic %K Smoothing
    Numeric stochasticDSmoothing(3); // Stochastic %D Smoothing
    Numeric supertrendPeriod(10); // SUPERTREND Period
    Numeric supertrendFactor(2); // SUPERTREND Factor
    Series<Numeric> rpmValue; // RPM Value
    Series<Numeric> bboValue; // BBO Value
    Series<Numeric> macdValue; // MACD Value
    Series<Numeric> macdSignal; // MACD Signal Line
    Series<Numeric> macdHist; // MACD Histogram
    Series<Numeric> rsiValue; // RSI Value
    Series<Numeric> stochasticValue; // Stochastic Value
    Series<Numeric> cciValue; // CCI Value
    Series<Numeric> supertrendValue; // SUPERTREND Value
	Series<Numeric> netChgAvg ;
	Series<Numeric> totChgAvg ;
	Series<Numeric> score;
	Numeric EntryStrength(90);       // ǿȵĽֵ
	Numeric Length(5);               // ǿֵָͨ
    Series<Numeric> CloseChange;     // ̼۱䶯ֵ
    Numeric i;                       // ѭƱ
    Numeric UpCloses;                // ̼ۼֵ
    Numeric DnCloses;                // ̼µۼֵ
    Numeric SumChange;               // ̼۱䶯ۼֵ
    Series<Numeric> MarketStrength;  // гǿָ
    Plot plt1; // ڻͼĶ
Events
    OnInit()
    {
    	Range[0:DataCount-1]
		{
			//=========Դ==============
			AddDataFlag(Enum_Data_RolloverBackWard());	//úȨ

			AddDataFlag(Enum_Data_RolloverRealPrice());	//ӳʵ۸

			AddDataFlag(Enum_Data_AutoSwapPosition());	//Զ


			//AddDataFlag(Enum_Data_IgnoreSwapSignalCalc());	//úԻźż
		}
        // һͼ
        plt1.figure(0);  // ͼ
        plt1.setOption("Score", "width", Enum_7Pix);  // ָꡰMA1߿Ϊ3
        plt1.setOption("Score","color",Blue);
    }
    
    OnBar(ArrayRef<Integer> indexs)
    {
    	// 㽻
        Lots = Max(1, Round(Fund/(O*ContractUnit*BigPointValue* MarginRatio/rollover), 0));
    	 // гǿָ
        CloseChange = Close - Close[1];
        UpCloses = 0;
        DnCloses = 0;
        For i = 0 To Length-1
        {
            // ̼ǼǷۼ
            If(CloseChange[i] > 0)
                UpCloses = UpCloses + CloseChange[i];
            // ۼ
            Else
                DnCloses = DnCloses + CloseChange[i];
        }
        // ǵ
        SumChange = Summation(CloseChange, Length);
        // ǣǿȣ0-100֮
        If(SumChange >= 0)
        {
            MarketStrength = SumChange / UpCloses * 100;
        }
        // µµǿȣ0-100֮
        Else
        {
            MarketStrength = SumChange / Abs(DnCloses) * 100;
        }
        
        //  MACD ֵ
        Numeric emaFast = XAverage(Close, macdFastPeriod);
        Numeric emaSlow = XAverage(Close, macdSlowPeriod);
        macdValue = emaFast - emaSlow;
        macdSignal = XAverage(macdValue, macdSignalPeriod);
        macdHist = macdValue - macdSignal;

        //  RSI ֵ
        Numeric SF = 1 / rsiPeriod;
        Numeric change = Close - Close[1];
        If(CurrentBar <= rsiPeriod - 1)
        {
            netChgAvg = (Close - Close[rsiPeriod]) / rsiPeriod;
            totChgAvg = Average(Abs(Close - Close[1]), rsiPeriod);
        }
        Else
        {
            netChgAvg = netChgAvg[1] + SF * (change - netChgAvg[1]);
            totChgAvg = totChgAvg[1] + SF * (Abs(change) - totChgAvg[1]);
        }
        If(totChgAvg <> 0)
        {
            rsiValue = 50 * (netChgAvg / totChgAvg + 1);
        }
        Else
        {
            rsiValue = 50;
        }

        //  Stochastic ֵ
        Numeric highestHigh = Highest(High, stochasticKLength);
        Numeric lowestLow = Lowest(Low, stochasticKLength);
        Numeric stochasticRaw = (Close - lowestLow) / (highestHigh - lowestLow) * 100;
        stochasticValue = Average(stochasticRaw, stochasticKSmoothing);

        //  CCI ֵ
        Numeric typicalPrice = (High + Low + Close) / 3;
        Numeric smaTypicalPrice = Average(typicalPrice, cciPeriod);
        Numeric meanDeviation = Summation(Abs(typicalPrice - smaTypicalPrice), cciPeriod) / cciPeriod;
        If(meanDeviation <> 0)
        {
            cciValue = (typicalPrice - smaTypicalPrice) / (0.015 * meanDeviation);
        }
        Else
        {
            cciValue = 0;
        }

        //  SUPERTREND ֵ
        Numeric atr = AvgTrueRange(supertrendPeriod);
        Numeric basicUpperBand = (High + Low) / 2 + supertrendFactor * atr;
        Numeric basicLowerBand = (High + Low) / 2 - supertrendFactor * atr;
        Numeric finalUpperBand = IIF(basicUpperBand < supertrendValue[1] || Close[1] > supertrendValue[1], basicUpperBand, supertrendValue[1]);
        Numeric finalLowerBand = IIF(basicLowerBand > supertrendValue[1] || Close[1] < supertrendValue[1], basicLowerBand, supertrendValue[1]);
        supertrendValue = IIF(Close > supertrendValue[1], finalLowerBand, finalUpperBand);
		
		// ÿͷ
		Bool macdCondition = macdValue < macdSignal;
		Bool rsiCondition = rsiValue <50;
		Bool stochasticCondition = stochasticValue < 50;
		Bool cciCondition = cciValue <0;
		Bool supertrendCondition = Close < supertrendValue;
		Bool Marketbool = MarketStrength >= EntryStrength;
		
		 // ÷
        score = 0;
        If(macdCondition)
        {
            score = score + 1;
        }
        If(rsiCondition)
        {
            score = score + 1;
        }
        If(stochasticCondition)
        {
            score = score + 1;
        }
        If(cciCondition)
        {
            score = score + 1;
        }
        If(supertrendCondition)
        {
            score = score + 1;
        }
        if(Marketbool)
        {
        	score = score + 1;
        }

        // ڸͼʾ÷
        Commentary("Score: " + Text(score));


		plt1.barv("Score",score);
        // жǷﵽ
        If(score == totalScore)
        {
            Commentary("All conditions met!");
        }
        // 
        If(score[1] >= totalScore And MarketPosition == 0 ) //
        {
            SellShort(Lots, Open);
        }

        // ƽ
        If(score[1] == 0 And MarketPosition == -1)
        {
            BuyToCover(0, Open);
        }

		// ڸͼʾͲ
		
		If(macdCondition)
		{
			PlotAuto("MACD_Condition",1,1,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("MACD_Condition",1,1,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}

		If(rsiCondition)
		{
			PlotAuto("RSI_Condition",2,2,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("RSI_Condition",2,2,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}

		If(stochasticCondition)
		{
			PlotAuto("Stochastic_Condition", 3,3,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("Stochastic_Condition", 3,3,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}

		If(cciCondition)
		{
			PlotAuto("CCI_Condition", 4,4,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("CCI_Condition", 4,4,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}

		If(supertrendCondition)
		{
			PlotAuto("Supertrend_Condition", 5,5,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("Supertrend_Condition", 5,5,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		If(Marketbool)
		{
			PlotAuto("Marketbool", 6,6,Green,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		Else
		{
			PlotAuto("Marketbool", 6,6,Red,Enum_Line,Enum_Solid,Enum_7Pix);
		}
		
    }


//------------------------------------------------------------------------
// 汾	2024/11/27 150615
// Ȩ	songshu123
// 	TradeBlazer SoftwareTradeBlazerƽ̨
//			ÿһ汾TradeBlazerʽ޸ĺдȨ
//------------------------------------------------------------------------