Overall Statistics
Total Orders
54
Average Win
2.18%
Average Loss
-1.94%
Compounding Annual Return
-1.397%
Drawdown
16.000%
Expectancy
-0.152
Start Equity
100000.00
End Equity
91787.04
Net Profit
-8.213%
Sharpe Ratio
-0.321
Sortino Ratio
-0.234
Probabilistic Sharpe Ratio
0.024%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
1.12
Alpha
-0.02
Beta
-0.128
Annual Standard Deviation
0.05
Annual Variance
0.002
Information Ratio
0.162
Tracking Error
0.104
Treynor Ratio
0.125
Total Fees
$0.00
Estimated Strategy Capacity
$1400000.00
Lowest Capacity Asset
EURUSD 8G
Portfolio Turnover
2.32%
#region imports
from AlgorithmImports import *

from datetime import datetime
import decimal
import numpy as np
#endregion


class DynamicBreakoutAlgorithm(QCAlgorithm):
    
    def initialize(self):
        self.set_start_date(2010,1,15)
        self.set_end_date(2016,2,15)
        self.set_cash(100000)
        fx = self.add_forex("EURUSD", Resolution.HOUR, Market.OANDA)
        self._syl = fx.symbol
        self.schedule.on(self.date_rules.every_day(self._syl), self.time_rules.before_market_close(self._syl,1), self._set_signal)
        self._numdays = 20
        self._ceiling,self._floor = 60,20
        self._buypoint, self._sellpoint= None, None
        self._long_liq_point, self._short_liq_point, self._yesterdayclose= None, None, None
        self.set_benchmark(self._syl)
        self._bolband = self.bb(self._syl, self._numdays, 2, MovingAverageType.SIMPLE, Resolution.DAILY)
   
    def _set_signal(self):
        close = self.history(self._syl, 31, Resolution.DAILY)['close']
        todayvol = np.std(close[1:31])
        yesterdayvol = np.std(close[0:30])
        deltavol = (todayvol - yesterdayvol) / todayvol
        self._numdays = int(round(self._numdays * (1 + deltavol)))

        if self._numdays > self._ceiling:
           self._numdays = self._ceiling
        elif self._numdays < self._floor:
            self._numdays = self._floor
        
        high = self.history(self._syl, self._numdays, Resolution.DAILY)['high']
        low = self.history(self._syl, self._numdays, Resolution.DAILY)['low']      

        self._buypoint = max(high)
        self._sellpoint = min(low)
        historyclose = self.history(self._syl, self._numdays, Resolution.DAILY)['close'] 
        self._long_liq_point = np.mean(historyclose)
        self._short_liq_point = np.mean(historyclose)
        self._yesterdayclose = historyclose.iloc[-1]
        
        # wait for our BollingerBand to fully initialize
        if not self._bolband.is_ready: return

        holdings = self.portfolio[self._syl].quantity
    
        if self._yesterdayclose > self._bolband.upper_band.current.value and self.portfolio[self._syl].price >= self._buypoint:
            self.set_holdings(self._syl, 1)
        elif self._yesterdayclose < self._bolband.lower_band.current.value and self.portfolio[self._syl].price <= self._sellpoint:
            self.set_holdings(self._syl, -1)

        if holdings > 0 and self.portfolio[self._syl].price <= self._short_liq_point:
            self.liquidate(self._syl)
        elif holdings < 0 and self.portfolio[self._syl].price >= self._short_liq_point:
            self.liquidate(self._syl)
      
        self.log(str(self._yesterdayclose)+(" # of days ")+(str(self._numdays)))