Get direction of trade?

Moderator: admin

Get direction of trade?

Postby cyanidez » Mon May 30, 2011 8:08 am

Hi guys!

Can you please help me? I have written my own strategy, where one of the parameters is as follows:

Code: Select all
strategy.parameters:addString("ActiveTrade", "Active trade to apply on", "", "");
strategy.parameters:setFlag("ActiveTrade", core.FLAG_TRADE);


Now, how can I retrieve the selected trade's ID? I assume it is:

Code: Select all
instance.parameters.ActiveTrade


...right?

Then finally, how can I see if it's a long or short trade? In other words, I need to determine if this trade is "Buy" or "Sell" so that I can reverse it.

Thanks, you guys are the best!
cyanidez
cyanidez
 
Posts: 52
Joined: Tue Mar 15, 2011 1:10 pm
Location: South Africa

Re: Get direction of trade?

Postby Nikolay.Gekht » Mon May 30, 2011 8:44 am

cyanidez wrote:Hi guys!

Can you please help me? I have written my own strategy, where one of the parameters is as follows:

Code: Select all
strategy.parameters:addString("ActiveTrade", "Active trade to apply on", "", "");
strategy.parameters:setFlag("ActiveTrade", core.FLAG_TRADE);


Now, how can I retrieve the selected trade's ID? I assume it is:

Code: Select all
instance.parameters.ActiveTrade


...right?

Exactly so.

cyanidez wrote:Then finally, how can I see if it's a long or short trade? In other words, I need to determine if this trade is "Buy" or "Sell" so that I can reverse it.


Code: Select all
-- try to find a row in the list of the trades:
local row = core.host:findTable("trades"):find("TradeID", instance.parameters.ActiveTrade);

if row == nil then
   -- row is not found. closed?
   row = core.host:findTable("closed trades"):find("TradeID", instance.parameters.ActiveTrade);
   if row ~= nil then
      -- closed!
   else
      -- ???: wrong trade id passed!
   end
else
   -- row is found, get direction:
   local dir = row.BS;
   if dir == "B" then
      -- long trade

   else -- dir == "S"
      -- short trade

   end
end
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Get direction of trade?

Postby Nikolay.Gekht » Mon May 30, 2011 8:45 am

BTW, hint:

Strategies like this usually ought to be stopped when the action is performed (trade is reversed in your case). To do it right from the strategy just use:

core.host:execute("stop");
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Get direction of trade?

Postby cyanidez » Mon May 30, 2011 9:56 am

Nikolay, you are a legend my friend, that was exactly what I was looking for!

Thanks so much!
cyanidez

P.S. Awesome tip about stopping the strategy, because the strategy I'm coding should do exactly that. :D
cyanidez
 
Posts: 52
Joined: Tue Mar 15, 2011 1:10 pm
Location: South Africa


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 80 guests