Overall Statistics Total Trades0Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Probabilistic Sharpe Ratio0%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio-0.724Tracking Error0.259Treynor Ratio0Total Fees\$0.00
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
#
# you may not use this file except in compliance with the License.
#
# Unless required by applicable law or agreed to in writing, software
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import numpy as np
import math

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="using quantconnect" />
### <meta name="tag" content="trading and orders" />
class TosAlgorithm(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''

def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.SetStartDate(2019, 3, 31)  # Set Start Date
self.SetEndDate(2020, 8, 30)  # Set Start Date
self.SetCash(100000)  # Set Strategy Cash
self.ST_Period = 5
self.ST_Coeff = 1
self.SetWarmUp(self.ST_Period)
self.superTrend = SuperTrend('custom', self.ST_Period,self.ST_Coeff) # suepr trend
self.lastSuperTrendTrend = 0 # the last self.superTrend.Trend
#self.PlotIndicator("ST",self.superTrend)

def OnData(self, data):
return
self.superTrend.Update(data["IBM"], self.ibm[1])
self.Debug(" time:" + str(self.Time) +
" open:" + str(self.superTrend.o) +
" high:" + str(self.superTrend.h) +
" low:" +  str(self.superTrend.l) +
" close:" + str(self.superTrend.c) +
" ha_close:" + str(round(self.superTrend.ha_close,2)) +
" ha_open:"  +  str(round(self.superTrend.ha_open,2)) +
" ha_high:"  +  str(round(self.superTrend.ha_high,2)) +
" ha_low:"   +  str(round(self.superTrend.ha_low,2)) +
" hahl2:"    +  str(round(self.superTrend.hahl2_1,2)) +
" tmpUp1:"   +  str(round(self.superTrend.tmpUp1,2)) +
" tmpDn1:"   +  str(round(self.superTrend.tmpDn1,2)) +
" finalUp1:"   +  str(round(self.superTrend.finalUp1,2)) +
" finalDn1:"   +  str(round(self.superTrend.finalDn1,2)) +
" atr:"      +  str(self.superTrend.atr) +
" super trend:" + str(self.superTrend.trendDir1)
)
if self.lastSuperTrendTrend == -1 and self.superTrend.trendDir1 == 1:
if self.Portfolio.Invested:
self.Liquidate()
self.SetHoldings("IBM", 1)
self.Debug("Long")
if self.lastSuperTrendTrend == 1 and self.superTrend.trendDir1 == -1:
if self.Portfolio.Invested:
self.Liquidate()
self.SetHoldings("IBM", -1)
self.Debug("Short")
self.lastSuperTrendTrend = self.superTrend.trendDir1

class SuperTrend:
def __init__(self, name, period,coeff):
self.Name = name
self.o = 0
self.h = 0
self.l = 0
self.c = 0
self.ha_close = 0
self.ha_open = 0
self.ha_high = 0
self.ha_low = 0
self.hahl2_1 = 0
self.trendDir1 = 0
self.tmpUp1 = 0
self.tmpDn1 = 0
self.atr = AverageTrueRange(period, MovingAverageType.Wilders)
self.test= 0
self.finalUp1 = 0
self.finalDn1 = 0
self.coeff = coeff
self.qca = QCAlgorithm()
self.result = 0

def Update(self, input, lastBar):
self.atr.Update(input)
self.test = 0
self.finalUp1 = self.tmpUp1
self.finalDn1 = self.tmpDn1

self.o  =  input.Open         ## Open price
self.h  =  input.High         ## High price
self.l  =  input.Low          ## Low price
self.c  =  input.Close        ## Close price

self.ha_close = (self.o + self.h + self.l + self.c) / 4.0
self.ha_open = (lastBar.Open + self.ha_close) / 2.0
self.ha_high = Math.Max(self.o, Math.Max(self.ha_open, self.ha_close))
self.ha_low = Math.Min(self.l, Math.Min(self.ha_open, self.ha_close))
self.hahl2_1 = (self.ha_high  + self.ha_low) / 2
#hl2 = ((input.High - input.Low) / 2 ) + input.Low
self.tmpUp1 = self.hahl2_1 + (self.atr.Current.Value * self.coeff)
self.tmpDn1 = self.hahl2_1 - (self.atr.Current.Value * self.coeff)

if lastBar.Close > self.finalUp1:
self.finalUp1 = max(self.tmpUp1, self.finalUp1)
else:
self.finalUp1 = self.tmpUp1

if lastBar.Close < self.finalDn1:
self.finalDn1 = min(self.tmpDn1, self.finalDn1)
else:
self.finalDn1 = self.tmpDn1

# calculate trendDir1

if input.Close > self.finalDn1:
self.trendDir1 = 1
elif input.Close < self.finalUp1:
self.trendDir1 = -1

if not math.isnan(self.trendDir1):
self.trendDir = self.trendDir1
else:
self.trendDir1 = 1

#if self.finalUp1 != 0 and input.Close > self.finalUp1:
#    self.trendDir1 = 1
#if self.finalDn1 != 0 and input.Close < self.finalDn1:
#   self.trendDir1 = -1
self.IsReady = self.trendDir1 != 0