基于c#的交易模拟器
当本金小于20%时算出局,目标为100倍
输出结果为:成功局数,出局局数,以及成功局数中达成目标所需要的平均步骤数(交易次数)
using System;
namespace TradingSimulator
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
double initialCapital = 500; //本金
double target = 50000; //目标
double winRate = 0.45; //胜率
double profitLossRatio = 3.5; //盈亏比
double betRatio = 0.25; // 每次动用的仓位大小
int successRuns = 0;
int outOfGameRuns = 0;
int totalSuccessTrades = 0;
for (int run = 0; run < 10000; run ). //这里是跑了1w局
{
double capital = initialCapital;
int totalTrades = 0;
while (capital < target)
{
double bet = capital * betRatio;
if (random.NextDouble() < winRate) // win the trade
{
capital = bet * profitLossRatio;
}
else // lose the trade
{
capital -= bet;
}
totalTrades ;
// check if the capital drops below 20% of the initial capital
if (capital < initialCapital * 0.2)
{
outOfGameRuns ;
break;
}
}
if (capital >= target)
{
successRuns ;
totalSuccessTrades = totalTrades;
}
}
double avgSuccessTrades = (double)totalSuccessTrades / successRuns;
Console.WriteLine("Total runs: 10000");
Console.WriteLine("Successful runs: " successRuns);
Console.WriteLine("Out-of-game runs: " outOfGameRuns);
Console.WriteLine("Average trades in successful runs: " avgSuccessTrades);
}
}
}