Calculating Highest High and Lowest Low over the past X bars
Created at 20 Dec 2023, 01:18
Calculating Highest High and Lowest Low over the past X bars
20 Dec 2023, 01:18
Hello,
I'm trying to calculate the highest highs and the lowest lows over the past few bars. I have the following script, but it keeps printing out the same values every bar. Can someone see what I'm doing wrong?
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Robot()]
public class TradingBot : Robot
{
// Define your variables and indicators here
private double entryHighestHigh;
private double entryLowestLow;
private double exitHighestHigh;
private double exitLowestLow;
// Input parameters
[Parameter("Entry Lookback Period", DefaultValue = 50, MinValue = 1)]
public int EntryNumberOfBars { get; set; }
[Parameter("Exit Lookback Period", DefaultValue = 25, MinValue = 1)]
public int ExitNumberOfBars { get; set; }
[Parameter("Volume (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double VolumeInLots { get; set; }
protected override void OnTick()
{
// Calculate entry and exit highest highs and lowest lows
entryHighestHigh = Bars.HighPrices.Maximum(EntryNumberOfBars);
entryLowestLow = Bars.LowPrices.Maximum(EntryNumberOfBars);
exitHighestHigh = Bars.HighPrices.Maximum(ExitNumberOfBars);
exitLowestLow = Bars.LowPrices.Maximum(ExitNumberOfBars);
// Print entry and exit highest highs and lowest lows
Print("New day started");
Print("Entry Highest High: {0}, Entry Lowest Low: {1}", entryHighestHigh, entryLowestLow);
Print("Exit Highest High: {0}, Exit Lowest Low: {1}", exitHighestHigh, exitLowestLow);
// Check and execute orders
if (Bars.HighPrices[0] > entryHighestHigh)
{
ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, VolumeInUnits, "Long Entry", 0, 0, PositionOpened);
}
if (Bars.LowPrices[0] < entryLowestLow)
{
ExecuteMarketOrderAsync(TradeType.Sell, SymbolName, VolumeInUnits, "Short Entry", 0, 0, PositionOpened);
}
// Check for exit conditions
var position = Positions.Find("HighLowTraderPosition");
if (position != null)
{
if (Bars.LowPrices[0] < exitLowestLow || Bars.HighPrices[0] > exitHighestHigh)
{
ClosePositionAsync(position, PositionClosed);
Stop();
}
}
}
// Callback function for position opened
private void PositionOpened(TradeResult tradeResult)
{
var position = tradeResult.Position;
if (tradeResult.IsSuccessful)
{
Print("Position {0} opened at {1}", position.Id, position.EntryPrice);
}
else
{
Print("Failed to open position. Error: {0}", tradeResult.Error);
}
}
// Callback function for position closed
private void PositionClosed(TradeResult tradeResult)
{
var position = tradeResult.Position;
if (tradeResult.IsSuccessful)
{
Print("Position {0} closed. Gross profit: {1}", position.Id, position.GrossProfit);
}
else
{
Print("Failed to close position. Error: {0}", tradeResult.Error);
}
}
//Calculate Lots to Units
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(VolumeInLots); }
}
// Add other methods and logic as needed
}
}
PanagiotisCharalampous
20 Dec 2023, 06:42
Hi there,
An obvious mistake is that you use Maximum for the low prices, while you should use Minimum. From there and on, you should provide more specific information, like examples of what you get and what you expected to get instead.
Best regards,
Panagiotis
@PanagiotisCharalampous