actual direction (not opening)

Created at 18 Jun 2018, 12:53
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!
swingfish's avatar

swingfish

Joined 25.06.2016

actual direction (not opening)
18 Jun 2018, 12:53


i use the following code to count positions and its sizes 

it works fine until is reverse a trade. so iassume it uses the opening direction instead of the actual one.

anyone knows how to change that so it counts the actual positions and not the opening direction?

long VolumeBuy = symbolPositions.Where(x => x.TradeType == TradeType.Buy).Sum(x => -x.Volume);
long VolumeSell = symbolPositions.Where(x => x.TradeType == TradeType.Sell).Sum(x => x.Volume);

 


@swingfish
Replies

PanagiotisCharalampous
18 Jun 2018, 15:24

Hi swingfish,

I tried it but I did not notice any problem. Positions are considered properly even if direction is reserved. Here is the code I used

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            long VolumeBuy = Positions.Where(x => x.TradeType == TradeType.Buy).Sum(x => -x.Volume);
            long VolumeSell = Positions.Where(x => x.TradeType == TradeType.Sell).Sum(x => x.Volume);
            Print("Buy: " + VolumeBuy);
            Print("Sell: " + VolumeSell);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
18 Jun 2018, 22:06

thanks alot .. but using just "positions" will count all positions, i need to filter them by Symbol

 

so i used 

var symbolPositions = Positions.Where(t => t.SymbolCode == Symbol.Code);

to populate "symbolPositions"

 


@swingfish

swingfish
18 Jun 2018, 22:18

never mind, i figured it out ;) 

long VolumeBuy = Positions.Where(x => x.TradeType == TradeType.Buy && x.SymbolCode == Symbol.Code).Sum(x => x.Volume);
long VolumeSell = Positions.Where(x => x.TradeType == TradeType.Sell && x.SymbolCode == Symbol.Code).Sum(x => -x.Volume);
Print("Buy: " + VolumeBuy);
Print("Sell: " + VolumeSell);

 

 

that works .. thanks for the help 

 


@swingfish