private Stochastic stochastic;
protected override void OnStart()
{
stochastic = Indicators.Create<Stochastic>(Bars.LowPrices, Bars.HighPrices, Bars.ClosePrices, 14, 3, 3, 0, SmoothMethod.Exponential);
}
protected override void OnTick()
{
// Check if a new bar has formed
if (Bars.OpenTimes.LastValue > lastBarTime)
{
lastBarTime = Bars.OpenTimes.LastValue;
int index = Bars.ClosePrices.Count - 1; // Index of the last closed bar
if (fastMA.Result.HasCrossedAbove(slowMA.Result, index) && IsHigherTimeframeUptrend())
{
// Calculate Stochastic Oscillator values
double mainLineValue = stochastic.MainLine.Result.LastValue;
double signalLineValue = stochastic.SignalLine.Result.LastValue;
double entryPrice = Symbol.Bid; // You can use Ask for buy orders
double stopLoss = entryPrice - Symbol.PipSize * 20; // Example stop loss
double takeProfit = entryPrice + Symbol.PipSize * 40; // Example take profit
// Check Stochastic Oscillator conditions
if (mainLineValue > signalLineValue && mainLineValue > 20)
{
TradeResult buyResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Symbol.LotSize),
"Buy Order", stopLoss, takeProfit, null);
if (buyResult.IsSuccessful)
{
Print("Buy order executed at price: " + buyResult.EntryPrice);
}
else
{
Print("Buy order failed: " + buyResult.Error);
}
}
}
}
}