Can anyone assist me in making this bot place a trade?

Created at 09 Jul 2018, 14:15
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!
ZE

zedodia

Joined 30.05.2018

Can anyone assist me in making this bot place a trade?
09 Jul 2018, 14:15


Upon building this bot, it backtests fine, but it wont place a trade. There are alot of 'is obsolite warnings' and the like. i tryed replacing the scripts with the recomendation but then it fails to build. any help would be great and maybe a quick note as to why its doing it.

 

thanks

 

 

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class NewRobot : Robot
    {
    
        [Parameter("Vol", DefaultValue = 10000)]
        public int Vol { get; set; }
        
        [Parameter("SL", DefaultValue = 200)]
        public int SL { get; set; }
        
        [Parameter("TP", DefaultValue = 200)]
        public int TP { get; set; }
        
        [Parameter("Dev", DefaultValue = 200)]
        public int Dev { get; set; }
        
        [Parameter("N", DefaultValue = 5)]
        public int N { get; set; }
        
        private Position position1,pos1;
        
        protected override void OnStart()
        {
        Print("Welcome to the world of infinite financial possibilities!");   
        
        double Bid=Symbol.Bid;
        double Ask=Symbol.Ask;
        double Point=Symbol.PointSize;
            
            for(int i = 1; i <= N; i++)
            Trade.CreateBuyStopOrder(Symbol, Vol, Ask+Dev*i*Point, 0, 0);
            
            for(int i = 1; i <= N; i++)
            Trade.CreateSellStopOrder(Symbol, Vol, Bid-Dev*i*Point, 0, 0);
        }
        
         protected override void OnPositionOpened(Position openedPosition)
        {
            position1 = openedPosition;
            
            if (position1.TradeType == TradeType.Buy)
            {
            Trade.CreateBuyStopOrder(Symbol, Vol, position1.EntryPrice+Dev*N*Symbol.PointSize, 0, 0);
            Trade.ModifyPosition(openedPosition, position1.EntryPrice-SL*Symbol.PointSize, position1.EntryPrice+TP*Symbol.PointSize);
            Print("StopLoss and TakeProfit were successfully established");
            }
            
            if (position1.TradeType == TradeType.Sell)
            {
            Trade.CreateSellStopOrder(Symbol, Vol, position1.EntryPrice-Dev*N*Symbol.PointSize, 0, 0);
            Trade.ModifyPosition(openedPosition, position1.EntryPrice+SL*Symbol.PointSize, position1.EntryPrice-TP*Symbol.PointSize);
            Print("StopLoss and TakeProfit were successfully established");
            }
        }

        protected override void OnPositionClosed(Position pos)
         {
          pos1=pos;
         
         if(pos1.TradeType== TradeType.Buy)
         {
         if (pos1.GrossProfit>0)
         Trade.CreateSellStopOrder(Symbol, Vol, pos1.EntryPrice, 0, 0);
         
         if (pos1.GrossProfit<0)
         Trade.CreateBuyStopOrder(Symbol, Vol, pos1.EntryPrice, 0, 0);
         }
         
         if(pos1.TradeType== TradeType.Sell)
         {
         if (pos1.GrossProfit>0)
         Trade.CreateBuyStopOrder(Symbol, Vol, pos1.EntryPrice, 0, 0);
         
         if (pos1.GrossProfit<0)
         Trade.CreateSellStopOrder(Symbol, Vol, pos1.EntryPrice, 0, 0);
         }
         }

        protected override void OnStop()
        {
          Print("Successful day!");   
        }
    }
}


@zedodia
Replies

ap11
09 Jul 2018, 15:57

Hi Zedodia,

It seems like a very old code. Here is the list of changes required to switch code from deprecated API

1. Move from OnPositionOpened and OnPositionClosed overrides to Positions events

2. Replace Trade.CreateBuyStopOrder and Trade.CreateSellStopOrder to PlaceStopOrder methods. You will need to add TradeType as first parameter and label before StopLoss

3. Replace Trade.ModifyPosition to ModifyPosition

4. Change Vol parameter type from int to double

5. Replace usage of Symbol.PointSize to Symbol.TickSize

See results below:

[Parameter("Vol", DefaultValue = 10000)]
public double Vol { get; set; }

[Parameter("SL", DefaultValue = 200)]
public int SL { get; set; }

[Parameter("TP", DefaultValue = 200)]
public int TP { get; set; }

[Parameter("Dev", DefaultValue = 200)]
public int Dev { get; set; }

[Parameter("N", DefaultValue = 5)]
public int N { get; set; }

private Position position1, pos1;

protected override void OnStart()
{
    Print("Welcome to the world of infinite financial possibilities!");

    Positions.Opened += Positions_Opened;
    Positions.Closed += Positions_Closed;

    double Bid = Symbol.Bid;
    double Ask = Symbol.Ask;
    double Point = Symbol.TickSize;

    for (int i = 1; i <= N; i++)
        PlaceStopOrder(TradeType.Buy, Symbol, Vol, Ask + Dev * i * Point, null, 0, 0);

    for (int i = 1; i <= N; i++)
        PlaceStopOrder(TradeType.Sell, Symbol, Vol, Bid - Dev * i * Point, null, 0, 0);
}



private void Positions_Opened(PositionOpenedEventArgs obj)
{
    Position openedPosition = obj.Position;
    position1 = openedPosition;

    if (position1.TradeType == TradeType.Buy)
    {
        PlaceStopOrder(TradeType.Buy, Symbol, Vol, position1.EntryPrice + Dev * N * Symbol.TickSize, null, 0, 0);
        ModifyPosition(openedPosition, position1.EntryPrice - SL * Symbol.TickSize, position1.EntryPrice + TP * Symbol.TickSize);
        Print("StopLoss and TakeProfit were successfully established");
    }

    if (position1.TradeType == TradeType.Sell)
    {
        PlaceStopOrder(TradeType.Sell, Symbol, Vol, position1.EntryPrice - Dev * N * Symbol.TickSize, null, 0, 0);
        ModifyPosition(openedPosition, position1.EntryPrice + SL * Symbol.TickSize, position1.EntryPrice - TP * Symbol.TickSize);
        Print("StopLoss and TakeProfit were successfully established");
    }
}

private void Positions_Closed(PositionClosedEventArgs obj)
{
    pos1 = obj.Position;

    if (pos1.TradeType == TradeType.Buy)
    {
        if (pos1.GrossProfit > 0)
            PlaceStopOrder(TradeType.Sell, Symbol, Vol, pos1.EntryPrice, null, 0, 0);

        if (pos1.GrossProfit < 0)
            PlaceStopOrder(TradeType.Buy, Symbol, Vol, pos1.EntryPrice, null, 0, 0);
    }

    if (pos1.TradeType == TradeType.Sell)
    {
        if (pos1.GrossProfit > 0)
            PlaceStopOrder(TradeType.Buy, Symbol, Vol, pos1.EntryPrice, null, 0, 0);

        if (pos1.GrossProfit < 0)
            PlaceStopOrder(TradeType.Sell, Symbol, Vol, pos1.EntryPrice, null, 0, 0);
    }
}

protected override void OnStop()
{
    Print("Successful day!");
}

Regards,
Andrey

 


@ap11

zedodia
10 Jul 2018, 12:08

wow thankyou. i didnt expect you to do it all for me. i will first try and rewrite it with your notes at the top. then check it against yours. yes its old code. just trying to familiarise myself with the language still.

 

thankyou


@zedodia

zedodia
16 Jul 2018, 11:39 ( Updated at: 15 Jan 2024, 14:51 )

I got it.. thankyou.. now im trying a different bot and ive got all the problems removed except 6 things.

 

[Andrey Pisarev do yoiu have an email i could send you the code to? i dont want to post this code here right this minute.]

 

thankyou


@zedodia

zedodia
16 Jul 2018, 11:44

otherwise this is the line and error i get.

Trade.CreateBuyMarketOrder(Symbol, lots);   

which says to 'Use ExecuteMarketOrder instead" but when i change it the bot fails to build

 

I also get a problem with a line

protected override void OnPositionOpened(Position openedPosition)

 

any help is greatly appreciated

 

thankyou


@zedodia

zedodia
16 Jul 2018, 12:59

Trade.CreatBuyMarketOrder(Symbol, lots);

is now

ExecuteMarketOrder(TradeType.Buy, Symbol, lots);

now only two errors to go


@zedodia

zedodia
16 Jul 2018, 13:15

        protected override void OnPositionOpened(Position openedPosition)
        {
            pos.Add(openedPosition);
            ModifyPosition(openedPosition, SL, TP);
        }

        protected override void OnPositionClosed(Position position)
        {
            for (int i = 0; i < pos.Count; i++)
            {
                if (pos[i].Id == position.Id)
                {
                    pos.RemoveAt(i);
                    break;

 

i get this errors

 

Error CS0672: Member 'cAlgo.Robots.GravyTrain.OnPositionOpened(cAlgo.API.Position)' overrides obsolete member 'cAlgo.API.Robot.OnPositionOpened(cAlgo.API.Position)'. Add the Obsolete attribute to 'cAlgo.Robots.GravyTrain.OnPositionOpened(cAlgo.API.Position)'.

Error CS0672: Member 'cAlgo.Robots.GravyTrain.OnPositionClosed(cAlgo.API.Position)' overrides obsolete member 'cAlgo.API.Robot.OnPositionClosed(cAlgo.API.Position)'. Add the Obsolete attribute to 'cAlgo.Robots.GravyTrain.OnPositionClosed(cAlgo.API.Position)'.


@zedodia

PanagiotisCharalampous
16 Jul 2018, 15:22

Hi zedodia,

You can send me the code at community@spotware.com

Best Regards,

Panagiotis


@PanagiotisCharalampous