Overall Statistics
Total Trades
195
Average Win
11.67%
Average Loss
-3.44%
Compounding Annual Return
20.379%
Drawdown
47.500%
Expectancy
0.360
Net Profit
140.245%
Sharpe Ratio
0.571
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
3.40
Alpha
0.333
Beta
-6.562
Annual Standard Deviation
0.4
Annual Variance
0.16
Information Ratio
0.531
Tracking Error
0.4
Treynor Ratio
-0.035
Total Fees
$0.00
import numpy as np
from datetime import datetime
import decimal

### <summary>
### Basic EMA algorithm simply buys above EMA and sells below EMA.
### </summary>
class BasicEMAAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initializes cash and max trade size. Sets target currencies to run on.'''

        self.SetStartDate(2013,10, 7)  #Set Start Date
        #self.SetEndDate(2016,6,11)    #Set End Date
        
        self.SetCash(1000)           #Set Strategy Cash
        self.maxTradeSize = 10000
        
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        
        self.ema = {}
        
        self.currenciesToUse = {"AUDUSD"}
        
        for cur in self.currenciesToUse:
            # Add symbols to universe
            self.forex = self.AddForex(cur, Resolution.Daily, Market.Oanda)
            self.ema[cur] = self.EMA(cur, 30, Resolution.Daily)
    
        self.__previous = datetime.min
        
        self.tradesize = round(self.maxTradeSize / len(self.currenciesToUse))
        
        
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

        Arguments:
            data: Slice object keyed by symbol containing the stock data
        '''
         # only once per day
        if self.__previous.date() == self.Time.date(): 
           return
        
        self.__previous = self.Time
        
        # Grab our universe, is there an easier way?
        for universe in self.UniverseManager.Values:

            # User defined universe has symbols from AddSecurity/AddEquity calls
            if universe is UserDefinedUniverse:
                break

        symbols = universe.Members.Keys
        
        for symbol in symbols:
        
            self.symStr = str(symbol)
            
            holdings = self.Portfolio[self.symStr].Quantity
        
            if holdings == 0:
            
                # initiate new position
                
                # Price above EMA, so buy
                if self.ema[self.symStr].Current.Value < data[symbol.Value].Ask.Close:
                    self.MarketOrder(self.forex.Symbol, self.tradesize)
                
                # Price below EMA, so sell    
                elif self.ema[self.symStr].Current.Value > data[self.symStr].Ask.Close:
                    self.MarketOrder(self.forex.Symbol, -self.tradesize)
            
            else:    
                # We have holdings, so determine if we liquidate
        
                # if long and we drop below EMA, close it
                if holdings > 0 and self.ema[self.symStr].Current.Value > data[self.symStr].Ask.Close:
                    self.Liquidate(self.symStr)
                    
                # if short and we go above EMA, close it
                elif holdings < 0 and self.ema[self.symStr].Current.Value < data[self.symStr].Ask.Close:
                    self.Liquidate(self.symStr)