Basket-Close - AlgoGURU helps you

by AlgoGuru in category Other at 13/03/2019
Description

I get a lot of messages day by day and I see some people want to write codes but they do not have enough knowledge to make it. There are some pitfalls in this profession but I have made a decision: I’ll help them to be better coder.

If you are trying to write algo and you have a coder problem: visit my site www.algoguru.hu and send your problem to me with the following link: http://www.algoguru.hu/index.php/2019/03/13/algoguru-helps-you-to-be-better-coder/.

Every week I’ll pick one of them and I’ll publish it on the ctdn forum for everyone’s learning.

First code is the classic request: The Basket Close.  The cBot closes all open positions when the NetProfit >= MaxProfit or <= MinProfit. Max and MinProfit are used as input parameter. 

With this lesson you can learn how to reach the positions and how to close them

.

 

Download
26 downloads
How to install
Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.GMTStandardTime, AccessRights = AccessRights.None)]
    public class Basket : Robot
    {

        [Parameter("Close when profit below:", DefaultValue = -100)]
        public double ProfitMin { get; set; }
        [Parameter("Close when profit above:", DefaultValue = 100)]
        public double ProfitMax { get; set; }

//--------------------------------------------------------------------------------
        protected override void OnStart()
        {
//--------------------------------------------------------------------------------
            Timer.Start(1);
            Print("AlgoGURU helps you! For more info check: www.algoguru.hu");
        }
//--------------------------------------------------------------------------------
        protected override void OnTimer()
        {
//--------------------------------------------------------------------------------			
            if (Positions.Count == 0)
                return;

            double profit = Account.UnrealizedNetProfit;
            if (profit <= ProfitMin || profit >= ProfitMax)
                CloseAllPos();
        }
//+------------------------------------------------------------------+
        void CloseAllPos()
        {
//+------------------------------------------------------------------+
            TradeResult TR;
            foreach (var pos in Positions)
            {
                TR = ClosePosition(pos);
                if (TR.IsSuccessful)
                    Print("{0}-{1} closed", pos.Id, pos.SymbolCode);
                else
                    Print("Close Error: {0}-{1} {2}", pos.Id, pos.SymbolCode, TR.Error);
            }
        }
    }
}
Comments
0