Topics
16 Oct 2024, 19:30
 116
 3
12 Sep 2024, 18:33
 122
 1
Replies

rosscortb
18 Oct 2024, 09:55

RE: Stop Loss Calculation

firemyst said: 

Depends on what you want.

You're getting the LastValue, which is value as of the moment it's taken in the current bar. As you know, values for the current bar can change depending on what the price does. 

So at the beginning of the bar for example, the top band could be one value, but then if price really skyrockets and the band expands, the top band could end up being another value by the close of the bar.

If that's what you want, fine.

Perhaps what you want is the value of the previous bar when price closed? With Bollinger Bands, that shouldn't change. So instead of getting the .LastValue, you need to get .Last(1).

 

@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

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