Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-6.602%
Drawdown
1.600%
Expectancy
0
Net Profit
-1.044%
Sharpe Ratio
-1.818
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.047
Beta
0.023
Annual Standard Deviation
0.026
Annual Variance
0.001
Information Ratio
-0.332
Tracking Error
0.132
Treynor Ratio
-2.06
Total Fees
$0.00
import numpy as np
import decimal
import datetime

class ALM20(QCAlgorithm):
    
    stop_market_ticket = None
    highest_price = -1
    lowest_price = 9999
    position_type = None
    symbol = 'EURUSD'
    
    def Initialize(self):
        self.SetCash(10000)
        self.SetStartDate(2019,7,1)
        self.AddForex(self.symbol, Resolution.Minute, Market.Oanda, 100.0)
        
        self.SetWarmUp(15)
        
    def OnData(self, data):
        self.Plot("Data Chart", "Asset Price", data[self.symbol].Close)
        
        if not self.Portfolio.Invested:
            self.current_price = data[self.symbol].Price
            self.eur_usd_df = self.History([self.symbol], 15, Resolution.Minute)
            
            if not self.eur_usd_df.empty:
                eur_usd_quotebars = self.eur_usd_df.loc['EURUSD']
                
                if eur_usd_quotebars['close'][0] < self.current_price:
                    # self.Debug('Buy')
                    self.position_type = 'Long'
                    self.highest_price = self.current_price
                    self.Log(f'Buying - Current: {self.current_price}, SL: {self.current_price - 0.0020}')
                    self.stop_market_ticket = self.StopMarketOrder(self.symbol, 5000, self.current_price - 0.0020)
                else:
                    # self.Debug('Sell')
                    self.position_type = 'Short'
                    self.stop_market_ticket = self.StopMarketOrder(self.symbol, -5000, self.current_price + 0.0020)
    
        else:
            # TRY CLOSED PRICE
            if self.position_type == 'Long':
                if data[self.symbol].Price > self.highest_price:
                    new_stop = decimal.Decimal(data[self.symbol].Price - 0.0020)
                    
                    self.Log('Price is higher, update StopLoss')
                    self.Log(f'Highest: {self.highest_price}, Current: {data[self.symbol].Price}, new SL: {new_stop}')
                    
                    self.highest_price = data[self.symbol].Price
                    
                    update_fields = UpdateOrderFields()
                    update_fields.StopPrice = new_stop
                    self.stop_market_ticket.Update(update_fields)
                    self.Log(f'ticket {self.stop_market_ticket}')
                    
                    
            elif self.position_type == 'Short':
                if data[self.symbol].Price < self.lowest_price:
                    self.lowest_price = data[self.symbol].Price
                    difference = data[self.symbol].Price + 0.0020
                    self.Debug('Short hit')
                    update_fields = UpdateOrderFields()
                    update_fields.StopPrice = difference
                    self.stop_market_ticket.Update(update_fields)
                    
                    
    def OnOrderEvent(self, orderEvent):
        self.Log(f'Orderevent: {orderEvent}')
        # if orderEvent.Status != OrderStatus.Filled:
        #     return
        
        # if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId: 
        #     self.stopMarketOrderFillTime = self.Time