Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic 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
Estimated Strategy Capacity
$7700000.00
Lowest Capacity Asset
GBPUSD 8G
Portfolio Turnover
2030.81%
from AlgorithmImports import *
import numpy as np
import pandas as pd
import math
from datetime import datetime

class MyTradingAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2023, 4, 25)
        self.SetEndDate(2023, 4 , 25)
        self.SetCash(100000)
        
        self.forecast_period = 15
        self.GBPUSD = self.AddForex("GBPUSD", Resolution.Minute).Symbol

        self.close_price_window = RollingWindow[float](240) # Holds the last 2 EMA values

        self.highestPriceWindow = RollingWindow[float](2)
        self.lowestPriceWindow = RollingWindow[float](2)

        self.leverage_power = 10



        # Order variables
        self.entryTicket = None
        self.stopMarketTicket = None


        self.counter = 0
        self.SetWarmUp(101)


        stockPlot = Chart('Price Plot')
        stockPlot.AddSeries(Series('Close Price', SeriesType.Line, '$', Color.Black))
        stockPlot.AddSeries(Series('Low Price', SeriesType.Line, '$', Color.Red))
        stockPlot.AddSeries(Series('High Price', SeriesType.Line, '$', Color.Green))
        stockPlot.AddSeries(Series('Long', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.Triangle))


        self.AddChart(stockPlot)




    def OnData(self, data):
        if self.IsWarmingUp:

            return

        if not data.ContainsKey(self.GBPUSD):
            return

        self.close_price_window.Add(data[self.GBPUSD].Close)


        
        # Add the high and low prices from the previous time period to the respective RollingWindows
        self.highestPriceWindow.Add(data[self.GBPUSD].High)
        self.lowestPriceWindow.Add(data[self.GBPUSD].Low)


        self.counter = self.counter + 1


        if self.Time.hour == 9 and self.Time.minute == 22:
            self.enter_trade(data[self.GBPUSD].Symbol, self.leverage_power, self.lowestPriceWindow[1], data[self.GBPUSD].Close )

        self.Plot('Price Plot', 'Close Price', data[self.GBPUSD].Close)
        self.Plot('Price Plot', 'High Price', data[self.GBPUSD].High)
        self.Plot('Price Plot', 'Low Price', data[self.GBPUSD].Low)



    def OnOrderEvent(self, orderEvent):

        if orderEvent.Status != OrderStatus.Filled:
            return

        order = self.Transactions.GetOrderById(orderEvent.OrderId)

        # save fill time of stop loss order 
        if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == order.Id: 
            self.stopMarketOrderFillTime = self.Time
            self.Plot('Price Plot', 'Stop Loss', self.stopMarketTicket.AverageFillPrice)
            self.Log("Stop Loss: " + self.stopMarketTicket.Tag)
            self.stopMarketTicket = None



    # Enter Trade
    def enter_trade(self, Symbol, leverage, stop_price, close_price ):

        quantity = self.CalculateOrderQuantity(Symbol, leverage)
        self.entryTicket =  self.MarketOrder(Symbol, (quantity))
        self.Plot('Price Plot', 'Long', self.entryTicket.AverageFillPrice)

        if quantity > 0:
            stop_price = stop_price * (1- 0.0008)
        else:
            stop_price = stop_price * (1.0008)
        # send stop loss order if market order is filled
        if self.entryTicket is not None:
            self.stopMarketTicket = self.StopMarketOrder(self.GBPUSD, -self.entryTicket.Quantity, stop_price)
            self.Log("Stop Loss Quantity filled:" + str(self.stopMarketTicket.QuantityFilled) + "Fill price:" + str(self.stopMarketTicket.AverageFillPrice))

        if quantity > 0:
            self.Log("Long: " + str(close_price))    
        else:
            self.Log("Short: " + str(close_price))    
        self.entryTicket = None