Overall Statistics
import numpy as np
from datetime import timedelta
from datetime import datetime
import pandas as pd


class MovingAverage(QCAlgorithm):

 

     def Initialize(self):
        self.SetStartDate(2018, 1, 1)
        #self.SetEndDate(2016, 1, 10)
        self.SetCash(100000)
        self.a = self.AddEquity("VIXY", Resolution.Minute).Symbol
        self.b = self.AddEquity("SVXY", Resolution.Minute).Symbol
        self.SetWarmUp(2860,Resolution.Hour)
        self.previous = None
        self.position = None
        self.macd1 = self.MACD(self.a, 40, 85, 30, MovingAverageType.Exponential, Resolution.Hour)
        self.macd2 = self.MACD(self.b, 40, 85, 30, MovingAverageType.Exponential, Resolution.Hour)
        #self.rsi_vixy = self.RSI("VIXY", 18, MovingAverageType.Exponential,Resolution.Hour)
        #self.rsi_svxy = self.RSI("SVXY", 18, MovingAverageType.Exponential,Resolution.Hour)
        
        #PlusPercent = Decimal(1.2)
        #MinusPercent = Decimal(0.9)
        #APrice = (data["VIXY"].Open)
        #self.LimitOrder("VIXY", self.CalculateOrderQuantity("VIXY", .3), APrice * PlusPercent)
        ##reason for not working due to price change---since price changes number of shares change
        #need to change number of shares of set it to what it was in beginning 
        ##invalid orders need something to check if orders are vaild 
       
        ##stop loss 
        ###stop gain
        #self.LimitOrder("SPY", -10, price * plusThreePercent)
        #self.CalculateOrderQuantity("SVXY", .3) to calculate number of shares
        ###stop loss
        #self.StopMarketOrder("SPY", -10, price * minusThreePercent)
        
        #leverage
        #self.a = self.AddEquity("VIXY", Resolution.Minute,Market.USA,True,2).Symbol
        #self.b = self.AddEquity("SVXY", Resolution.Minute,Market.USA,True,2).Symbol

     def OnData(self,Data):
        if self.IsWarmingUp: return
     
     
     
     
        if self.macd1.Current.Value > self.macd1.Signal.Current.Value and self.macd2.Current.Value < self.macd2.Signal.Current.Value:
            #self.rsi_spy.Current.Value >= 70
            
            if self.position == None:
                    self.Sell("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", .5))
                    #1.3
            else:
                    self.Liquidate("SVXY")
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", .3)) 
                    self.Sell("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", .2))
                    #1
            
            
                    
                
                  
                  
        if self.macd1.Current.Value < self.macd1.Signal.Current.Value and self.macd2.Current.Value > self.macd2.Signal.Current.Value:
                #self.rsi_spy.Current.Value <= 40
                
            if self.position == None:
                self.Sell("VIXY",self.CalculateOrderQuantity("VIXY", .3))
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", .5))
                #1.3
            else:
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                self.Liquidate("VIXY") 
                self.Sell("VIXY",self.CalculateOrderQuantity("VIXY", .3))
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", .2))
                #1
                
                
                
                
                
                
                ###
import numpy as np
from decimal import *

class stopLossTakeprofitExample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013,10, 7)  #Set Start Date
        self.SetEndDate(2013,10,31)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Hour)
    
    def OnData(self, data):
        if not self.Portfolio.Invested:
            price = data["SPY"].Close
            self.Debug('price : ' + str(price))
            plusThreePercent = Decimal(1.03)
            minusThreePercent = Decimal(0.97)
            ### Buy at current market price and simultaneously send limit sell order and stop loss order
            self.Buy("SPY", 10)
            self.LimitOrder("SPY", -10, price * plusThreePercent)
            self.StopMarketOrder("SPY", -10, price * minusThreePercent)
    #self.CalculateOrderQuantity use for quantity
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        
        ### Cancel remaining order if limit order or stop loss order is executed
        if order.Status == OrderStatus.Filled:
            if order.Type == OrderType.Limit or OrderType.StopLimit:
                self.Transactions.CancelOpenOrders(order.Symbol)
            
            if order.Status == OrderStatus.Canceled:
                self.Log(str(orderEvent))
import numpy as np
from datetime import timedelta
from datetime import datetime
import pandas as pd


class MovingAverage(QCAlgorithm):

 

     def Initialize(self):
        self.SetStartDate(2018, 1, 1)
        #self.SetEndDate(2016, 1, 10)
        self.SetCash(100000)
        self.a = self.AddEquity("VIXY", Resolution.Minute).Symbol
        self.b = self.AddEquity("SVXY", Resolution.Minute).Symbol
        self.SetWarmUp(2860,Resolution.Hour)
        self.previous = None
        self.position = None
        self.macd1 = self.MACD(self.a, 40, 85, 30, MovingAverageType.Exponential, Resolution.Hour)
        self.macd2 = self.MACD(self.b, 40, 85, 30, MovingAverageType.Exponential, Resolution.Hour)
        #self.rsi_vixy = self.RSI("VIXY", 18, MovingAverageType.Exponential,Resolution.Hour)
        #self.rsi_svxy = self.RSI("SVXY", 18, MovingAverageType.Exponential,Resolution.Hour)
        #PlusPercent = Decimal(1.2)
        #MinusPercent = Decimal(0.9)
        #APrice = (data["VIXY"].Open)
        #self.LimitOrder("VIXY", self.CalculateOrderQuantity("VIXY", .3), APrice * PlusPercent)
        ##reason for not working due to price change---since price changes number of shares change
        #need to change number of shares of set it to what it was in beginning 
        ##invalid orders need something to check if orders are vaild 
       
        ##stop loss 
        ###stop gain
        #self.LimitOrder("SPY", -10, price * plusThreePercent)
        #self.CalculateOrderQuantity("SVXY", .3) to calculate number of shares
        ###stop loss
        #self.StopMarketOrder("SPY", -10, price * minusThreePercent)
        
        #leverage
        #self.a = self.AddEquity("VIXY", Resolution.Minute,Market.USA,True,2).Symbol
        #self.b = self.AddEquity("SVXY", Resolution.Minute,Market.USA,True,2).Symbol
    

        

     def OnData(self,Data):
        if self.IsWarmingUp: return
        #svxypointthree=self.CalculateOrderQuantity("SVXY", .3)
        #svxyone=self.CalculateOrderQuantity("SVXY", 1)
        #svxyonepointthree=self.CalculateOrderQuantity("SVXY", 1.3)
            
        #vixypointthree=self.CalculateOrderQuantity("VIXY", .3)
        #vixyone=self.CalculateOrderQuantity("VIXY", 1)
        #vixyonepointthree=self.CalculateOrderQuantity("VIXY", 1.3)
        
     
     
     
     
        if self.macd1.Current.Value > self.macd1.Signal.Current.Value and self.macd2.Current.Value < self.macd2.Signal.Current.Value:
            #self.rsi_spy.Current.Value >= 70
            
            if self.position == None:
                    self.Sell("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", 1.3))
            else:
                    self.Liquidate("SVXY")
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", .3)) 
                    self.Sell("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                    self.Buy("VIXY", self.CalculateOrderQuantity("VIXY", 1))
            
            
                    
                
                  
                  
        if self.macd1.Current.Value < self.macd1.Signal.Current.Value and self.macd2.Current.Value > self.macd2.Signal.Current.Value:
                #self.rsi_spy.Current.Value <= 40
                
            if self.position == None:
                self.Sell("VIXY",self.CalculateOrderQuantity("VIXY", .3))
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", 1.3))
            else:
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", .3))
                self.Liquidate("VIXY") 
                self.Sell("VIXY",self.CalculateOrderQuantity("VIXY", .3))
                self.Buy("SVXY",self.CalculateOrderQuantity("SVXY", 1))
                
                
                
                
                
                
                ###