Can anyone assist me in making this bot place a trade?
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!");
}
}
}
Replies
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, 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
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:
Regards,
Andrey
@ap11