C#でIronPythonスクリプトを呼び出す
C#からIronPythonスクリプトを呼び出してみました。
呼び出すIronPythonスクリプト。スクリプトの内容は何でも構いませんが、ここでは自作のスクリプトを呼び出してみます。
import System
def 名前():
return "外為どっとコム"
def 通貨ペア():
return [ "AUDJPY", "AUDUSD", "CADJPY", "CHFJPY", "EURGBP", "EURJPY", "EURUSD", "GBPCHF", "GBPJPY", "GBPUSD", "HKDJPY", "NZDJPY", "NZDUSD", "USDCAD", "USDCHF", "USDJPY", "ZARJPY" ]
def 開場時間(年月日):
年 = int.Parse(年月日[0] + 年月日[1] + 年月日[2] + 年月日[3])
月 = int.Parse(年月日[4] + 年月日[5])
日 = int.Parse(年月日[6] + 年月日[7])
if 月 < 3:
年 = 年 - 1
月 = 月 + 12
(年 + (int)(年 / 4) - (int)(年 / 100) + (int)(年 / 400) + (int)((13 * 月 + 8) / 5) + 日) % 7
if 年 == 2010:
if 月 > 11 and 日 > 8:
if 曜日 == 1:
return (7, 0)
elif 曜日 == 2:
return (7, 0)
elif 曜日 == 3:
return (7, 0)
elif 曜日 == 4:
return (7, 0)
elif 曜日 == 5:
return (7, 0)
else:
return None
else:
if 曜日 == 1:
return (7, 0)
elif 曜日 == 2:
return (6, 0)
elif 曜日 == 3:
return (6, 0)
elif 曜日 == 4:
return (6, 0)
elif 曜日 == 5:
return (6, 0)
else:
return None
else:
return None
def 閉場時間(年月日):
年 = int.Parse(年月日[0] + 年月日[1] + 年月日[2] + 年月日[3])
月 = int.Parse(年月日[4] + 年月日[5])
日 = int.Parse(年月日[6] + 年月日[7])
if 月 < 3:
年 = 年 - 1
月 = 月 + 12
(年 + (int)(年 / 4) - (int)(年 / 100) + (int)(年 / 400) + (int)((13 * 月 + 8) / 5) + 日) % 7
if 年 == 2010:
if 月 > 11 and 日 > 8:
if 曜日 == 1:
return (6, 29)
elif 曜日 == 2:
return (6, 29)
elif 曜日 == 3:
return (6, 29)
elif 曜日 == 4:
return (6, 29)
elif 曜日 == 5:
return (6, 29)
else:
return None
else:
if 曜日 == 1:
return (5, 29)
elif 曜日 == 2:
return (5, 29)
elif 曜日 == 3:
return (5, 29)
elif 曜日 == 4:
return (5, 29)
elif 曜日 == 5:
return (5, 29)
else:
return None
else:
return None
def 取得可能年月日(通貨ペア):
取得可能年月日 = []
ディレクトリ = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(__file__), 通貨ペア)
if not System.IO.Directory.Exists(ディレクトリ):
return 取得可能年月日
else:
for ファイル in System.IO.Directory.GetFiles(ディレクトリ):
ファイル名 = System.IO.Path.GetFileName(ファイル)
if ファイル名.Length == 12 and ファイル名[8] == '.' and ファイル名[9] == 't' and ファイル名[10] == 'x' and ファイル名[11] == 't' and ファイル名[0] >= '0' and ファイル名[0] <= '9' and ファイル名[1] >= '0' and ファイル名[1] <= '9' and ファイル名[2] >= '0' and ファイル名[2] <= '9' and ファイル名[3] >= '0' and ファイル名[3] <= '9' and ファイル名[4] >= '0' and ファイル名[4] <= '9' and ファイル名[5] >= '0' and ファイル名[5] <= '9' and ファイル名[6] >= '0' and ファイル名[6] <= '9' and ファイル名[7] >= '0' and ファイル名[7] <= '9':
取得可能年月日.append(ファイル名.Substring(0, 8))
return 取得可能年月日
def 歩み値(通貨ペア, 年月日):
sb = System.Text.StringBuilder()
ファイル名 = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(__file__), 通貨ペア, 年月日 + ".txt")
if not System.IO.File.Exists(ファイル名):
return sb.ToString()
else:
for 行 in System.IO.File.ReadAllLines(ファイル名):
分割 = 行.Split(' ')
sb.Append(分割[0] + " " + 分割[1] + ":00 " + 分割[2] + System.Environment.NewLine)
return sb.ToString()
呼び出し側のプログラム。
このように簡単に呼び出すことができます。ただし、IronPython
アセンブリ、Microsoft.Scripting
アセンブリへの参照を追加する必要があります。
OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Pythonスクリプト(*.py)|*.py" };
if (openFileDialog.ShowDialog() == true)
{
dynamic スクリプト = python.UseFile(openFileDialog.FileName);
dynamic 通貨ペア = スクリプト.通貨ペア();
for (int i = 0; i < 通貨ペア.Count; i++)
Console.WriteLine(通貨ペア[i]);
Console.WriteLine();
dynamic 取得可能年月日 = スクリプト.取得可能年月日("USDJPY");
for (int i = 0; i < 取得可能年月日.Count; i++)
Console.WriteLine(取得可能年月日[i]);
dynamic 歩み値 = スクリプト.歩み値("USDJPY", "20101014");
Console.WriteLine(歩み値);
}

スポンサーリンク