Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
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 = {}
        self.Data = {}
        self.trades = {}
        self.c = 0 
        BarPeriod = TimeSpan.FromHours(20)
        # This is the period of our sma indicators
        SimpleMovingAveragePeriod = 20
        # This is the number of consolidated bars we'll hold in symbol data for reference
        RollingWindowSize = 20
        
        
        # Contains all of our forex symbols
        ForexSymbols =["EURUSD", "GBPUSD", "USDCHF", "AUDUSD","NZDUSD"]
     
        
        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
            

    
        
    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
            
         
        
        
            self.bb_ind = self.BB(symbol, 20, 1, MovingAverageType.Simple, Resolution.Hour);
            self.slow = self.SMA(symbol, 1, Resolution.Hour)
            self.fast = self.SMA(symbol, 1, Resolution.Hour)
            
           
            
            
            
            
            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)
            
            Console.Write(self.slow.Current.Value)
            Console.Write(self.fast.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 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:
                       # Console.Write('99999')
                      #  Console.Write(symbol)
                      #  Console.Write(spread_bin)
                        self.first_stg_down[symbol] = 0
                        self.trade_ind[symbol] = 1
                
            
          
           
        
        
            
            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)
                    
                    
                    
                    
                    
                      
        
    

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