This board has been archived, no new registrations are allowed. Please come join us on our discord!

This script is logging price data too often

General discussion about the C# Script Indicators, Safeties and Insurances
hlalibe
Registered User
Posts: 6
Joined: Tue Oct 24, 2017 12:06 pm

This script is logging price data too often

Postby hlalibe » Mon Nov 20, 2017 9:03 pm

Hi guys,,

not sure what is wrong with this script, i use it as indicator script, but it spits out data 5 times per second in my log file, the closing price is not in line with the closing price candle , not sure where the data is coming from. I used the same core part of the script as insurance script and the data was fine, i would get one log per minute>

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TradeServer.ScriptingDriver.DataObjects;
using TradeServer.ScriptingDriver.Interfaces;

namespace ExampleNameSpace
{
    public class hllPrcPriceFall : IIndicatorScript
    {
      // hll from SimpleMovingAverage script
        private int neededHistorryTicks = 1; // default field, used to store the needed depth
        private int longLength = 35;
        private int shortLength = 12;

        private double ma_short = 0;
        private double ma_long = 0;
 
   // hll
   // private double targetPriceChange = 10;
   private double targetPriceFallPrc = 2;
   private double LastCyclePrice = 0;
   private double MaxPrice = 0;
   private bool BuyOK = false;

        // Properties
        public string Name
        {
            get { return "hllPrcPriceFall"; }
        }

      
        public int TicksBack
        {
            get { return 1; }
        }

        public List<ScriptParameter> GetParameters()
        {
            List<ScriptParameter> res = new List<ScriptParameter>();
            res.Add(new ScriptParameter("Change %", ScriptParameterType.Decimal, targetPriceFallPrc.ToString()));
            return res;
        }

        public void SetParameters(Dictionary<string, object> parameters)
        {
            foreach (var item in parameters) // lets just loop them all
            {
                if (item.Key == "Change") { targetPriceFallPrc = Convert.ToDouble(item.Value); }
            }
        }

        // Chart lines
        public List<ChartRecord> GetChartLines()
        {
            List<ChartRecord> chartLines = new List<ChartRecord>();
            chartLines.Add(new ChartRecord() { ChartIndex = 0, Name = "Short", HexLineColor = "#FF00FF" });
            chartLines.Add(new ChartRecord() { ChartIndex = 0, Name = "Long", HexLineColor = "#00FFFF" });

            return chartLines;
        }
        public List<decimal> GetChartData()
        {
            List<decimal> chartData = new List<decimal>();
            chartData.Add(Convert.ToDecimal(ma_short));
            chartData.Add(Convert.ToDecimal(ma_long));

           // context.Logger.LogToFile(string.Format("Called short {0} and long {1}", ma_short, ma_long));

            return chartData;
        }   
      
      
        public void Init()
        {
            // do nothing
        }

        // This is the brain so the say, this is where the calculation is done and an output must be produced.
        public IndicatorResult GetResult(IndicatorContext context)
        {
         
      // debug text
      context.Logger.LogToFile("*****************************************************");
      context.Logger.LogToFile("Current Date&Ttime= " + DateTime.Now.ToString("dd.MM h:mm:ss"));
      context.Logger.LogToFile("PriceTick.Close= " + context.PriceTick.Close);
      context.Logger.LogToFile("LastBuy= " + (double)context.LastBuyPrice);
      context.Logger.LogToFile("LastBuy-2%= " + ((double)context.LastBuyPrice - (targetPriceFallPrc/100)*(double)context.LastBuyPrice));
      context.Logger.LogToFile("LastCyclePrice= " + LastCyclePrice);
      context.Logger.LogToFile("MaxPrice= " + MaxPrice);

      if (context.PriceTick.Close < ((double)context.LastBuyPrice - (targetPriceFallPrc/100)*(double)context.LastBuyPrice)) // check if price below last buy by 2%
      {   
         context.Logger.LogToFile(string.Format("Price < LastBuyPrice - 2%"));            
         BuyOK = true;
      }
      else
      {    
      context.Logger.LogToFile(string.Format("Price > LastBuyPrice - 2%"));
         if (context.PriceTick.Close > (LastCyclePrice)) // check if price greater than last cycle price
         {
            if (context.PriceTick.Close > (MaxPrice))  // check if price greater than MaxPrice
            {
               MaxPrice = context.PriceTick.Close;
               context.Logger.LogToFile("MaxPrice= " + (MaxPrice));
               LastCyclePrice = context.PriceTick.Close;
               BuyOK = false;
               context.Logger.LogToFile("Price>LastCyclePrice and Price>MaxPrice");
            }
            else
            {   
               LastCyclePrice = context.PriceTick.Close;
               BuyOK = false;
               context.Logger.LogToFile("Price>LastCyclePrice and Price<MaxPrice");
            }
         }   
         else
         {   
            if (context.PriceTick.Close < ((MaxPrice) - (targetPriceFallPrc/100)*(MaxPrice) ))  // check if price smaller than MaxPrice -20%
            {
               context.Logger.LogToFile(string.Format("Price < MaxPrice - 2%"));
               BuyOK = true;
            }            
            
            else
            {   
               context.Logger.LogToFile(string.Format("Price > MaxPrice - 2%"));
               LastCyclePrice = context.PriceTick.Close;
               BuyOK = false;
            }
         }
      }   

            context.Logger.LogToFile("BuyOK= " + BuyOK.ToString());         
         
         
            try
            {

                if (BuyOK == false)
                    return IndicatorResult.SellShort;
            else
               return IndicatorResult.BuyLong;


            }
            catch (Exception ex) // This try catch block is an highly recommend practice
            {
                context.Logger.LogToFile(string.Format("Exception detected: {0}", ex.Message));
            }

            return IndicatorResult.StayNoPosition; // Default output
        }
    }
}

User avatar
pshai
King of Scripts
Posts: 284
Joined: Fri Mar 18, 2016 11:33 am

Re: This script is logging price data too often

Postby pshai » Fri Dec 08, 2017 3:36 am

Hello hlalibe,

I can see your script is filled with these:

Code: Select all

context.Logger.LogToFile();


So if you want to get lesser logs, I suggest you get rid of these functions in your script. You can also do all kinds of limitations for them: "1 set of logs per minute", or "5 times in one minute" etc.


Hope this helps,
~pshai
~Thou shall profit if thou art patient.~

Check out my kingdom for custom scripts for trade bots here!


Return to “C# Script Indicators, Safeties and Insurances”

Who is online

Users browsing this forum: No registered users and 2 guests