Overall Statistics
Total Trades
529
Average Win
0.04%
Average Loss
-0.02%
Compounding Annual Return
0.158%
Drawdown
0.900%
Expectancy
0.054
Net Profit
0.307%
Sharpe Ratio
0.3
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
1.81
Alpha
0.005
Beta
-0.225
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
-3.469
Tracking Error
0.004
Treynor Ratio
-0.006
Total Fees
$0.00
import decimal as d
import datetime
from datetime import timedelta

class ParameterizedAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016,2,1)  #Set Start Date
        self.SetEndDate(2017,12,30)    #Set End Date
        self.SetCash(1000)       #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        
        self.forex = self.AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Hour)
        self.chosenForex = self.forex.Symbol
        self.Securities[self.chosenForex].SetLeverage(20)
        
        self.Log("~~~~~~~~~~~LEVERAGE~~~~~~~~~~~~")
        self.Log(str(self.Securities[self.chosenForex].Leverage))
        self.Log("~~~~~~~~~~~OPEN HOURS~~~~~~~~~~~~")
        self.Log(str(self.Securities[self.chosenForex].Exchange.TradingDaysPerYear))

        self.HMAValue30 = self.HMA(self.chosenForex, 30, Resolution.Hour)
        self.HMAValue20 = self.HMA(self.chosenForex, 20, Resolution.Hour)
        self.HMAValue10 = self.HMA(self.chosenForex, 10, Resolution.Hour)
        self.PSARValue = self.PSAR(self.chosenForex, d.Decimal(0.002), d.Decimal(0.002), d.Decimal(0.2), Resolution.Hour)
        
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday, DayOfWeek.Friday), self.TimeRules.At(17, 0), Action(self.EveryWeekBeforeMarketClose))
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Sunday, DayOfWeek.Sunday), self.TimeRules.At(17, 0), Action(self.EveryWeekAfterMarketOpen))
        
        self.IsInvested = False
        self.StopLoss = None
        self.ProfitTake = None
        self.MarketIsOpen = False
        self.repeatSell = False
        self.repeatBuy = False
        self.HMA30History = []
        self.HMA20History = []
        self.HMA10History = []
        self.HMA30HistoryLength = 3
        self.HMA20HistoryLength = 3
        self.HMA10HistoryLength = 3
        self.PSARHistory =[]
        self.PSARHistoryLength = 3
        self.PriceHistory = []
        self.PriceHistoryLength = 3
        self.PreviousHMA = 0
        
        self.Log("USDJPY START")
        self.SetWarmUp(200)

    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
        
        currentPrice = data[self.chosenForex].Value
        self.currentPrice = data[self.chosenForex].Value
        
        if len(self.PriceHistory) >= self.PriceHistoryLength:
            self.PriceHistory.pop(0)
        
        self.PriceHistory.append(data[self.chosenForex].Value)
        
        if len(self.HMA30History) >= self.HMA30HistoryLength:
            self.HMA30History.pop(0)
        
        self.HMA30History.append(self.HMAValue30.Current.Value)
        
        if len(self.HMA20History) >= self.HMA20HistoryLength:
            self.HMA20History.pop(0)
        
        self.HMA20History.append(self.HMAValue20.Current.Value)
        
        if len(self.HMA10History) >= self.HMA10HistoryLength:
            self.HMA10History.pop(0)
        
        self.HMA10History.append(self.HMAValue10.Current.Value)
        
        if len(self.PSARHistory) >= self.PSARHistoryLength:
            self.PSARHistory.pop(0)
        
        self.PSARHistory.append(self.PSARValue.Current.Value)
        
        if len(self.HMA30History) < self.HMA30HistoryLength or not self.HMAValue30.IsReady:
            return
        
        OneTwoThreeHMA = False
        if self.HMAValue10.Current.Value > self.HMAValue20.Current.Value and self.HMAValue20.Current.Value > self.HMAValue30.Current.Value:
            OneTwoThreeHMA = True
            
        HMAInUpturn = self.HMA30History[0] < self.HMA30History[1] and self.HMA30History[2] > self.HMA30History[1]
        HMAInDownturn = self.HMA30History[2] < self.HMA30History[1]
        PSARInBull = data[self.chosenForex].Value > self.PSARValue.Current.Value
        
        if self.currentPrice > 0:
            self.Plot("Price", "Price", d.Decimal(10000*d.Decimal(self.currentPrice)))
            self.Plot("Price", "HMA30", d.Decimal(10000*d.Decimal(self.HMAValue30.Current.Value)))
            self.Plot("Price", "HMA20", d.Decimal(10000*d.Decimal(self.HMAValue20.Current.Value)))
            self.Plot("Price", "HMA10", d.Decimal(10000*d.Decimal(self.HMAValue10.Current.Value)))
            self.Plot("Price", "PSAR", d.Decimal(10000*d.Decimal(self.PSARValue.Current.Value)))
    
        if self.IsWarmingUp:
            return
        
        if HMAInUpturn and PSARInBull and self.MarketIsOpen and not self.IsInvested:
            self.Log("BOUGHT!")
            marketOrder = self.MarketOrder(self.chosenForex, 100, False, "MarketOrder in intial investment in Ondata")
            if marketOrder.Status == OrderStatus.Filled:
                self.IsInvested = True
                #self.ProfitTake = self.LimitOrder(self.chosenForex, -100, d.Decimal(data[self.chosenForex].Value + d.Decimal(0.05)), "Profit take in initial investment")
                #self.StopLoss = self.StopMarketOrder(self.chosenForex, -100, d.Decimal(data[self.chosenForex].Value - d.Decimal(0.05)), "Loss stop in initial investment")
            else:
                marketOrder.Cancel("Order not filled so cancelled")
        elif (HMAInDownturn or not PSARInBull) and self.IsInvested:
            self.Log("SOLD!")
            marketOrder = self.Liquidate(self.chosenForex)
            #self.StopLoss.Cancel("Bull Market so stoploss cancelled.")
            #self.ProfitTake.Cancel("Bull Market so takeprofit cancelled.")
            self.IsInvested = False

    #def OnEndOfDay(self):
    #    if self.currentPrice > 0:
    #        self.Plot("Price", "Price", d.Decimal(10000*d.Decimal(self.currentPrice)))
        #self.Plot("RSI", "RSIValue", d.Decimal(self.RSIValue.Current.Value))

    def EveryWeekBeforeMarketClose(self):
        self.Liquidate(self.chosenForex)
        self.MarketIsOpen = False

    def EveryWeekAfterMarketOpen(self):
        self.MarketIsOpen = True