| Overall Statistics |
|
Total Trades 921 Average Win 1.27% Average Loss -0.73% Compounding Annual Return 19.741% Drawdown 14.900% Expectancy 0.087 Net Profit 29.049% Sharpe Ratio 0.794 Loss Rate 60% Win Rate 40% Profit-Loss Ratio 1.75 Alpha 0.184 Beta -1.211 Annual Standard Deviation 0.208 Annual Variance 0.043 Information Ratio 0.717 Tracking Error 0.208 Treynor Ratio -0.136 Total Fees $1703.85 |
import numpy as np
import pandas as pd
import datetime
class EClass:
def __init__(self,e):
self.e = e
self.ex_count = 0
self.highSearch = True
self.high = 0
self.low = 10000
self.pXm = [0]
self.highs = [0 for x in range(3)]
self.lows = [0 for x in range(3)]
class Instrument:
def __init__(self, instrumentPreferences, ticker):
self.tradeC = 0
self.elist = []
for e in instrumentPreferences:
self.elist.append(EClass(e))
self.ticker = ticker
def high_low_update(self, price):
for e in self.elist:
if price <= e.low:
e.low = price
elif price >= e.high:
e.high = price
def how_far(self,price, tick_size):
for e in self.elist:
if e.highSearch == True:
if ((e.high - price)/tick_size) > e.e:
e.highs.insert(0,round(e.high,4))
e.highSearch = False
e.high = price
e.low = price
e.ex_count += 1
if (e.ex_count)>1:
e.pXm.insert(0,round((e.highs[0]-e.lows[0])/tick_size))
else:
if ((price - e.low)/tick_size) > e.e:
e.highSearch = True
e.lows.insert(0,round(e.low,4))
e.high = price
e.low = price
e.ex_count += 1
if (e.ex_count)>1:
e.pXm.insert(0,round((e.lows[0]-e.highs[0])/tick_size))
return 0from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Securities import *
from datetime import timedelta
import pandas as pd
import numpy as np
from decimal import Decimal
import Deborah
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(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.Log("Initialize")
self.pref = ["GC", float(0.1), [60]]
self.SetStartDate(2017,1,1) #Set Start Date
self.SetEndDate(2018,6,1) #Set End Date
self.SetCash(50000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
nq = self.AddFuture(self.pref[0], Resolution.Minute)
self.Log("_________")
nq.SetFilter(timedelta(0), timedelta(182))
self.deborah_nq = Deborah.Instrument(self.pref[2],self.pref[0])
self.prev_len = len(self.deborah_nq.elist[0].pXm)
def OnData(self, slice):
for chain in slice.FutureChains:
# Get contracts expiring no earlier than in 90 days
contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(90), chain.Value))
# if there is any contract, trade the front contract
if len(contracts) == 0:
continue
front = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
self.deborah_nq.high_low_update(float(front.LastPrice))
self.deborah_nq.how_far(float(front.LastPrice),self.pref[1])
if len(self.deborah_nq.elist[0].pXm) > self.prev_len:
volume = (self.deborah_nq.elist[0].pXm[0]/abs(self.deborah_nq.elist[0].pXm[0]))*(-1)
if self.Portfolio.Invested:
self.Liquidate()
self.MarketOrder(front.Symbol , int(volume))
self.prev_len = len(self.deborah_nq.elist[0].pXm)