Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Imbalance Strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Imbalance Strategy

    Hello. I'am developing an strategy to test the imbalence in the market...

    I have this code:

    Variables
    public class AskBidRow
    {
    public double askVolumen = 0.0;
    public double bidVolumen = 0.0;
    }

    public class BarItem
    {

    public ConcurrentDictionary<double, AskBidRow> diccItem = new ConcurrentDictionary<double, AskBidRow>();
    }

    private double priceAsk = 0.0;
    private double priceBid = 0.0;
    private double price = 0.0;
    private double volumen = 0.0;
    private DateTime time;

    private Series<BarItem> BarItems;

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (Bars.Count < 0) { return; }
    if (CurrentBar < 1) { return; }


    if (BarItems[0] == null)
    {
    BarItems[0] = new BarItem();
    }

    if (e.MarketDataType == MarketDataType.Last)
    {

    priceAsk = e.Ask;
    priceBid = e.Bid;
    price = e.Price;
    volumen = e.Volume;
    time = e.Time;

    //No existe todavia....
    if (!BarItems[0].diccItem.ContainsKey(price))
    {
    BarItems[0].diccItem.TryAdd(price, new AskBidRow());

    }

    //ASK
    if (price >= priceAsk)
    {

    BarItems[0].diccItem[price].askVolumen += volumen;
    }

    //BID
    if (price <= priceBid)
    {

    BarItems[0].diccItem[price].bidVolumen += volumen;
    }
    }
    }

    protected override void OnBarUpdate() //OnBarUpdate event..
    {

    askSum = 0;
    bidSum = 0;

    if (CurrentBar < 1) { return; }



    if (BarItems.IsValidDataPointAt(CurrentBar))
    {

    tmpBarItem = BarItems.GetValueAt(CurrentBar);

    foreach (KeyValuePair<double, AskBidRow> fila in tmpBarItem.diccItem)
    {
    askSum += fila.Value.askVolumen;
    bidSum += fila.Value.bidVolumen;

    }

    //Print("Barra anterior numero: " + CurrentBar + "El volumen en el ASK es de: " + askSum + "-----" + "el volumen en el Bid es de: " + bidSum);

    }
    }

    If I set up the event OnBarUpdate to acces to the BarItem Series object is not working, they don't get in...

    If I try to acces like this way BarItems[0] they get an error....

    I want to know the ASK and the BID every OnBarUpdate, to take a decision if I get into the market or not. I have a 10 Range series...

    If the onBarUpdate is set up to every onTickdate is working fine, the tell me every tick the ASK and the Bid Volume.

    Thanks a lot...

    #2
    Hello brokerbombero,

    Thanks for writing in.

    With your code, are you experiencing issues accessing these values only within OnBarUpdate()? I would recommend to follow the code back and use Prints to see if the assignments are happening correctly.

    I've attached an example that collects the Bid and Ask volume and assigns it to a Series<ConcurrentDictionary<double, AskBidRow>> synchronized with the primary data series. You may wish to take a similar approach with your NinjaScript. Keep in mind that we do not create sample code by request. I came up with this while testing and thought it might be worthwhile for you to try.

    Please let me know if I can be of further assistance.
    Attached Files
    Last edited by NinjaTrader_Jim; 10-27-2017, 05:50 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Fixed

      Hello. Thaks for the code.

      I have fixed...Now it's working fine.


      Thaks for the code.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by jewelkulas, Today, 10:30 PM
      0 responses
      3 views
      0 likes
      Last Post jewelkulas  
      Started by jc.am, Today, 07:59 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_EricB  
      Started by ybhx0315, Today, 09:06 PM
      0 responses
      9 views
      0 likes
      Last Post ybhx0315  
      Started by parkinsonbr, Today, 08:23 PM
      0 responses
      6 views
      0 likes
      Last Post parkinsonbr  
      Started by i2w8am9ii2, Today, 04:36 PM
      2 responses
      16 views
      0 likes
      Last Post i2w8am9ii2  
      Working...
      X