PlaceStopOrder - Stop Loss to be previous days low
PlaceStopOrder - Stop Loss to be previous days low
16 Jul 2020, 00:24
Hi
I am trying to get my stop loss on a Stop Order to be @ the previous days low. My code does'nt seem to work :( may be something simple...
Help needed please. Code below:
// Calculate bars
var PrevDayHigh = dailySeries.High.Last(1);
var PrevDayLow = dailySeries.Low.Last(1);
var PrevDayOpen = dailySeries.Open.Last(1);
var PrevDayClose = dailySeries.Close.Last(1);
// Calculate last price
var prevClose = dailySeries.Close.Last(1);
var prevEma = ema.Result.Last(1);
var SmashBarB = PrevDayClose <= (PrevDayLow + (PrevDayHigh - PrevDayLow) * 0.25);
var SmashBarS = PrevDayClose >= (PrevDayHigh - (PrevDayHigh - PrevDayLow) * 0.25);
pipsize = Symbol.PipSize;
var stoppipsB = ((PrevDayHigh - PrevDayLow) / Symbol.PipSize);
var stoppipsS = ((PrevDayHigh - PrevDayLow) / Symbol.PipSize);
var profitpips = 0;
/* int Volume = Convert.ToInt32(Lots * 100000); */
// Our total balance is our account balance plus any reserve funds. We do not always keep all our money in the trading account.
double totalBalance = Account.Balance;
// Calculate the total risk allowed per trade.
double riskPerTrade = (totalBalance * RiskPercent) / 100;
// Add the stop loss, commission pips and spread to get the total pips used for the volume calculation.
double totalPips = stoppipsB + Symbol.Spread;
// Calculate the exact volume to be traded. Then round the volume to the nearest 100,000 and convert to an int so that it can be returned to the caller.
double exactVolume = Math.Round(riskPerTrade / (Symbol.PipValue * totalPips), 2);
//Choose your own rounding mode
exactVolume = (int)Symbol.NormalizeVolumeInUnits(exactVolume, RoundingMode.Down);
var expiredtime = openTime.AddDays(2);
if (Positions.Count == 0 && SmashBarB == true && prevClose > prevEma && ema.Result.IsRising())
{
PlaceStopOrder(TradeType.Buy, Symbol.Name, exactVolume, PrevDayHigh + bslevel * Symbol.PipSize, InstanceName, stoppipsB, profitpips, expiredtime);
// PlaceStopOrder(TradeType.Sell, Symbol.Name, exactVolume, childCandleLow - bslevel * Symbol.PipSize, InstanceName, stoppipsS, profitpips, expiredtime);
}
if (Positions.Count == 0 && SmashBarS == true && prevClose < prevEma && ema.Result.IsFalling())
{
// PlaceStopOrder(TradeType.Buy, Symbol.Name, exactVolume, childCandleHigh + bslevel * Symbol.PipSize, InstanceName, stoppipsB, profitpips, expiredtime);
PlaceStopOrder(TradeType.Sell, Symbol.Name, exactVolume, PrevDayLow - bslevel * Symbol.PipSize, InstanceName, stoppipsS, profitpips, expiredtime);
}
Replies
samuelbreezey
16 Jul 2020, 22:16
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi samuelbreezey,
Can you please explain what is not working. What does it do and what would you expect it to do instead?
Best Regards,
Panagiotis
Hi Panagiotis,
See the below, I was expecting the stop loss to be where I have drawn the red line.
Many thanks
Sam
@samuelbreezey
PanagiotisCharalampous
17 Jul 2020, 08:25
Hi Sam,
Can you provide the complete cBot code so that we can reproduce this and explain what happens?
Best Regards,
Panagiotis
@PanagiotisCharalampous
samuelbreezey
18 Jul 2020, 11:03
RE:
PanagiotisCharalampous said:
Hi Sam,
Can you provide the complete cBot code so that we can reproduce this and explain what happens?
Best Regards,
Panagiotis
Hi Panagiotis,
Yes sure, see below:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
/*
* INSIDE BAR STRATEGY
*
* 07.11.2019 Moving average filter added
* 09.11.2019 Inside Bar Ratio added
* 14.11.2019 % risk lot sizing added
* 22.11.2019 % stop loss and take profit added
* prev candle within 25% of its low price for the day
*/
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter("Instance Name", DefaultValue = "Smash Bar")]
public string InstanceName { get; set; }
[Parameter("Another Timeframe", DefaultValue = "Hour")]
public TimeFrame AnotherTimeFrame { get; set; }
[Parameter("Risk Percentage", Group = "Money Management", DefaultValue = 1.0, MinValue = 0.1, MaxValue = 10.0)]
public double RiskPercent { get; set; }
[Parameter("Buy/Sell Order Level (pips)", DefaultValue = 4)]
public double bslevel { get; set; }
/* Moving Average */
[Parameter("MA Type", Group = "Moving Average")]
public MovingAverageType MAType { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "Moving Average", DefaultValue = 10)]
public int Periods { get; set; }
/* Money Management */
private MovingAverage ema;
private double pipsize = 0;
protected override void OnStart()
{
ema = Indicators.MovingAverage(Source, Periods, MAType);
}
protected override void OnTick()
{
if (Positions.Count == 1)
{
foreach (var order in PendingOrders)
{
CancelPendingOrder(order);
}
}
}
protected override void OnBar()
{
var dailySeries = MarketData.GetSeries(AnotherTimeFrame);
var openTime = dailySeries.OpenTime.Last(1);
// Calculate bars
var PrevDayHigh = dailySeries.High.Last(1);
var PrevDayLow = dailySeries.Low.Last(1);
var PrevDayOpen = dailySeries.Open.Last(1);
var PrevDayClose = dailySeries.Close.Last(1);
// Calculate last price
var prevClose = dailySeries.Close.Last(1);
var prevEma = ema.Result.Last(1);
var SmashBarB = PrevDayClose <= (PrevDayLow + (PrevDayHigh - PrevDayLow) * 0.25);
var SmashBarS = PrevDayClose >= (PrevDayHigh - (PrevDayHigh - PrevDayLow) * 0.25);
pipsize = Symbol.PipSize;
var stoppipsB = ((PrevDayHigh - PrevDayLow) / Symbol.PipSize);
var stoppipsS = ((PrevDayHigh - PrevDayLow) / Symbol.PipSize);
var profitpips = 0;
/* int Volume = Convert.ToInt32(Lots * 100000); */
// Our total balance is our account balance plus any reserve funds. We do not always keep all our money in the trading account.
double totalBalance = Account.Balance;
// Calculate the total risk allowed per trade.
double riskPerTrade = (totalBalance * RiskPercent) / 100;
// Add the stop loss, commission pips and spread to get the total pips used for the volume calculation.
double totalPips = stoppipsB + Symbol.Spread;
// Calculate the exact volume to be traded. Then round the volume to the nearest 100,000 and convert to an int so that it can be returned to the caller.
double exactVolume = Math.Round(riskPerTrade / (Symbol.PipValue * totalPips), 2);
//Choose your own rounding mode
exactVolume = (int)Symbol.NormalizeVolumeInUnits(exactVolume, RoundingMode.Down);
var expiredtime = openTime.AddDays(2);
if (Positions.Count == 0 && SmashBarB == true && prevClose > prevEma && ema.Result.IsRising())
{
PlaceStopOrder(TradeType.Buy, Symbol.Name, exactVolume, PrevDayHigh + bslevel * Symbol.PipSize, InstanceName, stoppipsB, profitpips, expiredtime);
// PlaceStopOrder(TradeType.Sell, Symbol.Name, exactVolume, childCandleLow - bslevel * Symbol.PipSize, InstanceName, stoppipsS, profitpips, expiredtime);
}
if (Positions.Count == 0 && SmashBarS == true && prevClose < prevEma && ema.Result.IsFalling())
{
// PlaceStopOrder(TradeType.Buy, Symbol.Name, exactVolume, childCandleHigh + bslevel * Symbol.PipSize, InstanceName, stoppipsB, profitpips, expiredtime);
PlaceStopOrder(TradeType.Sell, Symbol.Name, exactVolume, PrevDayLow - bslevel * Symbol.PipSize, InstanceName, stoppipsS, profitpips, expiredtime);
}
}
/*
foreach (var position in Positions)
{
var shortPosition = Positions.Find(InstanceName, SymbolName, TradeType.Sell);
var longPosition = Positions.Find(InstanceName, SymbolName, TradeType.Buy);
var shortPositionsCount = Positions.Count(p => p.TradeType == TradeType.Sell);
var longPositionsCount = Positions.Count(p => p.TradeType == TradeType.Buy);
if (Positions.Count == 1 && longPositionsCount > 0)
{
Positions[0].ModifyStopLossPrice(ema.Result.Last(1));
}
else if (Positions.Count == 1 && shortPositionsCount > 0)
{
Positions[0].ModifyStopLossPrice(ema.Result.Last(1));
}
}
*/
// stop loss at low of prev bar
// var ma = Indicators.SimpleMovingAverage(Bars.ClosePrices, 14);
// Positions[0].ModifyStopLossPrice(ma.Result.Last(1));
/* foreach (var position in Positions)
{
var shortPosition = Positions.Find(InstanceName, SymbolName, TradeType.Sell);
var longPosition = Positions.Find(InstanceName, SymbolName, TradeType.Buy);
var shortPositionsCount = Positions.Count(p => p.TradeType == TradeType.Sell);
var longPositionsCount = Positions.Count(p => p.TradeType == TradeType.Buy);
if (shortPositionsCount > 0)
{
Positions[0].ModifyStopLossPrice(ema.Result.Last(1));
}
else if (longPositionsCount > 0)
Positions[0].ModifyStopLossPrice(ema.Result.Last(1));
}
*/
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Thank you
Sam
@samuelbreezey
PanagiotisCharalampous
20 Jul 2020, 08:59
Hi Sam,
I checked it and seems to work fine for me.
Best Regards,
Panagiotis
@PanagiotisCharalampous
samuelbreezey
20 Jul 2020, 19:27
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi Sam,
I checked it and seems to work fine for me.
Best Regards,
Panagiotis
Thank you Panagiotis,
I tried on the EURUSD oair like you did, however it still does not appear to work. See the following:
The order is being places as at the circle candle and I am expecting the stop loss to be where I have drawn the line.
@samuelbreezey
PanagiotisCharalampous
21 Jul 2020, 08:22
Hi Sam,
What parameters are you using?
Best Regards,
Panagiotis
@PanagiotisCharalampous
samuelbreezey
21 Jul 2020, 11:03
RE:
PanagiotisCharalampous said:
Hi Sam,
What parameters are you using?
Best Regards,
Panagiotis
Sorry, it does appear to work. The timeframe was not set to daily in the parameters.
Thank you for your time :)
Samuel
@samuelbreezey
PanagiotisCharalampous
16 Jul 2020, 08:39
Hi samuelbreezey,
Can you please explain what is not working. What does it do and what would you expect it to do instead?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous