Replies

samyelzallat
13 Nov 2024, 11:16 ( Updated at: 13 Nov 2024, 11:22 )

RE: RE: Error | CBot instance crashed with error #ED2F94F3.

PanagiotisCharalampous said: 

samyelzallat said: 

when I remove onbarclosed from the bot and use onbar it doesn't cause an error

Hi there,

Please provide the complete cBot code

Best regards,

Panagiotis

using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace cAlgo.Robots {
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)] public class WaveRider: Robot {
        [Parameter("Trade Volume (in units)", DefaultValue = 2000, MinValue = 2000, Step = 2000)] public double TradeVolume {
          get;
          set;
        } [Parameter("Partial Take Profit (in units)", DefaultValue = 1000, MinValue = 1000, Step = 1000)] public double VolumeToClose {
          get;
          set;
        } [Parameter("ATR Period", Group = "ATR", DefaultValue = 20)] public int AtrPeriod {
          get;
          set;
        } [Parameter("ATR MA Type", Group = "ATR", DefaultValue = MovingAverageType.Simple)] public MovingAverageType AtrMa {
          get;
          set;
        }
        private AverageTrueRange atr;
        [Parameter("First Source", DefaultValue = "Open", Group = "Moving Averages")] public DataSeries OpenSmaSource {
          get;
          set;
        } [Parameter("Second Source", DefaultValue = "Close", Group = "Moving Averages")] public DataSeries CloseSmaSource {
          get;
          set;
        } [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 20)] public int SmaPeriod {
          get;
          set;
        } [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 100)] public int Sma2Period {
          get;
          set;
        }
        private SimpleMovingAverage OpenSma;
        private SimpleMovingAverage CloseSma;
        private SimpleMovingAverage BiggerSma;
        private
        const string myComment = "Trendat";
        private bool FirstTradeActive = false;
        private bool IsItLong = false;
        private bool IsItShort = false;
        private bool ModifiedOnce = false;
        private bool ModifiedTwice = false;
        private bool MultipleTrades = false;
        private bool GridStopped = false;
        private bool StepOne = false;
        private bool StepTwo = false;
        private bool GridStopLossSet = false;
        private double MultiplierReference = 0;
        private double GlobalAtrThreshold;
        private double GlobalFirstEntry;
        private double GlobalExitProfit;
        private double GlobalExitMeasure;
        private double GlobalGrid;
        private double GridSl;
        double GridModifiedSL;
        private double LotsTraded = 0;
        protected override void OnStart() {
          atr = Indicators.AverageTrueRange(AtrPeriod, AtrMa);
          OpenSma = Indicators.SimpleMovingAverage(OpenSmaSource, SmaPeriod);
          CloseSma = Indicators.SimpleMovingAverage(CloseSmaSource, SmaPeriod);
          BiggerSma = Indicators.SimpleMovingAverage(CloseSmaSource, Sma2Period);
        }
        protected override void OnBarClosed() {
          double closingPrice = Bars.ClosePrices.Last(1);
          double openingPrice = Bars.OpenPrices.Last(1);
          double closingPrice2ago = Bars.ClosePrices.Last(2);
          double openingPrice2ago = Bars.OpenPrices.Last(2);
          double closingPrice3ago = Bars.ClosePrices.Last(3);
          double openingPrice3ago = Bars.OpenPrices.Last(3);
          double closingPrice4ago = Bars.ClosePrices.Last(4);
          double openingPrice4ago = Bars.OpenPrices.Last(4);
          double closingPrice5ago = Bars.ClosePrices.Last(5);
          double openingPrice5ago = Bars.OpenPrices.Last(5);
          double closingPrice6ago = Bars.ClosePrices.Last(6);
          double openingPrice6ago = Bars.OpenPrices.Last(6);
          double closingPrice7ago = Bars.ClosePrices.Last(7);
          double openingPrice7ago = Bars.OpenPrices.Last(7);
          double closingPrice8ago = Bars.ClosePrices.Last(8);
          double openingPrice8ago = Bars.OpenPrices.Last(8);
          double closingPrice9ago = Bars.ClosePrices.Last(9);
          double openingPrice9ago = Bars.OpenPrices.Last(9);
          double closingPrice10ago = Bars.ClosePrices.Last(10);
          double openingPrice10ago = Bars.OpenPrices.Last(10);
          double OpenSmaLast = OpenSma.Result.Last(1);
          double OpenSma3Ago = OpenSma.Result.Last(3);
          double CloseSmaLast = CloseSma.Result.Last(1);
          double CloseSma3Ago = CloseSma.Result.Last(3);
          double BiggerSmaLast = BiggerSma.Result.Last(1);
          bool BuySignal = CloseSmaLast > OpenSmaLast && OpenSmaLast > BiggerSmaLast && openingPrice3ago < OpenSma3Ago && closingPrice3ago > CloseSma3Ago && closingPrice2ago > openingPrice2ago && closingPrice > openingPrice;
          bool SellSignal = CloseSmaLast < OpenSmaLast && CloseSmaLast < BiggerSmaLast && openingPrice3ago > OpenSma3Ago && closingPrice3ago < CloseSma3Ago && closingPrice2ago < openingPrice2ago && closingPrice < openingPrice;
          double GridRange = Math.Abs(closingPrice2ago - openingPrice8ago);
          double DistanceFromEntry = Math.Abs(GlobalFirstEntry - closingPrice);
          int DistanceMultipled = 0;
          if (GlobalGrid != 0) {
            DistanceMultipled = (int) Math.Floor(DistanceFromEntry / GlobalGrid);
          }
          double SpreadAtr = Math.Round(atr.Result.Last(1) / Symbol.PipSize, 4);
          double StopLossAtr = Math.Round(GridRange / Symbol.PipSize, 4) * 4;
          double spread = Symbol.Spread / Symbol.PipSize;
          double MultipleTradesMeasure = atr.Result.Last(1);
          double MultipleTradesAtr = Math.Round((atr.Result.Last(1)) / Symbol.PipSize, 4);
          GlobalExitProfit = MultipleTradesAtr;
          GlobalExitMeasure = MultipleTradesMeasure;
          var HourlyTf = MarketData.GetBars(TimeFrame.Hour);
          double HrlyOpening = HourlyTf.OpenPrices.Last(0);
          double HrlyHigh = HourlyTf.HighPrices.Last(0);
          double HrlyLow = HourlyTf.LowPrices.Last(0);
          double HrlyOpening2Ago = HourlyTf.OpenPrices.Last(1);
          double HrlyClosing2Ago = HourlyTf.ClosePrices.Last(1);
          double HrlyHigh2Ago = HourlyTf.HighPrices.Last(1);
          double HrlyLow2Ago = HourlyTf.LowPrices.Last(1);
          double HrlyOpening3Ago = HourlyTf.OpenPrices.Last(2);
          double HrlyClosing3Ago = HourlyTf.ClosePrices.Last(2);
          double HrlyCandleBody = Math.Abs(HrlyOpening2Ago - HrlyClosing2Ago);
          double HrlyUpperWick;
          double HrlyLowerWick;
          bool PossibleReverse = false;
          if (HrlyClosing2Ago > HrlyOpening2Ago) {
            HrlyUpperWick = HrlyHigh2Ago - HrlyClosing2Ago;
            HrlyLowerWick = HrlyOpening2Ago - HrlyLow2Ago;
            if (HrlyUpperWick >= 0.4 * HrlyCandleBody) {
              PossibleReverse = true;
            }
          } else if (HrlyClosing2Ago < HrlyOpening2Ago) {
            HrlyUpperWick = HrlyHigh2Ago - HrlyOpening2Ago;
            HrlyLowerWick = HrlyClosing2Ago - HrlyLow2Ago;
            if (HrlyLowerWick >= 0.4 * HrlyCandleBody) {
              PossibleReverse = true;
            }
          }
          bool BullishHr = HrlyClosing2Ago > HrlyOpening2Ago && HrlyClosing3Ago > HrlyOpening3Ago;
          bool BearishHr = HrlyClosing2Ago < HrlyOpening2Ago && HrlyClosing3Ago < HrlyOpening3Ago;
          if (DistanceMultipled > MultiplierReference && !ModifiedOnce && !GridStopped && (IsItLong || IsItShort)) {
            MultiplierReference = DistanceMultipled;
            MultipleTrades = true;
            TradeType tradeType = IsItLong ? TradeType.Buy : TradeType.Sell;
            ExecuteMarketOrder(tradeType, SymbolName, TradeVolume, MultiplierReference.ToString(), GridSl, null, myComment);
            LotsTraded += 0.01;
          }
          if (FirstTradeActive) {
            return;
          }
          if (BuySignal && spread <= .9 && spread < SpreadAtr) {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeVolume, "First Buy", StopLossAtr, null, myComment);
            IsItLong = true;
            FirstTradeActive = true;
            GlobalFirstEntry = Symbol.Ask;
            GlobalAtrThreshold = atr.Result.Last(1);
            GlobalGrid = GridRange;
            GridSl = StopLossAtr;
            LotsTraded += 0.01;
          } else if (SellSignal && spread <= .9 && spread < SpreadAtr) {
            ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeVolume, "First Sell", StopLossAtr, null, myComment);
            IsItShort = true;
            FirstTradeActive = true;
            GlobalFirstEntry = Symbol.Bid;
            GlobalAtrThreshold = atr.Result.Last(1);
            GlobalGrid = GridRange;
            GridSl = StopLossAtr;
            LotsTraded += 0.01;
          }
        }

this will crash until you replace OnBarClosed with OnBar, which I did to have it work
also works with 1hr tf correctly, and it crashes with both fp markets and fusion markets accounts


@samyelzallat

samyelzallat
12 Nov 2024, 15:00

when I remove onbarclosed from the bot and use onbar it doesn't cause an error


@samyelzallat

samyelzallat
12 Nov 2024, 13:47

My cbot crashes after adding sma to the code and backtesting on 10 and 1 minute bars

 [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSma
        {
            get;
            set;
        }
        [Parameter("SMA Period", Group = "Moving Average", DefaultValue = 20)]
        public int SmaPeriod
        {
            get;
            set;
        }
 protected override void OnStart()
        {
Sma20 = Indicators.SimpleMovingAverage(SourceSma, SmaPeriod);
}

 

it also works fine with 1 hr time frame, what had me shaking my head is that old bots I made work , but when i copy the exact same code here to a new cbot it doesn't work


@samyelzallat

samyelzallat
04 Nov 2024, 11:56

Hello , I've been experiencing this too for around 3 days now

it happens when Algo tries to sync, it can only do the first sync when you open the software, then is never able to reconnect and gets stuck
The only solution is restarting over and over

 


@samyelzallat

samyelzallat
22 Oct 2024, 07:06

RE: An issue with multi timeframe code

PanagiotisCharalampous said: 

Hi there,

Your indicator only declares two parameters while you are passing three. The code does not make much sense.

Best regards,

Panagiotis

Hey Panagiotis,
But isn't this how it works?

How Do I declare a custom indicator to get data from another tf?


@samyelzallat

samyelzallat
08 Oct 2024, 09:19 ( Updated at: 08 Oct 2024, 10:44 )

RE: Modify position isn't working

jeffrey597doss said: 

Hello!

It looks like the modify position functions aren’t working as expected. To address this, ensure that GlobalAtrThreshold and GlobalAtrPips are correctly assigned and verify their values through debugging. Check that isTradeActive, IsItLong, and IsItShort flags are set and reset correctly, reflecting the true state of your positions. In the OnTick method, TheDisneyHub ensure the loop iterates correctly and that all conditions for modifying positions are met. Reviewing these variables and conditions should help pinpoint the issue. Does this help clarify things?

Your reply gave so much insight it actually helped,The problem was the way globalatrthreshold is compared to entry pricce, i fixed it so current price for selling eg. - position.Entryprice> threshold, written like that directly in code seemed to work, thanks a lot and have great day


@samyelzallat

samyelzallat
07 Oct 2024, 07:39 ( Updated at: 07 Oct 2024, 12:05 )

RE: This Cbot isn't executing Any trades

PanagiotisCharalampous said: 

Hi there,

It will be hard to get help by just throwing dozens of lines of code and ask for people why it doesn't work. This is because readers don't know what you expect the code to do and what it does instead. Even if they spend time interrogating you, then they will need to spend hours to figure out what you did wrong in your logic. Not many people have this surplus of time.

Did you try debugging your strategy first? Did you place a break point at the line where the conditions are evaluated to check what are the values when a trade is expected to be placed? It will help you understand why the trade is not placed when you expect it to do so.

You need to narrow down the problem to a question that can be answered by somebody in 10-15 minutes before asking for help.

Best regards,

Panagiotis

Thanks a lot, 

I will make an other post with issues Iam still facing and keep it as clear as I can


@samyelzallat