forked from slashadminsource/PSIBTrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IBStart.ps1
157 lines (131 loc) · 4.37 KB
/
IBStart.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using module '.\Modules\DataStorageManager.psm1'
Write-Host "################################################################"
Write-Host "#"
Write-Host "# Watchdog powershell script for IBTrader"
Write-Host "#"
Write-Host "# Script will start the data collector and ibot scripts"
Write-Host "# Will also restart both after the daily IB Program restarts"
Write-Host "#"
Write-Host "# Will also warn you if it detects issues collecting data"
Write-Host "#"
Write-Host "################################################################"
Write-Host ""
$global:dcProcess = $null
$global:ibProcess = $null
[DataStorageManager]$global:storage = New-Object DataStorageManager
function Start-Scripts()
{
Write-Host "Starting data collector"
$global:dcProcess = Start-Process powershell -argument ".\IBDataCollector.ps1 -AutoStart $true" -passthru
$global:dcProcess.id
Start-Sleep -Seconds 60
Write-Host "Done"
Write-Host "Starting bot"
$global:ibProcess = Start-Process powershell -argument ".\IBBot.ps1" -passthru
$global:ibPprocess.id
Start-Sleep -Seconds 60
Write-Host "Done"
}
function Restart-Scripts()
{
Write-Host "Stopping scripts ready for restart"
Stop-Process $global:dcProcess.Id
Stop-Process $global:ibProcess.Id
Write-Host "Done"
Write-Host "Waiting 20 minutes before restart"
Start-Sleep -Seconds 600
Write-Host "Done.. Restarting scripts"
Start-Scripts
}
function Start-DataCollectionChecks()
{
Write-Host "Starting data collection checks"
$stocks = $global:storage.GetStocks()
foreach($stock in $stocks)
{
Write-host "Stock:" $stock.symbol
$lastPrice = $global:storage.GetLastPriceDate($stock.symbol)
$currentTime = Get-Date
$diff = New-TimeSpan $lastPrice $currenttime
Write-host "Last price capture time:" $lastPrice -ForegroundColor Red
Write-host "current time:" $currentTime -ForegroundColor Red
Write-host "diff minutes:" $diff.TotalMinutes -ForegroundColor Red
if($diff.TotalMinutes -gt 30)
{
return $true
}
}
Write-Host "Done"
return $false
}
function Send-Mail()
{
$notificationToEmailAddress = "<enter your email address here>"
$notificationFromEmailAddress = "<enter from address here>"
$smtpServer = "<enter smtp server here>"
$smtpPort = "25"
$subject = "CRITICAL ALERT"
$body = "Critical alert triggered on bot"
Send-MailMessage -From $notificationFromEmailAddress -to $notificationToEmailAddress -Subject $subject -Body $body -SmtpServer $smtpServer -port $smtpPort
}
Function Check-BankHoliday
{
Param (
[CmdletBinding()]
[Parameter(Mandatory,ValueFromPipeline)][datetime]$Date
)
$bankHolidays = (Invoke-RestMethod -Uri "https://www.gov.uk/bank-holidays.json" -Method GET)
$bankHolidays.'england-and-wales'.events.date -contains (Get-Date $Date -Format "yyyy-MM-dd")
}
function IsTradingHours([DateTime] $dt)
{
$dt = $dt.AddHours(-5)
$hour = $dt.hour
$dayofWeek = $dt.DayofWeek.value__
if($hour -gt 9 -and $hour -lt 15 -and $dayofWeek -gt 0 -and $dayofweek -lt 6 )
{
#Write-Host "IN TRADING HOURS" -ForegroundColor Green
return $true
}
else
{
#Write-Host "OUTSIDE OF TRADING HOURS" -ForegroundColor Red
return $false
}
}
$running = $true
Start-Scripts
While($running)
{
$dt = Get-Date
if($dt.Hour -eq 6 -and $dt.Minute -gt 55)
{
Restart-Scripts
}
if(IsTradingHours $dt)
{
Write-Host "IN TRADING HOURS" -ForegroundColor Green
if(check-bankholiday $dt)
{
Write-Host "OUTSIDE OF TRADING HOURS (BANK HOLIDAY)" -ForegroundColor Green
}
else
{
$badResults = Start-DataCollectionChecks
if($badResults -eq $true)
{
#no data captured within 20 minutes so call for help
Write-Host "CALLING FOR HELP" -ForegroundColor Red
Send-Mail
Start-Sleep -Seconds 3600
}
}
}
else
{
Write-Host "OUTSIDE TRADING HOURS" -ForegroundColor Red
}
#dont hog cpu
Start-Sleep -Seconds 30
}