import pandas as pd
import numpy as np
import decimal as d
import datetime
from datetime import timedelta, time
class Test(QCAlgorithm):
def Initialize(self):
# feed 3 days into strategy before starting
self.SetWarmup(timedelta(3))
self.SetStartDate(2020, 9, 15)
self.SetCash(100000)
futureBTC = self.AddFuture(Futures.Currencies.BTC, Resolution.Minute)
futureBTC.SetFilter(lambda x: x.FrontMonth())
# set fees
# futureBTC.SetFeeModel(CustomFeeModel(self)) didn't work
self.Securities[Futures.Currencies.BTC].SetFeeModel(CustomFeeModel(self)) # throws error The ticker BTC was not found
def OnData(self, slice):
for chain in slice.FutureChains:
for i in chain.Value:
fut_symbol = i.Symbol
fut_ask = i.AskPrice
fut_bid = i.BidPrice
openinterest = i.OpenInterest
expiry = i.Expiry
# self.Debug(f'{symbol}: Expiry: {expiry} | AskPrice: {askprice} | BidPrice: {bidprice} | OpenInterest: {openinterest}')
event = True
# enter trade if not invested
if not self.Portfolio.Invested:
if event:
t_btc_cme = self.MarketOrder(fut_symbol, 10)
if self.Portfolio.Invested:
if not event:
self.SetHoldings(fut_symbol, 0)
def OnOrderEvent(self, orderEvent):
if orderEvent.FillQuantity == 0:
return
self.Log(orderEvent)
#self.Debug(f"Symbol:{orderEvent.Symbol} Fee:{orderEvent.OrderFee.Value.Amount}")
#self.Log(f"Symbol:{orderEvent.Symbol} Fee:{orderEvent.OrderFee.Value.Amount}")
class CustomFeeModel(FeeModel):
def GetOrderFee(self, parameters):
# custom fee math
# self.debug(f"fee: {parameters.Order.AbsoluteQuantity} * {parameters.Security.Price}")
type = parameters.Security.Type
if type == SecurityType.Forex: fee = 1
elif type == SecurityType.Option: fee = 2 # (parameters.Order.AbsoluteQuantity * 1.5)
elif type == SecurityType.Future: fee = 3 # (parameters.Order.AbsoluteQuantity/10 * 2.5) # 10 is 1 coin
elif type == SecurityType.Crypto: fee = 4 # (parameters.Order.AbsoluteQuantity * parameters.Security.Price * 0.001)
elif type == SecurityType.Equity: fee = 5
return OrderFee(CashAmount(fee, "USD"))