0

The following script is intended to have the following functions:

  1. find a row in eurusd_m1 where volume=250,
  2. find the row before row 1,
  3. find the row after row 1,
  4. copy the required values from each of these rows into a single row in obsh4

    Create procedure doji_result ()
    begin 
    
    DECLARE initial DATETIME;
    DECLARE final DATETIME;
    DECLARE centre DATETIME;
    DECLARE x int;
    
    SET x = 0;
    
    SET @initial = (select MQLTime from eurusd_m1 where volume=250 
    order by mqltime asc limit 1);
    
    SET @final = (select MQLTime from eurusd_m1 where volume=250 
    order by mqltime desc limit 1);
    
    REPEAT
    
    SET @centre = (select MQLTime from eurusd_m1 where volume=250 
    order by mqltime asc limit x,1);
    
    INSERT INTO obsh4 (MQLrefTime,RrefTime,Open,High,Low,Close,Volume) 
    select MQLTime,RTime,Open,high,Low,Close,Volume 
    from eurusd_m1 where MQLTime = @centre 
    order by MQLTime asc limit 1; 
    
    INSERT INTO obsh4 (Open2,High2,Low2,Close2,Volume2) 
    select Open,high,Low,Close,Volume 
    from eurusd_m1 where MQLTime < @centre 
    order by MQLTime desc limit 1; 
    
    INSERT INTO obsh4 (Open3,High3,Low3,Close3,Volume3) 
    select Open,high,Low,Close,Volume 
    from eurusd_m1 where MQLTime > @centre 
    order by MQLTime asc limit 1; 
    
    SET x=x+1;
    
    UNTIL @centre=@final
    END REPEAT;
    
    END $$
    DELIMITER ;
    

    When I run this each row from eurusd_m1 is copied to a new row in obsh4 (rather than all three rows being collapsed into a single row with each loop iteration)

    I tried to use UPDATE for adding the second and third row of data using the following script however I get a syntax issue on FROM and I'm not sure how to remedy that either :/

    update obsh4
    set  Open2 = open,
         High2 = high,
         Low2 = low,
         Close2 = close,
         Volume2 = volume, 
    from select Open,high,Low,Close,Volume from eurusd_m1
    where
    MQLTime < @centre order by MQLTime desc limit 1; 
    

I can see three options:

1) the update statement needs to be remedied

2) there needs to be row specification added to the insertion of the second/third rows

3) binning and generating something from scratch.

Any suggestions on either of these would be most welcome. Thank you

Edit: the loop itself operates correctly and has been tested independently of the current problem.

LHristov
  • 1,103
  • 7
  • 16
user3180258
  • 97
  • 13
  • I have also tried looking at an inner join block to solve this issue but I am getting error 1221 (incorrect usage of update and order by). – user3180258 Aug 03 '14 at 11:21
  • I believe this (http://www.xaprb.com/blog/2006/08/10/how-to-use-order-by-and-limit-on-multi-table-updates-in-mysql/) may be of use and is also reference on SO here (http://stackoverflow.com/questions/10544502/update-syntax-with-order-by-limit-and-multiple-tables). I am trying to get this up an running as follows: – user3180258 Aug 04 '14 at 01:16
  • {update obsh4 as target inner join ( select ta.MQLrefTime from obsh4 as ta inner join temph4 as tb on tb.MQLTime = ta.MQLrefTime where tb.MQLTime < ta.MQLrefTime order by ta.mqlreftime desc limit 1) as sources on sources.MQLTime = target.MQLrefTime set target.open2 = sources.Open;} This is throwing unknown column sources.MQLTime in ON clause. Could someone please help me understand what is happening here? – user3180258 Aug 04 '14 at 01:18

1 Answers1

0

Problem answered with this lengthy piece of code. I think I could be simplified somewhat so I am well open to suggestions.

drop procedure if exists doji_result; 
DELIMITER $$
Create procedure doji_result ()
begin 

DECLARE final DATETIME;
DECLARE centre DATETIME;
DECLARE openB1 DOUBLE;
DECLARE highB1 DOUBLE;
DECLARE lowB1 DOUBLE;
DECLARE closeB1 DOUBLE;
DECLARE volumeB1 DOUBLE;
DECLARE openA1 DOUBLE;
DECLARE highA1 DOUBLE;
DECLARE lowA1 DOUBLE;
DECLARE closeA1 DOUBLE;
DECLARE volumeA1 DOUBLE;

DECLARE x int;

SET x = 0;

SET @final = (select MQLTime from eurusd_m1 where volume=250 
order by mqltime desc limit 1);

REPEAT

SET @centre = (select MQLTime from eurusd_m1 where volume=250 
order by mqltime asc limit x,1);

SET @openB1 = (select open from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @highB1 = (select high from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @lowB1 = (select low from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @closeB1 = (select close from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @volumeB1 = (select volume from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);

SET @openA1 = (select open from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @highA1 = (select high from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @lowA1 = (select low from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @closeA1 = (select close from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @volumeA1 = (select volume from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);

INSERT INTO obsh4 (MQLrefTime,RrefTime,Open,High,Low,Close,Volume) 
select MQLTime,RTime,Open,high,Low,Close,Volume 
from eurusd_m1 where MQLTime = @centre 
order by MQLTime asc limit 1; 

update obsh4
SET open2 = @openB1,
    high2 = @highB1,
    low2 = @lowB1,
    close2 = @closeB1,
    volume2 = @volumeB1,
    open3 = @openA1,
    high3 = @highA1,
    low3 = @lowA1,
    close3 = @closeA1,
    volume3 = @volumeA1
order by mqlreftime desc limit 1;

SET x=x+1;

UNTIL @centre=@final
END REPEAT;

END $$
DELIMITER ;
user3180258
  • 97
  • 13