I'm trying to set a stop loss and take profit as a percent value of my accounti.e: I have 1000$ in my account and I want to risk 1% per trade and set the stop loss 40 pips below the market order price. Or set a stop loss n pips below the current price and always risk 1%.
So far this is what I came with, but from the backtest results I see that order quantity are around 27000 units.
Any feedback will be much appreciated.
import decimal as d
from datetime import timedelta
class MovingAverageCrossAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2020, 1, 1) #Set Start Date
self.SetEndDate(2020, 12, 31) #Set End Date
self.SetCash(1000) #Set Strategy Cash
self.currencies = ["EURUSD","NZDUSD"]
self.Data = {}
for ticker in self.currencies:
symbol = self.AddForex(ticker, Resolution.Hour).Symbol
self.Data[symbol] = SymbolData(self.SMA(symbol, 50, Resolution.Hour), self.SMA(symbol, 200, Resolution.Hour))
self.SetWarmUp(203)
self.quant = 1000
def OnData(self, data):
if self.IsWarmingUp:
return
self.Log(str(self.Portfolio.MarginRemaining))
for symbol, symbolData in self.Data.items():
fastPastValue = symbolData.fastSMAWindow[1].Value
slowPastValue = symbolData.slowSMAWindow[1].Value
fast = symbolData.fast.Current.Value
slow = symbolData.slow.Current.Value
if self.Portfolio[symbol]:
price = data[symbol].Close
pip = self.Securities[symbol].SymbolProperties.MinimumPriceVariation
leverage = self.Securities[symbol].Leverage
margin = self.Portfolio.MarginRemaining
risk = 0.01
orderSize = (risk * margin * price) / (40 * pip )
stopLoss = (price - 0.0040)
profitTarget = (price + 0.0040)
if fast > slow and fastPastValue < slow:
self.MarketOrder(symbol, (orderSize))
self.StopMarketOrder(symbol, -orderSize, stopLoss)
self.LimitOrder(symbol, -orderSize, profitTarget)
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))
class SymbolData:
def __init__(self, fast, slow):
self.fast = fast
self.slow = slow
self.fastSMAWindow = RollingWindow[IndicatorDataPoint](2)
self.fast.Updated += self.FastSmaUpdated
self.slowSMAWindow = RollingWindow[IndicatorDataPoint](3)
self.slow.Updated += self.SlowSmaUpdated
def FastSmaUpdated(self, sender, updated):
if self.fast.IsReady:
self.fastSMAWindow.Add(updated)
def SlowSmaUpdated(self, sender, updated):
if self.slow.IsReady:
self.slowSMAWindow.Add(updated)
SIG_94
Hi Derek,
Great now I can set it properly, thank you.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!