Topics
Replies
rosscortb
03 Oct 2024, 14:16
( Updated at: 04 Oct 2024, 05:17 )
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DoubleSMABotinTick : Robot
{
[Parameter("Volume", DefaultValue = 10000)]
public int volume { get; set; }
[Parameter("SMA1 Period", DefaultValue = 3)]
public int PeriodsSma1 { get; set; }
[Parameter("SMA2 Period", DefaultValue = 3)]
public int PeriodsSma2 { get; set; }
[Parameter("SMA1 Pips", DefaultValue = 5)]
public int SMA1_Pips { get; set; }
[Parameter("SMA2 Pips", DefaultValue = 5)]
public int SMA2_Pips { get; set; }
[Parameter("Take Profit", DefaultValue = 5)]
public int TP { get; set; }
[Parameter("Stop Loss", DefaultValue = 5)]
public int SL { get; set; }
private SimpleMovingAverage _sma1;
private SimpleMovingAverage _sma2;
protected override void OnStart()
{
_sma1 = Indicators.SimpleMovingAverage(Bars.HighPrices, PeriodsSma1);
_sma2 = Indicators.SimpleMovingAverage(Bars.LowPrices, PeriodsSma2);
}
protected override void OnTick()
{
double pipValue = Symbol.PipSize * SMA1_Pips;
if (IsPoOpen() && Symbol.Bid > _sma1.Result.LastValue + pipValue)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "DSMA", SL, TP);
}
if (IsPoOpen() && Symbol.Ask < _sma2.Result.LastValue - pipValue)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "DSMA", SL, TP);
}
}
protected bool IsPoOpen()
{
var pos = Positions.FindAll("DSMA", SymbolName);
return pos.Length == 0;
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@rosscortb
rosscortb
18 Oct 2024, 09:55
RE: Stop Loss Calculation
firemyst said:
@firemyst
Thanks for your reply.
This never really worked me, been struggling with this for a while. When back testing I would expect to see a stop loss line or Take profit line above or below the bands. No matter what pip level I set the variable to just stays the same.
@rosscortb