Overall Statistics
import numpy as np
from System.Drawing import Color

from decimal import Decimal

class stochasticIndicatorExample(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2017, 6, 1)
        self.SetEndDate(2019, 1, 1)
        self.SetCash(5000)
        self.forex = self.AddForex("EURUSD", Resolution.Daily, Market.FXCM)
        self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
        
        
        # Set the Stochastic Indicator
        self.stochasticIndicator = self.STO(self.forex.Symbol, 14,14, 3, Resolution.Daily)

        
        # Set parameters for "Trade Plot"
        self.overBought = 80
        self.overSold = 20
        
        
        # Construct "Trade Plot"
        IndicatorPlot = Chart("Trade Plot")
        IndicatorPlot.AddSeries(Series("D", SeriesType.Line,"", Color.Red))
        IndicatorPlot.AddSeries(Series("K", SeriesType.Line,"", Color.Blue))
        IndicatorPlot.AddSeries(Series("Over Bought", SeriesType.Line,"", Color.Black))
        IndicatorPlot.AddSeries(Series("Over Sold", SeriesType.Line, "", Color.Black))

        self.AddChart(IndicatorPlot)
        
    def OnData(self,data):
        if not self.stochasticIndicator.IsReady: return

        stochD=self.stochasticIndicator.StochD
        stochK=self.stochasticIndicator.StochK

        # Add values to "Trade Plot"
        self.Plot("Trade Plot", "D", str(stochD))
        self.Plot("Trade Plot", "K", str(stochK))
        self.Plot("Trade Plot", "Over Bought", str(self.overBought))
        self.Plot("Trade Plot", "Over Sold", str(self.overSold))

        # Enter a long position when the current value of the indicator is less than the 20
        if not self.Portfolio.Invested and stochD.Current.Value < self.overSold:
            
                self.MarketOrder(self.forex.Symbol,10000)

        # Liquidate position when the current value of the indicator is greater than 80
        elif self.Portfolio.Invested and stochD.Current.Value > self.overBought:
                
                self.Liquidate()