<%
	Dim AiTrade_Info,AI_Setting
	AiTrade_Info=Split(Exsm.Stock_Setting(2),",")
	If Exsm.UserID>0 And datediff("s",Session(Exsm.PlaceName&"ExsmTeUserID")(8),Now())>60 Then RndIncident
	Sub RndIncident()
		Dim Rnum,RndID,Mycontent,Color,MaxID
		Randomize
		Rnum=rnd
		If Rnum<=Cdbl(Exsm.Setting_Info(17)/100) Then
			MaxID=stock_conn.execute("select top 1 Cid from EX_Stock order by Cid desc")(0)
			Randomize rnd
			RndID=int(MaxID*rnd)+1
			SQL="select * from EX_Stock where Cid="&RndID
			Set Rs=stock_conn.execute(SQL)
			If (Rs("TradePrice")-Rs("ClosingPrice"))/Rs("ClosingPrice")>=Cdbl(AiTrade_Info(3)/100) Then
				Mycontent="上市公司["&Rs("Company")&"]走势过红，打开涨停状态！"
				Color="red"
				stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Color&"',"&SqlSmNowString&")")
			Elseif (Rs("TradePrice")-Rs("ClosingPrice"))/Rs("ClosingPrice")<=-Cdbl(AiTrade_Info(4)/100) Then
				Mycontent="上市公司["&Rs("Company")&"]走势低迷，打开跌停状态！"
				Color="black"
				stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Color&"',"&SqlSmNowString&")")
			Elseif Rs("TradePrice")<Cdbl(AiTrade_Info(5)) Then
				Mycontent="上市公司["&Rs("Company")&"]正在重组，打开停牌状态！"
				Color="black"
				stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Color&"',"&SqlSmNowString&")")
			Elseif Rs("LockStock")=1 Then
				Mycontent="上市公司["&Rs("Company")&"]由于不明原因，打开封盘状态！"
				Color="purple"
				stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Color&"',"&SqlSmNowString&")")
			Else
				Randomize
				If Rnd<Cdbl(Exsm.Setting_Info(18)/100) Then
					Rnum=Formatnumber(100*rnd+1,0,-1)
					If Rnum<10 Then
						ExFluctuate Rnum*10,1,Rs("Company"),"资产重组，现价微涨","maroon",RndID,1 
					Elseif Rnum<20 Then
						ExFluctuate Rnum*10,1,Rs("Company"),"资产重组，现价上涨","purple",RndID,1
					Elseif Rnum<30 Then
						ExFluctuate Rnum*10,10,Rs("Company"),"业绩提高，投资人信心加大，现价上涨","fuchsia",RndID,1
					Elseif Rnum<40 Then
						ExFluctuate Rnum*10,20,Rs("Company"),"大规模资产重组，现价上涨","olive",RndID,1
					Elseif Rnum<50 Then
						ExFluctuate Rnum*10,300,Rs("Company"),"业绩大幅提高，注入大量资金，现价上涨","purple",RndID,1
					Elseif Rnum<60 Then
						ExFluctuate 1000-Rnum*10,40,Rs("Company"),"业绩大幅提高，注入大量资金，现价上涨","fuchsia",RndID,1 
					Elseif Rnum<70 Then
						ExFluctuate 1000-Rnum*10,30,Rs("Company"),"大规模资产重组，现价上涨","red",RndID,1
					Elseif Rnum<80 Then
						ExFluctuate 1000-Rnum*10,20,Rs("Company"),"业绩提高，投资人信心加大，现价上涨","maroon",RndID,1
					Elseif Rnum<90 Then
						ExFluctuate 1000-Rnum*10,10,Rs("Company"),"资产重组，现价上涨","fuchsia",RndID,1
					Else
						ExFluctuate 1000-Rnum*10,1,Rs("Company"),"资产重组，现价微涨","olive",RndID,1
					End if
				Else
					Randomize
					Rnum=Formatnumber(100*rnd+1,0,-1)
					If Rnum<10 Then
						ExFluctuate Rnum*10,1,Rs("Company"),"放出非利好消息，现价微跌","navy",RndID,-1 
					Elseif Rnum<20 Then
						ExFluctuate Rnum*10,1,Rs("Company"),"放出非利好消息，现价下滑","teal",RndID,-1
					Elseif Rnum<30 Then
						ExFluctuate Rnum*10,10,Rs("Company"),"业绩不佳，投资人信心不足，现价下滑","green",RndID,-1
					Elseif Rnum<40 Then
						ExFluctuate Rnum*10,20,Rs("Company"),"业绩开始滑坡，现价下跌","blue",RndID,-1
					Elseif Rnum<50 Then
						ExFluctuate Rnum*10,300,Rs("Company"),"业绩大幅滑坡，资金不足，现价下跌","green",RndID,-1
					Elseif Rnum<60 Then 
						ExFluctuate 1000-Rnum*10,40,Rs("Company"),"业绩大幅滑坡，遭受大户抛售，现价下跌","navy",RndID,-1 
					Elseif Rnum<70 Then
						ExFluctuate 1000-Rnum*10,30,Rs("Company"),"业绩开始滑坡，现价下跌","teal",RndID,-1
					Elseif Rnum<80 Then
						ExFluctuate 1000-Rnum*10,20,Rs("Company"),"业绩不佳，投资人信心不足，现价下滑","gray",RndID,-1
					Elseif Rnum<90 Then
						ExFluctuate 1000-Rnum*10,10,Rs("Company"),"放出非利好消息，现价下滑","purple",RndID,-1
					Else
						ExFluctuate 1000-Rnum*10,1,Rs("Company"),"放出非利好消息，现价微跌","blue",RndID,-1
					End if
				End if
			End if
			Exsm.ExsmUserInfo(8)=Now()
			Session(Exsm.PlaceName&"ExsmTeUserID")=Exsm.ExsmUserInfo
			If Clng(Exsm.Setting_Info(13))>0 Then
				stock_conn.execute("delete from EX_News where id not in(select top "&Clng(Exsm.Setting_Info(13))&" id from EX_News order by ID desc)")
			End if
		End if
	End Sub
	Sub ExFluctuate(Rmax,Rmin,obj,message,msgColor,GPid,Flag)
		Dim AMP,AiStockName,NewPrice,TradePrice,ClosingPrice,ZangDieRange,Color,Mycontent
		AiStockName=obj
		Randomize
		AMP=Round(((Rmax-Rmin+1)*rnd+Rmin)/100,2)
		Set Rs=stock_conn.execute("select Sid,TradePrice,ClosingPrice from EX_Stock Where Cid="&GPid)
		NewPrice=Round(AMP*Rs(2)*Flag/100,2)
		TradePrice=Round(Rs(1),2)
		ClosingPrice=Round(Rs(2),2)
		AMP=NewPrice/ClosingPrice
		ZangDieRange=Round(((TradePrice+NewPrice-ClosingPrice)/ClosingPrice)*100,2)
		If ZangDieRange>Round(AiTrade_Info(3),2) Then
			NewPrice=Round(ClosingPrice*(1+AiTrade_Info(3)/100)-TradePrice,2)
			AMP=Round(AiTrade_Info(3)/100,2)-(TradePrice-ClosingPrice)/ClosingPrice
		Elseif ZangDieRange<Round(-AiTrade_Info(4),2) Then
			NewPrice=Round(ClosingPrice*(1+-AiTrade_Info(4)/100)-TradePrice,2)
			AMP=Round(-AiTrade_Info(4)/100,2)-(TradePrice-ClosingPrice)/ClosingPrice
		End if
		If Round(AMP,2)<>0 And TradePrice+NewPrice>=1 Then
			Mycontent="上市公司["&AiStockName&"]"&message&"["&Replace(FormatPercent(AMP,2,-1),"-","")&"]"
			Color=""&msgColor&""
			stock_conn.execute("insert into EX_News(content,Color,addtime) values ('"&Mycontent&"','"&Color&"',"&SqlSmNowString&")")
			stock_conn.execute("update EX_Stock Set TradePrice=TradePrice+"&Round(NewPrice,2)&",TodayWave=TodayWave+"&Cdbl(AMP*100)&",TotalWave=TotalWave+"&Cdbl(AMP*100)&" Where Cid="&GPid&"")
			stock_conn.execute("update EX_Config Set ExColExp=ExColExp+"&Cdbl(AMP*100)&"")
			Call Exsm.CalculateFund("No",Rs(0))
		End if
	End Sub
	AI_Setting=split(Exsm.Stock_Setting(5),"|")
	if Cint(AI_Setting(0))=1 And Exsm.UserID>0 Then ExAi
	Sub ExAi()
		Dim Rnum
		Randomize
		Rnum=rnd
		If Rnum<=Cdbl(AI_Setting(2)/100) Then
			Dim RndID,Stocksid,StockCode,StockNum,StockName,StockPrice,ClosingPrice,TodayPermitTradeSum,LockStock
			Dim BuyNum
			RndID=stock_conn.execute("select top 1 Cid from EX_Stock order by Cid desc")(0)
			Randomize Rnd
			RndID=int(RndID*rnd)+1
			set Rs=stock_conn.execute("select Sid,Code,Company,TradePrice,ClosingPrice,StockSum,TodayPermitTradeSum,LockStock from EX_Stock where Cid="&RndID)
			Stocksid=Rs(0)	:	StockCode=Rs(1)	:	StockName=Rs(2)	:	StockPrice=Round(Rs(3),2)	:	ClosingPrice=Round(Rs(4),2)	:	StockNum=Rs(5)	:	TodayPermitTradeSum=Rs(6)	:	LockStock=Rs(7)
			If TodayPermitTradeSum<=0 Then Exit Sub
			If LockStock=1 Then Exit Sub
			If StockPrice<=Round(AiTrade_Info(5),2) Then Exit Sub
			If StockNum<5000000 Or StockNum>20000000 Then StockNum=20000000
			StockNum=StockNum/30
			Randomize
			Rnum=Int(rnd*10+1)
			Select Case Rnum
				Case 1
					BuyNum=Cint(rnd*1000+1)
					If BuyNum<100 Then BuyNum=100
					If TodayPermitTradeSum-BuyNum<=0 Then BuyNum=TodayPermitTradeSum
					call ExAI_Buy(BuyNum,-1,Stocksid,StockCode,StockName,StockPrice,ClosingPrice,StockNum)
				Case 2
					BuyNum=Cint(rnd*10000+1)
					If BuyNum<100 Then BuyNum=100
					If TodayPermitTradeSum-BuyNum<=0 Then BuyNum=TodayPermitTradeSum
					call ExAI_Buy(BuyNum,-1,Stocksid,StockCode,StockName,StockPrice,ClosingPrice,StockNum)			
				Case 4
					BuyNum=Cint(rnd*5000+1)
					If BuyNum<100 Then BuyNum=100
					If TodayPermitTradeSum-BuyNum<=0 Then BuyNum=TodayPermitTradeSum
					call ExAI_Buy(BuyNum,-1,Stocksid,StockCode,StockName,StockPrice,ClosingPrice,StockNum)			
				Case 7
					BuyNum=Cint(rnd*7500+1)
					If BuyNum<100 Then BuyNum=100
					If TodayPermitTradeSum-BuyNum<=0 Then BuyNum=TodayPermitTradeSum
					call ExAI_Buy(BuyNum,-1,Stocksid,StockCode,StockName,StockPrice,ClosingPrice,StockNum)			
				Case 9
					BuyNum=Cint(rnd*500+1)
					If BuyNum<100 Then BuyNum=100
					If TodayPermitTradeSum-BuyNum<=0 Then BuyNum=TodayPermitTradeSum
					call ExAI_Buy(BuyNum,-1,Stocksid,StockCode,StockName,StockPrice,ClosingPrice,StockNum)			
				Case Else
					Call ExAI_Sale(-1)
			End Select
		Elseif Rnum<0.10 Then
			stock_conn.execute("insert into EX_News (content,Color,addtime) values('["&AI_Setting(1)&"]在股票市场转了一圈，一笔交易都没有做成。','"&Exsm.RndColor&"',"&SqlSmNowString&")") 
		End if
	End Sub
	Sub ExAI_Buy(BuyNum,Uid,Sid,StockCode,GupiaoName,NowPrice,ClosingPrice,StockNum)
		Dim Rnum,Rate,BuyNumhldcg,NewPrice,Zd,ZangDieRange,Mycontent
		BuyNumhldcg=BuyNum
		If BuyNum>10000 Then BuyNumhldcg=10000
		Randomize
		Rnum=Rnd
		Rate=Rnd
		If Rnum<=0.4 Then Rnum=Rnum+0.5
		If Rate<Round(AiTrade_Info(8)/100,2) Then
			NewPrice=Round(ClosingPrice*BuyNumhldcg*Rnum/StockNum,2)
			ZD=NewPrice/ClosingPrice
			ZangDieRange=Round(((NowPrice+NewPrice-ClosingPrice)/ClosingPrice)*100,2)
			If ZangDieRange>Round(AiTrade_Info(3),2) Then
				NewPrice=Round(ClosingPrice*(1+Round(AiTrade_Info(3)/100,2))-NowPrice,2)
				ZD=Round(AiTrade_Info(3)/100,2)-((NowPrice-ClosingPrice)/ClosingPrice)
			End if
			If NowPrice+NewPrice<Round(AiTrade_Info(5),2) Then NewPrice=0	:	ZD=0
			If Round(ZD,4)=0 Then
				Mycontent="股民["&AI_Setting(1)&"]买入["&GupiaoName&"]["&BuyNum&"]股，现价没有波动。"
			Else
				Mycontent="股民["&AI_Setting(1)&"]买入["&GupiaoName&"]["&BuyNum&"]股，现价上涨["&FormatPercent(ZD,2,-1)&"]。"
			End if
		Elseif Rate<Round(AiTrade_Info(8)/100,2)+Round(AiTrade_Info(9)/100,2) Then
			NewPrice=Round(-ClosingPrice*BuyNumhldcg*Rnum/StockNum,2)
			ZD=NewPrice/ClosingPrice
			ZangDieRange=Round(((NowPrice+NewPrice-ClosingPrice)/ClosingPrice)*100,2)
			If ZangDieRange<Round(-AiTrade_Info(4),2) Then
				NewPrice=Round(ClosingPrice*(1+-Round(AiTrade_Info(4)/100,2))-NowPrice,2)
				ZD=Round(-AiTrade_Info(4)/100,2)-((NowPrice-ClosingPrice)/ClosingPrice)
			End if
			If NowPrice+NewPrice<Round(AiTrade_Info(5),2) Then NewPrice=0	:	ZD=0
			If Round(ZD,4)=0 Then
				Mycontent="股民["&AI_Setting(1)&"]买入["&GupiaoName&"]["&BuyNum&"]股，现价没有波动。"
			Else
				Mycontent="股民["&AI_Setting(1)&"]买入["&GupiaoName&"]["&BuyNum&"]股，现价下滑["&Replace(FormatPercent(ZD,2,-1),"-","")&"]。"
			End if
		Else
			NewPrice=0
			ZD=0
			Mycontent="股民["&AI_Setting(1)&"]买入["&GupiaoName&"]["&BuyNum&"]股，现价没有波动。"
		End if
		Set Rs=stock_conn.execute("select PossessStockSum,BuyPrice from EX_Customer Where Sid="&sid&" And UserId="&Uid&"")
		If Rs.eof and Rs.bof Then
			stock_conn.execute("update EX_Stock Set TradePrice=TradePrice+"&Round(NewPrice,2)&", TodayPermitTradeSum=TodayPermitTradeSum-"&Round(BuyNum)&",SurplusShare=SurplusShare-"&Round(BuyNum)&",TodayAdTradeSum=TodayAdTradeSum+"&Round(BuyNum)&",TodayBuyNumber=TodayBuyNumber+1 Where Sid="&sid&"")
			stock_conn.execute("insert into EX_Customer (UserName,UserId,Code,Company,Sid,BuyPrice,AveragePrice,PossessStockSum,BuyTime) values ('"&AI_Setting(1)&"',"&Uid&",'"&StockCode&"','"&GupiaoName&"',"&sid&","&Round(NowPrice,2)&","&Round(NowPrice,2)&","&BuyNum&","&SqlSmNowString&")")
			stock_conn.execute("update EX_Ai Set UserWealth=UserWealth-"&Round(BuyNum*NowPrice,2)&",UserAssets=UserAssets-"&Round(BuyNum*NowPrice,2)&",PossessSort=PossessSort+1,TotalBuy=TotalBuy+1 Where UserName='"&AI_Setting(1)&"'")
			stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Exsm.RndColor&"',"&SqlSmNowString&")")
		Else
			Dim MyNum,AgvPrice
			MyNum=Rs(0)
			AgvPrice=Round((NowPrice*BuyNum+Rs(1)*MyNum)/(BuyNum+MyNum),2)
			stock_conn.execute("update EX_Stock Set TradePrice=TradePrice+"&Cdbl(NewPrice)&", TodayPermitTradeSum=TodayPermitTradeSum-"&Round(BuyNum)&",SurplusShare=SurplusShare-"&Round(BuyNum)&",TodayAdTradeSum=TodayAdTradeSum+"&Round(BuyNum)&",TodayBuyNumber=TodayBuyNumber+1 Where Sid="&sid&"")
			stock_conn.execute("update EX_Customer Set BuyPrice="&Round(NowPrice,2)&",AveragePrice="&Round(AgvPrice,2)&",PossessStockSum=PossessStockSum+"&Round(BuyNum)&",BuyTime="&SqlSmNowString&" Where UserId="&Uid&" And Sid="&sid&"")
			stock_conn.execute("update EX_Ai Set UserWealth=UserWealth-"&Round(BuyNum*NowPrice,2)&",UserAssets=UserAssets-"&Round(BuyNum*NowPrice,2)&",PossessSort=PossessSort+1,TotalBuy=TotalBuy+1 Where UserName='"&AI_Setting(1)&"'")
			stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Exsm.RndColor&"',"&SqlSmNowString&")")
		End if
		stock_conn.execute("update EX_Config Set TodayBuy=TodayBuy+1,TodayTotal=TodayTotal+"&Cdbl(BuyNum*NowPrice)&",ExColExp=ExColExp+"&Cdbl(ZD*100)&"")
		Call Exsm.CalculateFund("No",Sid)
		Rs.close
		Set Rs=Nothing
	End Sub
	Sub ExAI_Sale(Uid)
		Dim Rnum,Rst,Sid,BuyPrice,SaleNum,NowPrice,GupiaoName,StockNum
		Dim Rate,SaleNumhldcg,NewPrice,ZD,ZangDieRange,ClosingPrice,Mycontent
		SQL="select d.Sid,d.PossessStockSum,g.Company,g.TradePrice,g.ClosingPrice,g.StockSum,g.LockStock from EX_Customer d inner join EX_Stock g on d.Sid=g.Sid where d.UserId="&Uid&""
		set Rst=server.createobject("adodb.recordset")
		Rst.open SQL,stock_conn,1,1
		If not Rst.eof Then
			Randomize
			Rst.absolutePosition=Fix(Rst.recordcount*Rnd)+1
			If Rst(3)<=Cdbl(AiTrade_Info(5)) Then Exit Sub
			If Rst(6)=1 Then Exit Sub
			Sid=Rst(0)
			SaleNum=Rst(1)
			GupiaoName=Rst(2)
			NowPrice=Round(Rst(3),2)
			ClosingPrice=Round(Rst(4),2)
			StockNum=Rst(5)
			If StockNum<5000000 Or StockNum>20000000 Then StockNum=20000000
			StockNum=StockNum/30
			Randomize
			If rnd<0.3 then
				SaleNum=Rst(1)
			Else
				Rnum=Rnd
				If Rnum>0.5 then Rnum=1-Rnum
				SaleNum=Cint(Rst(1)*Rnum+1)
				If SaleNum<100 Then SaleNum=100
				If SaleNum-Rst(1)<=0 Then SaleNum=Rst(1)
			End if
			SaleNumhldcg=SaleNum
			If SaleNum>10000 Then SaleNumhldcg=10000
			Randomize
			Rate=Rnd
			Rnum=Rnd
			If Rnum<=0.4 Then Rnum=Rnum+0.5
			If Rate<Cdbl(AiTrade_Info(10)/100) Then
				NewPrice=Round(-ClosingPrice*SaleNumhldcg*Rnum/StockNum,2)
				ZD=NewPrice/ClosingPrice
				ZangDieRange=Round(((NowPrice+NewPrice-ClosingPrice)/ClosingPrice)*100,2)
				If ZangDieRange<Round(-AiTrade_Info(4),2) Then
					NewPrice=Round(ClosingPrice*(1+-Round(AiTrade_Info(4)/100,2))-NowPrice,2)
					ZD=Round(-AiTrade_Info(4)/100,2)-((NowPrice-ClosingPrice)/ClosingPrice)
				End if
				If NowPrice+NewPrice<Round(AiTrade_Info(5),2) Then NewPrice=0	:	ZD=0
				If Round(ZD,4)=0 Then
					Mycontent="股民["&AI_Setting(1)&"]卖出["&GupiaoName&"]["&SaleNum&"]股，现价没有波动。"
				Else
					Mycontent="股民["&AI_Setting(1)&"]卖出["&GupiaoName&"]["&SaleNum&"]股，现价下滑["&Replace(FormatPercent(ZD,2,-1),"-","")&"]。"
				End if
			Elseif Rate<Round(AiTrade_Info(10)/100,2)+Round(AiTrade_Info(11)/100,2) Then
				NewPrice=Round(ClosingPrice*SaleNumhldcg*Rnum/StockNum,2)
				ZD=NewPrice/ClosingPrice
				ZangDieRange=Round(((NowPrice+NewPrice-ClosingPrice)/ClosingPrice)*100,2)
				If ZangDieRange>Round(AiTrade_Info(3),2) Then
					NewPrice=Round(ClosingPrice*(1+Round(AiTrade_Info(3)/100,2))-NowPrice,2)
					ZD=Round(AiTrade_Info(3)/100,2)-((NowPrice-ClosingPrice)/ClosingPrice)
				End if
				If NowPrice+NewPrice<Round(AiTrade_Info(5),2) Then NewPrice=0	:	ZD=0
				If Round(ZD,4)=0 Then
					Mycontent="股民["&AI_Setting(1)&"]卖出["&GupiaoName&"]["&SaleNum&"]股，现价没有波动。"
				Else
					Mycontent="股民["&AI_Setting(1)&"]卖出["&GupiaoName&"]["&SaleNum&"]股，现价上涨["&FormatPercent(ZD,2,-1)&"]。"
				End if
			Else
				NewPrice=0
				ZD=0
				Mycontent="股民["&AI_Setting(1)&"]卖出["&GupiaoName&"]["&SaleNum&"]股，现价没有波动。"
			End if
			stock_conn.execute("update EX_Stock Set TradePrice=TradePrice+"&Round(NewPrice,2)&", TodayPermitTradeSum=TodayPermitTradeSum+"&Round(SaleNum)&",SurplusShare=SurplusShare+"&Round(SaleNum)&",TodayAdTradeSum=TodayAdTradeSum+"&Round(SaleNum)&",TodaySellNumber=TodaySellNumber+1 Where Sid="&sid&"")
			stock_conn.execute("update EX_Customer Set PossessStockSum=PossessStockSum-"&Round(SaleNum)&",Selltime="&SqlSmNowString&" Where UserId="&Uid&" And Sid="&sid&"")
			stock_conn.execute("update EX_Ai Set UserWealth=UserWealth+"&Round(SaleNum*NowPrice,2)&",UserAssets=UserAssets+"&Round(SaleNum*NowPrice,2)&",PossessSort=PossessSort-1,TotalSell=TotalSell+1 Where UserName='"&AI_Setting(1)&"'")
			stock_conn.execute("insert into EX_News (content,Color,addtime) values('"&Mycontent&"','"&Exsm.RndColor&"',"&SqlSmNowString&")")
			stock_conn.execute("update EX_Config Set TodaySell=TodaySell+1,TodayTotal=TodayTotal+"&Cdbl(SaleNum*NowPrice)&",ExColExp=ExColExp+"&Cdbl(ZD*100)&"")
			stock_conn.execute("delete from EX_Customer Where PossessStockSum<=0")
			Call Exsm.CalculateFund("No",sid)
        	End if
		Rst.close
		Set Rst=nothing
	End Sub
%>