| Overall Statistics |
|
Total Trades 2344 Average Win 0.56% Average Loss -0.34% Compounding Annual Return -99.996% Drawdown 59.300% Expectancy -0.228 Net Profit -58.501% Sharpe Ratio -10.493 Loss Rate 71% Win Rate 29% Profit-Loss Ratio 1.67 Alpha -11.725 Beta 240.562 Annual Standard Deviation 0.742 Annual Variance 0.551 Information Ratio -10.516 Tracking Error 0.742 Treynor Ratio -0.032 Total Fees $0.00 |
### <summary>
### Simple RSI Strategy intended to provide a minimal algorithm example using
### one indicator
### </summary>
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
import time
import matplotlib.pyplot as plt
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from datetime import datetime
from System import *
from QuantConnect import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Market import *
from QuantConnect.Orders import OrderStatus
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Indicators import *
import numpy as np
from datetime import timedelta, datetime
import pandas
import numpy as np
### <summary>
### Simple RSI Strategy intended to provide a minimal algorithm example using
### one indicator
### </summary>
class MultipleSymbolConsolidationAlgorithm(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(2015, 1, 1)
self.SetEndDate(2015, 2, 1) # Set End Date
self.SetCash(10000)
self.quant = 10000
self.spread = 15/10000
self.spread_15 = 15/10000
self.first_stg_up = 0
self.first_stg_down = 0
self.pip_s = 5
#self.spread = 2
self.tp = 33/10000
self.sl = 14/10000
self.tp_s = {}
self.sl_s = {}
self.tp_s_n = 0
self.sl_s_n = 0
self.long = {}
self.short = {}
self.price = {}
self.trade_ind = {}
self.first_stg_up = {}
self.first_stg_down = {}
BarPeriod = TimeSpan.FromHours(20)
SimpleMovingAveragePeriod = 10
RollingWindowSize = 20
self.Data = {}
self.trades = {}
self.c = 0
# Contains all of our forex symbols
ForexSymbols =["EURUSD", "GBPUSD", "USDCHF", "AUDUSD","NZDUSD"]
Console.Write("init")
self.fast = {}
self.slow = {}
self.bb_ind = {}
for symbol in ForexSymbols:
forex = self.AddForex(symbol)
self.Data[symbol] = SymbolData(forex.Symbol, BarPeriod, RollingWindowSize)
self.trades[symbol] = 0
# self.Data[symbol] = 0
self.price[symbol] = 0
self.short[symbol] = 0
self.long[symbol] = 0
self.sl_s[symbol] = 0
self.tp_s[symbol] = 0
self.trade_ind[symbol] = 0
self.first_stg_up[symbol] = 0
self.first_stg_down[symbol] = 0
self.bb_ind[symbol] = self.BB(symbol, 20, 1, MovingAverageType.Simple, Resolution.Hour);
self.slow[symbol] = self.SMA(symbol, 1, Resolution.Hour)
self.fast[symbol] = self.SMA(symbol, 1, Resolution.Hour)
for symbol, symbolData in self.Data.items():
# define the indicator
symbolData.SMA = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriod), Resolution.Hour), SimpleMovingAveragePeriod)
# define a consolidator to consolidate data for this symbol on the requested period
consolidator = TradeBarConsolidator(BarPeriod) if symbolData.Symbol.SecurityType == SecurityType.Equity else QuoteBarConsolidator(BarPeriod)
# write up our consolidator to update the indicator
consolidator.DataConsolidated += self.OnDataConsolidated
# we need to add this consolidator so it gets auto updates
self.SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator)
def OnDataConsolidated(self, sender, bar):
self.Data[bar.Symbol.Value].SMA.Update(bar.Time, bar.Close)
self.Data[bar.Symbol.Value].Bars.Add(bar)
def OnData(self,data):
# loop through each symbol in our structure
#Console.Write(self.c)
for symbol in self.Data.keys():
symbolData = self.Data[symbol]
# this check proves that this symbol was JUST updated prior to this OnData function being called
#trend_sma = np.where(fast.Current.Value > slow.Current.Value,1,0)
#spread = bb_ind.UpperBand.Current.Value - bb_ind.LowerBand.Current.Value
fxOpen = data[symbol].Open
fxClose = data[symbol].Close
fxHigh = data[symbol].High
fxLow = data[symbol].Low
price = data[symbol].Price
#trend = self.trend_h(symbolData)
pip = (fxHigh - fxLow)*10000
# bb_ind = BB(data[symbol], 20, 1, MovingAverageType.Simple, Resolution.Hour);
# slow = self.SMA(data[symbol], 20, Resolution.Hour)
# fast = self.SMA(data[symbol], 7, Resolution.Hour)
# trend_sma = np.where(self.fast.Current.Value > self.slow.Current.Value,1,0)
#spread = self.bb_ind.UpperBand.Current.Value - self.bb_ind.LowerBand.Current.Value
# spread_bin = np.where(spread > self.spread,1,0)
#Console.Write(self.slow[symbol])
Console.Write(symbolData.SMA.Current.Value)
# Console.Write(self.bb_ind.UpperBand.Current.Value)
if self.long[symbol] == 0 and self.short[symbol] == 0:
if not self.Portfolio[symbol].Invested:
if fxClose > fxOpen:# and fxClose > self.bb_ind.UpperBand.Current.Value:# and trend_sma == 1 :#
self.first_stg_up[symbol] = 1
# Console.Write(symbol)
# Console.Write("heeey stage 1 signal buy")
if self.first_stg_up[symbol] == 1:# and fxClose > self.bb_ind.LowerBand.Current.Value:# and spread_bin == 1:#
# Console.Write('99999')
# Console.Write(symbol)
# Console.Write(spread_bin)
self.first_stg_down[symbol] = 0
self.trade_ind[symbol] = 1
# if fxClose < fxOpen:# and fxClose < self.bb_ind.LowerBand.Current.Value :#trend_sma == 0 and
# Console.Write("eeeeeee" )
# self.first_stg_down[symbol] = 1
# if self.first_stg_down[symbol] == 1 :#and fxClose <= self.bb_ind.UpperBand.Current.Value :#and spread_bin == 1
# Console.Write("-11111")
# self.first_stg_up[symbol] = 0
# self.trade_ind[symbol] = -1
# if spread_bin == 0:
# self.first_stg_up[symbol] = 0
# self.first_stg_down[symbol] = 0
# Console.Write("-0000000")
# return 0
#spread_bin = np.where(spread > self.spread,1,0)
if not self.Portfolio[symbol].Invested:
if pip >self.pip_s and fxOpen < fxClose and self.trade_ind[symbol] == 1 :
# Console.Write("trade 1 trade " )
if self.long[symbol] == 0 and self.short[symbol] == 0:
# Console.Write("trade 1 trade!!!!!!!!!! " )
self.price[symbol] = data[symbol].Price
self.tp_s[symbol] = price + self.tp
self.sl_s[symbol] = price - self.sl
self.long[symbol] = 1
# self.signal_h_s = 0
#self.signal_h_b = 0
self.MarketOrder(symbol, self.quant)
# Console.Write("trade 1 trade !!!!!!!!! " )
if self.price[symbol] > 0 and self.long[symbol] == 1 :
if data[symbol].Price >= self.tp_s[symbol] :
self.long[symbol] = 0
# Console.Write("terminer tp" )
# Console.Write(symbol)
self.Liquidate(symbol)
if data[symbol].Price <= self.sl_s[symbol] :
self.long[symbol] = 0
# Console.Write("terminer sl" )
# Console.Write(symbol)
self.Liquidate(symbol)
def trend_h(self, data):
fxClose = data.Close
fxHigh = data.High
fxOpen = data.Open
if trend_sma == 1 and fxClose > fxOpen and fxClose > bb_ind.UpperBand.Current.Value:
self.first_stg_up = 1
Console.Write("trend buy stage 1" )
if self.first_stg_up == 1 and spread_bin == 1 and fxClose > bb_ind.LowerBand.Current.Value :
self.first_stg_down = 0
Console.Write("trend buy stage 2" )
return 1
if trend_sma == 0 and fxClose < fxOpen and fxClose < bb_ind.LowerBand.Current.Value :
self.first_stg_down = 1
if self.first_stg_down == 1 and spread_bin == 1 and fxClose <= bb_ind.UpperBand.Current.Value :
self.first_stg_up = 0
return -1
Console.Write(self.trend_n)
if spread_bin == 0:
self.first_stg_up = 0
self.first_stg_down = 0
return 0
class SymbolData(object):
def __init__(self, symbol, barPeriod, windowSize):
self.Symbol = symbol
# The period used when population the Bars rolling window
self.BarPeriod = barPeriod
# A rolling window of data, data needs to be pumped into Bars by using Bars.Update( tradeBar ) and can be accessed like:
# mySymbolData.Bars[0] - most first recent piece of data
# mySymbolData.Bars[5] - the sixth most recent piece of data (zero based indexing)
self.Bars = RollingWindow[IBaseDataBar](windowSize)
# The simple moving average indicator for our symbol
def IsReady(self):
return self.Bars.IsReady and self.SMA.IsReady
# Returns true if the most recent trade bar time matches the current time minus the bar's period, this
# indicates that update was just called on this instance
def WasJustUpdated(self, current):
return self.Bars.Count > 0 and self.Bars[0].Time == current - self.BarPeriod