Overall Statistics
Total Trades
89
Average Win
2.66%
Average Loss
-3.43%
Compounding Annual Return
-2.551%
Drawdown
22.700%
Expectancy
0.009
Net Profit
-2.781%
Sharpe Ratio
0.099
Probabilistic Sharpe Ratio
14.686%
Loss Rate
43%
Win Rate
57%
Profit-Loss Ratio
0.78
Alpha
-0.063
Beta
0.509
Annual Standard Deviation
0.328
Annual Variance
0.108
Information Ratio
-0.476
Tracking Error
0.326
Treynor Ratio
0.064
Total Fees
$0.00
import numpy as np
from QuantConnect import *
from QuantConnect.Parameters import *
from QuantConnect.Benchmarks import *
from QuantConnect.Brokerages import *
from QuantConnect.Util import *
from QuantConnect.Interfaces import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Selection import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Risk import *
from QuantConnect.Indicators import *
from QuantConnect.Data import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Fundamental import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.UniverseSelection import *
from QuantConnect.Notifications import *
from QuantConnect.Orders import *
from QuantConnect.Orders.Fees import *
from QuantConnect.Orders.Fills import *
from QuantConnect.Orders.Slippage import *
from QuantConnect.Scheduling import *
from QuantConnect.Securities import *
from QuantConnect.Securities.Equity import *
from QuantConnect.Securities.Forex import *
from QuantConnect.Securities.Interfaces import *
from datetime import date, datetime, timedelta
from QuantConnect.Python import *
from QuantConnect.Storage import *
from System import String, TimeSpan
QCAlgorithmFramework = QCAlgorithm
QCAlgorithmFrameworkBridge = QCAlgorithm


class MagicAlgo(QCAlgorithm):

    def Initialize(self):
        #self.SetTimeZone(TimeZones.Paris)

        # param velocity 
        self.velocity = 5
        # param trailing stop loss
        self.trailingStopRisk = 0.9
        # param stop loss in %. 90 = 10%
        self.initialStopLoss = 90
        # param rsi +-% needed before triggering trades
        # param take profit max
        # param volume minimum
        # param risk %
        self.risk = 1

        # Set the cash for backtest
        self.SetCash(10000)

        # Start and end dates for backtest
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2021, 2, 1)

        # Add asset
        self.symbol = self.AddForex("CADJPY", Resolution.Hour).Symbol

        # Lookback length for b/o (in days)
        self.lookback = 20

        # Upper/lower limit for lookback length
        self.ceiling, self.floor = 30, 10

        # Price offset for stop order
        self.initialStopRisk = 0.98

        self.limitOrder=None
        self.stopOrder=None
        self.positiveExcessRSIReached=False

        # Schedule function every five minutes
        self.Schedule.On(self.DateRules.EveryDay(self.symbol),
                         self.TimeRules.Every(timedelta(minutes=10)),
                         self.EveryMarketMinute)

        self.rsi = self.RSI(self.symbol, 14,MovingAverageType.Simple,Resolution.Hour)
        self.SetWarmUp(timedelta(20))
        

    def OnData(self, data):

        # Plot security's price
        self.Plot("Data Chart", self.symbol,
                  self.Securities[self.symbol].Close)

    def OnOrderEvent(self,orderEvent):
        self.Log(orderEvent)
        order = self.Transactions.GetOrderById(orderEvent.OrderId)

        if self.limitOrder is not None and self.stopOrder is not None and orderEvent.Status==OrderStatus.Filled and (order.Id== self.limitOrder.OrderId or order.Id==self.stopOrder.OrderId):
            if order.Id==self.limitOrder.OrderId:
                #self.Log("Stop order cancelled: %d" %self.limitOrder.OrderId)
                self.stopOrder.Cancel()
            else:
                #self.Log("limit order cancelled: %d" %self.stopOrder.OrderId)
                self.limitOrder.Cancel()    
    def EveryMarketMinute(self):
        if self.rsi.IsReady:
            # get the current RSI value
            rsi_value = self.rsi.Current.Value
            if rsi_value > 70 and rsi_value <= 80  and (not self.History(self.symbol, 20, Resolution.Minute).empty) and (not self.positiveExcessRSIReached):
                lastClosesMinuteBars = self.History(self.symbol, 20, Resolution.Minute)["close"]
                price=self.Securities[self.symbol].Price
               # self.Log(previousHigh)
               
                if len(lastClosesMinuteBars) == 20:
                    if len(self.History(self.symbol,2,Resolution.Hour))==2:
                        previousHourHigh=self.History(self.symbol,2,Resolution.Hour)["high"][0]
                        priceFiveMinutesAgo=lastClosesMinuteBars[15]
                        
                        #self.Log("DIFFERENCE: %f" % (currentHigh-previousHigh, ) )
                        #self.Log("Velocity %f" %((priceFiveMinutesAgo-price)*100,))
                        #self.Log("Price %f" %(price ,))
                        #self.Log("Previous %f" %(previousHourHigh,))
                        if (price > previousHourHigh) and ((priceFiveMinutesAgo-price)*100>self.velocity):
                        #if ((priceFiveMinutesAgo-price)*100>self.velocity):    

                            if not self.Transactions.GetOpenOrders(self.symbol):
                                #self.SetHoldings(self.symbol, self.risk)
                                self.marketOrder=self.MarketOrder( self.symbol,-50000)
                            
                                self.limitOrder=self.LimitOrder(self.symbol,50000,price-0.20)
                                self.stopOrder=self.StopMarketOrder(self.symbol,50000,price+0.10)

                                self.positiveExcessRSIReached=True
                                
                                #self.Log("PRICE %f" %(self.Securities[self.symbol].Price,))
                                #self.Log("RSI %f" %(self.rsi.Current.Value,))
                                #self.Log("DATE: " +self.Securities[self.symbol].LocalTime.strftime("%d/%m/%Y, %H:%M:%S"))
                                #self.Log("Limit price: %f" %(currentHigh-0.50,))
                                #self.Log("Quantity : %f"%(self.Portfolio[self.symbol].Quantity,))
                            else:
                                while (self.stopOrder.AverageFillPrice-price)*100>=20: 
                                    self.stopOrder.UpdateStopPrice(self.stopOrder.UpdateStopPrice(self.stopOrder.AverageFillPrice-0.10))
                        #if self.Transactions.GetOpenOrders(self.symbol):
                            # Update the stop price
                            #updateFields = UpdateOrderFields()
                            #updateFields.StopPrice = self.Securities[self.symbol].Close * self.trailingStopRisk

                            # self.stopMarketTicket.Update(updateFields)
                       
            elif rsi_value<70:
                self.positiveExcessRSIReached=False
           # elif rsi_value<30:
        
        self.Plot("Data Chart", "RSI", self.rsi.Current.Value)
# Your New Python File