Overall Statistics
Total Trades
9
Average Win
0.20%
Average Loss
-0.35%
Compounding Annual Return
-19.390%
Drawdown
29.500%
Expectancy
-0.480
Net Profit
-19.390%
Sharpe Ratio
-0.476
Loss Rate
67%
Win Rate
33%
Profit-Loss Ratio
0.56
Alpha
-0.448
Beta
19.41
Annual Standard Deviation
0.282
Annual Variance
0.08
Information Ratio
-0.533
Tracking Error
0.282
Treynor Ratio
-0.007
Total Fees
$0.00
import pandas 
import numpy as np 
### <summary>
### Simple RSI Strategy intended to provide a minimal algorithm example using
### one indicator
### </summary>
class RSIAlgorithm(QCAlgorithm):
    
    
    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.'''
        
      
        # Set our main strategy parameters
        self.SetStartDate(2013,1, 1)   # Set Start Date
        self.SetEndDate(2014,1,1)      # Set End Date
        self.SetCash(10000)            # Set Strategy Cash
        
        RSI_Period    = 14                # RSI Look back period 
        self.RSI_OB   = 60                # RSI Overbought level
        self.RSI_OS   = 40                # RSI Oversold level
        self.Allocate = 2              # Percentage of captital to allocate
        self.tolerance = 0.00015
        self.spread = 20/10000
        self.first_stg_up = 0
        self.first_stg_down = 0
        self.trend = 0
        self.trend_n = 0
        
        self.symbol = "EURUSD"
        
        ## Rename variables for percentages to make more readable and less confusing
        self.tpPercent = 20/10000
        self.slPercent = 10/10000


        ## Reassign these to be variables for Order Tickets
        self.tp = None
        self.sl = None
        self.holdings = 0 
        self.quant = 10000
        
        
        # Find more symbols here: http://quantconnect.com/data
        self.AddForex("EURUSD", Resolution.Hour)
        
        self.RSI_Ind = self.RSI("EURUSD", RSI_Period)
        self.bb_ind = self.BB("EURUSD", 20, 1, MovingAverageType.Simple);
        self.slow = self.SMA("EURUSD", 20, Resolution.Hour)
        self.fast = self.SMA("EURUSD", 7, Resolution.Hour)
        
        
      
        # Ensure that the Indicator has enough data before trading,. 
        self.SetWarmUp(20)
    
    
         
        
        
    def OnData(self, data):
        
        ## Assign to self.trend, as just 'trend' is not referred to elsewhere
        self.trend =1 
            
            
        if not self.Portfolio.Invested and not self.IsWarmingUp:
            # If not, we check the RSI Indicator
            if self.trend == 1 :
                #
                ## you need to assign to self.order or else variable will remain local
                self.order = self.MarketOrder(self.symbol, self.quant)
                
                ## you need to define fxClose or else the orders won't be properly placed
                fxClose = data[self.symbol].Close
                
                ## you need to replace sl with self.sl, as you want these to be global variables
                ## otherwise, they will not be accessible outside of OnData
                self.sl = self.StopMarketOrder(self.symbol, -self.quant, fxClose - self.slPercent)  
                self.tp = self.LimitOrder(self.symbol, -self.quant, fxClose + self.tpPercent)
            
            #if self.RSI_Ind.Current.Value < self.RSI_OS:
                # Buy Apple
             #   self.SetHoldings("EURUSD", self.Allocate)
        else:
            if self.trend_n == -1:
                # Sell Apple 
                self.Liquidate("EURUSD")
                
    def OnOrderEvent(self, orderEvent):  
        
        ## This will check for the boolean value of whether or not the order has been filled
        if not (orderEvent.Status == 'Filled'):
            return 
        
        ## python doesn't support null. Instead, check for None
        if (self.tp is None) or (self.sl is None): 
            return 
        
        
        filledOrderid = orderEvent.OrderId
        
        if self.tp == filledOrderId: 
            self.sl.Cancel() 
            
        if self.sl == filledOrderId: 
            self.tp.Cancel()