Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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, 06: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 bortz, 11-06-2023, 08:04 AM
      47 responses
      1,606 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      8 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      4 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      13 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X