Module

Last-modified: 2007-09-02 (日) 00:09:23

Setup

データワークシートの一番左端のセル(0,0)に銘柄名を記入しておく。
それによって、倍率、呼び値、手数料を変更する。

 
Sub Setup
	Select Case DATA.getCellByPosition(0, 0).String
		Case "T-Gasoline"
			UNIT = 50  : TICK = 10 : COST = 500
		Case "T-Ruber"
			UNIT = 5000: TICK = 0.1: COST = 500
		Case "T-Platinum"
			UNIT = 500 : TICK = 1  : COST = 500
		Case "N255F"
			UNIT = 1000: TICK = 10 : COST = 1000
		Case "N255F-Mini"
			UNIT = 100 : TICK = 5  : COST = 200
	End Select
End Sub
 
 

ClearData

 
Sub ClearData(COL1 As Integer, COL2 As Integer)
	DATA.getCellRangeByPosition(COL1, START_ROW, COL2, END_ROW).clearContents(com.sun.star.sheet.CellFlags.VALUE)
End Sub
 
 

NewSheet

 
Sub NewSheet(P1 As String, P2 As String, P3 As String, P4 As String, P5 As String, P6 As String, P7 As String)
	ScreenUpdatingOff()
	RSLT_ROW = 1
	ThisComponent.Sheets.insertNewByName(Format(Hour(Now()),"00") & Format(Minute(Now()),"00") & Format(Second(Now()),"00"), 0)
	Set RSLT = ThisComponent.Sheets(0)
	RSLT.getCellByPosition( 1, 0).String = Now()
	RSLT.getCellByPosition( 3, 0).String = P1
	RSLT.getCellByPosition( 5, 0).String = P2
	RSLT.getCellByPosition( 6, 0).String = P3
	RSLT.getCellByPosition( 7, 0).String = P4
	RSLT.getCellByPosition( 8, 0).String = P5
	RSLT.getCellByPosition( 9, 0).String = P6
	RSLT.getCellByPosition(10, 0).String = P7
	For SHEET_NUM = 1 To ThisComponent.Sheets.getCount() - 1
		Set DATA = ThisComponent.Sheets(SHEET_NUM)
		If DATA.getCellByPosition(0, 0).String <> Empty Then
			Setup()
			Exit Sub
		End If
	Next SHEET_NUM
End Sub
 
 

ScreenUpdatingOff

 
Sub ScreenUpdatingOff
	ThisComponent.addActionLock
	ThisComponent.lockControllers
	ThisComponent.CurrentController.Frame.ContainerWindow.Enable = False
End Sub
 
 

ScreenUpdatingOn

 
sub ScreenUpdatingOn
	ThisComponent.removeActionLock
	ThisComponent.unLockControllers
	ThisComponent.CurrentController.Frame.ContainerWindow.Enable = True
end sub
 
 

Entry

 
Sub Entry(ENTRY_PRICE As Single, SL As String)
	Dim oLocale As New com.sun.star.lang.Locale
	If RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String <> Empty Then Exit Sub
	If SL = "LongEntry"  And DATA.getCellByPosition(HIGH_F_COL, DATA_ROW).String = "H" Then Exit Sub
	If SL = "ShortEntry" And DATA.getCellByPosition(LOW_F_COL , DATA_ROW).String = "L" Then Exit Sub
	RSLT.getCellByPosition(R_DATE_COL , RSLT_ROW).String = DATA.getCellByPosition(DATE_COL , DATA_ROW).String
 	RSLT.getCellByPosition(R_CRACT_COL, RSLT_ROW).String = DATA.getCellByPosition(CRACT_COL, DATA_ROW).String
	RSLT.getCellByPosition(R_TIME1_COL, RSLT_ROW).String = DATA.getCellByPosition(TIME_COL , DATA_ROW).String
	RSLT.getCellByPosition(R_ENTRY_COL, RSLT_ROW).Value  = ENTRY_PRICE
	RSLT.getCellByPosition(R_SL_COL   , RSLT_ROW).String = SL
	RSLT.getCellByPosition(R_ENTRY_COL, RSLT_ROW).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0.0", oLocale, True)
End Sub
 
 

Cover

 
Sub Cover(COVER_PRICE As Single, SL As String)
	Dim ENTRY_PRICE As Single: ENTRY_PRICE = RSLT.getCellByPosition(R_ENTRY_COL, RSLT_ROW).Value
	Dim oLocale     As New com.sun.star.lang.Locale
	If RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String = Empty Then Exit Sub
	If RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String = "LongEntry"  And SL = "ShortCover" Then Exit Sub
	If RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String = "ShortEntry" And SL = "LongCover"  Then Exit Sub
	RSLT.getCellByPosition(R_TIME2_COL, RSLT_ROW).String = DATA.getCellByPosition(TIME_COL, DATA_ROW).String
	RSLT.getCellByPosition(R_COVER_COL, RSLT_ROW).Value  = COVER_PRICE
	If RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String = "LongEntry" Then
		RSLT.getCellByPosition(R_PL_COL, RSLT_ROW).Value = (COVER_PRICE - ENTRY_PRICE) * UNIT
	ElseIf RSLT.getCellByPosition(R_SL_COL, RSLT_ROW).String = "ShortEntry" Then
		RSLT.getCellByPosition(R_PL_COL, RSLT_ROW).Value = (ENTRY_PRICE - COVER_PRICE) * UNIT
	End If
	RSLT.getCellByPosition(R_COVER_COL, RSLT_ROW).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0.0", oLocale, True)
	RSLT.getCellByPosition(R_PL_COL   , RSLT_ROW).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
	RSLT_ROW = RSLT_ROW + 1
End Sub
 
 

TerminalTreatment

 
Sub TerminalTreatment()
	Dim PROFIT_COUNT As Integer
	Dim LOSS_COUNT   As Integer
	Dim RANGE        As Object
	Dim oLocale      As New com.sun.star.lang.Locale
	RANGE = ThisComponent.Sheets(0).getCellRangeByPosition(R_PL_COL, 1, R_PL_COL, 65535)
	PROFIT_COUNT = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("COUNTIF", Array(RANGE, ">0"))
	LOSS_COUNT   = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("COUNTIF", Array(RANGE, "<=0"))
	With RSLT
			.getCellByPosition(10,  1).String = "損益合計"
			.getCellByPosition(10,  2).String = "利益合計"
			.getCellByPosition(10,  3).String = "損失合計"
			.getCellByPosition(10,  4).String = "利益回数"
			.getCellByPosition(10,  5).String = "損失回数"
			.getCellByPosition(10,  6).String = "損益/回数"
			.getCellByPosition(10,  7).String = "利益平均"
			.getCellByPosition(10,  8).String = "損失平均"
			.getCellByPosition(10,  9).String = "最大利益"
			.getCellByPosition(10, 10).String = "最大損失"
			.getCellByPosition(10, 11).String = "プロフィットファクター"
			.getCellByPosition(10, 12).String = "ペイオフレシオ"
			.getCellByPosition(10, 13).String = "勝率"
			.getCellByPosition(10, 14).String = "手数料"
			.getCellByPosition(10, 15).String = "TLレシオ"
			.getCellByPosition(11,  1).Value  = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("SUM", Array(RANGE)) - COST * (PROFIT_COUNT + LOSS_COUNT)
			.getCellByPosition(11,  2).Value  = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("SUMIF", Array(RANGE, ">0"))  - COST * PROFIT_COUNT
			.getCellByPosition(11,  3).Value  = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("SUMIF", Array(RANGE, "<=0")) - COST * LOSS_COUNT
			.getCellByPosition(11,  4).Value  = PROFIT_COUNT
			.getCellByPosition(11,  5).Value  = LOSS_COUNT
			.getCellByPosition(11,  6).Value  = .getCellByPosition(11, 1).Value / (.getCellByPosition(11, 4).Value + .getCellByPosition(11, 5).Value)
			.getCellByPosition(11,  7).Value  = .getCellByPosition(11, 2).Value / .getCellByPosition(11, 4).Value
			.getCellByPosition(11,  8).Value  = .getCellByPosition(11, 3).Value / .getCellByPosition(11, 5).Value
			.getCellByPosition(11,  9).Value  = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("MAX", Array(RANGE)) - COST
			.getCellByPosition(11, 10).Value  = CreateUnoService("com.sun.star.sheet.FunctionAccess").callFunction("MIN", Array(RANGE)) - COST
			.getCellByPosition(11, 11).Value  = .getCellByPosition(11, 2).Value / .getCellByPosition(11, 3).Value * -1
			.getCellByPosition(11, 12).Value  = .getCellByPosition(11, 7).Value / .getCellByPosition(11, 8).Value * -1
			.getCellByPosition(11, 13).Value  = .getCellByPosition(11, 4).Value / (.getCellByPosition(11, 4).Value + .getCellByPosition(11, 5).Value)
			.getCellByPosition(11, 14).Value  = (.getCellByPosition(11, 4).Value + .getCellByPosition(11, 5).Value) * COST
			.getCellByPosition(11, 15).Value  = .getCellByPosition(11, 1).Value / .getCellByPosition(11, 3).Value * -1
			.getCellByPosition(11,  1).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
			.getCellByPosition(11,  2).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale , True)
			.getCellByPosition(11,  3).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
			.getCellByPosition(11,  4).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale , True)
			.getCellByPosition(11,  5).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale , True)
			.getCellByPosition(11,  6).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
			.getCellByPosition(11,  7).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale , True)
			.getCellByPosition(11,  8).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
			.getCellByPosition(11,  9).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale , True)
			.getCellByPosition(11, 10).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0;[RED]-#,##0", oLocale, True)
			.getCellByPosition(11, 11).NumberFormat = ThisComponent.NumberFormats.queryKey("0.00", oLocale, True)
			.getCellByPosition(11, 12).NumberFormat = ThisComponent.NumberFormats.queryKey("0.00", oLocale, True)
			.getCellByPosition(11, 13).NumberFormat = ThisComponent.NumberFormats.queryKey("0.00%", oLocale, True)
			.getCellByPosition(11, 14).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0", oLocale, True)
			.getCellByPosition(11, 15).NumberFormat = ThisComponent.NumberFormats.queryKey("#,##0.00;[RED]-#,##0.00", oLocale, True)
	End With
	RSLT.getCellByPosition( 8, 0).String = Now()
	ScreenUpdatingOn()
	MsgBox "バックテストが終了致しました。"
End Sub
 
 

CalculateTechnical

 
Sub CalculateTechnical
	ScreenUpdatingOff()
	For SHEET_NUM = 0 To ThisComponent.Sheets.getCount() - 1
		Set DATA = ThisComponent.Sheets(SHEET_NUM)
'		If DATA.getCellByPosition(0, 0).String <> Empty Then
		If DATA.getCellByPosition(1, 0).Value = 1 Then
			TechnicalSetup()
			Do Until DATA.getCellByPosition(DATE_COL, DATA_ROW).String = Empty
				SMA()
				MACD()
				Technical.ICHIMOKU()
				DATA_ROW = DATA_ROW + 1
			Loop
		End If
	Next SHEET_NUM
	ScreenUpdatingOn()
	MsgBox "テクニカル指数の計算が終了致しました。"
End Sub
 
 

TechnicalSetup

 
Sub TechnicalSetup
	DATA_ROW = START_ROW
	ClearData(SMA1_COL  , SMA3_COL  )
	ClearData(MACD_COL  , HIST_COL  )
	ClearData(TENKAN_COL, CHIKOU_COL)
	DATA.getCellByPosition(SMA1_COL  , 0).Value = SMA1_P / 5
	DATA.getCellByPosition(SMA2_COL  , 0).Value = SMA2_P / 5
	DATA.getCellByPosition(SMA3_COL  , 0).Value = SMA3_P / 5
	DATA.getCellByPosition(MACD_COL  , 0).Value = EMA1_P / 5
	DATA.getCellByPosition(SIGNAL_COL, 0).Value = EMA2_P / 5
	DATA.getCellByPosition(HIST_COL  , 0).Value = SIGNAL_P / 5
	DATA.getCellByPosition(TENKAN_COL, 0).Value = TENKAN_P / 5
	DATA.getCellByPosition(KIJYUN_COL, 0).Value = KIJYUN_P / 5
	DATA.getCellByPosition(SPAN1_COL , 0).Value = SPAN1_P / 5
	DATA.getCellByPosition(SPAN2_COL , 0).Value = SPAN2_P / 5
	DATA.getCellByPosition(CHIKOU_COL, 0).Value = CHIKOU_P / 5
End Sub