Bug in History/HistoricalTrade

Created at 24 Oct 2023, 10:56
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
VE

VEI5S6C4OUNT0

Joined 06.12.2022

Bug in History/HistoricalTrade
24 Oct 2023, 10:56


This is a method I have used before but now I have found an issue.

// Code for bug report, When history loop is commented out this cBot trades as expected
// If ussing the history , then there is no trade action.
// I have used this method in the past in other cBots with success but I can not find the problem here
// Comment out as indicated to confirm working cBot   // <<<<<                                          <<<<<                                          <<<<<

using System ;
using System.Linq ;
using cAlgo.API ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo.Robots

{ [ Robot ( TimeZone = TimeZones.UTC , AccessRights = AccessRights.None ) ]
  public class Wild2s : Robot {
  
      [ Parameter ( "Start Hour" , DefaultValue = 1 , Step = 1 ) ]
      public double SR { get ; set ; }
      
      [ Parameter ( "Stop Hour" , DefaultValue = 6 , Step = 1 ) ]
      public double SP { get ; set ; }
      
      [ Parameter ( "Per Account" , DefaultValue = 1 , MinValue = 0.1 ) ]
      public double Q { get ; set ; }
      
      [ Parameter ( "Stop Loss" , DefaultValue = 58 , Step = 1 ) ]
      public int SL { get ; set ; }
      
      [ Parameter ( "Take Profit" , DefaultValue = 78 , Step = 1 ) ]
      public int TP { get ; set ; }
      
      [ Parameter ( "MaxPosi" , DefaultValue = 2 , MinValue = 1 ) ]
      public int MaxPos { get ; set ; }
      
      [ Parameter ( "Fast Periods" , DefaultValue = 8 , Step = 1 ) ]
      public int FPs { get ; set ; }
      
      [ Parameter ( "Slow Periods" , DefaultValue = 32 , Step = 1 ) ]
      public int SPs { get ; set ; }
      
      [ Parameter ( "Mega Periods" , DefaultValue = 96 , Step = 1 ) ]
      public int MPs { get ; set ; }
      
      public DataSeries Source { get ; set ; }
      
      private MovingAverage fastMa ;
      private MovingAverage slowMa ;
      private MovingAverage megaMa ;
      private const string lab = "Wild2s" ;
      
      public History HIST { get ; }  // <<<<<                                          <<<<<                                          <<<<<
      
      protected override void OnStart ( )
       {
        fastMa = Indicators.MovingAverage ( Bars.ClosePrices , FPs , MovingAverageType.WilderSmoothing ) ;
        slowMa = Indicators.MovingAverage ( Bars.ClosePrices , SPs , MovingAverageType.WilderSmoothing ) ;
        megaMa = Indicators.MovingAverage ( Bars.ClosePrices , MPs , MovingAverageType.WilderSmoothing ) ;
       }
      protected override void OnBar ( )
       {

        var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  // <<<<<                                          <<<<<                                          <<<<<

        var Units  = Symbol.NormalizeVolumeInUnits ( Q * Account.Balance , RoundingMode.Down ) ;
        var cFMa = fastMa.Result.Last ( 1 ) ;
        var cSMa = slowMa.Result.Last ( 1 ) ;
        var cMMa = megaMa.Result.Last ( 1 ) ;
        var pFMa = fastMa.Result.Last ( 2 ) ;
        var pSMa = slowMa.Result.Last ( 2 ) ;
        var MaxPos2 = MaxPos * 2 ;
        var MaxPos1 = MaxPos * 1 ;
        var PC = Positions.Count ;
        var TIMENOW = Server.Time.TimeOfDay.TotalHours ;
         {
          if ( TIMENOW > SR && TIMENOW < SP )
           {
            foreach ( var trade in HIST )  // <<<<<                                          <<<<<                                          <<<<<
             {
              if ( trade == null || trade.NetProfit < 0 )  // <<<<<                                          <<<<<                                          <<<<<
               {
                if ( cSMa < cMMa && cFMa > cSMa && pFMa < pSMa && PC > MaxPos1 && PC < MaxPos2 )
                 {
                  ExecuteMarketOrder ( TradeType.Buy , SymbolName , Units , lab , SL , TP ) ;
                 }
                if ( cSMa > cMMa && cFMa < cSMa && pFMa > pSMa && PC > MaxPos1 && PC < MaxPos2 )
                 {
                  ExecuteMarketOrder ( TradeType.Sell , SymbolName , Units , lab , SL , TP ) ;
                 }
               }
              else if ( trade != null && trade.NetProfit > 0 )  // <<<<<                                          <<<<<                                          <<<<<
               {
                if ( cSMa < cMMa && cFMa > cSMa && pFMa < pSMa && PC < MaxPos1 )
                 {
                  ExecuteMarketOrder ( TradeType.Buy , SymbolName , Units * 2 , lab , SL , TP ) ;
                 }
                if ( cSMa > cMMa && cFMa < cSMa && pFMa > pSMa && PC < MaxPos1 )
                 {
                  ExecuteMarketOrder ( TradeType.Sell , SymbolName , Units * 2 , lab , SL , TP ) ;
                 }
               }
             }
           }
         }
       }
     }
   }


@VEI5S6C4OUNT0
Replies

firemyst
27 Oct 2023, 03:26

Have you written anything to the logs or confirmed that the HIST object actually has data and isn't null?

var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  


 


@firemyst

VEI5S6C4OUNT0
31 Oct 2023, 03:29

RE: Bug in History/HistoricalTrade

firemyst said: 

Have you written anything to the logs or confirmed that the HIST object actually has data and isn't null?

var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  


 

Yes I tried adding an ExecuteMarketOrder (…….. as part of OnStart  and still no trades after that one , it just does nothing.


@VEI5S6C4OUNT0

firemyst
31 Oct 2023, 04:03

RE: RE: Bug in History/HistoricalTrade

VEI5S6C4OUNT0 said: 

firemyst said: 

Have you written anything to the logs or confirmed that the HIST object actually has data and isn't null?

var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  


 

Yes I tried adding an ExecuteMarketOrder (…….. as part of OnStart  and still no trades after that one , it just does nothing.

UPdate the entirety of your code with a Print statement after every conditional check to see where it gets to.

 

Unless you're using Visual studio and can debug?


@firemyst

VEI5S6C4OUNT0
31 Oct 2023, 05:56 ( Updated at: 31 Oct 2023, 07:02 )

RE: RE: RE: Bug in History/HistoricalTrade

firemyst said: 

VEI5S6C4OUNT0 said: 

firemyst said: 

Have you written anything to the logs or confirmed that the HIST object actually has data and isn't null?

var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  


 

Yes I tried adding an ExecuteMarketOrder (…….. as part of OnStart  and still no trades after that one , it just does nothing.

UPdate the entirety of your code with a Print statement after every conditional check to see where it gets to.

 

Unless you're using Visual studio and can debug?

I see what you mean , as the log show bot>started and bot >stopped at the end of a back test but no explanation as what is happening , I'll try that and see what happens.

So here is what I did 

 protected override void OnStart ( )
       {
        fastMa = Indicators.MovingAverage ( Bars.ClosePrices , FPs , MovingAverageType.WilderSmoothing ) ;
        slowMa = Indicators.MovingAverage ( Bars.ClosePrices , SPs , MovingAverageType.WilderSmoothing ) ;
        megaMa = Indicators.MovingAverage ( Bars.ClosePrices , MPs , MovingAverageType.WilderSmoothing ) ;
       Print("Bot OnStart");
       }
      protected override void OnBar ( )
       {Print("Bot OnBar");

        var HIST = History.OrderByDescending ( trade => trade.ClosingTime ) .Take ( 1 ) ;  // <<<<<
        
        var Units  = Symbol.NormalizeVolumeInUnits ( Q * Account.Balance , RoundingMode.Down ) ;
        var cFMa = fastMa.Result.Last ( 1 ) ;
        var cSMa = slowMa.Result.Last ( 1 ) ;
        var cMMa = megaMa.Result.Last ( 1 ) ;
        var pFMa = fastMa.Result.Last ( 2 ) ;
        var pSMa = slowMa.Result.Last ( 2 ) ;
        var MaxPos2 = MaxPos * 2 ;
        var MaxPos1 = MaxPos * 1 ;
        var PC = Positions.Count ;
        var TIMENOW = Server.Time.TimeOfDay.TotalHours ;
         {Print("TIMENOW Checking Time...");
          if ( TIMENOW < SR || TIMENOW > SP )
          {Print ("Time Check False");}
          if ( TIMENOW > SR && TIMENOW < SP )
           {Print("Time Check True");{
            foreach ( var trade in HIST )  // <<<<<
             {Print("HIST Ck");{
              if ( trade == null || trade.NetProfit < 0 )  // <<<<<
               {Print("Trade is Null or NetPro Less Than Ck True");{
                if ( cSMa < cMMa && cFMa > cSMa && pFMa < pSMa && PC > MaxPos1 && PC < MaxPos2 )
                 {Print("MA Ck - E M O Bx1");{
                  ExecuteMarketOrder ( TradeType.Buy , SymbolName , Units , lab , SL , TP ) ;
                 }
               }
                if ( cSMa > cMMa && cFMa < cSMa && pFMa > pSMa && PC > MaxPos1 && PC < MaxPos2 )
                 {Print("MA Ck - E M O Sx1");{
                  ExecuteMarketOrder ( TradeType.Sell , SymbolName , Units , lab , SL , TP ) ;
                 }
               }
             }
           }
              else if ( trade != null && trade.NetProfit > 0 )  // <<<<<
               {Print("Trade is Null Ck False and NetPro More Than Ck True");{
                if ( cSMa < cMMa && cFMa > cSMa && pFMa < pSMa && PC < MaxPos1 )
                 {Print("MA Ck - E M O Bx2");{
                  ExecuteMarketOrder ( TradeType.Buy , SymbolName , Units * 2 , lab , SL , TP ) ;
                 }}
                if ( cSMa > cMMa && cFMa < cSMa && pFMa > pSMa && PC < MaxPos1 )
                 {Print("MA Ck - E M O Sx2");}
                  ExecuteMarketOrder ( TradeType.Sell , SymbolName , Units * 2 , lab , SL , TP ) ;
                 }}}
               }
             }
           }
         }
       }
       protected override void OnStop()
       {Print ("Bot OnStop");}
     }
   }

It can check the TIMENOW and prints “Time Check True” or “Time Check False”

and the next log is “Bot OnBar” again so it has a problem with the foreach statement ???


@VEI5S6C4OUNT0