我创建了一个生成买入和卖出股票信号的程序。我还创建了测试不同信号的逻辑,并为每笔交易提供回报。
下一步是在很长一段时间内模拟战略及其规则。所有信息都导出到文本文件并导入SQL Server数据库中的表。我意识到我需要声明一些变量,例如StartCapital
,CurrentCapital
,NumberOfPositions
,PositionsLeft
。其中一列名为BuyPrice
,表示何时购买,以及在何时发生价格NumberOfPositions
应减去1. {/ p>
列SellPrice
表示何时出售以及出现何种价格时{1}}需要加一个NumberOfPositions
。 NumberOfPositions
的最大值应为5且最小值为0.期望的结果是查看CurrentCapital
如何展开。
我非常感谢任何输入和某种SQL代码来启动表单。
答案 0 :(得分:23)
还有另一种循环方式。我已经看到很多答案将计数器增加1.然而,根据我的经验,无法保证数据集中的ID不会有间隙。
这是我经常使用的解决方案:
declare @idColumn int
select @idColumn = min( TableID ) from Table
while @idColumn is not null
begin
/*
Do all the stuff that you need to do
*/
select @idColumn = min( TableID ) from Table where TableID > @idColumn
end
答案 1 :(得分:7)
根据您问题的标题。这是我使用TABLE
类型的变量循环遍历表的每一行的方式:
DECLARE
@counter INT = 1,
@max INT = 0
-- Declare a variable of type TABLE. It will be used as a temporary table.
DECLARE @myTable TABLE (
[Id] int identity,
[Column1] nvarchar(max),
[Column2] nvarchar(100)
)
-- Insert your required data in the variable of type TABLE
INSERT INTO @myTable
SELECT Column1, Column2
FROM [dbo].[YOUR_DATABASE_TABLE]
-- Initialize the @max variable. We'll use thie variable in the next WHILE loop.
SELECT @max = COUNT(ID) FROM @myTable
-- Loop
WHILE @counter <= @max
BEGIN
-- Do whatever you want with each row in your table variable filtering by the Id column
SELECT Column1, Column2
FROM @myTable
WHERE Id = @counter
SET @counter = @counter + 1
END