Overall Statistics |
Total Trades 1 Average Win 0% Average Loss 0% Compounding Annual Return -0.231% Drawdown 16.400% Expectancy 0 Net Profit 0% Sharpe Ratio -0.049 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.001 Beta -0.01 Annual Standard Deviation 0.026 Annual Variance 0.001 Information Ratio -0.272 Tracking Error 0.167 Treynor Ratio 0.122 Total Fees $0.00 |
#------------------------------------------------------------------------------- # example of rolling windows and consolidators in python #------------------------------------------------------------------------------- import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") # #import numpy as np #import pandas as pd #import datetime as dt #import statsmodels.formula.api as sm #import statsmodels.tsa.stattools as ts #import statsmodels.formula.api as sm # from System.Collections.Generic import List from scipy import stats from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * import decimal as d from QuantConnect.Data.Consolidators import * from QuantConnect.Data.Market import * from datetime import timedelta #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Class Forex class Forex(QCAlgorithm): #--------------------------------------------------------------------------- # init class def __init__(self): self.Debug("__init__:") #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- # init some vars def Initialize(self): self.Debug("Initialize:") self.SetStartDate(2006,1,1) self.SetEndDate(2016,1,2) self.SetCash(10000) # add symbols self.symbols = ['EURUSD', 'GBPJPY'] self.assign(self.symbols) # Rolling Window self.SetWarmup(timedelta(400)) self.HIST = 200 self.FHDW = RollingWindow[QuoteBar](self.HIST) self.OMDW = RollingWindow[QuoteBar](self.HIST) # 4 hour studies # Rolling Indicator Windows self.emaSW = RollingWindow[IndicatorDataPoint](self.HIST) self.emaFW = RollingWindow[IndicatorDataPoint](self.HIST) # indicator self.slow = ExponentialMovingAverage(200) self.fast = ExponentialMovingAverage(2) # update rolling Windows self.slow.Updated += self.emaSlowUpdated self.fast.Updated += self.emaFastUpdated #define consolidator con4h = QuoteBarConsolidator(timedelta(minutes=240)) #attach our event handler con4h.DataConsolidated += self.FourHourBarHandler # add our consolidator to manager self.SubscriptionManager.AddConsolidator(self.symbols[1],con4h) # Register self.RegisterIndicator(self.symbols[1], self.slow, con4h) self.RegisterIndicator(self.symbols[1], self.fast, con4h) self.previous_bar = None # define a small tolerance to avoid bouncing self.tolerance = 0.00010 #charts stockPlot = Chart('Plot 0') stockPlot.AddSeries(Series('emaSW', SeriesType.Line,0)) stockPlot.AddSeries(Series('emaFW', SeriesType.Line,0)) #stockPlot.AddSeries(Series('FHDW', SeriesType.Line,0)) self.AddChart(stockPlot) self.dbg = False #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- def FourHourBarHandler(self, sender, bar): #if (self.dbg): self.Debug("FourHourBarHandler:") # add quotebar to rolling window self.FHDW.Add(bar) # Wait for windows to be ready. if not (self.FHDW.IsReady): return # Wait for windows to be ready. if not (self.emaSW.IsReady and self.emaFW.IsReady): return holdings0 = self.Portfolio[self.symbols[0]].Quantity holdings1 = self.Portfolio[self.symbols[1]].Quantity weighting = 1.0 / self.Securities.Count profit = self.Portfolio.TotalUnrealizedProfit # conditions if self.emaFW[0].Value > self.emaSW[0].Value * d.Decimal(1 + self.tolerance): up0=True if self.emaFW[0].Value < self.emaSW[0].Value * d.Decimal(1 + self.tolerance): up0=False if self.emaFW[0].Value > self.emaSW[0].Value * d.Decimal(1 + self.tolerance): dn0=False if self.emaFW[0].Value < self.emaSW[0].Value * d.Decimal(1 + self.tolerance): dn0=True if self.emaFW[1].Value > self.emaSW[1].Value * d.Decimal(1 + self.tolerance): up1=True if self.emaFW[1].Value < self.emaSW[1].Value * d.Decimal(1 + self.tolerance): up1=False if self.emaFW[1].Value > self.emaSW[1].Value * d.Decimal(1 + self.tolerance): dn1=False if self.emaFW[1].Value < self.emaSW[1].Value * d.Decimal(1 + self.tolerance): dn1=True if profit > 10.00: pr0=True elif profit < 10.00: pr0=False # Trades if holdings1 == 0: if dn1 and up0: #self.Log("Buy >> {0}".format(self.Securities[self.symbols[1]].Price)) self.SetHoldings(self.symbols[1], weighting) elif up1 and dn0: #self.Log("Sell >> {0}".format(self.Securities[self.symbols[1]].Price)) self.SetHoldings(self.symbols[1], -weighting) elif holdings1 >0: if not up1: #self.Log("exit long >> {0}".format(self.Securities[self.symbols[1]].Price)) self.SetHoldings(self.symbols[1], 0) if (self.Portfolio.TotalUnrealizedProfit > 10.00): self.Debug("Profit: "+str(profit)) self.Liquidate() elif holdings1 <0: if not dn1: #self.Log("exit short >> {0}".format(self.Securities[self.symbols[1]].Price)) self.SetHoldings(self.symbols[1], 0) if (self.Portfolio.TotalUnrealizedProfit > 10.00): self.Debug("Profit: "+str(profit)) self.Liquidate() # #if profit>0: # self.Debug("Profit: "+str(profit)) # pass #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- def emaSlowUpdated(self, sender, updated): if (self.dbg): self.Debug("emaSlowUpdated:") self.emaSW.Add(updated) #self.Debug("emaSlowUpdated[i]: "+str(self.emaSW[0])) pass #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- def emaFastUpdated(self, sender, updated): if (self.dbg): self.Debug("emaFastUpdated:") self.emaFW.Add(updated) #self.Debug("ema'FastUpdated[i]: "+str(self.emaFW[0])) pass #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- # add the pairs def assign(self,list): #if (self.dbg): self.Debug("assign:") for i in range(len(list)): currency = self.AddForex(list[i],Resolution.Minute,Market.Oanda,False,50) #self.Debug("currency "+str(currency)) pass #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- # every bar do something def OnData(self,data): #if (self.dbg): self.Debug("OnData:") #Add QuoteBar to RollingWindow #self.OMDW.Add(data) #broken pass #--------------------------------------------------------------------------- #-------------------------------------------------------------------------------