automated trading - code vba 1


/ Published in: Visual Basic
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Option Explicit
  2.  
  3. Public dTime5min As Date
  4. Public dTime5sec As Date
  5.  
  6. Public bTimeTick As Boolean
  7. Public dTimeTick As Date
  8.  
  9. Sub Timer5min()
  10. Dim h As Integer, m As Integer
  11. Dim isell As Integer
  12. Dim ibuy As Integer
  13. Dim bCont As Boolean
  14. Dim dt As Date
  15. Dim min As Integer
  16. Dim lot As Integer
  17. Dim lot_s As Integer
  18.  
  19. ' Break real-time calculation
  20. On Error Resume Next
  21. Application.OnTime EarliestTime:=dTime5sec, Procedure:="Timer5sec", Schedule:=False
  22. shtDDE.Range("F2").Value = 0
  23. On Error GoTo 0
  24.  
  25. ' Set new 5-min event
  26. dTime5min = Now + TimeValue("00:05:00")
  27. dTime5min = dTime5min - TimeValue("00:00:" & itoa00(Second(dTime5min)))
  28.  
  29. min = Minute(dTime5min)
  30. min = min - Int(min / 5) * 5
  31.  
  32. dTime5min = dTime5min - TimeValue("00:" & itoa00(min) & ":00")
  33.  
  34. Application.OnTime dTime5min, "Timer5min"
  35.  
  36. ' Update sheet
  37. shtDDE.Calculate
  38.  
  39. If IsNumeric(shtDDE.Range("B2").Value) = True And _
  40. IsNumeric(shtDDE.Range("C2").Value) = True And _
  41. IsDate(shtDDE.Range("D2").Value) = True And _
  42. IsDate(shtDDE.Range("E2").Value) = True Then
  43.  
  44. ' Update DB
  45. h = Hour(shtDDE.Range("D2").Value)
  46. m = Minute(shtDDE.Range("D2").Value)
  47. If (h < 8) Or (h > 22) Or (h = 22 And m > 0) Then
  48. ' DAX is closed
  49. Else
  50. bCont = True
  51. If bTimeTick = True Then
  52. If dTimeTick = shtDDE.Range("D2").Value Then
  53. bCont = False
  54. End If
  55. End If
  56.  
  57. 'If dTimeTick <> shtDDE.Range("D2").Value Then
  58. If bCont Then
  59. Application.ScreenUpdating = False
  60.  
  61. ' main algo
  62. isell = shtTradingDAX.Range("isell").Value
  63. ibuy = shtTradingDAX.Range("ibuy").Value
  64. lot = shtTradingDAX.Range("lot_enter").Value
  65. lot_s = shtTradingDAX.Range("lot_enter_s").Value
  66.  
  67. Call DBDaxUpdate2
  68.  
  69. ' play sound buy/sell
  70. If isell = 1 And CInt(shtTradingDAX.Range("isell").Value) = 0 Then
  71.  
  72. ' play sound "BUY"
  73.  
  74. 'If lot_s > 0 Then
  75. PlayWavFile "buy.wav", False
  76. 'End If
  77.  
  78. If lot_s > 0 Then
  79. ' send signal to the web server
  80. ' undo...
  81.  
  82. ' Call WebServer_SendSignal( _
  83.   ' shtDDE.Range("E2").Value, shtDDE.Range("D2").Value, _
  84.   ' "AX", "buy", shtDDE.Range("B2").Value)
  85. End If
  86. End If
  87.  
  88. If isell = 0 And CInt(shtTradingDAX.Range("isell").Value) = 1 Then
  89.  
  90. ' play sound "SELL"
  91.  
  92. 'If shtTradingDAX.Range("lot_enter_s").Value > 0 Then
  93. PlayWavFile "sell.wav", False
  94. 'End If
  95.  
  96. If shtTradingDAX.Range("lot_enter_s").Value > 0 Then
  97. ' send signal to the web server
  98. ' undo...
  99.  
  100. ' Call WebServer_SendSignal( _
  101.   ' shtDDE.Range("E2").Value, shtDDE.Range("D2").Value, _
  102.   ' "AX", "sell", shtDDE.Range("B2").Value)
  103. End If
  104. End If
  105.  
  106. If ibuy = 1 And CInt(shtTradingDAX.Range("ibuy").Value) = 0 Then
  107.  
  108. ' play sound "SELL"
  109.  
  110. 'If lot > 0 Then
  111. PlayWavFile "sell.wav", False
  112. 'End If
  113.  
  114. If lot > 0 Then
  115. ' send signal to the web server
  116. ' undo...
  117.  
  118. ' Call WebServer_SendSignal( _
  119.   ' shtDDE.Range("E2").Value, shtDDE.Range("D2").Value, _
  120.   ' "AX", "sell", shtDDE.Range("B2").Value)
  121. End If
  122. End If
  123.  
  124. If ibuy = 0 And CInt(shtTradingDAX.Range("ibuy").Value) = 1 Then
  125.  
  126. ' play sound "BUY"
  127.  
  128. 'If shtTradingDAX.Range("lot_enter").Value > 0 Then
  129. PlayWavFile "buy.wav", False
  130. 'End If
  131.  
  132. If shtTradingDAX.Range("lot_enter").Value > 0 Then
  133. ' send signal to the web server
  134. ' undo...
  135.  
  136. ' Call WebServer_SendSignal( _
  137.   ' shtDDE.Range("E2").Value, shtDDE.Range("D2").Value, _
  138.   ' "AX", "buy", shtDDE.Range("B2").Value)
  139. End If
  140. End If
  141.  
  142. ' real-time
  143. If shtDDE.Range("F2").Value <> 0 Then
  144. ' Start real-time calculation
  145. dTime5sec = Now + TimeValue("00:00:05")
  146. Application.OnTime dTime5sec, "Timer5sec"
  147. End If
  148.  
  149. Application.ScreenUpdating = True
  150. End If
  151. End If
  152. End If
  153.  
  154. ' save a new tick date
  155. Call SaveLastTickDate
  156. End Sub
  157.  
  158. Sub Timer5sec()
  159. dTime5sec = Now + TimeValue("00:00:05")
  160. Application.OnTime dTime5sec, "Timer5sec"
  161.  
  162. ' Update sheet
  163. shtDDE.Calculate
  164.  
  165. ' Real-time calculation
  166. Application.ScreenUpdating = False
  167.  
  168. If shtDDE.Range("F2").Value = 1 Then
  169. '--------------------------------------
  170. '--------------------------------------
  171. shtDDE.Range("F2").Value = 0
  172. '--------------------------------------
  173. '--------------------------------------
  174. 'Call RealTimeCheck(shtDAX, "B2", shtTradingDAX)
  175. End If
  176.  
  177. If shtDDE.Range("F2").Value = 0 Then
  178.  
  179. ' Break real-time calculation
  180. On Error Resume Next
  181. Application.OnTime EarliestTime:=dTime5sec, Procedure:="Timer5sec", Schedule:=False
  182. On Error GoTo 0
  183.  
  184. End If
  185.  
  186. Application.ScreenUpdating = True
  187. End Sub
  188.  
  189. Sub MyTimerOff()
  190. On Error Resume Next
  191. Application.OnTime EarliestTime:=dTime5min, Procedure:="Timer5min", Schedule:=False
  192. Application.OnTime EarliestTime:=dTime5sec, Procedure:="Timer5sec", Schedule:=False
  193. On Error GoTo 0
  194. End Sub
  195.  
  196. Public Sub btnStopTimer()
  197. Call MyTimerOff
  198. MsgBox "5min timer is stoped now. Use ""Recovery"" button to restart it"
  199. End Sub
  200.  
  201. Public Sub btnStartTimer(Optional bShowMsg As Boolean = True)
  202. ' kill timers
  203. Call MyTimerOff
  204.  
  205. ' start a new 5min timer
  206. Dim min As Integer
  207. min = Minute(Now)
  208. min = min - 5 * Int(min / 5)
  209. dTime5min = Now + TimeValue("00:0" & Trim(CStr(5 - min)) & ":00")
  210.  
  211. Dim sec As Integer
  212. sec = Second(dTime5min)
  213.  
  214. dTime5min = dTime5min - TimeValue("00:00:" & itoa00(sec))
  215.  
  216. Application.OnTime dTime5min, "Timer5min"
  217.  
  218. ' get last DDE time value
  219. Call SaveLastTickDate
  220.  
  221. If bShowMsg = True Then
  222. MsgBox "5min timer has been started"
  223. End If
  224. End Sub
  225.  
  226. Sub SaveLastTickDate()
  227. If IsDate(shtDDE.Range("D2").Value) = False Then
  228. bTimeTick = False
  229. Else
  230. bTimeTick = True
  231. dTimeTick = shtDDE.Range("D2").Value
  232. End If
  233. End Sub
  234.  
  235. Sub btnCloseMonth()
  236. Call DBEndOfMonth("B2", shtTradingDAX)
  237. MsgBox "Done"
  238. End Sub
  239.  
  240. Sub DBDaxUpdate2()
  241. Call UpdateDB(shtDAX, "B2", shtTradingDAX)
  242.  
  243. Dim sFile As String
  244. sFile = ThisWorkbook.Path & "\DAX-" & Trim(CStr(Year(shtDDE.Range("B2").Cells(1, 4).Value))) & "-"
  245. If Month(shtDDE.Range("B2").Cells(1, 4).Value) < 10 Then
  246. sFile = sFile & "0"
  247. End If
  248. sFile = sFile & Trim(CStr(Month(shtDDE.Range("B2").Cells(1, 4).Value))) & ".txt"
  249.  
  250. 'sFile = ...\DAX-YYYY-MM.txt
  251. 'Call WriteToFile(sFile, shtDAX, "B2")
  252.  
  253. 'sFile = ...\DAX.txt
  254. sFile = ThisWorkbook.Path & "\DAX.txt"
  255. 'Call WriteToFile(sFile, shtDAX, "B2")
  256. End Sub
  257.  
  258. Function FileExists(ByVal FileSpec As String) As Boolean
  259. Dim Attr As Long
  260. On Error Resume Next
  261. Attr = GetAttr(FileSpec)
  262. If Err.Number = 0 Then
  263. FileExists = Not ((Attr And vbDirectory) = vbDirectory)
  264. End If
  265. End Function
  266.  
  267. Sub WriteToFile(sFileName As String, sht As Worksheet, sRange As String)
  268. Dim iFile As Integer
  269.  
  270. Dim fLast As Double
  271. Dim iVolume As Long
  272. Dim sTime As String
  273. Dim sDate As String
  274.  
  275. Dim signalBB As Integer
  276. Dim signalBS As Integer
  277. Dim signalSS As Integer
  278. Dim signalSB As Integer
  279.  
  280. Dim equity_perday As Variant
  281. Dim equitys_perday As Variant
  282.  
  283. ' open text file to writenew signal
  284. iFile = FreeFile
  285. If FileExists(sFileName) Then
  286. ' append to file
  287. Open sFileName For Append Shared As iFile
  288. Else
  289. ' make new file
  290. Open sFileName For Output As iFile
  291. End If
  292.  
  293. fLast = shtDDE.Range(sRange).Cells(1, 1).Value
  294. iVolume = shtDDE.Range(sRange).Cells(1, 2).Value
  295.  
  296. sTime = WorksheetFunction.Text(shtDDE.Range(sRange).Cells(1, 3).Value, "hh:mm:ss")
  297. sDate = WorksheetFunction.Text(shtDDE.Range(sRange).Cells(1, 4).Value, "dd.mm.yyyy")
  298.  
  299. signalBB = 0
  300. If sht.Range("A1").End(xlDown).Cells(1, 29).Value Then signalBB = 1
  301.  
  302. signalBS = 0
  303. If sht.Range("A1").End(xlDown).Cells(1, 30).Value Then signalBS = 1
  304.  
  305. signalSS = 0
  306. If sht.Range("A1").End(xlDown).Cells(1, 31).Value Then signalSS = 1
  307.  
  308. signalSB = 0
  309. If sht.Range("A1").End(xlDown).Cells(1, 32).Value Then signalSB = 1
  310.  
  311. equity_perday = shtTradingDAX.Range("equity_perday").Value
  312. equitys_perday = shtTradingDAX.Range("equitys_perday").Value
  313.  
  314. Write #iFile, fLast, iVolume, sTime, sDate, signalBB, signalBS, signalSS, signalSB, equity_perday, equitys_perday
  315. Close #iFile
  316. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.