Skip to content

Commit 88bc274

Browse files
committedMay 24, 2017
TradingStrategiesBasedOnGeneticAlgorithms running.
Added different log filenames to avoid access issue.
1 parent de1432a commit 88bc274

File tree

6 files changed

+219
-91
lines changed

6 files changed

+219
-91
lines changed
 

‎Optimization.Example/ParameterizedAlgorithm.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Licensed under the Apache License, Version 2.0 (the "License");
2+
* Licensed under the Apache License, Version 2.0 (the "License");
33
* you may not use this file except in compliance with the License.
44
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5-
*
5+
*
66
* Unless required by applicable law or agreed to in writing, software
77
* distributed under the License is distributed on an "AS IS" BASIS,
88
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,20 +15,19 @@
1515
using QuantConnect.Configuration;
1616
using QuantConnect.Data.Market;
1717
using QuantConnect.Indicators;
18-
using QuantConnect.Parameters;
1918

2019
namespace Optimization.Example
2120
{
2221
public class ParameterizedAlgorithm : QCAlgorithm
2322
{
24-
23+
private readonly decimal stopLoss = Config.GetValue("stop", 0.2m);
24+
private readonly decimal takeProfit = Config.GetValue("take", 0.1m);
25+
public ExponentialMovingAverage Fast;
2526
public int FastPeriod = Config.GetInt("fast", 13);
27+
public ExponentialMovingAverage Slow;
2628

2729
public int SlowPeriod = Config.GetInt("slow", 56);
2830

29-
public ExponentialMovingAverage Fast;
30-
public ExponentialMovingAverage Slow;
31-
3231
public override void Initialize()
3332
{
3433
SetStartDate(2013, 10, 07);
@@ -46,15 +45,18 @@ public void OnData(TradeBars data)
4645
// wait for our indicators to ready
4746
if (!Fast.IsReady || !Slow.IsReady) return;
4847

49-
if (Fast > Slow * 1.001m)
48+
if (!Portfolio["SPY"].HoldStock)
5049
{
51-
SetHoldings("SPY", 1);
50+
if (Fast > Slow * 1.001m)
51+
{
52+
SetHoldings("SPY", 1);
53+
}
5254
}
53-
else if (Portfolio["SPY"].HoldStock && Portfolio["SPY"].UnrealizedProfitPercent > Config.GetValue<decimal>("take", 0.2m))
55+
else if (Portfolio["SPY"].UnrealizedProfitPercent > takeProfit ||
56+
Portfolio["SPY"].UnrealizedProfitPercent < stopLoss)
5457
{
5558
Liquidate("SPY");
5659
}
57-
5860
}
5961
}
60-
}
62+
}

‎Optimization/Optimization.csproj

+22-18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
<HintPath>..\packages\Accord.Statistics.3.3.0\lib\net45\Accord.Statistics.dll</HintPath>
5555
<Private>True</Private>
5656
</Reference>
57+
<Reference Include="DynamicExpresso.Core, Version=1.3.3.0, Culture=neutral, processorArchitecture=MSIL">
58+
<HintPath>..\packages\DynamicExpresso.Core.1.3.3.5\lib\net40\DynamicExpresso.Core.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
5761
<Reference Include="GeneticSharp.Domain, Version=1.0.5894.2743, Culture=neutral, PublicKeyToken=d63d2a65268366c6, processorArchitecture=MSIL">
5862
<HintPath>..\packages\GeneticSharp.1.0.100\lib\net35\GeneticSharp.Domain.dll</HintPath>
5963
<Private>True</Private>
@@ -86,41 +90,41 @@
8690
<HintPath>..\packages\NLog.4.4.5\lib\net45\NLog.dll</HintPath>
8791
<Private>True</Private>
8892
</Reference>
89-
<Reference Include="QuantConnect.Algorithm, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
93+
<Reference Include="QuantConnect.Algorithm, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
9094
<SpecificVersion>False</SpecificVersion>
91-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Algorithm.dll</HintPath>
95+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Algorithm.dll</HintPath>
9296
</Reference>
93-
<Reference Include="QuantConnect.Api, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
97+
<Reference Include="QuantConnect.Api, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
9498
<SpecificVersion>False</SpecificVersion>
95-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Api.dll</HintPath>
99+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Api.dll</HintPath>
96100
</Reference>
97-
<Reference Include="QuantConnect.Common, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
101+
<Reference Include="QuantConnect.Common, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
98102
<SpecificVersion>False</SpecificVersion>
99-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Common.dll</HintPath>
103+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Common.dll</HintPath>
100104
</Reference>
101-
<Reference Include="QuantConnect.Configuration, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
105+
<Reference Include="QuantConnect.Configuration, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
102106
<SpecificVersion>False</SpecificVersion>
103-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Configuration.dll</HintPath>
107+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Configuration.dll</HintPath>
104108
</Reference>
105-
<Reference Include="QuantConnect.Indicators, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
109+
<Reference Include="QuantConnect.Indicators, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
106110
<SpecificVersion>False</SpecificVersion>
107-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Indicators.dll</HintPath>
111+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Indicators.dll</HintPath>
108112
</Reference>
109-
<Reference Include="QuantConnect.Lean.Engine, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
113+
<Reference Include="QuantConnect.Lean.Engine, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
110114
<SpecificVersion>False</SpecificVersion>
111-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Lean.Engine.dll</HintPath>
115+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Lean.Engine.dll</HintPath>
112116
</Reference>
113-
<Reference Include="QuantConnect.Logging, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
117+
<Reference Include="QuantConnect.Logging, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
114118
<SpecificVersion>False</SpecificVersion>
115-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Logging.dll</HintPath>
119+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Logging.dll</HintPath>
116120
</Reference>
117-
<Reference Include="QuantConnect.Messaging, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
121+
<Reference Include="QuantConnect.Messaging, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
118122
<SpecificVersion>False</SpecificVersion>
119-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Messaging.dll</HintPath>
123+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Messaging.dll</HintPath>
120124
</Reference>
121-
<Reference Include="QuantConnect.Queues, Version=2.2.0.2, Culture=neutral, processorArchitecture=MSIL">
125+
<Reference Include="QuantConnect.Queues, Version=2.3.0.2, Culture=neutral, processorArchitecture=MSIL">
122126
<SpecificVersion>False</SpecificVersion>
123-
<HintPath>..\..\Lean\Launcher\bin\Debug\QuantConnect.Queues.dll</HintPath>
127+
<HintPath>..\..\LeanSTP\Launcher\bin\Release\QuantConnect.Queues.dll</HintPath>
124128
</Reference>
125129
<Reference Include="SmartThreadPool, Version=2.2.3.0, Culture=neutral, PublicKeyToken=1126fe8b671e8a79, processorArchitecture=MSIL">
126130
<HintPath>..\packages\SmartThreadPool.dll.2.2.3\lib\SmartThreadPool.dll</HintPath>

‎Optimization/Program.cs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1-
using GeneticSharp.Domain.Fitnesses;
2-
using Newtonsoft.Json;
3-
using Newtonsoft.Json.Linq;
4-
using System;
1+
using System;
52
using System.IO;
63
using System.Reflection;
4+
using Newtonsoft.Json;
75
using NLog;
86

97
namespace Optimization
108
{
11-
129
public class Program
1310
{
14-
15-
#region Declarations
16-
static OptimizerConfiguration _config;
17-
public static Logger Logger = LogManager.GetLogger("optimizer");
18-
#endregion
19-
2011
public static void Main(string[] args)
2112
{
2213
_config = LoadConfig(args);
2314
File.Copy(_config.ConfigPath, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"), true);
2415

25-
string path = _config.AlgorithmLocation;
16+
var path = _config.AlgorithmLocation;
2617
if (!string.IsNullOrEmpty(path))
2718
{
2819
File.Copy(path, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(path)), true);
29-
string pdb = path.Replace(Path.GetExtension(path), ".pdb");
20+
var pdb = path.Replace(Path.GetExtension(path), ".pdb");
3021
if (File.Exists(pdb))
3122
{
3223
File.Copy(pdb, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(pdb)), true);
@@ -35,8 +26,9 @@ public static void Main(string[] args)
3526

3627
AppDomainManager.Initialize(_config);
3728

38-
OptimizerFitness instance = (OptimizerFitness)Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName, false, BindingFlags.Default, null,
39-
new[] { _config }, null, null);
29+
var instance = (OptimizerFitness) Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName,
30+
false, BindingFlags.Default, null,
31+
new[] {_config}, null, null);
4032

4133
var manager = new GeneManager(_config, instance);
4234
manager.Start();
@@ -46,7 +38,7 @@ public static void Main(string[] args)
4638

4739
private static OptimizerConfiguration LoadConfig(string[] args)
4840
{
49-
string path = "optimization.json";
41+
var path = "optimization.json";
5042
if (args != null && args.Length > 0 && !string.IsNullOrEmpty(args[0]))
5143
{
5244
path = args[0];
@@ -55,5 +47,11 @@ private static OptimizerConfiguration LoadConfig(string[] args)
5547
return JsonConvert.DeserializeObject<OptimizerConfiguration>(File.ReadAllText(path));
5648
}
5749

50+
#region Declarations
51+
52+
private static OptimizerConfiguration _config;
53+
public static Logger Logger = LogManager.GetLogger("optimizer");
54+
55+
#endregion
5856
}
5957
}

‎Optimization/Runner.cs

+29-28
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,37 @@ private void LaunchLean()
7676
var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
7777
systemHandlers.Initialize();
7878

79-
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));
80-
//Log.DebuggingEnabled = false;
81-
//Log.DebuggingLevel = 1;
79+
var logFileName = "log_" + Guid.NewGuid().ToString() + ".txt";
80+
var logHandlers = new ILogHandler[] { new FileLogHandler(logFileName, true) };
8281

83-
LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;
84-
try
82+
using (Log.LogHandler = new CompositeLogHandler(logHandlers))
8583
{
86-
leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
87-
_resultsHandler = (BacktestingResultHandler)leanEngineAlgorithmHandlers.Results;
88-
}
89-
catch (CompositionException compositionException)
90-
{
91-
Log.Error("Engine.Main(): Failed to load library: " + compositionException);
92-
throw;
93-
}
94-
string algorithmPath;
95-
AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath);
96-
try
97-
{
98-
var _engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode"));
99-
_engine.Run(job, algorithmPath);
100-
}
101-
finally
102-
{
103-
Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);
104-
105-
// clean up resources
106-
systemHandlers.Dispose();
107-
leanEngineAlgorithmHandlers.Dispose();
108-
Log.LogHandler.Dispose();
84+
LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;
85+
try
86+
{
87+
leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
88+
_resultsHandler = (BacktestingResultHandler)leanEngineAlgorithmHandlers.Results;
89+
}
90+
catch (CompositionException compositionException)
91+
{
92+
Log.Error("Engine.Main(): Failed to load library: " + compositionException);
93+
throw;
94+
}
95+
string algorithmPath;
96+
AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath);
97+
try
98+
{
99+
var _engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode"));
100+
_engine.Run(job, algorithmPath);
101+
}
102+
finally
103+
{
104+
Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);
105+
106+
// clean up resources
107+
systemHandlers.Dispose();
108+
leanEngineAlgorithmHandlers.Dispose();
109+
}
109110
}
110111
}
111112

‎Optimization/optimization.json

+139-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,157 @@
11
{
22
"genes": [
33
{
4-
"key": "take",
5-
"min": 0.02,
6-
"max": 0.1,
7-
"precision": 2
4+
"key": "EntryIndicator1",
5+
"min": 0,
6+
"max": 7
87
},
98
{
10-
"key": "fast",
11-
"min": 6,
12-
"max": 48,
13-
"precision": 0,
14-
"actual": 13
9+
"key": "EntryIndicator2",
10+
"min": 0,
11+
"max": 7
1512
},
1613
{
17-
"key": "slow",
18-
"min": 48,
19-
"max": 500,
14+
"key": "EntryIndicator3",
15+
"min": 0,
16+
"max": 7
17+
},
18+
{
19+
"key": "EntryIndicator4",
20+
"min": 0,
21+
"max": 7
22+
},
23+
{
24+
"key": "EntryIndicator5",
25+
"min": 0,
26+
"max": 7
27+
},
28+
{
29+
"key": "EntryIndicator1Direction",
30+
"min": 0,
31+
"max": 1
32+
},
33+
{
34+
"key": "EntryIndicator2Direction",
35+
"min": 0,
36+
"max": 1
37+
},
38+
{
39+
"key": "EntryIndicator3Direction",
40+
"min": 0,
41+
"max": 1
42+
},
43+
{
44+
"key": "EntryIndicator4Direction",
45+
"min": 0,
46+
"max": 1
47+
},
48+
{
49+
"key": "EntryIndicator5Direction",
50+
"min": 0,
51+
"max": 1
52+
},
53+
{
54+
"key": "EntryOperator1",
55+
"min": 0,
56+
"max": 1
57+
},
58+
{
59+
"key": "EntryOperator2",
60+
"min": 0,
61+
"max": 1
62+
},
63+
{
64+
"key": "EntryOperator3",
65+
"min": 0,
66+
"max": 1
67+
},
68+
{
69+
"key": "EntryOperator4",
70+
"min": 0,
71+
"max": 1
72+
},
73+
{
74+
"key": "ExitIndicator1",
75+
"min": 0,
76+
"max": 7
77+
},
78+
{
79+
"key": "ExitIndicator2",
80+
"min": 0,
81+
"max": 7
82+
},
83+
{
84+
"key": "ExitIndicator3",
85+
"min": 0,
86+
"max": 7
87+
},
88+
{
89+
"key": "ExitIndicator4",
90+
"min": 0,
91+
"max": 7
92+
},
93+
{
94+
"key": "ExitIndicator5",
95+
"min": 0,
96+
"max": 7
97+
},
98+
{
99+
"key": "ExitIndicator1Direction",
100+
"min": 0,
101+
"max": 1
102+
},
103+
{
104+
"key": "ExitIndicator2Direction",
105+
"min": 0,
106+
"max": 1
107+
},
108+
{
109+
"key": "ExitIndicator3Direction",
110+
"min": 0,
111+
"max": 1
112+
},
113+
{
114+
"key": "ExitIndicator4Direction",
115+
"min": 0,
116+
"max": 1
117+
},
118+
{
119+
"key": "ExitIndicator5Direction",
120+
"min": 0,
121+
"max": 1
122+
},
123+
{
124+
"key": "ExitOperator1",
125+
"min": 0,
126+
"max": 1
127+
},
128+
{
129+
"key": "ExitOperator2",
130+
"min": 0,
131+
"max": 1
132+
},
133+
{
134+
"key": "ExitOperator3",
135+
"min": 0,
136+
"max": 1
137+
},
138+
{
139+
"key": "ExitOperator4",
140+
"min": 0,
141+
"max": 1
20142
}
21143
],
22144
"populationSize": 24,
23145
"populationSizeMaximum": 48,
24146
"generations": 1000,
25147
"stagnationGenerations": 30,
26148
"maxThreads": 8,
27-
"configPath": "../../../../Lean/Launcher/config.json",
28-
"algorithmTypeName": "ParameterizedAlgorithm",
29-
"algorithmLocation": "../../../Optimization.Example/bin/debug/Optimization.Example.dll",
149+
"configPath": "../../../../LeanSTP/Launcher/config.json",
150+
"algorithmTypeName": "TradingStrategiesBasedOnGeneticAlgorithms",
151+
"algorithmLocation": "D:/REPOS/LeanSTP/Algorithm.CSharp/bin/Release/QuantConnect.Algorithm.CSharp.dll",
30152
"onePointCrossover": "true",
31153
"fitnessTypeName": "Optimization.OptimizerFitness",
32-
"dataFolder": "../../../../Lean/Data",
154+
"dataFolder": "../../../../../AlgorithmicTrading/DATA/ForexQuotes"
33155
//"mutationProbability": 0.5,
34156
//"crossoverProbability": 0.75
35157
//"fitnessTypeName": "Optimization.ConfiguredFitness",
@@ -39,4 +161,4 @@
39161
// "resultKey": "Beta",
40162
// "name": "MinimizeBeta"
41163
//}
42-
}
164+
}

‎Optimization/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<package id="Accord.MachineLearning" version="3.3.0" targetFramework="net45" />
55
<package id="Accord.Math" version="3.3.0" targetFramework="net45" />
66
<package id="Accord.Statistics" version="3.3.0" targetFramework="net45" />
7+
<package id="DynamicExpresso.Core" version="1.3.3.5" targetFramework="net45" />
78
<package id="GeneticSharp" version="1.0.100" targetFramework="net45" />
89
<package id="HelperSharp" version="0.0.4.2" targetFramework="net45" />
910
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />

2 commit comments

Comments
 (2)

jameschch commented on Jun 11, 2017

@jameschch

I've now adapted your approach to splitting log files in my fork. Thanks for the suggestion.

Jay-Jay-D commented on Jun 11, 2017

@Jay-Jay-D
OwnerAuthor

Hey James!

Glad to help.

I have in my todo list to clean my fork and make a PR.

Please sign in to comment.